fix workflow consistency: check state/transition name uniqness in the wf when state/transition name is modified ; use the new msg argument of rql constraints to have a nicer error message on failure stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 02 Dec 2009 12:55:41 +0100
branchstable
changeset 3966 f742c525b7b4
parent 3965 94f95928f5ae
child 3967 dfb4b3797113
fix workflow consistency: check state/transition name uniqness in the wf when state/transition name is modified ; use the new msg argument of rql constraints to have a nicer error message on failure
entities/test/unittest_wfobjs.py
schemas/workflow.py
--- a/entities/test/unittest_wfobjs.py	Wed Dec 02 12:54:16 2009 +0100
+++ b/entities/test/unittest_wfobjs.py	Wed Dec 02 12:55:41 2009 +0100
@@ -37,12 +37,17 @@
         self.commit()
         wf.add_state(u'foo')
         ex = self.assertRaises(ValidationError, self.commit)
-        # XXX enhance message
-        self.assertEquals(ex.errors, {'state_of': 'unique constraint S name N, Y state_of O, Y name N failed'})
+        self.assertEquals(ex.errors, {'name': 'workflow already have a state of that name'})
         # no pb if not in the same workflow
         wf2 = add_wf(self, 'Company')
         foo = wf2.add_state(u'foo', initial=True)
         self.commit()
+        # gnark gnark
+        bar = wf.add_state(u'bar')
+        self.commit()
+        bar.set_attributes(name=u'foo')
+        ex = self.assertRaises(ValidationError, self.commit)
+        self.assertEquals(ex.errors, {'name': 'workflow already have a state of that name'})
 
     def test_duplicated_transition(self):
         wf = add_wf(self, 'Company')
@@ -51,8 +56,19 @@
         wf.add_transition(u'baz', (foo,), bar, ('managers',))
         wf.add_transition(u'baz', (bar,), foo)
         ex = self.assertRaises(ValidationError, self.commit)
-        # XXX enhance message
-        self.assertEquals(ex.errors, {'transition_of': 'unique constraint S name N, Y transition_of O, Y name N failed'})
+        self.assertEquals(ex.errors, {'name': 'workflow already have a transition of that name'})
+        # no pb if not in the same workflow
+        wf2 = add_wf(self, 'Company')
+        foo = wf.add_state(u'foo', initial=True)
+        bar = wf.add_state(u'bar')
+        wf.add_transition(u'baz', (foo,), bar, ('managers',))
+        self.commit()
+        # gnark gnark
+        biz = wf.add_transition(u'biz', (bar,), foo)
+        self.commit()
+        biz.set_attributes(name=u'baz')
+        ex = self.assertRaises(ValidationError, self.commit)
+        self.assertEquals(ex.errors, {'name': 'workflow already have a transition of that name'})
 
 
 class WorkflowTC(EnvBasedTC):
--- a/schemas/workflow.py	Wed Dec 02 12:54:16 2009 +0100
+++ b/schemas/workflow.py	Wed Dec 02 12:55:41 2009 +0100
@@ -48,7 +48,9 @@
     permissions = META_ETYPE_PERMS
 
     name = String(required=True, indexed=True, internationalizable=True,
-                  maxsize=256)
+                  maxsize=256,
+                  constraints=[RQLUniqueConstraint('S name N, S state_of WF, Y state_of WF, Y name N', 'Y',
+                                                   _('workflow already have a state of that name'))])
     description = RichString(fulltextindexed=True, default_format='text/rest',
                              description=_('semantic description of this state'))
 
@@ -59,7 +61,8 @@
                                          description=_('allowed transitions from this state'))
     state_of = SubjectRelation('Workflow', cardinality='1*', composite='object',
                                description=_('workflow to which this state belongs'),
-                               constraints=[RQLUniqueConstraint('S name N, Y state_of O, Y name N', 'Y')])
+                               constraints=[RQLUniqueConstraint('S name N, Y state_of O, Y name N', 'Y',
+                                                                _('workflow already have a state of that name'))])
 
 
 class BaseTransition(EntityType):
@@ -67,7 +70,9 @@
     permissions = META_ETYPE_PERMS
 
     name = String(required=True, indexed=True, internationalizable=True,
-                  maxsize=256)
+                  maxsize=256,
+                  constraints=[RQLUniqueConstraint('S name N, S transition_of WF, Y transition_of WF, Y name N', 'Y',
+                                                   _('workflow already have a transition of that name'))])
     type = String(vocabulary=(_('normal'), _('auto')), default='normal')
     description = RichString(fulltextindexed=True,
                          description=_('semantic description of this transition'))
@@ -83,7 +88,8 @@
                                                   'allowed to pass this transition'))
     transition_of = SubjectRelation('Workflow', cardinality='1*', composite='object',
                                     description=_('workflow to which this transition belongs'),
-                                    constraints=[RQLUniqueConstraint('S name N, Y transition_of O, Y name N', 'Y')])
+                                    constraints=[RQLUniqueConstraint('S name N, Y transition_of O, Y name N', 'Y',
+                                                                     _('workflow already have a state of that name'))])
 
 
 class Transition(BaseTransition):