[FIX] base_action_rule: fix tests
[odoo/odoo.git] / addons / base_action_rule / tests / base_action_rule_test.py
1 from openerp import SUPERUSER_ID
2 from openerp.tests import common
3 from .. import test_models
4
5 class base_action_rule_test(common.TransactionCase):
6
7     def setUp(self):
8         """*****setUp*****"""
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
14     def create_filter_done(self, cr, uid, context=None):
15         filter_pool = self.registry('ir.filters')
16         return filter_pool.create(cr, uid, {
17             'name': "Lead is in done state",
18             'is_default': False,
19             'model_id': 'base.action.rule.lead.test',
20             'domain' : "[('state','=','done')]",
21             }, context=context)
22
23     def create_filter_draft(self, cr, uid, context=None):
24         filter_pool = self.registry('ir.filters')
25         return filter_pool.create(cr, uid, {
26             'name': "Lead is in draft state",
27             'is_default': False,
28             'model_id': "base.action.rule.lead.test",
29             'domain' : "[('state','=','draft')]",
30             }, context=context)
31
32     def create_lead_test_1(self, cr, uid, context=None):
33         """
34             Create a new lead_test
35         """
36         lead_pool = self.registry('base.action.rule.lead.test')
37         return lead_pool.create(cr, uid, {
38             'name': "Lead Test 1",
39             'user_id': self.admin,
40             }, context=context)
41
42     def create_rule(self, cr, uid, filter_id=None, filter_pre_id=None, context=None):
43         """
44             The "Rule 1" says that when a lead goes to the 'draft' state, the responsible for that lead changes to user "demo"
45         """
46         self.action_pool = self.registry('base.action.rule')
47         return self.action_pool.create(cr,uid,{
48             'name' : "Rule 1",
49             'model_id': self.registry('ir.model').search(cr, uid, [('model','=','base.action.rule.lead.test')], context=context)[0],
50             'active' : 1,
51             'trg_date_type' : 'none',
52             'filter_pre_id' : filter_pre_id,
53             'filter_id' : filter_id,
54             'act_user_id': self.demo,
55             }, context=context)
56
57     def test_00_check_to_state_draft_pre(self):
58         """
59         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.
60         """
61         cr, uid = self.cr, self.uid
62         context = {}
63         filter_draft = self.create_filter_draft(cr, uid, context=context)
64         rule_1_id = self.create_rule(cr, uid, filter_pre_id=filter_draft, context=context)
65         new_lead_id = self.create_lead_test_1(cr,uid,context=context)
66         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
67         self.assertTrue(new_lead.state=='draft')
68         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
69
70     def test_01_check_to_state_draft_post(self):
71         """
72         Check that a new record (with state = draft) changes its responsible when there is a postcondition filter which check that the state is draft.
73         """
74         cr, uid = self.cr, self.uid
75         context = {}
76         filter_draft = self.create_filter_draft(cr, uid, context=context)
77         rule_1_id = self.create_rule(cr, uid, filter_id=filter_draft, context=context)
78         new_lead_id = self.create_lead_test_1(cr,uid,context=context)
79         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
80         self.assertTrue(new_lead.state=='draft')
81         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.demo, context=context))
82
83     def test_02_check_from_draft_to_done_with_steps(self):
84         """
85         A new record will be created and will goes from draft to done state via the other states (open, pending and cancel)
86         We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
87         that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
88         verified, the responsible will stay the same
89         The responsible in that test will never change
90         """
91         cr, uid = self.cr, self.uid
92         context = {}
93         filter_draft = self.create_filter_draft(cr, uid, context=context)
94         filter_done = self.create_filter_done(cr, uid, context=context)
95         self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done, context=context)
96         new_lead_id = self.create_lead_test_1(cr,uid,context=context)
97         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
98         self.assertTrue(new_lead.state=='draft')
99         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
100         """ change the state of new_lead to open and check that responsible doen't change"""
101         new_lead.write({'state': 'open'}, context=context)
102         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
103         self.assertTrue(new_lead.state=='open')
104         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
105         """ change the state of new_lead to pending and check that responsible doen't change"""
106         new_lead.write({'state': 'pending'}, context=context)
107         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
108         self.assertTrue(new_lead.state=='pending')
109         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
110         """ change the state of new_lead to cancel and check that responsible doen't change"""
111         new_lead.write({'state': 'cancel'}, context=context)
112         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
113         self.assertTrue(new_lead.state=='cancel')
114         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
115         """ change the state of new_lead to done and check that responsible doen't change """
116         new_lead.write({'state': 'done'}, context=context)
117         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
118         self.assertTrue(new_lead.state=='done')
119         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
120
121     def test_02_check_from_draft_to_done_without_steps(self):
122         """
123         A new record will be created and will goes from draft to done in one operation
124         We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
125         that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
126         verified, the responsible will stay the same
127         The responsible in that test will change to user "demo"
128         """
129         cr, uid = self.cr, self.uid
130         context = {}
131         filter_draft = self.create_filter_draft(cr, uid, context=context)
132         filter_done = self.create_filter_done(cr, uid, context=context)
133         self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done, context=context)
134         new_lead_id = self.create_lead_test_1(cr,uid,context=context)
135         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
136         self.assertTrue(new_lead.state=='draft')
137         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.admin, context=context))
138         """ change the state of new_lead to done and check that responsible change to Demo_user"""
139         new_lead.write({'state': 'done'}, context=context)
140         new_lead = self.registry('base.action.rule.lead.test').browse(cr, uid, new_lead_id, context=context)
141         self.assertTrue(new_lead.state=='done')
142         self.assertTrue(new_lead.user_id==self.registry('res.users').browse(cr, uid, self.demo, context=context))