1 from openerp import SUPERUSER_ID
2 from openerp.tests import common
3 from .. import test_models
5 class base_action_rule_test(common.TransactionCase):
9 super(base_action_rule_test, self).setUp()
10 cr, uid = self.cr, self.uid
11 self.demo = self.registry('ir.model.data').get_object(cr, uid, 'base', 'user_demo').id
12 self.admin = SUPERUSER_ID
13 self.model = self.registry('base.action.rule.lead.test')
14 self.base_action_rule = self.registry('base.action.rule')
16 def create_filter_done(self, cr, uid, context=None):
17 filter_pool = self.registry('ir.filters')
18 return filter_pool.create(cr, uid, {
19 'name': "Lead is in done state",
21 'model_id': 'base.action.rule.lead.test',
22 'domain' : "[('state','=','done')]",
25 def create_filter_draft(self, cr, uid, context=None):
26 filter_pool = self.registry('ir.filters')
27 return filter_pool.create(cr, uid, {
28 'name': "Lead is in draft state",
30 'model_id': "base.action.rule.lead.test",
31 'domain' : "[('state','=','draft')]",
34 def create_lead_test_1(self, cr, uid, context=None):
36 Create a new lead_test
38 return self.model.create(cr, uid, {
39 'name': "Lead Test 1",
40 'user_id': self.admin,
43 def create_rule(self, cr, uid, filter_id=False, filter_pre_id=False, context=None):
45 The "Rule 1" says that when a lead goes to the 'draft' state, the responsible for that lead changes to user "demo"
47 return self.base_action_rule.create(cr,uid,{
49 'model_id': self.registry('ir.model').search(cr, uid, [('model','=','base.action.rule.lead.test')], context=context)[0],
51 'filter_pre_id' : filter_pre_id,
52 'filter_id' : filter_id,
53 'act_user_id': self.demo,
56 def delete_rules(self, cr, uid, context=None):
57 """ delete all the rules on model 'base.action.rule.lead.test' """
58 action_ids = self.base_action_rule.search(cr, uid, [('model', '=', self.model._name)], context=context)
59 return self.base_action_rule.unlink(cr, uid, action_ids, context=context)
61 def test_00_check_to_state_draft_pre(self):
63 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.
65 cr, uid = self.cr, self.uid
66 filter_draft = self.create_filter_draft(cr, uid)
67 self.create_rule(cr, uid, filter_pre_id=filter_draft)
68 new_lead_id = self.create_lead_test_1(cr, uid)
69 new_lead = self.model.browse(cr, uid, new_lead_id)
70 self.assertEquals(new_lead.state, 'draft')
71 self.assertEquals(new_lead.user_id.id, self.admin)
72 self.delete_rules(cr, uid)
74 def test_01_check_to_state_draft_post(self):
76 Check that a new record (with state = draft) changes its responsible when there is a postcondition filter which check that the state is draft.
78 cr, uid = self.cr, self.uid
79 filter_draft = self.create_filter_draft(cr, uid)
80 self.create_rule(cr, uid, filter_id=filter_draft)
81 new_lead_id = self.create_lead_test_1(cr, uid)
82 new_lead = self.model.browse(cr, uid, new_lead_id)
83 self.assertEquals(new_lead.state, 'draft')
84 self.assertEquals(new_lead.user_id.id, self.demo)
85 self.delete_rules(cr, uid)
87 def test_02_check_from_draft_to_done_with_steps(self):
89 A new record will be created and will goes from draft to done state via the other states (open, pending and cancel)
90 We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
91 that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
92 verified, the responsible will stay the same
93 The responsible in that test will never change
95 cr, uid = self.cr, self.uid
96 filter_draft = self.create_filter_draft(cr, uid)
97 filter_done = self.create_filter_done(cr, uid)
98 self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done)
99 new_lead_id = self.create_lead_test_1(cr, uid)
100 new_lead = self.model.browse(cr, uid, new_lead_id)
101 self.assertEquals(new_lead.state, 'draft')
102 self.assertEquals(new_lead.user_id.id, self.admin)
103 """ change the state of new_lead to open and check that responsible doen't change"""
104 new_lead.write({'state': 'open'})
105 new_lead = self.model.browse(cr, uid, new_lead_id)
106 self.assertEquals(new_lead.state, 'open')
107 self.assertEquals(new_lead.user_id.id, self.admin)
108 """ change the state of new_lead to pending and check that responsible doen't change"""
109 new_lead.write({'state': 'pending'})
110 new_lead = self.model.browse(cr, uid, new_lead_id)
111 self.assertEquals(new_lead.state, 'pending')
112 self.assertEquals(new_lead.user_id.id, self.admin)
113 """ change the state of new_lead to cancel and check that responsible doen't change"""
114 new_lead.write({'state': 'cancel'})
115 new_lead = self.model.browse(cr, uid, new_lead_id)
116 self.assertEquals(new_lead.state, 'cancel')
117 self.assertEquals(new_lead.user_id.id, self.admin)
118 """ change the state of new_lead to done and check that responsible doen't change """
119 new_lead.write({'state': 'done'})
120 new_lead = self.model.browse(cr, uid, new_lead_id)
121 self.assertEquals(new_lead.state, 'done')
122 self.assertEquals(new_lead.user_id.id, self.admin)
123 self.delete_rules(cr, uid)
125 def test_02_check_from_draft_to_done_without_steps(self):
127 A new record will be created and will goes from draft to done in one operation
128 We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
129 that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
130 verified, the responsible will stay the same
131 The responsible in that test will change to user "demo"
133 cr, uid = self.cr, self.uid
134 filter_draft = self.create_filter_draft(cr, uid)
135 filter_done = self.create_filter_done(cr, uid)
136 self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done)
137 new_lead_id = self.create_lead_test_1(cr, uid)
138 new_lead = self.model.browse(cr, uid, new_lead_id)
139 self.assertEquals(new_lead.state, 'draft')
140 self.assertEquals(new_lead.user_id.id, self.admin)
141 """ change the state of new_lead to done and check that responsible change to Demo_user"""
142 new_lead.write({'state': 'done'})
143 new_lead = self.model.browse(cr, uid, new_lead_id)
144 self.assertEquals(new_lead.state, 'done')
145 self.assertEquals(new_lead.user_id.id, self.demo)
146 self.delete_rules(cr, uid)