[test] Introduce assertValidationError context manager
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 31 Mar 2017 14:54:19 +0200
changeset 2590 54ded6ab2e4f
parent 2589 694ba9fce008
child 2591 d33d90d4daad
[test] Introduce assertValidationError context manager similarly to assertUnauthorized.
cubicweb_seda/testutils.py
test/test_hooks.py
test/test_schema.py
--- a/cubicweb_seda/testutils.py	Fri Mar 31 14:53:32 2017 +0200
+++ b/cubicweb_seda/testutils.py	Fri Mar 31 14:54:19 2017 +0200
@@ -17,11 +17,19 @@
 
 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()
+
+
+@contextmanager
 def assertUnauthorized(self, cnx):
     with self.assertRaises(Unauthorized) as cm:
         yield cm
--- a/test/test_hooks.py	Fri Mar 31 14:53:32 2017 +0200
+++ b/test/test_hooks.py	Fri Mar 31 14:54:19 2017 +0200
@@ -17,7 +17,6 @@
 
 from itertools import chain, repeat
 
-from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 
 from cubicweb_seda import testutils
@@ -25,6 +24,8 @@
 
 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
@@ -51,9 +52,8 @@
             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)
@@ -65,10 +65,9 @@
             # 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))
 
@@ -98,10 +97,9 @@
                                     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))
 
@@ -190,6 +188,8 @@
 
 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')
@@ -197,9 +197,8 @@
             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)
@@ -208,13 +207,11 @@
             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__':
--- a/test/test_schema.py	Fri Mar 31 14:53:32 2017 +0200
+++ b/test/test_schema.py	Fri Mar 31 14:54:19 2017 +0200
@@ -15,7 +15,6 @@
 # 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
 
@@ -26,6 +25,8 @@
 
 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)
@@ -47,9 +48,8 @@
                               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)
@@ -58,11 +58,10 @@
     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',
@@ -86,11 +85,10 @@
     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',
@@ -114,9 +112,8 @@
     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)
@@ -147,6 +144,8 @@
 
 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'):
@@ -246,9 +245,8 @@
             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()