[Zope-CVS] CVS: Packages3/workflow/stateful/tests - test_instance.py:1.1 test_definition.py:1.2

Ulrich Eck ueck@net-labs.de
Wed, 5 Feb 2003 20:10:06 -0500


Update of /cvs-repository/Packages3/workflow/stateful/tests
In directory cvs.zope.org:/tmp/cvs-serv29874/stateful/tests

Modified Files:
	test_definition.py 
Added Files:
	test_instance.py 
Log Message:
- new ProcessDefinitionElementContainer
- extended tests
- Stateful ProcessDefinition/Instance:
  + basic functionality for Instance (getOutgoingTransitions/fireTransition)
  - no relevant-data, conditions yet
- Added views for basic TTW interaction


=== Added File Packages3/workflow/stateful/tests/test_instance.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

Revision information:
$Id: test_instance.py,v 1.1 2003/02/06 01:09:33 jack-e Exp $
"""

import unittest

from zope.interface import Interface
from zope.interface.verify import verifyClass

from zope.proxy.context import ContextWrapper
from zope.app.traversing import traverse
from zope.app.container.zopecontainer import ZopeContainerAdapter
from zope.app.services.service import ServiceManager
from zope.app.services.service import ServiceConfiguration
from zope.app.interfaces.dependable import IDependable
from zope.app.services.tests.placefulsetup \
     import PlacefulSetup
from zope.app.interfaces.annotation import IAnnotatable
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.attributeannotations import AttributeAnnotations
from zope.app.interfaces.annotation import IAnnotations
from zope.app.interfaces.dependable import IDependable
from zope.app.dependable import Dependable
from zope.component.adapter import provideAdapter
from zope.app.interfaces.services.configuration \
     import Active, Unregistered, Registered

from zope.app.workflow.service import WorkflowService
from zope.app.workflow.service import ProcessDefinitionConfiguration
from zope.app.interfaces.workflow.stateful \
     import IStatefulProcessInstance
from zope.app.workflow.stateful.definition \
     import StatefulProcessDefinition, State, Transition
from zope.app.workflow.stateful.instance \
     import StatefulProcessInstance


class WorkflowServiceForTests(WorkflowService):

    __implements__ = WorkflowService.__implements__, IAttributeAnnotatable


# define and create ProcessDefinition (PD) for tests
class TestProcessDefinition(StatefulProcessDefinition):
    __implements__ = IAttributeAnnotatable, StatefulProcessDefinition.__implements__

   
def sort(l):
    l.sort()
    return l

class SimpleProcessInstanceTests(PlacefulSetup, unittest.TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)

        provideAdapter(IAttributeAnnotatable,
                       IAnnotations, AttributeAnnotations)
        provideAdapter(IAnnotatable, IDependable, Dependable)

        # Set up a local workflow service
        self.buildFolders()
        self.rootFolder.setServiceManager(ServiceManager())

        self.default = traverse(self.rootFolder,
                                '++etc++Services/Packages/default')
        self.default.setObject('workflow_srv', WorkflowServiceForTests())
        self.service = traverse(self.default, 'workflow_srv')

        self.cm = ZopeContainerAdapter(traverse(self.default, "configure"))
        self.cm.setObject('', ServiceConfiguration('Workflows',
                                '/++etc++Services/Packages/default/workflow_srv'))
        traverse(self.default, 'configure/1').status = Active

        pd = TestProcessDefinition()

        pd.states.setObject('private', State())
        pd.states.setObject('published', State())
        pd.states.setObject('pending', State())
        
        pd.transitions.setObject('show',
                                 Transition('INITIAL', 'private', None))
        pd.transitions.setObject('publish_direct',
                                 Transition('private', 'published', None))
        pd.transitions.setObject('publish_pending',
                                 Transition('pending', 'published', None))
        pd.transitions.setObject('submit_pending',
                                 Transition('private', 'pending', None))
        pd.transitions.setObject('retract_published',
                                 Transition('published', 'private', None))
        pd.transitions.setObject('retract_pending',
                                 Transition('pending', 'private', None))

        self.default.setObject('pd1', pd )

        self.cm.setObject('', ProcessDefinitionConfiguration('definition1',
                                '/++etc++Services/Packages/default/pd1'))
        traverse(self.default, 'configure/2').status = Active

        self.pd = self.service.getProcessDefinition('definition1')
        # give the pi some context to find a service
        self.pi = ContextWrapper(self.service.createProcessInstance('definition1'),
                                 self.rootFolder)


    def testInterface(self):
        verifyClass(IStatefulProcessInstance, StatefulProcessInstance)

    def testSimpleTranstitions(self):
        pi = self.pi
        pd = self.pd
        self.assertEqual(pi.status, pd.getInitialStateName())
        self.assertEqual(pi.getOutgoingTransitions(), ['show'])
        
        pi.fireTransition('show')
        self.assertEqual(pi.status, 'private')
        self.assertEqual(sort(pi.getOutgoingTransitions()),
                         ['publish_direct', 'submit_pending'])

        pi.fireTransition('submit_pending')
        self.assertEqual(pi.status, 'pending')
        self.assertEqual(sort(pi.getOutgoingTransitions()),
                         ['publish_pending', 'retract_pending'])

        pi.fireTransition('publish_pending')
        self.assertEqual(pi.status, 'published')
        self.assertEqual(sort(pi.getOutgoingTransitions()),
                         ['retract_published'])

        pi.fireTransition('retract_published')
        self.assertEqual(pi.status, 'private')

        pi.fireTransition('submit_pending')
        self.assertEqual(pi.status, 'pending')

        pi.fireTransition('retract_pending')
        self.assertEqual(pi.status, 'private')


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(
        SimpleProcessInstanceTests))
    return suite

if __name__ == '__main__':
    unittest.main()


=== Packages3/workflow/stateful/tests/test_definition.py 1.1 => 1.2 ===
--- Packages3/workflow/stateful/tests/test_definition.py:1.1	Tue Feb  4 16:42:20 2003
+++ Packages3/workflow/stateful/tests/test_definition.py	Wed Feb  5 20:09:33 2003
@@ -112,13 +112,91 @@
         self.failIf('tr1' in names)
         self.failUnless('tr2' in names)
 
+    # this needs a rather complicated setup
+    # that is done in the test_instance.py tests
+    # can we leave this test out in here ??
     def tobedone_testCreateProcessInstance(self):
         pass
 
+class StatefulProcessDefinitionAttributesTests(unittest.TestCase):
+
+    def setUp(self):
+        self.pd = pd = StatefulProcessDefinition()
+        self.doMinimalSetup()
+
+    def doMinimalSetup(self):
+        pd = self.pd
+        self.st1 = st1 = DummyState()
+        self.st2 = st2 = DummyState()
+        pd.states.setObject('st1', st1)
+        pd.states.setObject('st2', st2)
+
+        self.tr1 = tr1 = DummyTransition()
+        self.tr2 = tr2 = DummyTransition()
+        pd.transitions.setObject('tr1', tr1)
+        pd.transitions.setObject('tr2', tr2)
+
+    def testInterface(self):
+        verifyClass(IStatefulProcessDefinition, StatefulProcessDefinition)
+
+    def testGetStateNames(self):
+        pd = self.pd
+
+        names = pd.states.keys()
+
+        self.failUnless('st1' in names)
+        self.failUnless('st2' in names)
+        self.failUnless(pd.getInitialStateName() in names)
+
+    def testGetState(self):
+        pd = self.pd
+
+        st1 = pd.states['st1']
+
+        self.assertEqual(st1, self.st1)
+
+    def testRemoveState(self):
+        pd = self.pd
+
+        del pd.states['st1']
+        names = pd.getStateNames()
+
+        self.assertRaises(KeyError, pd.states.__getitem__, 'st1')
+        self.failIf('st1' in names)
+        self.failUnless('st2' in names)
+        self.failUnless(pd.getInitialStateName() in names)
+        
+    def testGetTransistionNames(self):
+        pd = self.pd
+
+        names = pd.transitions.keys()
+
+        self.failUnless('tr1' in names)
+        self.failUnless('tr2' in names)
+
+    def testGetTransation(self):
+        pd = self.pd
+
+        tr1 = pd.transitions['tr1']
+
+        self.assertEqual(tr1, self.tr1)
+
+    def testRemoveTransistion(self):
+        pd = self.pd
+
+        del pd.transitions['tr1']
+        names = pd.transitions.keys()
+
+        self.assertRaises(KeyError, pd.transitions.__getitem__, 'tr1')
+        self.failIf('tr1' in names)
+        self.failUnless('tr2' in names)
+
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(
-        StatefulProcessDefinitionTests))
+        StatefulProcessDefinitionTests, StatefulProcessDefinitionAttributesTests))
     return suite
 
 if __name__ == '__main__':