[PATCH 1 of 2 saem_ref v2] [ark] Add an insert_ark helper function

Denis Laxalde denis.laxalde at logilab.fr
Wed Feb 21 17:17:12 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1519227628 -3600
#      Wed Feb 21 16:40:28 2018 +0100
# Node ID 00a7985081f1e0c376e9dd2d7de605ce9e7e3bd7
# Parent  4e2b908a791f0250f1bc26f460ecad8b5bb3574b
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 00a7985081f1
# EXP-Topic ark/unique-overall
[ark] Add an insert_ark helper function

diff --git a/cubicweb_saem_ref/ark.py b/cubicweb_saem_ref/ark.py
--- a/cubicweb_saem_ref/ark.py
+++ b/cubicweb_saem_ref/ark.py
@@ -74,3 +74,18 @@ def generate_qualified_ark(cnx, naan, na
     )
     qualifier, = cu.fetchone()
     return qualifier
+
+
+def insert_ark(cnx, naan, name, qualifier=None):
+    """Insert a record in "ark" table from specified values."""
+    if qualifier is None:
+        qs = 'INSERT INTO ark VALUES (%s, %s);'
+        values = (naan, name)
+    else:
+        qs = 'INSERT INTO ark VALUES (%s, %s, %s);'
+        values = (naan, name, qualifier)
+    cnx.system_sql(qs, values)
+    ark = u'/'.join([naan, name])
+    if qualifier is not None:
+        ark += u'/' + qualifier
+    return ark
diff --git a/test/test_ark.py b/test/test_ark.py
--- a/test/test_ark.py
+++ b/test/test_ark.py
@@ -26,6 +26,7 @@ from cubicweb.devtools import (
 
 from cubicweb_saem_ref.ark import (
     ARK_RGX,
+    insert_ark,
     match as ark_match,
 )
 import testutils
@@ -173,6 +174,22 @@ class ArkServiceTC(testlib.CubicWebTC):
                 (_, _, qualifier), = cu.fetchall()
         self.assertEqual(len(qualifier), 3, qualifier)
 
+    def test_insert_ark(self):
+        with self.admin_access.cnx() as cnx:
+            ark = insert_ark(cnx, '12345', u'bob')
+            cnx.commit()
+            self.assertEqual(ark, u'12345/bob')
+            results = cnx.system_sql('SELECT naan, name, qualifier FROM ark;').fetchall()
+            self.assertEqual(results, [(12345, u'bob', u'')])
+
+    def test_insert_ark_qualifier(self):
+        with self.admin_access.cnx() as cnx:
+            ark = insert_ark(cnx, '12345', u'bob', u'1')
+            cnx.commit()
+            self.assertEqual(ark, u'12345/bob/1')
+            results = cnx.system_sql('SELECT naan, name, qualifier FROM ark;').fetchall()
+            self.assertEqual(results, [(12345, u'bob', u'1')])
+
 
 if __name__ == '__main__':
     import unittest



More information about the saem-devel mailing list