[test] Add a migration test for update of a static vocabulary constraint
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 07 Jul 2016 14:10:45 +0200
changeset 11414 69ea7be0373a
parent 11413 c172fa18565e
child 11415 f87da59faea1
[test] Add a migration test for update of a static vocabulary constraint This is a constraint serialized to be checked by the DB backend, hence ensure the old constraint is dropped and the new one is created.
cubicweb/server/test/data-migractions/migratedapp/schema.py
cubicweb/server/test/unittest_migractions.py
--- a/cubicweb/server/test/data-migractions/migratedapp/schema.py	Thu Jun 30 13:42:16 2016 +0200
+++ b/cubicweb/server/test/data-migractions/migratedapp/schema.py	Thu Jul 07 14:10:45 2016 +0200
@@ -119,7 +119,7 @@
     nom    = String(fulltextindexed=True, required=True, maxsize=64)
     prenom = String(fulltextindexed=True, maxsize=64)
     civility   = String(maxsize=1, default='M', fulltextindexed=True)
-    promo  = String(vocabulary=('bon','pasbon'))
+    promo  = String(vocabulary=('bon','pasbon', 'pasbondutout'))
     titre  = String(fulltextindexed=True, maxsize=128)
     adel   = String(maxsize=128)
     ass    = String(maxsize=128)
--- a/cubicweb/server/test/unittest_migractions.py	Thu Jun 30 13:42:16 2016 +0200
+++ b/cubicweb/server/test/unittest_migractions.py	Thu Jul 07 14:10:45 2016 +0200
@@ -22,6 +22,7 @@
 from datetime import date
 from contextlib import contextmanager
 import tempfile
+from hashlib import md5
 
 from logilab.common.testlib import unittest_main, Tags, tag, with_tempdir
 from logilab.common import tempattr
@@ -113,6 +114,14 @@
         assert result, 'no table %s' % tablename
         return dict((x[0], (x[1], x[2])) for x in result)
 
+    def table_constraints(self, mh, tablename):
+        result = mh.sqlexec(
+            "SELECT DISTINCT constraint_name FROM information_schema.constraint_column_usage "
+            "WHERE LOWER(table_name) = '%(table)s' AND constraint_name LIKE 'cstr%%'"
+            % {'table': tablename.lower()})
+        assert result, 'no table %s' % tablename
+        return set(x[0] for x in result)
+
 
 class MigrationCommandsTC(MigrationTC):
 
@@ -585,6 +594,15 @@
             relations = [r.name for r in rset.get_entity(0, 0).relations]
             self.assertCountEqual(relations, ('nom', 'prenom', 'datenaiss'))
 
+            # serialized constraint changed
+            constraints = self.table_constraints(mh, 'cw_Personne')
+            self.assertEqual(len(constraints), 1, constraints)
+            rdef = migrschema['promo'].rdefs['Personne', 'String']
+            cstr = rdef.constraint_by_type('StaticVocabularyConstraint')
+            cstrname = 'cstr' + md5((rdef.subject.type + rdef.rtype.type + cstr.type() +
+                                     (cstr.serialize() or '')).encode('ascii')).hexdigest()
+            self.assertIn(cstrname, constraints)
+
     def _erqlexpr_rset(self, cnx, action, ertype):
         rql = 'RQLExpression X WHERE ET is CWEType, ET %s_permission X, ET name %%(name)s' % action
         return cnx.execute(rql, {'name': ertype})