entities/test/unittest_wfobjs.py
author Rémi Cardona <remi.cardona@logilab.fr>
Mon, 12 Oct 2015 21:06:45 +0200
changeset 10733 9f66ac5eec1e
parent 10663 54b8a1f249fb
permissions -rw-r--r--
[py3k] http headers should be ascii-only unicode objects
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5056
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6796
e70ca9abfc51 [unittest2] update to unittest2 assertRaises api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    18
6868
2d40f3c48f31 [tests] use migration api to create workflow
Julien Jehannet <julien@smaf.org>
parents: 6796
diff changeset
    19
from cubicweb import ValidationError
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
    20
from cubicweb.devtools.testlib import CubicWebTC
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    22
def add_wf(shell, etype, name=None, default=False):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    23
    if name is None:
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
    24
        name = etype
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    25
    return shell.add_workflow(name, etype, default=default,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    26
                              ensure_workflowable=False)
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    27
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    28
def parse_hist(wfhist):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    29
    return [(ti.previous_state.name, ti.new_state.name,
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    30
             ti.transition and ti.transition.name, ti.comment)
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    31
            for ti in wfhist]
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    32
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
    33
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
    34
class WorkflowBuildingTC(CubicWebTC):
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    35
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    36
    def test_wf_construction(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    37
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    38
            wf = add_wf(shell, 'Company')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    39
            foo = wf.add_state(u'foo', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    40
            bar = wf.add_state(u'bar')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    41
            self.assertEqual(wf.state_by_name('bar').eid, bar.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    42
            self.assertEqual(wf.state_by_name('barrr'), None)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    43
            baz = wf.add_transition(u'baz', (foo,), bar, ('managers',))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    44
            self.assertEqual(wf.transition_by_name('baz').eid, baz.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    45
            self.assertEqual(len(baz.require_group), 1)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    46
            self.assertEqual(baz.require_group[0].name, 'managers')
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    47
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    48
    def test_duplicated_state(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    49
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    50
            wf = add_wf(shell, 'Company')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    51
            wf.add_state(u'foo', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    52
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    53
            with self.assertRaises(ValidationError) as cm:
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    54
                wf.add_state(u'foo')
10280
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    55
            self.assertEqual({'name': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    56
                              'state_of': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    57
                              '': u'some relations violate a unicity constraint'},
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    58
                             cm.exception.errors)
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    59
            shell.rollback()
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    60
            # no pb if not in the same workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    61
            wf2 = add_wf(shell, 'Company')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    62
            foo = wf2.add_state(u'foo', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    63
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    64
            # gnark gnark
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    65
            bar = wf.add_state(u'bar')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    66
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    67
            with self.assertRaises(ValidationError) as cm:
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    68
                bar.cw_set(name=u'foo')
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    69
            shell.rollback()
10280
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    70
            self.assertEqual({'name': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    71
                              'state_of': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    72
                              '': u'some relations violate a unicity constraint'},
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    73
                             cm.exception.errors)
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    74
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    75
    def test_duplicated_transition(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    76
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    77
            wf = add_wf(shell, 'Company')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    78
            foo = wf.add_state(u'foo', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    79
            bar = wf.add_state(u'bar')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    80
            wf.add_transition(u'baz', (foo,), bar, ('managers',))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    81
            with self.assertRaises(ValidationError) as cm:
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    82
                wf.add_transition(u'baz', (bar,), foo)
10280
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    83
            self.assertEqual({'name': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    84
                              'transition_of': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
    85
                              '': u'some relations violate a unicity constraint'},
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    86
                             cm.exception.errors)
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    87
            shell.rollback()
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    88
            # no pb if not in the same workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    89
            wf2 = add_wf(shell, 'Company')
10353
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10280
diff changeset
    90
            foo = wf2.add_state(u'foo', initial=True)
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10280
diff changeset
    91
            bar = wf2.add_state(u'bar')
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10280
diff changeset
    92
            wf2.add_transition(u'baz', (foo,), bar, ('managers',))
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    93
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    94
            # gnark gnark
10353
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10280
diff changeset
    95
            biz = wf2.add_transition(u'biz', (bar,), foo)
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    96
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
    97
            with self.assertRaises(ValidationError) as cm:
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    98
                biz.cw_set(name=u'baz')
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
    99
            shell.rollback()
10280
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
   100
            self.assertEqual({'name': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
   101
                              'transition_of': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10092
diff changeset
   102
                              '': u'some relations violate a unicity constraint'},
10092
f0363da0b5a0 [schema] stop using RQLUniqueConstraint (closes #3753250)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9752
diff changeset
   103
                             cm.exception.errors)
2939
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   104
a613cc003ab7 make state/transition name unique within a workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   105
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
   106
class WorkflowTC(CubicWebTC):
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   108
    def setup_database(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
        rschema = self.schema['in_state']
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10374
diff changeset
   110
        for rdef in rschema.rdefs.values():
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   111
            self.assertEqual(rdef.cardinality, '1*')
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   112
        with self.admin_access.client_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   113
            self.member_eid = self.create_user(cnx, 'member').eid
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   114
            cnx.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   115
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
    def test_workflow_base(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   117
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   118
            e = self.create_user(req, 'toto')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   119
            iworkflowable = e.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   120
            self.assertEqual(iworkflowable.state, 'activated')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   121
            iworkflowable.change_state('deactivated', u'deactivate 1')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   122
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   123
            iworkflowable.change_state('activated', u'activate 1')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   124
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   125
            iworkflowable.change_state('deactivated', u'deactivate 2')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   126
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   127
            e.cw_clear_relation_cache('wf_info_for', 'object')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   128
            self.assertEqual([tr.comment for tr in e.reverse_wf_info_for],
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   129
                              ['deactivate 1', 'activate 1', 'deactivate 2'])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   130
            self.assertEqual(iworkflowable.latest_trinfo().comment, 'deactivate 2')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
    def test_possible_transitions(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   133
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   134
            user = req.execute('CWUser X').get_entity(0, 0)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   135
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   136
            trs = list(iworkflowable.possible_transitions())
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   137
            self.assertEqual(len(trs), 1)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   138
            self.assertEqual(trs[0].name, u'deactivate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   139
            self.assertEqual(trs[0].destination(None).name, u'deactivated')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
        # test a std user get no possible transition
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   141
        with self.new_access('member').web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   142
            # fetch the entity using the new session
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   143
            trs = list(req.user.cw_adapt_to('IWorkflowable').possible_transitions())
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   144
            self.assertEqual(len(trs), 0)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
    def _test_manager_deactivate(self, user):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5538
diff changeset
   147
        iworkflowable = user.cw_adapt_to('IWorkflowable')
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   148
        user.cw_clear_relation_cache('in_state', 'subject')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   149
        self.assertEqual(len(user.in_state), 1)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   150
        self.assertEqual(iworkflowable.state, 'deactivated')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5538
diff changeset
   151
        trinfo = iworkflowable.latest_trinfo()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   152
        self.assertEqual(trinfo.previous_state.name, 'activated')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   153
        self.assertEqual(trinfo.new_state.name, 'deactivated')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   154
        self.assertEqual(trinfo.comment, 'deactivate user')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5557
diff changeset
   155
        self.assertEqual(trinfo.comment_format, 'text/plain')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
        return trinfo
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
    def test_change_state(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   159
        with self.admin_access.client_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   160
            user = cnx.user
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   161
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   162
            iworkflowable.change_state('deactivated', comment=u'deactivate user')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   163
            trinfo = self._test_manager_deactivate(user)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   164
            self.assertEqual(trinfo.transition, None)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   166
    def test_set_in_state_bad_wf(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   167
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   168
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   169
            s = wf.add_state(u'foo', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   170
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   171
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   172
            with cnx.security_enabled(write=False):
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   173
                with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   174
                    cnx.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   175
                                {'x': cnx.user.eid, 's': s.eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   176
                self.assertEqual(cm.exception.errors, {'in_state-subject': "state doesn't belong to entity's workflow. "
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   177
                                          "You may want to set a custom workflow for this entity first."})
2983
b458631fe347 [wf] test in_state consistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2982
diff changeset
   178
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
    def test_fire_transition(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   180
        with self.admin_access.client_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   181
            user = cnx.user
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   182
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   183
            iworkflowable.fire_transition('deactivate', comment=u'deactivate user')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   184
            user.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   185
            self.assertEqual(iworkflowable.state, 'deactivated')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   186
            self._test_manager_deactivate(user)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   187
            trinfo = self._test_manager_deactivate(user)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   188
            self.assertEqual(trinfo.transition.name, 'deactivate')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   189
4644
021035b9a7ab introduce 'go back' transition: transition without destination state will go to the state we were coming from
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4030
diff changeset
   190
    def test_goback_transition(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   191
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   192
            wf = req.user.cw_adapt_to('IWorkflowable').current_workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   193
            asleep = wf.add_state('asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   194
            wf.add_transition('rest', (wf.state_by_name('activated'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   195
                                       wf.state_by_name('deactivated')),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   196
                              asleep)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   197
            wf.add_transition('wake up', asleep)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   198
            user = self.create_user(req, 'stduser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   199
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   200
            iworkflowable.fire_transition('rest')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   201
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   202
            iworkflowable.fire_transition('wake up')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   203
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   204
            self.assertEqual(iworkflowable.state, 'activated')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   205
            iworkflowable.fire_transition('deactivate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   206
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   207
            iworkflowable.fire_transition('rest')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   208
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   209
            iworkflowable.fire_transition('wake up')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   210
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   211
            user.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   212
            self.assertEqual(iworkflowable.state, 'deactivated')
4644
021035b9a7ab introduce 'go back' transition: transition without destination state will go to the state we were coming from
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4030
diff changeset
   213
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   214
    # XXX test managers can change state without matching transition
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   215
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
    def _test_stduser_deactivate(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   217
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   218
            self.create_user(cnx, 'tutu')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   219
        with self.new_access('tutu').web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   220
            iworkflowable = req.entity_from_eid(self.member_eid).cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   221
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   222
                iworkflowable.fire_transition('deactivate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   223
            self.assertEqual(cm.exception.errors, {'by_transition-subject': "transition may not be fired"})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   224
        with self.new_access('member').web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   225
            iworkflowable = req.entity_from_eid(self.member_eid).cw_adapt_to('IWorkflowable')
6796
e70ca9abfc51 [unittest2] update to unittest2 assertRaises api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
   226
            iworkflowable.fire_transition('deactivate')
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   227
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   228
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   229
                iworkflowable.fire_transition('activate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   230
            self.assertEqual(cm.exception.errors, {'by_transition-subject': "transition may not be fired"})
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   231
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   232
    def test_fire_transition_owned_by(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   233
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   234
            cnx.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   235
                        'X expression "X owned_by U", T condition X '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   236
                        'WHERE T name "deactivate"')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   237
            cnx.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   238
        self._test_stduser_deactivate()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   239
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   240
    def test_fire_transition_has_update_perm(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   241
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   242
            cnx.execute('INSERT RQLExpression X: X exprtype "ERQLExpression", '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   243
                        'X expression "U has_update_permission X", T condition X '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   244
                        'WHERE T name "deactivate"')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   245
            cnx.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   246
        self._test_stduser_deactivate()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   247
3628
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   248
    def test_swf_base(self):
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   249
        """subworkflow
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   250
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   251
        +-----------+  tr1   +-----------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   252
        | swfstate1 | ------>| swfstate2 |
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   253
        +-----------+        +-----------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   254
                  |  tr2  +-----------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   255
                  `------>| swfstate3 |
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   256
                          +-----------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   257
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   258
        main workflow
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   259
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   260
        +--------+  swftr1             +--------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   261
        | state1 | -------[swfstate2]->| state2 |
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   262
        +--------+     |               +--------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   263
                       |               +--------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   264
                       `-[swfstate3]-->| state3 |
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   265
                                       +--------+
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   266
        """
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   267
        # sub-workflow
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   268
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   269
            swf = add_wf(shell, 'CWGroup', name='subworkflow')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   270
            swfstate1 = swf.add_state(u'swfstate1', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   271
            swfstate2 = swf.add_state(u'swfstate2')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   272
            swfstate3 = swf.add_state(u'swfstate3')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   273
            tr1 = swf.add_transition(u'tr1', (swfstate1,), swfstate2)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   274
            tr2 = swf.add_transition(u'tr2', (swfstate1,), swfstate3)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   275
            # main workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   276
            mwf = add_wf(shell, 'CWGroup', name='main workflow', default=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   277
            state1 = mwf.add_state(u'state1', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   278
            state2 = mwf.add_state(u'state2')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   279
            state3 = mwf.add_state(u'state3')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   280
            swftr1 = mwf.add_wftransition(u'swftr1', swf, state1,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   281
                                          [(swfstate2, state2), (swfstate3, state3)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   282
            swf.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   283
            self.assertEqual(swftr1.destination(None).eid, swfstate1.eid)
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   284
        # workflows built, begin test
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   285
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   286
            group = req.create_entity('CWGroup', name=u'grp1')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   287
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   288
            iworkflowable = group.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   289
            self.assertEqual(iworkflowable.current_state.eid, state1.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   290
            self.assertEqual(iworkflowable.current_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   291
            self.assertEqual(iworkflowable.main_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   292
            self.assertEqual(iworkflowable.subworkflow_input_transition(), None)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   293
            iworkflowable.fire_transition('swftr1', u'go')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   294
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   295
            group.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   296
            self.assertEqual(iworkflowable.current_state.eid, swfstate1.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   297
            self.assertEqual(iworkflowable.current_workflow.eid, swf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   298
            self.assertEqual(iworkflowable.main_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   299
            self.assertEqual(iworkflowable.subworkflow_input_transition().eid, swftr1.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   300
            iworkflowable.fire_transition('tr1', u'go')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   301
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   302
            group.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   303
            self.assertEqual(iworkflowable.current_state.eid, state2.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   304
            self.assertEqual(iworkflowable.current_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   305
            self.assertEqual(iworkflowable.main_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   306
            self.assertEqual(iworkflowable.subworkflow_input_transition(), None)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   307
            # force back to swfstate1 is impossible since we can't any more find
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   308
            # subworkflow input transition
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   309
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   310
                iworkflowable.change_state(swfstate1, u'gadget')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   311
            self.assertEqual(cm.exception.errors, {'to_state-subject': "state doesn't belong to entity's workflow"})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   312
            req.cnx.rollback()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   313
            # force back to state1
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   314
            iworkflowable.change_state('state1', u'gadget')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   315
            iworkflowable.fire_transition('swftr1', u'au')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   316
            group.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   317
            iworkflowable.fire_transition('tr2', u'chapeau')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   318
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   319
            group.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   320
            self.assertEqual(iworkflowable.current_state.eid, state3.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   321
            self.assertEqual(iworkflowable.current_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   322
            self.assertEqual(iworkflowable.main_workflow.eid, mwf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   323
            self.assertListEqual(parse_hist(iworkflowable.workflow_history),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   324
                                  [('state1', 'swfstate1', 'swftr1', 'go'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   325
                                   ('swfstate1', 'swfstate2', 'tr1', 'go'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   326
                                   ('swfstate2', 'state2', 'swftr1', 'exiting from subworkflow subworkflow'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   327
                                   ('state2', 'state1', None, 'gadget'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   328
                                   ('state1', 'swfstate1', 'swftr1', 'au'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   329
                                   ('swfstate1', 'swfstate3', 'tr2', 'chapeau'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   330
                                   ('swfstate3', 'state3', 'swftr1', 'exiting from subworkflow subworkflow'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   331
                                   ])
2992
a5b8bf107a1a [wf] test and hooks for WorkflowTransition support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2985
diff changeset
   332
3628
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   333
    def test_swf_exit_consistency(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   334
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   335
            # sub-workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   336
            swf = add_wf(shell, 'CWGroup', name='subworkflow')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   337
            swfstate1 = swf.add_state(u'swfstate1', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   338
            swfstate2 = swf.add_state(u'swfstate2')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   339
            tr1 = swf.add_transition(u'tr1', (swfstate1,), swfstate2)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   340
            # main workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   341
            mwf = add_wf(shell, 'CWGroup', name='main workflow', default=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   342
            state1 = mwf.add_state(u'state1', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   343
            state2 = mwf.add_state(u'state2')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   344
            state3 = mwf.add_state(u'state3')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   345
            mwf.add_wftransition(u'swftr1', swf, state1,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   346
                                 [(swfstate2, state2), (swfstate2, state3)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   347
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   348
                shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   349
            self.assertEqual(cm.exception.errors, {'subworkflow_exit-subject': u"can't have multiple exits on the same state"})
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   350
3628
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   351
    def test_swf_fire_in_a_row(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   352
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   353
            # sub-workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   354
            subwf = add_wf(shell, 'CWGroup', name='subworkflow')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   355
            xsigning = subwf.add_state('xsigning', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   356
            xaborted = subwf.add_state('xaborted')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   357
            xsigned = subwf.add_state('xsigned')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   358
            xabort = subwf.add_transition('xabort', (xsigning,), xaborted)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   359
            xsign = subwf.add_transition('xsign', (xsigning,), xsigning)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   360
            xcomplete = subwf.add_transition('xcomplete', (xsigning,), xsigned,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   361
                                             type=u'auto')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   362
            # main workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   363
            twf = add_wf(shell, 'CWGroup', name='mainwf', default=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   364
            created    = twf.add_state(_('created'), initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   365
            identified = twf.add_state(_('identified'))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   366
            released   = twf.add_state(_('released'))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   367
            closed   = twf.add_state(_('closed'))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   368
            twf.add_wftransition(_('identify'), subwf, (created,),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   369
                                 [(xsigned, identified), (xaborted, created)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   370
            twf.add_wftransition(_('release'), subwf, (identified,),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   371
                                 [(xsigned, released), (xaborted, identified)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   372
            twf.add_wftransition(_('close'), subwf, (released,),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   373
                                 [(xsigned, closed), (xaborted, released)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   374
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   375
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   376
            group = cnx.create_entity('CWGroup', name=u'grp1')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   377
            cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   378
            iworkflowable = group.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   379
            for trans in ('identify', 'release', 'close'):
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   380
                iworkflowable.fire_transition(trans)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   381
                cnx.commit()
3628
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   382
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   383
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   384
    def test_swf_magic_tr(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   385
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   386
            # sub-workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   387
            subwf = add_wf(shell, 'CWGroup', name='subworkflow')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   388
            xsigning = subwf.add_state('xsigning', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   389
            xaborted = subwf.add_state('xaborted')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   390
            xsigned = subwf.add_state('xsigned')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   391
            xabort = subwf.add_transition('xabort', (xsigning,), xaborted)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   392
            xsign = subwf.add_transition('xsign', (xsigning,), xsigned)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   393
            # main workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   394
            twf = add_wf(shell, 'CWGroup', name='mainwf', default=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   395
            created    = twf.add_state(_('created'), initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   396
            identified = twf.add_state(_('identified'))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   397
            released   = twf.add_state(_('released'))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   398
            twf.add_wftransition(_('identify'), subwf, created,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   399
                                 [(xaborted, None), (xsigned, identified)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   400
            twf.add_wftransition(_('release'), subwf, identified,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   401
                                 [(xaborted, None)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   402
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   403
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   404
            group = req.create_entity('CWGroup', name=u'grp1')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   405
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   406
            iworkflowable = group.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   407
            for trans, nextstate in (('identify', 'xsigning'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   408
                                     ('xabort', 'created'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   409
                                     ('identify', 'xsigning'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   410
                                     ('xsign', 'identified'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   411
                                     ('release', 'xsigning'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   412
                                     ('xabort', 'identified')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   413
                                     ):
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   414
                iworkflowable.fire_transition(trans)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   415
                req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   416
                group.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   417
                self.assertEqual(iworkflowable.state, nextstate)
3628
440931181322 [wf engine] support for subwf exit point with no destination state: go back to state from which we entered into the subworkflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3617
diff changeset
   418
10374
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   419
    def test_replace_state(self):
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   420
        with self.admin_access.shell() as shell:
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   421
            wf = add_wf(shell, 'CWGroup', name='groupwf', default=True)
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   422
            s_new = wf.add_state('new', initial=True)
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   423
            s_state1 = wf.add_state('state1')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   424
            wf.add_transition('tr', (s_new,), s_state1)
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   425
            shell.commit()
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   426
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   427
        with self.admin_access.repo_cnx() as cnx:
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   428
            group = cnx.create_entity('CWGroup', name=u'grp1')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   429
            cnx.commit()
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   430
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   431
            iwf = group.cw_adapt_to('IWorkflowable')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   432
            iwf.fire_transition('tr')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   433
            cnx.commit()
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   434
            group.cw_clear_all_caches()
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   435
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   436
            wf = cnx.entity_from_eid(wf.eid)
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   437
            wf.add_state('state2')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   438
            with cnx.security_enabled(write=False):
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   439
                wf.replace_state('state1', 'state2')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   440
            cnx.commit()
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   441
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   442
            self.assertEqual(iwf.state, 'state2')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   443
            self.assertEqual(iwf.latest_trinfo().to_state[0].name, 'state2')
699e49d76334 [wf] Fix Workflow.replace_state()
Christophe de Vienne <christophe@unlish.com>
parents: 10354
diff changeset
   444
2954
48507919b6e3 [wf] oops, BaseTransition.__init__ may be called for subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2949
diff changeset
   445
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
   446
class CustomWorkflowTC(CubicWebTC):
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   447
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   448
    def setup_database(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   449
        with self.admin_access.repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   450
            self.member_eid = self.create_user(cnx, 'member').eid
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   451
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   452
    def test_custom_wf_replace_state_no_history(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   453
        """member in inital state with no previous history, state is simply
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   454
        redirected when changing workflow
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   455
        """
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   456
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   457
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   458
            wf.add_state('asleep', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   459
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   460
            req.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   461
                     {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   462
            member = req.entity_from_eid(self.member_eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   463
            iworkflowable = member.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   464
            self.assertEqual(iworkflowable.state, 'activated') # no change before commit
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   465
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   466
            member.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   467
            self.assertEqual(iworkflowable.current_workflow.eid, wf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   468
            self.assertEqual(iworkflowable.state, 'asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   469
            self.assertEqual(iworkflowable.workflow_history, ())
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   470
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   471
    def test_custom_wf_replace_state_keep_history(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   472
        """member in inital state with some history, state is redirected and
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   473
        state change is recorded to history
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   474
        """
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   475
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   476
            member = req.entity_from_eid(self.member_eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   477
            iworkflowable = member.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   478
            iworkflowable.fire_transition('deactivate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   479
            iworkflowable.fire_transition('activate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   480
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   481
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   482
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   483
            wf.add_state('asleep', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   484
            shell.rqlexec('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   485
                          {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   486
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   487
            member = req.entity_from_eid(self.member_eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   488
            iworkflowable = member.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   489
            self.assertEqual(iworkflowable.current_workflow.eid, wf.eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   490
            self.assertEqual(iworkflowable.state, 'asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   491
            self.assertEqual(parse_hist(iworkflowable.workflow_history),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   492
                             [('activated', 'deactivated', 'deactivate', None),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   493
                              ('deactivated', 'activated', 'activate', None),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   494
                              ('activated', 'asleep', None, 'workflow changed to "CWUser"')])
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   495
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   496
    def test_custom_wf_no_initial_state(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   497
        """try to set a custom workflow which has no initial state"""
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   498
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   499
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   500
            wf.add_state('asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   501
            shell.rqlexec('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   502
                          {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   503
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   504
                shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   505
            self.assertEqual(cm.exception.errors, {'custom_workflow-subject': u'workflow has no initial state'})
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   506
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   507
    def test_custom_wf_bad_etype(self):
2985
79185b3ccf2c cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2984
diff changeset
   508
        """try to set a custom workflow which doesn't apply to entity type"""
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   509
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   510
            wf = add_wf(shell, 'Company')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   511
            wf.add_state('asleep', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   512
            shell.rqlexec('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   513
                         {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   514
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   515
                shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   516
            self.assertEqual(cm.exception.errors, {'custom_workflow-subject': u"workflow isn't a workflow for this type"})
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   517
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   518
    def test_del_custom_wf(self):
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   519
        """member in some state shared by the new workflow, nothing has to be
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   520
        done
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   521
        """
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   522
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   523
            member = req.entity_from_eid(self.member_eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   524
            iworkflowable = member.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   525
            iworkflowable.fire_transition('deactivate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   526
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   527
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   528
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   529
            wf.add_state('asleep', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   530
            shell.rqlexec('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   531
                         {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   532
            shell.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   533
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   534
            req.execute('DELETE X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   535
                         {'wf': wf.eid, 'x': self.member_eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   536
            member = req.entity_from_eid(self.member_eid)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   537
            iworkflowable = member.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   538
            self.assertEqual(iworkflowable.state, 'asleep')# no change before commit
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   539
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   540
            member.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   541
            self.assertEqual(iworkflowable.current_workflow.name, "default user workflow")
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   542
            self.assertEqual(iworkflowable.state, 'activated')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   543
            self.assertEqual(parse_hist(iworkflowable.workflow_history),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   544
                              [('activated', 'deactivated', 'deactivate', None),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   545
                               ('deactivated', 'asleep', None, 'workflow changed to "CWUser"'),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   546
                               ('asleep', 'activated', None, 'workflow changed to "default user workflow"'),])
2949
a2aa2c51f3be test and implements workflow changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2939
diff changeset
   547
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   548
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3230 3528
diff changeset
   549
class AutoTransitionTC(CubicWebTC):
3528
77a69de16709 support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3206
diff changeset
   550
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   551
    def setup_custom_wf(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   552
        with self.admin_access.shell() as shell:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   553
            wf = add_wf(shell, 'CWUser')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   554
            asleep = wf.add_state('asleep', initial=True)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   555
            dead = wf.add_state('dead')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   556
            wf.add_transition('rest', asleep, asleep)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   557
            wf.add_transition('sick', asleep, dead, type=u'auto',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   558
                              conditions=({'expr': u'X surname "toto"',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   559
                                           'mainvars': u'X'},))
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   560
        return wf
3528
77a69de16709 support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3206
diff changeset
   561
77a69de16709 support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3206
diff changeset
   562
    def test_auto_transition_fired(self):
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   563
        wf = self.setup_custom_wf()
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   564
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   565
            user = self.create_user(req, 'member')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   566
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   567
            req.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   568
                        {'wf': wf.eid, 'x': user.eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   569
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   570
            user.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   571
            self.assertEqual(iworkflowable.state, 'asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   572
            self.assertEqual([t.name for t in iworkflowable.possible_transitions()],
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   573
                              ['rest'])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   574
            iworkflowable.fire_transition('rest')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   575
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   576
            user.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   577
            self.assertEqual(iworkflowable.state, 'asleep')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   578
            self.assertEqual([t.name for t in iworkflowable.possible_transitions()],
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   579
                              ['rest'])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   580
            self.assertEqual(parse_hist(iworkflowable.workflow_history),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   581
                              [('asleep', 'asleep', 'rest', None)])
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   582
            user.cw_set(surname=u'toto') # fulfill condition
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   583
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   584
            iworkflowable.fire_transition('rest')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   585
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   586
            user.cw_clear_all_caches()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   587
            self.assertEqual(iworkflowable.state, 'dead')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   588
            self.assertEqual(parse_hist(iworkflowable.workflow_history),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   589
                              [('asleep', 'asleep', 'rest', None),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   590
                               ('asleep', 'asleep', 'rest', None),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   591
                               ('asleep', 'dead', 'sick', None),])
3528
77a69de16709 support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3206
diff changeset
   592
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   593
    def test_auto_transition_custom_initial_state_fired(self):
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   594
        wf = self.setup_custom_wf()
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   595
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   596
            user = self.create_user(req, 'member', surname=u'toto')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   597
            req.execute('SET X custom_workflow WF WHERE X eid %(x)s, WF eid %(wf)s',
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   598
                        {'wf': wf.eid, 'x': user.eid})
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   599
            req.cnx.commit()
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   600
            user.cw_clear_all_caches()
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   601
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   602
            self.assertEqual(iworkflowable.state, 'dead')
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   603
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   604
    def test_auto_transition_initial_state_fired(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   605
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   606
            wf = req.execute('Any WF WHERE ET default_workflow WF, '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   607
                             'ET name %(et)s', {'et': 'CWUser'}).get_entity(0, 0)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   608
            dead = wf.add_state('dead')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   609
            wf.add_transition('sick', wf.state_by_name('activated'), dead,
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   610
                              type=u'auto', conditions=({'expr': u'X surname "toto"',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   611
                                                         'mainvars': u'X'},))
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   612
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   613
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   614
            user = self.create_user(req, 'member', surname=u'toto')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   615
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   616
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   617
            self.assertEqual(iworkflowable.state, 'dead')
5056
5de07c77d73f [workflow] support for automatic transition starting from the initial state
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5030
diff changeset
   618
3528
77a69de16709 support for automatic transition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3206
diff changeset
   619
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
   620
class WorkflowHooksTC(CubicWebTC):
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   621
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   622
    def setUp(self):
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2954
diff changeset
   623
        CubicWebTC.setUp(self)
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   624
        with self.admin_access.web_request() as req:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   625
            self.wf = req.user.cw_adapt_to('IWorkflowable').current_workflow
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   626
            self.s_activated = self.wf.state_by_name('activated').eid
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   627
            self.s_deactivated = self.wf.state_by_name('deactivated').eid
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   628
            self.s_dummy = self.wf.add_state(u'dummy').eid
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   629
            self.wf.add_transition(u'dummy', (self.s_deactivated,), self.s_dummy)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   630
            ueid = self.create_user(req, 'stduser', commit=False).eid
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   631
            # test initial state is set
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   632
            rset = req.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   633
                                {'x' : ueid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   634
            self.assertFalse(rset, rset.rows)
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   635
            req.cnx.commit()
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   636
            initialstate = req.execute('Any N WHERE S name N, X in_state S, X eid %(x)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   637
                                        {'x' : ueid})[0][0]
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   638
            self.assertEqual(initialstate, u'activated')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   639
            # give access to users group on the user's wf transitions
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   640
            # so we can test wf enforcing on euser (managers don't have anymore this
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   641
            # enforcement
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   642
            req.execute('SET X require_group G '
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   643
                         'WHERE G name "users", X transition_of WF, WF eid %(wf)s',
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   644
                         {'wf': self.wf.eid})
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   645
            req.cnx.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   646
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4644
diff changeset
   647
    # XXX currently, we've to rely on hooks to set initial state, or to use execute
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   648
    # def test_initial_state(self):
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   649
    #     cnx = self.login('stduser')
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   650
    #     cu = cnx.cursor()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   651
    #     self.assertRaises(ValidationError, cu.execute,
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   652
    #                       'INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   653
    #                       'X in_state S WHERE S name "deactivated"', {'pwd': 'oops'})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   654
    #     cnx.close()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   655
    #     # though managers can do whatever he want
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   656
    #     self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, '
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   657
    #                  'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'})
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   658
    #     self.commit()
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   659
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   660
    # test that the workflow is correctly enforced
3879
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   661
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   662
    def _cleanup_msg(self, msg):
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   663
        """remove the variable part of one specific error message"""
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   664
        lmsg = msg.split()
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   665
        lmsg.pop(1)
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   666
        lmsg.pop()
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   667
        return ' '.join(lmsg)
7bb87da97026 fix tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 3628
diff changeset
   668
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   669
    def test_transition_checking1(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   670
        with self.new_access('stduser').repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   671
            user = cnx.user
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   672
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   673
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   674
                iworkflowable.fire_transition('activate')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   675
            self.assertEqual(self._cleanup_msg(cm.exception.errors['by_transition-subject']),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   676
                             u"transition isn't allowed from")
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   677
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   678
    def test_transition_checking2(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   679
        with self.new_access('stduser').repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   680
            user = cnx.user
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   681
            iworkflowable = user.cw_adapt_to('IWorkflowable')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   682
            with self.assertRaises(ValidationError) as cm:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   683
                iworkflowable.fire_transition('dummy')
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   684
            self.assertEqual(self._cleanup_msg(cm.exception.errors['by_transition-subject']),
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   685
                             u"transition isn't allowed from")
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   686
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   687
    def test_transition_checking3(self):
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   688
        with self.new_access('stduser').repo_cnx() as cnx:
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   689
            user = cnx.user
8577
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   690
            iworkflowable = user.cw_adapt_to('IWorkflowable')
6796
e70ca9abfc51 [unittest2] update to unittest2 assertRaises api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
   691
            iworkflowable.fire_transition('deactivate')
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   692
            cnx.commit()
8577
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   693
            with self.assertRaises(ValidationError) as cm:
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   694
                iworkflowable.fire_transition('deactivate')
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   695
            self.assertEqual(self._cleanup_msg(cm.exception.errors['by_transition-subject']),
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   696
                                                u"transition isn't allowed from")
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   697
            cnx.rollback()
8577
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   698
            # get back now
3f082de68802 [test] use session commit/rollback to be consistent with the test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8562
diff changeset
   699
            iworkflowable.fire_transition('activate')
9752
5df3ac51a91a [test] update entities/test/unittest_wfobjs to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9582
diff changeset
   700
            cnx.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   701
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   702
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   703
if __name__ == '__main__':
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   704
    from logilab.common.testlib import unittest_main
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   705
    unittest_main()