[PATCH 3 of 3 saem_ref v3] [ark] Use random ARK generation for ARKExtIdentifierGenerator

Denis Laxalde denis.laxalde at logilab.fr
Wed Feb 14 16:22:14 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1518180780 -3600
#      Fri Feb 09 13:53:00 2018 +0100
# Node ID 61050d7d7825355249d75c749e17cbb9e5e1a745
# Parent  fddd9de51104d6c18b0240dc1e38483d9afff61e
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 61050d7d7825
# EXP-Topic ark/postgres-generated
[ark] Use random ARK generation for ARKExtIdentifierGenerator

Related to extranet #46881387.

diff --git a/cubicweb_saem_ref/entities/__init__.py b/cubicweb_saem_ref/entities/__init__.py
--- a/cubicweb_saem_ref/entities/__init__.py
+++ b/cubicweb_saem_ref/entities/__init__.py
@@ -22,6 +22,11 @@ from cubicweb.predicates import match_kw
 from cubicweb.entities import AnyEntity, fetch_config, authobjs, lib
 
 
+ARK_PREFIX = 'rf'
+ARK_CONTROLCHAR = 'g'
+ARK_NAME_LENGTH = 10
+
+
 def make_ark(name):
     return u'rf{name}g'.format(name=name)
 
@@ -46,11 +51,12 @@ class ARKExtIdentifierGenerator(ARKGener
 
     def assign_name(self):
         """Return a new unique identifier as unicode"""
-        dbh = self._cw.repo.system_source.dbhelper
-        for sql in dbh.sqls_increment_sequence('ext_ark_count'):
-            cu = self._cw.system_sql(sql)
-        count = cu.fetchone()[0]
-        return make_ark(u'{0:09d}'.format(count))
+        cu = self._cw.system_sql(
+            'select * from gen_ark(%s, %s, %s);',
+            (ARK_NAME_LENGTH, ARK_PREFIX, ARK_CONTROLCHAR),
+        )
+        ark_name, = cu.fetchone()
+        return ark_name
 
 
 class ARKCWIdentifierGenerator(ARKGeneratorMixIn, Adapter):
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -480,20 +480,26 @@ class AssignArkWebServiceTC(CubicWebTC):
         with self.admin_access.cnx() as cnx:
             org_ark = testutils.authority_with_naa(cnx).ark
             cnx.commit()
+
+        def check_ark(response_body):
+            json_response = json.loads(result)
+            self.assertEqual(len(json_response), 1)
+            self.assertEqual(list(json_response[0].keys()), ['ark'])
+            ark = json_response[0]['ark']
+            self.assertTrue(testutils.match_ark(ark), ark)
+
         params = {'organization': org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **params) as req:
             result = self.app_handle_request(req, 'ark')
-            self.assertEqual(json.loads(result),
-                             [{'ark': '0/rf000000001g'}])
+            check_ark(result)
         params = {'organization': 'ark:/' + org_ark}
         with self.admin_access.web_request(
                 headers={'Accept': 'application/json'},
                 method='POST', **params) as req:
             result = self.app_handle_request(req, 'ark')
-            self.assertEqual(json.loads(result),
-                             [{'ark': '0/rf000000002g'}])
+            check_ark(result)
 
 
 class ListArchivalUnitsServiceTC(CubicWebTC):
diff --git a/test/testutils.py b/test/testutils.py
--- a/test/testutils.py
+++ b/test/testutils.py
@@ -18,6 +18,7 @@
 from __future__ import print_function
 
 from os.path import join, dirname, exists
+import re
 import sys
 
 from doctest import Example
@@ -31,6 +32,11 @@ from cubicweb import NoResultError, devt
 from cubicweb.devtools import testlib
 
 from cubicweb_seda import testutils as seda_testutils
+from cubicweb_saem_ref.entities import (
+    ARK_PREFIX,
+    ARK_CONTROLCHAR,
+    ARK_NAME_LENGTH,
+)
 
 # make some names them appear in this module namespace to ease test API
 assertValidationError = seda_testutils.assertValidationError
@@ -116,6 +122,18 @@ def authority_with_naa(cnx, name=u'Defau
     return authority
 
 
+ARK_RGX = re.compile(
+    r'^0/%s.{%d}%s$'
+    % (ARK_PREFIX,
+       ARK_NAME_LENGTH - len(ARK_PREFIX) - len(ARK_CONTROLCHAR),
+       ARK_CONTROLCHAR),
+)
+
+
+def match_ark(text):
+    return ARK_RGX.match(text)
+
+
 def setup_scheme(cnx, title, *labels, **kwargs):
     """Return info new concept scheme"""
     scheme = cnx.create_entity('ConceptScheme', title=title, ark_naa=naa(cnx), **kwargs)
diff --git a/test/unittest_entities.py b/test/unittest_entities.py
--- a/test/unittest_entities.py
+++ b/test/unittest_entities.py
@@ -28,6 +28,9 @@ class ArkGeneratorTC(CubicWebTC):
 
     configcls = PostgresApptestConfiguration
 
+    def assertIsArk(self, text):
+        self.assertRegexpMatches(text, r'^saemref-test/rf.{7}g$')
+
     def test_eid(self):
         with self.admin_access.repo_cnx() as cnx:
             generator = self.vreg['adapters'].select(
@@ -39,7 +42,7 @@ class ArkGeneratorTC(CubicWebTC):
         with self.admin_access.repo_cnx() as cnx:
             generator = self.vreg['adapters'].select('IARKGenerator', cnx,
                                                      naa_what='saemref-test')
-            self.assertEqual(generator.generate_ark(), 'saemref-test/rf000000001g')
+            self.assertIsArk(generator.generate_ark())
 
 
 if __name__ == '__main__':



More information about the saem-devel mailing list