[PATCH 5 of 6 seda] [test] Introduce assertValidationError context manager

Sylvain Thenault sylvain.thenault at logilab.fr
Fri Mar 31 17:08:47 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490964859 -7200
#      Fri Mar 31 14:54:19 2017 +0200
# Node ID 89339f79ab6dd61cf5822d14da6b9081f6ab20a6
# Parent  33a6b739e71f2a04ef7c3065e50895db03932fe6
[test] Introduce assertValidationError context manager

similarly to assertUnauthorized.

diff --git a/cubicweb_seda/testutils.py b/cubicweb_seda/testutils.py
--- a/cubicweb_seda/testutils.py
+++ b/cubicweb_seda/testutils.py
@@ -15,15 +15,23 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """Miscellaneous test utilities."""
 
 from contextlib import contextmanager
 
-from cubicweb import Unauthorized
+from cubicweb import ValidationError, Unauthorized
 from cubicweb.rset import NoResultError
 
 
 @contextmanager
+def assertValidationError(self, cnx):
+    with self.assertRaises(ValidationError) as cm:
+        yield cm
+        cnx.commit()
+    cnx.rollback()
+
+
+ at contextmanager
 def assertUnauthorized(self, cnx):
     with self.assertRaises(Unauthorized) as cm:
         yield cm
         cnx.commit()
     cnx.rollback()
diff --git a/test/test_hooks.py b/test/test_hooks.py
--- a/test/test_hooks.py
+++ b/test/test_hooks.py
@@ -15,18 +15,19 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda unit tests for hooks"""
 
 from itertools import chain, repeat
 
-from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 
 from cubicweb_seda import testutils
 
 
 class ValidationHooksTC(CubicWebTC):
 
+    assertValidationError = testutils.assertValidationError
+
     def test_ref_non_rule_constraints(self):
         with self.admin_access.client_cnx() as cnx:
             create = cnx.create_entity
 
             access_scheme = create('ConceptScheme', title=u'access')
@@ -49,13 +50,12 @@ class ValidationHooksTC(CubicWebTC):
             rule_alt = create('SEDAAltAccessRulePreventInheritance',
                               reverse_seda_alt_access_rule_prevent_inheritance=rule_base)
             non_rule = create('SEDARefNonRuleId', seda_ref_non_rule_id_from=rule_alt)
             cnx.commit()
 
-            non_rule.cw_set(seda_ref_non_rule_id_to=reuse_concept)
-            with self.assertRaises(ValidationError) as cm:
-                cnx.commit()
+            with self.assertValidationError(cnx) as cm:
+                non_rule.cw_set(seda_ref_non_rule_id_to=reuse_concept)
             self.assertIn('seda_ref_non_rule_id_to-subject', cm.exception.errors)
 
             non_rule.cw_set(seda_ref_non_rule_id_to=access_concept)
             cnx.commit()
 
@@ -63,14 +63,13 @@ class ValidationHooksTC(CubicWebTC):
         """Check that a ValidationError is raised when an empty SEDAAlt... entity is created."""
         with self.admin_access.client_cnx() as cnx:
             # Create an empty SEDAAltAccessRulePreventInheritance
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
             access_rule = cnx.create_entity('SEDAAccessRule', seda_access_rule=transfer)
-            cnx.create_entity('SEDAAltAccessRulePreventInheritance',
-                              reverse_seda_alt_access_rule_prevent_inheritance=access_rule)
-            with self.assertRaises(ValidationError) as cm:
-                cnx.commit()
+            with self.assertValidationError(cnx) as cm:
+                cnx.create_entity('SEDAAltAccessRulePreventInheritance',
+                                  reverse_seda_alt_access_rule_prevent_inheritance=access_rule)
             self.assertIn('An alternative cannot be empty',
                           str(cm.exception))
 
     def test_valid_choice_created(self):
         """Check that everything goes fine when a valid SEDAAlt... entity is created."""
@@ -96,14 +95,13 @@ class ValidationHooksTC(CubicWebTC):
                                        reverse_seda_alt_access_rule_prevent_inheritance=access_rule)
             rel = cnx.create_entity('SEDAPreventInheritance', prevent_inheritance=False,
                                     seda_prevent_inheritance=choice)
             cnx.commit()
             # Delete SEDAPreventInheritance
-            cnx.execute('DELETE SEDAPreventInheritance X WHERE X eid %(rel_eid)s',
-                        {'rel_eid': rel.eid})
-            with self.assertRaises(ValidationError) as cm:
-                cnx.commit()
+            with self.assertValidationError(cnx) as cm:
+                cnx.execute('DELETE SEDAPreventInheritance X WHERE X eid %(rel_eid)s',
+                            {'rel_eid': rel.eid})
             self.assertIn('An alternative cannot be empty',
                           str(cm.exception))
 
     def test_item_in_choice_deleted_with_remaining_item(self):
         """Check that everything goes fine when the a relation from a SEDAAlt... entity
@@ -188,35 +186,34 @@ class SetDefaultHooksTC(CubicWebTC):
             self.assertEqual(ref.user_cardinality, '1..n')
 
 
 class CheckProfileTC(CubicWebTC):
 
+    assertValidationError = testutils.assertValidationError
+
     def test_base(self):
         with self.admin_access.repo_cnx() as cnx:
             transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'diagnosis testing')
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             access_rule = cnx.create_entity('SEDAAccessRule', seda_access_rule=unit_alt_seq)
             cnx.commit()
 
-            with self.assertRaises(ValidationError):
+            with self.assertValidationError(cnx):
                 transfer.cw_set(simplified_profile=True)
-                cnx.commit()
 
             access_rule_seq = cnx.create_entity('SEDASeqAccessRuleRule',
                                                 reverse_seda_seq_access_rule_rule=access_rule)
             start_date = cnx.create_entity('SEDAStartDate',
                                            seda_start_date=access_rule_seq)
             transfer.cw_set(simplified_profile=True)
             cnx.commit()
 
-            with self.assertRaises(ValidationError):
+            with self.assertValidationError(cnx):
                 start_date.cw_set(user_cardinality=u'0..1')
-                cnx.commit()
 
-            with self.assertRaises(ValidationError):
+            with self.assertValidationError(cnx):
                 access_rule_seq.cw_delete()
-                cnx.commit()
 
 
 if __name__ == '__main__':
     import unittest
     unittest.main()
diff --git a/test/test_schema.py b/test/test_schema.py
--- a/test/test_schema.py
+++ b/test/test_schema.py
@@ -13,21 +13,22 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda unit tests for schema"""
 
-from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.schema import ERQLExpression, RRQLExpression
 
 from cubicweb_seda import iter_all_rdefs
 
 from cubicweb_seda import testutils
 
 
 class SchemaConceptConstraintsTC(CubicWebTC):
 
+    assertValidationError = testutils.assertValidationError
+
     def setup_database(self):
         with self.admin_access.client_cnx() as cnx:
             mt_scheme = testutils.scheme_for_type(cnx, 'seda_mime_type_to', None)
             mt_concept = mt_scheme.add_concept(label=u'text/plain')
             enc_scheme = testutils.scheme_for_type(cnx, 'seda_encoding_to', None)
@@ -45,26 +46,24 @@ class SchemaConceptConstraintsTC(CubicWe
             cnx.create_entity('SEDAMessageDigestAlgorithmCodeListVersion',
                               seda_message_digest_algorithm_code_list_version_from=transfer,
                               seda_message_digest_algorithm_code_list_version_to=self.enc_scheme)
             cnx.commit()
 
-            bdo.cw_set(seda_algorithm=self.mt_concept)
-            with self.assertRaises(ValidationError) as cm:
-                cnx.commit()
+            with self.assertValidationError(cnx) as cm:
+                bdo.cw_set(seda_algorithm=self.mt_concept)
             self.assertIn('seda_algorithm-subject', cm.exception.errors)
 
             bdo.cw_set(seda_algorithm=self.enc_concept)
             cnx.commit()
 
     def assertMimeTypeConcept(self, bdo):
         cnx = bdo._cw
 
-        cnx.create_entity('SEDAMimeType',
-                          seda_mime_type_from=bdo,
-                          seda_mime_type_to=self.enc_concept)
-        with self.assertRaises(ValidationError) as cm:
-            cnx.commit()
+        with self.assertValidationError(cnx) as cm:
+            cnx.create_entity('SEDAMimeType',
+                              seda_mime_type_from=bdo,
+                              seda_mime_type_to=self.enc_concept)
         self.assertIn('seda_mime_type_to-subject', cm.exception.errors)
 
         cnx.create_entity('SEDAMimeType',
                           seda_mime_type_from=bdo,
                           seda_mime_type_to=self.mt_concept)
@@ -84,15 +83,14 @@ class SchemaConceptConstraintsTC(CubicWe
             self.assertMimeTypeConcept(bdo)
 
     def assertEncodingConcept(self, bdo):
         cnx = bdo._cw
 
-        cnx.create_entity('SEDAEncoding',
-                          seda_encoding_from=bdo,
-                          seda_encoding_to=self.mt_concept)
-        with self.assertRaises(ValidationError) as cm:
-            cnx.commit()
+        with self.assertValidationError(cnx) as cm:
+            cnx.create_entity('SEDAEncoding',
+                              seda_encoding_from=bdo,
+                              seda_encoding_to=self.mt_concept)
         self.assertIn('seda_encoding_to-subject', cm.exception.errors)
 
         cnx.create_entity('SEDAEncoding',
                           seda_encoding_from=bdo,
                           seda_encoding_to=self.enc_concept)
@@ -112,13 +110,12 @@ class SchemaConceptConstraintsTC(CubicWe
             self.assertEncodingConcept(bdo)
 
     def assertDigestAlgorithmConcept(self, bdo):
         cnx = bdo._cw
 
-        with self.assertRaises(ValidationError) as cm:
+        with self.assertValidationError(cnx) as cm:
             bdo.cw_set(seda_algorithm=self.mt_concept)
-            cnx.commit()
         self.assertIn('seda_algorithm-subject', cm.exception.errors)
 
         bdo.cw_set(seda_algorithm=self.enc_concept)
         cnx.commit()
 
@@ -145,10 +142,12 @@ class SchemaConceptConstraintsTC(CubicWe
             self.assertDigestAlgorithmConcept(bdo)
 
 
 class SchemaTC(CubicWebTC):
 
+    assertValidationError = testutils.assertValidationError
+
     def test_component_archive_unit_rule_constraint(self):
         with self.admin_access.client_cnx() as cnx:
             for rule_type in ('access', 'appraisal'):
                 etype = 'SEDASeq{0}RuleRule'.format(rule_type.capitalize())
                 scheme = testutils.scheme_for_type(cnx, 'seda_rule', etype)
@@ -244,13 +243,12 @@ class SchemaTC(CubicWebTC):
             ckt_concept = ckt_scheme.add_concept(label=u'geoname')
             scheme = testutils.scheme_for_type(cnx, 'seda_mime_type_to', None)
             concept = scheme.add_concept(label=u'text/plain')
             cnx.commit()
 
-            with self.assertRaises(ValidationError):
+            with self.assertValidationError(cnx):
                 scheme.cw_set(code_keyword_type=concept)
-                cnx.commit()
 
             scheme.cw_set(code_keyword_type=ckt_concept)
             cnx.commit()
 
     def test_container_relation(self):


More information about the saem-devel mailing list