[repo tests] add test case for RQLUniqueConstraint stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 14 Sep 2010 15:37:32 +0200
branchstable
changeset 6236 9138f23ee263
parent 6235 d5f7bd102282
child 6237 0c886f667b1f
[repo tests] add test case for RQLUniqueConstraint
server/test/data/schema.py
server/test/unittest_repository.py
--- a/server/test/data/schema.py	Tue Sep 14 15:36:29 2010 +0200
+++ b/server/test/data/schema.py	Tue Sep 14 15:37:32 2010 +0200
@@ -19,7 +19,8 @@
 from yams.buildobjs import (EntityType, RelationType, RelationDefinition,
                             SubjectRelation, RichString, String, Int, Boolean, Datetime)
 from yams.constraints import SizeConstraint
-from cubicweb.schema import (WorkflowableEntityType, RQLConstraint,
+from cubicweb.schema import (WorkflowableEntityType,
+                             RQLConstraint, RQLUniqueConstraint,
                              ERQLExpression, RRQLExpression)
 
 class Affaire(WorkflowableEntityType):
@@ -93,7 +94,10 @@
 
     migrated_from = SubjectRelation('Note')
     attachment = SubjectRelation('File')
-    inline1 = SubjectRelation('Affaire', inlined=True, cardinality='?*')
+    inline1 = SubjectRelation('Affaire', inlined=True, cardinality='?*',
+                              constraints=[RQLUniqueConstraint('S type T, S inline1 A1, A1 todo_by C, '
+                                                              'Y type T, Y inline1 A2, A2 todo_by C',
+                                                               'S,Y')])
     todo_by = SubjectRelation('CWUser')
 
 class Personne(EntityType):
--- a/server/test/unittest_repository.py	Tue Sep 14 15:36:29 2010 +0200
+++ b/server/test/unittest_repository.py	Tue Sep 14 15:37:32 2010 +0200
@@ -627,6 +627,18 @@
             self.assertEquals(CALLED, [('before_add_relation', eidn, 'ecrit_par', eidp),
                                        ('after_add_relation', eidn, 'ecrit_par', eidp)])
 
+    def test_unique_contraint(self):
+        req = self.request()
+        toto = req.create_entity('Personne', nom=u'toto')
+        a01 = req.create_entity('Affaire', ref=u'A01', todo_by=toto)
+        req.cnx.commit()
+        req = self.request()
+        req.create_entity('Note', type=u'todo', inline1=a01)
+        req.cnx.commit()
+        req = self.request()
+        req.create_entity('Note', type=u'todo', inline1=a01)
+        ex = self.assertRaises(ValidationError, req.cnx.commit)
+        self.assertEquals(ex.errors, {'inline1-subject': u'RQLUniqueConstraint S type T, S inline1 A1, A1 todo_by C, Y type T, Y inline1 A2, A2 todo_by C failed'})
 
 if __name__ == '__main__':
     unittest_main()