[ADD] test_workflow: added some tests for the workflows.

bzr revid: vmt@openerp.com-20130722151740-mucw6i5vn38blh66
This commit is contained in:
Vo Minh Thu 2013-07-22 17:17:40 +02:00
parent 67ac805e51
commit 0568f5c653
7 changed files with 259 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
import models
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
{
'name': 'test-workflow',
'version': '0.1',
'category': 'Tests',
'description': """A module to play with workflows.""",
'author': 'OpenERP SA',
'maintainer': 'OpenERP SA',
'website': 'http://www.openerp.com',
'depends': ['base'],
'data': ['data.xml'],
'installable': True,
'auto_install': False,
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_test_workflow_model" model="ir.ui.view">
<field name="name">Test workflow</field>
<field name="model">test.workflow.model</field>
<field name="arch" type="xml">
<form string="Test workflow">
<button name="a-b" string="a-b" type="workflow" icon="gtk-ok" colspan="1"/>
<label string="a-b"/>
<button name="trigger" string="trigger" type="object" icon="gtk-ok" colspan="1"/>
<label string="trigger"/>
</form>
</field>
</record>
<record id="action_test_workflow" model="ir.actions.act_window">
<field name="name">Test workflow</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">test.workflow.model</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem icon="STOCK_PREFERENCES" id="base.menu_tests" name="Tests" sequence="1000000"/>
<menuitem id="menu_test_workflow" parent="base.menu_tests" name="Test workflow"/>
<menuitem id="menu_test_workflow_leaf"
name="Test workflow"
action="action_test_workflow"
parent="menu_test_workflow"/>
<record id="test_workflow_trigger_1" model="test.workflow.trigger">
<!-- A single trigger record, with known ID 1 -->
</record>
<!-- A simple workflow:
a -signal-> b -trigger-> c
-->
<record id="test_workflow" model="workflow">
<field name="name">test.workflow</field>
<field name="osv">test.workflow.model</field>
<field name="on_create">True</field>
</record>
<record id="activity_a" model="workflow.activity">
<field name="wkf_id" ref="test_workflow"/>
<field name="flow_start">True</field>
<field name="name">a</field>
<field name="kind">function</field>
<field name="action">print_a()</field>
</record>
<record id="activity_b" model="workflow.activity">
<field name="wkf_id" ref="test_workflow"/>
<field name="name">b</field>
<field name="kind">function</field>
<field name="action">print_b()</field>
</record>
<record id="activity_c" model="workflow.activity">
<field name="wkf_id" ref="test_workflow"/>
<field name="flow_stop">True</field>
<field name="name">c</field>
<field name="kind">function</field>
<field name="action">print_c()</field>
</record>
<record id="trans_a_b" model="workflow.transition">
<field name="act_from" ref="activity_a"/>
<field name="act_to" ref="activity_b"/>
<field name="signal">a-b</field>
</record>
<record id="trans_b_c" model="workflow.transition">
<field name="act_from" ref="activity_b"/>
<field name="act_to" ref="activity_c"/>
<field name="condition">condition()</field>
<field name="trigger_model">test.workflow.trigger</field>
<field name="trigger_expr_id">[1]</field>
</record>
</data>
</openerp>

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
import openerp
class m(openerp.osv.orm.Model):
""" A model for which we will define a worflow (see data.xml). """
_name = 'test.workflow.model'
def print_(self, cr, uid, ids, s, context=None):
print ' Running activity `%s` for record %s' % (s, ids)
return True
def print_a(self, cr, uid, ids, context=None):
return self.print_(cr, uid, ids, 'a', context)
def print_b(self, cr, uid, ids, context=None):
return self.print_(cr, uid, ids, 'b', context)
def print_c(self, cr, uid, ids, context=None):
return self.print_(cr, uid, ids, 'c', context)
def condition(self, cr, uid, ids, context=None):
m = self.pool['test.workflow.trigger']
for r in m.browse(cr, uid, [1], context=context):
if not r.value:
return False
return True
def trigger(self, cr, uid, context=None):
return openerp.workflow.trg_trigger(uid, 'test.workflow.trigger', 1, cr)
class n(openerp.osv.orm.Model):
""" A model used for the trigger feature. """
_name = 'test.workflow.trigger'
_columns = { 'value': openerp.osv.fields.boolean('Value') }
_defaults = { 'value': False }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,44 @@
#! /usr/bin/env python2
import os
import openerp
from openerp import SUPERUSER_ID
DATABASE='t'
def configure():
conf = openerp.tools.config
conf['addons_path'] = os.path.join(os.path.dirname(os.path.dirname(__file__))) + ",/home/thu/repos/web/trunk/addons"
conf['log_handler'] = [':CRITICAL']
openerp.modules.module.initialize_sys_path()
openerp.netsvc.init_logger()
if __name__ == '__main__':
configure()
print '> Loading registry `%s`...' % DATABASE
registry = registry = openerp.registry(DATABASE)
cr = registry.db.cursor()
print '< Registry `%s` loaded.' % DATABASE
model = registry['test.workflow.model']
trigger = registry['test.workflow.trigger']
print '> Creating new record...'
i = model.create(cr, SUPERUSER_ID, {})
print '< Record created with ID %s' % i
print '> Calling signal `a-b`...'
model.signal_workflow(cr, SUPERUSER_ID, [i], 'a-b')
print '< Signal `a-b` called.'
print '> Triggering associated record...'
model.trigger(cr, SUPERUSER_ID, [i])
print '< Associated record triggered.'
print '> Triggering associated record (this time with the condition evaluating to True)...'
trigger.write(cr, SUPERUSER_ID, [1], {'value': True})
model.trigger(cr, SUPERUSER_ID)
print '< Associated record triggered.'
cr.close()

View File

@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from . import test_workflow
fast_suite = [
]
checks = [
test_workflow,
]
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
import openerp
from openerp import SUPERUSER_ID
from openerp.tests import common
class test_workflows(common.TransactionCase):
def check_activities(self, model_name, i, names):
""" Check that the record i has workitems in the given activity names.
"""
instance = self.registry('workflow.instance')
workitem = self.registry('workflow.workitem')
# Given the workflow instance associated to the record ...
instance_id = instance.search(
self.cr, SUPERUSER_ID,
[('res_type', '=', model_name), ('res_id', '=', i)])
self.assertTrue( instance_id, 'A workflow instance is expected.')
# ... get all its workitems ...
workitem_ids = workitem.search(
self.cr, SUPERUSER_ID,
[('inst_id', '=', instance_id[0])])
self.assertTrue(
workitem_ids,
'The workflow instance should have workitems.')
# ... and check the activity the are in against the provided names.
workitem_records = workitem.browse(
self.cr, SUPERUSER_ID, workitem_ids)
self.assertEqual(
sorted([item.act_id.name for item in workitem_records]),
sorted(names))
def test_workflow(self):
model = self.registry('test.workflow.model')
trigger = self.registry('test.workflow.trigger')
i = model.create(self.cr, SUPERUSER_ID, {})
self.check_activities(model._name, i, ['a'])
# a -> b is just a signal.
model.signal_workflow(self.cr, SUPERUSER_ID, [i], 'a-b')
self.check_activities(model._name, i, ['b'])
# b -> c is a trigger (which is False),
# so we remain in the b activity.
model.trigger(self.cr, SUPERUSER_ID, [i])
self.check_activities(model._name, i, ['b'])
# b -> c is a trigger (which is set to True).
# so we go in c when the trigger is called.
trigger.write(self.cr, SUPERUSER_ID, [1], {'value': True})
model.trigger(self.cr, SUPERUSER_ID)
self.check_activities(model._name, i, ['c'])
self.assertEqual(
True,
True)
model.unlink(self.cr, SUPERUSER_ID, [i])