cubicweb/test/unittest_wfutils.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 11963 64ecd4d96ac7
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11963
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
#
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
# This file is part of CubicWeb.
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
#
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
# any later version.
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
#
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
# details.
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
#
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
import copy
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
from cubicweb.devtools import testlib
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
from cubicweb.wfutils import setup_workflow
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
class TestWFUtils(testlib.CubicWebTC):
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
    defs = {
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
        'group': {
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
            'etypes': 'CWGroup',
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
            'default': True,
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
            'initial_state': u'draft',
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
            'states': [u'draft', u'published'],
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
            'transitions': {
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
                u'publish': {
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
                    'fromstates': u'draft',
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
                    'tostate': u'published',
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
                    'requiredgroups': u'managers'
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
                }
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
            }
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
        }
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
    }
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
    def test_create_workflow(self):
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
        with self.admin_access.cnx() as cnx:
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
            wf = setup_workflow(cnx, 'group', self.defs['group'])
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
            self.assertEqual(wf.name, 'group')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
            self.assertEqual(wf.initial.name, u'draft')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
            draft = wf.state_by_name(u'draft')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
            self.assertIsNotNone(draft)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
            published = wf.state_by_name(u'published')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
            self.assertIsNotNone(published)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
            publish = wf.transition_by_name(u'publish')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
            self.assertIsNotNone(publish)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
            self.assertEqual(publish.destination_state, (published, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
            self.assertEqual(draft.allowed_transition, (publish, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
            self.assertEqual(
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
                {g.name for g in publish.require_group},
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
                {'managers'})
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    64
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    65
    def test_update(self):
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    66
        with self.admin_access.cnx() as cnx:
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
            wf = setup_workflow(cnx, 'group', self.defs['group'])
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
            eid = wf.eid
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
        with self.admin_access.cnx() as cnx:
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
            wfdef = copy.deepcopy(self.defs['group'])
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
            wfdef['states'].append('new')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
            wfdef['initial_state'] = 'new'
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
            wfdef['transitions'][u'publish']['fromstates'] = ('draft', 'new')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
            wfdef['transitions'][u'publish']['requiredgroups'] = (
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
                u'managers', u'users')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
            wfdef['transitions'][u'todraft'] = {
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
                'fromstates': ('new', 'published'),
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    79
                'tostate': 'draft',
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
            }
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
            wf = setup_workflow(cnx, 'group', wfdef)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    83
            self.assertEqual(wf.eid, eid)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    84
            self.assertEqual(wf.name, 'group')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    85
            self.assertEqual(wf.initial.name, u'new')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    86
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    87
            new = wf.state_by_name(u'new')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    88
            self.assertIsNotNone(new)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    89
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
            draft = wf.state_by_name(u'draft')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    91
            self.assertIsNotNone(draft)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    92
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    93
            published = wf.state_by_name(u'published')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    94
            self.assertIsNotNone(published)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    95
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    96
            publish = wf.transition_by_name(u'publish')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    97
            self.assertIsNotNone(publish)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    98
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    99
            todraft = wf.transition_by_name(u'todraft')
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   100
            self.assertIsNotNone(todraft)
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   101
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   102
            self.assertEqual(
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   103
                {g.name for g in publish.require_group},
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   104
                {'managers', 'users'})
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   105
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   106
            self.assertEqual(publish.destination_state, (published, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   107
            self.assertEqual(draft.allowed_transition, (publish, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   108
            self.assertEqual(todraft.destination_state, (draft, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   109
            self.assertEqual(published.allowed_transition, (todraft, ))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   110
            self.assertCountEqual(new.allowed_transition, (publish, todraft))
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   111
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   112
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   113
if __name__ == '__main__':
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   114
    import unittest
64ecd4d96ac7 [workflow] Utilities for declarative definition of workflows
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   115
    unittest.main()