From 25251b2dbda9d52c79a9eb660bfe6995b23ad56c Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 5 Oct 2015 16:33:38 +0200 Subject: [PATCH] [IMP] base_action_rule: make tests work (rename file and rewrite them to work) Introduce test rules in demo data instead of during the test, because their creation/update restarts the registry. Moreover, the registry is only patched after all installations. So the test must be run at post-install phase. --- addons/base_action_rule/__openerp__.py | 4 +- .../base_action_rule_demo.xml | 39 +++++ addons/base_action_rule/tests/__init__.py | 2 +- .../tests/base_action_rule_test.py | 146 ------------------ .../tests/test_base_action_rule.py | 78 ++++++++++ 5 files changed, 121 insertions(+), 148 deletions(-) create mode 100644 addons/base_action_rule/base_action_rule_demo.xml delete mode 100644 addons/base_action_rule/tests/base_action_rule_test.py create mode 100644 addons/base_action_rule/tests/test_base_action_rule.py diff --git a/addons/base_action_rule/__openerp__.py b/addons/base_action_rule/__openerp__.py index ae5163caf69..cbccf86954a 100644 --- a/addons/base_action_rule/__openerp__.py +++ b/addons/base_action_rule/__openerp__.py @@ -21,7 +21,9 @@ trigger an automatic reminder email. 'base_action_rule_view.xml', 'security/ir.model.access.csv', ], - 'demo': [], + 'demo': [ + 'base_action_rule_demo.xml', + ], 'installable': True, 'auto_install': False, } diff --git a/addons/base_action_rule/base_action_rule_demo.xml b/addons/base_action_rule/base_action_rule_demo.xml new file mode 100644 index 00000000000..2ca6d8b44e5 --- /dev/null +++ b/addons/base_action_rule/base_action_rule_demo.xml @@ -0,0 +1,39 @@ + + + + + Test lead in state 'draft' + base.action.rule.lead.test + [('state', '=', 'draft')] + + + + Test lead in state 'open' + base.action.rule.lead.test + [('state', '=', 'open')] + + + + Test lead in state 'done' + base.action.rule.lead.test + [('state', '=', 'done')] + + + + Test rule on create + on_create + + + + + + + Test rule on write + on_write + + + + + + + diff --git a/addons/base_action_rule/tests/__init__.py b/addons/base_action_rule/tests/__init__.py index 64097fc88db..276ee9c9924 100644 --- a/addons/base_action_rule/tests/__init__.py +++ b/addons/base_action_rule/tests/__init__.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from . import base_action_rule_test +from . import test_base_action_rule diff --git a/addons/base_action_rule/tests/base_action_rule_test.py b/addons/base_action_rule/tests/base_action_rule_test.py deleted file mode 100644 index 54c72ba0607..00000000000 --- a/addons/base_action_rule/tests/base_action_rule_test.py +++ /dev/null @@ -1,146 +0,0 @@ -from openerp import SUPERUSER_ID -from openerp.tests import common -from .. import test_models - -class base_action_rule_test(common.TransactionCase): - - def setUp(self): - """*****setUp*****""" - super(base_action_rule_test, self).setUp() - cr, uid = self.cr, self.uid - self.demo = self.registry('ir.model.data').get_object(cr, uid, 'base', 'user_demo').id - self.admin = SUPERUSER_ID - self.model = self.registry('base.action.rule.lead.test') - self.base_action_rule = self.registry('base.action.rule') - - def create_filter_done(self, cr, uid, context=None): - filter_pool = self.registry('ir.filters') - return filter_pool.create(cr, uid, { - 'name': "Lead is in done state", - 'is_default': False, - 'model_id': 'base.action.rule.lead.test', - 'domain': "[('state','=','done')]", - }, context=context) - - def create_filter_draft(self, cr, uid, context=None): - filter_pool = self.registry('ir.filters') - return filter_pool.create(cr, uid, { - 'name': "Lead is in draft state", - 'is_default': False, - 'model_id': "base.action.rule.lead.test", - 'domain' : "[('state','=','draft')]", - }, context=context) - - def create_lead_test_1(self, cr, uid, context=None): - """ - Create a new lead_test - """ - return self.model.create(cr, uid, { - 'name': "Lead Test 1", - 'user_id': self.admin, - }, context=context) - - def create_rule(self, cr, uid, kind, filter_id=False, filter_pre_id=False, context=None): - """ - The "Rule 1" says that when a lead goes to the 'draft' state, the responsible for that lead changes to user "demo" - """ - return self.base_action_rule.create(cr,uid,{ - 'name': "Rule 1", - 'model_id': self.registry('ir.model').search(cr, uid, [('model','=','base.action.rule.lead.test')], context=context)[0], - 'kind': kind, - 'filter_pre_id': filter_pre_id, - 'filter_id': filter_id, - 'act_user_id': self.demo, - }, context=context) - - def delete_rules(self, cr, uid, context=None): - """ delete all the rules on model 'base.action.rule.lead.test' """ - action_ids = self.base_action_rule.search(cr, uid, [('model', '=', self.model._name)], context=context) - return self.base_action_rule.unlink(cr, uid, action_ids, context=context) - - def test_00_check_to_state_draft_pre(self): - """ - Check that a new record (with state = draft) doesn't change its responsible when there is a precondition filter which check that the state is draft. - """ - cr, uid = self.cr, self.uid - filter_draft = self.create_filter_draft(cr, uid) - self.create_rule(cr, uid, 'on_write', filter_pre_id=filter_draft) - new_lead_id = self.create_lead_test_1(cr, uid) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'draft') - self.assertEquals(new_lead.user_id.id, self.admin) - self.delete_rules(cr, uid) - - def test_01_check_to_state_draft_post(self): - """ - Check that a new record changes its responsible when there is a postcondition filter which check that the state is draft. - """ - cr, uid = self.cr, self.uid - filter_draft = self.create_filter_draft(cr, uid) - self.create_rule(cr, uid, 'on_create') - new_lead_id = self.create_lead_test_1(cr, uid) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'draft') - self.assertEquals(new_lead.user_id.id, self.demo) - self.delete_rules(cr, uid) - - def test_02_check_from_draft_to_done_with_steps(self): - """ - A new record will be created and will goes from draft to done state via the other states (open, pending and cancel) - We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says - that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't - verified, the responsible will stay the same - The responsible in that test will never change - """ - cr, uid = self.cr, self.uid - filter_draft = self.create_filter_draft(cr, uid) - filter_done = self.create_filter_done(cr, uid) - self.create_rule(cr, uid, 'on_write', filter_pre_id=filter_draft, filter_id=filter_done) - new_lead_id = self.create_lead_test_1(cr, uid) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'draft') - self.assertEquals(new_lead.user_id.id, self.admin) - """ change the state of new_lead to open and check that responsible doen't change""" - new_lead.write({'state': 'open'}) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'open') - self.assertEquals(new_lead.user_id.id, self.admin) - """ change the state of new_lead to pending and check that responsible doen't change""" - new_lead.write({'state': 'pending'}) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'pending') - self.assertEquals(new_lead.user_id.id, self.admin) - """ change the state of new_lead to cancel and check that responsible doen't change""" - new_lead.write({'state': 'cancel'}) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'cancel') - self.assertEquals(new_lead.user_id.id, self.admin) - """ change the state of new_lead to done and check that responsible doen't change """ - new_lead.write({'state': 'done'}) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'done') - self.assertEquals(new_lead.user_id.id, self.admin) - self.delete_rules(cr, uid) - - def test_02_check_from_draft_to_done_without_steps(self): - """ - A new record will be created and will goes from draft to done in one operation - We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says - that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't - verified, the responsible will stay the same - The responsible in that test will change to user "demo" - """ - cr, uid = self.cr, self.uid - filter_draft = self.create_filter_draft(cr, uid) - filter_done = self.create_filter_done(cr, uid) - self.create_rule(cr, uid, 'on_write', filter_pre_id=filter_draft, filter_id=filter_done) - new_lead_id = self.create_lead_test_1(cr, uid) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'draft') - self.assertEquals(new_lead.user_id.id, self.admin) - """ change the state of new_lead to done and check that responsible change to Demo_user""" - new_lead.write({'state': 'done'}) - new_lead = self.model.browse(cr, uid, new_lead_id) - self.assertEquals(new_lead.state, 'done') - self.assertEquals(new_lead.user_id.id, self.demo) - self.delete_rules(cr, uid) diff --git a/addons/base_action_rule/tests/test_base_action_rule.py b/addons/base_action_rule/tests/test_base_action_rule.py new file mode 100644 index 00000000000..1d8aa3619f6 --- /dev/null +++ b/addons/base_action_rule/tests/test_base_action_rule.py @@ -0,0 +1,78 @@ +from openerp import SUPERUSER_ID +from openerp.tests import common +from .. import test_models + +@common.at_install(False) +@common.post_install(True) +class base_action_rule_test(common.TransactionCase): + + def setUp(self): + super(base_action_rule_test, self).setUp() + self.user_admin = self.env.ref('base.user_root') + self.user_demo = self.env.ref('base.user_demo') + + def create_lead(self, **kwargs): + vals = { + 'name': "Lead Test", + 'user_id': self.user_admin.id, + } + vals.update(kwargs) + return self.env['base.action.rule.lead.test'].create(vals) + + def test_00_check_to_state_open_pre(self): + """ + Check that a new record (with state = open) doesn't change its responsible + when there is a precondition filter which check that the state is open. + """ + lead = self.create_lead(state='open') + self.assertEqual(lead.state, 'open') + self.assertEqual(lead.user_id, self.user_admin) + + def test_01_check_to_state_draft_post(self): + """ + Check that a new record changes its responsible when there is a postcondition + filter which check that the state is draft. + """ + lead = self.create_lead() + self.assertEqual(lead.state, 'draft') + self.assertEqual(lead.user_id, self.user_demo) + + def test_02_check_from_draft_to_done_with_steps(self): + """ + A new record is created and goes from states 'open' to 'done' via the + other states (open, pending and cancel). We have a rule with: + - precondition: the record is in "open" + - postcondition: that the record is "done". + + If the state goes from 'open' to 'done' the responsible is changed. + If those two conditions aren't verified, the responsible remains the same. + """ + lead = self.create_lead(state='open') + self.assertEqual(lead.state, 'open') + self.assertEqual(lead.user_id, self.user_admin) + # change state to pending and check that responsible has not changed + lead.write({'state': 'pending'}) + self.assertEqual(lead.state, 'pending') + self.assertEqual(lead.user_id, self.user_admin) + # change state to done and check that responsible has not changed + lead.write({'state': 'done'}) + self.assertEqual(lead.state, 'done') + self.assertEqual(lead.user_id, self.user_admin) + + def test_03_check_from_draft_to_done_without_steps(self): + """ + A new record is created and goes from states 'open' to 'done' via the + other states (open, pending and cancel). We have a rule with: + - precondition: the record is in "open" + - postcondition: that the record is "done". + + If the state goes from 'open' to 'done' the responsible is changed. + If those two conditions aren't verified, the responsible remains the same. + """ + lead = self.create_lead(state='open') + self.assertEqual(lead.state, 'open') + self.assertEqual(lead.user_id, self.user_admin) + # change state to done and check that responsible has changed + lead.write({'state': 'done'}) + self.assertEqual(lead.state, 'done') + self.assertEqual(lead.user_id, self.user_demo)