[PATCH 2 of 2 saem_ref V2] [schema] Add a unicity constraint on Agent(name, authority)

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Apr 12 10:24:41 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1491978511 -7200
#      Wed Apr 12 08:28:31 2017 +0200
# Node ID 57c9841da9003111224dd1dcab9e64f66dd3b248
# Parent  ca3741e6372d516fecf09a8677018a183934d917
[schema] Add a unicity constraint on Agent(name, authority)

It seems to make sense and is consistent with OrganizationUnit.

diff --git a/cubicweb_saem_ref/migration/0.15.1_Any.py b/cubicweb_saem_ref/migration/0.15.1_Any.py
--- a/cubicweb_saem_ref/migration/0.15.1_Any.py
+++ b/cubicweb_saem_ref/migration/0.15.1_Any.py
@@ -1,7 +1,7 @@
 for ertype in ('generated', 'used', 'associated_with', 'place_address', 'new_version_of',
-               'OrganizationUnit'):
+               'OrganizationUnit', 'Agent'):
     sync_schema_props_perms(ertype)
 
 
 sql("DELETE FROM container_relation WHERE EXISTS("
     "SELECT FROM entities WHERE eid_to=eid AND "
diff --git a/cubicweb_saem_ref/schema.py b/cubicweb_saem_ref/schema.py
--- a/cubicweb_saem_ref/schema.py
+++ b/cubicweb_saem_ref/schema.py
@@ -110,10 +110,11 @@ class OrganizationUnit(WorkflowableEntit
     name = String(required=True, fulltextindexed=True)
 
 
 @authority_permissions
 class Agent(WorkflowableEntityType):
+    __unique_together__ = [('name', 'authority')]
     name = String(required=True, fulltextindexed=True)
 
 
 class user_authority(RelationDefinition):
     __permissions__ = {
diff --git a/test/unittest_schema.py b/test/unittest_schema.py
--- a/test/unittest_schema.py
+++ b/test/unittest_schema.py
@@ -167,10 +167,22 @@ class SchemaConstraintsTC(CubicWebTC):
             other_authority = testutils.authority_with_naa(cnx, name=u'other authority')
             testutils.organization_unit(cnx, u'arch', archival_roles=[u'archival'],
                                         authority=other_authority)
             cnx.commit()
 
+    def test_agent_unicity(self):
+        with self.admin_access.cnx() as cnx:
+            testutils.agent(cnx, u'bob')
+            cnx.commit()
+
+            with self.assertValidationError(cnx):
+                testutils.agent(cnx, u'bob')
+
+            other_authority = testutils.authority_with_naa(cnx, name=u'other authority')
+            testutils.agent(cnx, u'bob', authority=other_authority)
+            cnx.commit()
+
 
 class AuthorityRecordTC(CubicWebTC):
 
     def test_fti(self):
         with self.admin_access.repo_cnx() as cnx:


More information about the saem-devel mailing list