[PATCH 2 of 2 saem] [seda] Add a published constraint on use_profile relation

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Apr 14 15:58:14 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1492178036 -7200
#      Fri Apr 14 15:53:56 2017 +0200
# Node ID 3533684c49dd6915a4e948f0d9b8fe312ceb49d6
# Parent  bcf704ab33970fe1d22e0606d64ca3d4c55419d1
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 3533684c49dd
[seda] Add a published constraint on use_profile relation


This requires to update some tests which use this relation to ensure the profile
is published before. In some case, more rewrite then expected is necessary
because of attempt to set the relation at profile creation time (where it *can't*
be yet published).

Related to extranet #11540258

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
@@ -4,11 +4,11 @@ for agent in rql('DISTINCT Any X WHERE X
                  'Y eid YE, X eid >YE').entities():
     agent.cw_delete()
 commit()
 
 for ertype in ('generated', 'used', 'associated_with', 'place_address', 'new_version_of',
-               'agent_kind', 'authority_record',
+               'agent_kind', 'authority_record', 'use_profile',
                'OrganizationUnit', 'Agent', 'ArkNameAssigningAuthority'):
     sync_schema_props_perms(ertype)
 
 
 sql("DELETE FROM container_relation WHERE EXISTS("
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
@@ -437,11 +437,12 @@ simplified_profile.default = True
 
 class use_profile(RelationDefinition):
     subject = 'OrganizationUnit'
     object = 'SEDAArchiveTransfer'
     cardinality = '**'
-    constraints = [RQLConstraint('S archival_role R, R name "deposit"')]
+    constraints = [RQLConstraint('S archival_role R, R name "deposit"'),
+                   RQLConstraint('O in_state ST, ST name "published"')]
 
 
 class new_version_of(RelationDefinition):
     __permissions__ = {'read': ('managers', 'users', 'guests'),
                        'add': ('managers', 'users',
diff --git a/test/test_oai.py b/test/test_oai.py
--- a/test/test_oai.py
+++ b/test/test_oai.py
@@ -374,11 +374,14 @@ class OAIPMHViewsTC(PyramidCWTest, OAITe
         profile = self._setup_profile()
         with self.admin_access.cnx() as cnx:
             tagent_eid = testutils.organization_unit(
                 cnx, u'blah', archival_roles=[u'deposit']).eid
             cnx.commit()
-        profile2 = self._setup_profile(reverse_use_profile=tagent_eid)
+        profile2 = self._setup_profile()
+        with self.admin_access.cnx() as cnx:
+            cnx.entity_from_eid(tagent_eid).cw_set(use_profile=profile2.eid)
+            cnx.commit()
         with self.admin_access.web_request() as req:
             transferring_agent_ark = req.entity_from_eid(tagent_eid).ark
             result = self.oai_request(
                 req, verb='ListIdentifiers',
                 set='profile:transferring_agent:' + transferring_agent_ark,
diff --git a/test/unittest_entities_container.py b/test/unittest_entities_container.py
--- a/test/unittest_entities_container.py
+++ b/test/unittest_entities_container.py
@@ -77,15 +77,18 @@ class ContainerTC(CubicWebTC):
 class TreeTC(CubicWebTC):
 
     def test_seda_profile_clone(self):
         """Functional test for SEDA profile cloning."""
         with self.admin_access.repo_cnx() as cnx:
-            agent = testutils.organization_unit(cnx, u'bob', archival_roles=['deposit'])
-            transfer = testutils.setup_profile(cnx, reverse_use_profile=agent)
+            transfer = testutils.setup_profile(cnx)
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             testutils.create_data_object(transfer)
             cnx.commit()
+            transfer.cw_adapt_to('IWorkflowable').fire_transition('publish')
+            agent = testutils.organization_unit(cnx, u'bob', archival_roles=['deposit'],
+                                                use_profile=transfer)
+            cnx.commit()
 
             clone = testutils.setup_profile(cnx, title=u'Clone', new_version_of=transfer)
             cnx.commit()
 
             # ark and cwuri should not have been copied
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
--- a/test/unittest_hooks.py
+++ b/test/unittest_hooks.py
@@ -91,10 +91,12 @@ class SAEMRefHooksTC(CubicWebTC):
     def test_sync_profile_md(self):
         with self.admin_access.repo_cnx() as cnx:
             scheme = testutils.scheme_for_type(cnx, 'seda_rule', 'SEDASeqAccessRuleRule', u'AR038')
             transfer = testutils.setup_profile(cnx)
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
+            cnx.commit()
+            transfer.cw_adapt_to('IWorkflowable').fire_transition('publish')
             transfer.cw_set(modification_date=YESTERDAY)
             cnx.commit()
 
             # edit composite children
             unit_alt_seq.reverse_seda_title[0].cw_set(title=u'archive name')
diff --git a/test/unittest_schema.py b/test/unittest_schema.py
--- a/test/unittest_schema.py
+++ b/test/unittest_schema.py
@@ -70,10 +70,25 @@ class SchemaConstraintsTC(CubicWebTC):
             # should be created by testutils.organization_unit() above.
             authority = cnx.find('Organization', name=u'Default authority').one()
             rset = authority.unrelated('archival_unit', 'OrganizationUnit')
             self.assertEqual(rset.one().eid, pou.eid)
 
+    def test_published_constraint_on_use_profile(self):
+        with self.admin_access.repo_cnx() as cnx:
+            deposit_unit = testutils.organization_unit(cnx, u'AU', archival_roles=['deposit'])
+            p1 = testutils.setup_profile(cnx)
+            p2 = testutils.setup_profile(cnx)
+            cnx.commit()
+            p1.cw_adapt_to('IWorkflowable').fire_transition('publish')
+            cnx.commit()
+
+            with self.assertValidationError(cnx):
+                deposit_unit.cw_set(use_profile=p2)
+
+            deposit_unit.cw_set(use_profile=p1)
+            cnx.commit()
+
     def test_agent_authority_record_is_a_person(self):
         """Authority record for an agent is limited to person"""
         with self.admin_access.repo_cnx() as cnx:
             create_published_records_panel(cnx)
             peter = testutils.agent(cnx, u'Peter')


More information about the saem-devel mailing list