[FIX] base_action_rule: 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         self.model = self.registry('base.action.rule.lead.test')
14         self.base_action_rule = self.registry('base.action.rule')
15
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",
20             'is_default': False,
21             'model_id': 'base.action.rule.lead.test',
22             'domain' : "[('state','=','done')]",
23             }, context=context)
24
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",
29             'is_default': False,
30             'model_id': "base.action.rule.lead.test",
31             'domain' : "[('state','=','draft')]",
32             }, context=context)
33
34     def create_lead_test_1(self, cr, uid, context=None):
35         """
36             Create a new lead_test
37         """
38         return self.model.create(cr, uid, {
39             'name': "Lead Test 1",
40             'user_id': self.admin,
41             }, context=context)
42
43     def create_rule(self, cr, uid, filter_id=False, filter_pre_id=False, context=None):
44         """
45             The "Rule 1" says that when a lead goes to the 'draft' state, the responsible for that lead changes to user "demo"
46         """
47         return self.base_action_rule.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 delete_rules(self, cr, uid, context=None):
58         """ delete all the rules on model 'base.action.rule.lead.test' """
59         action_ids = self.base_action_rule.search(cr, uid, [('model', '=', self.model._name)], context=context)
60         return self.base_action_rule.unlink(cr, uid, action_ids, context=context)
61
62     def test_00_check_to_state_draft_pre(self):
63         """
64         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         """
66         cr, uid = self.cr, self.uid
67         filter_draft = self.create_filter_draft(cr, uid)
68         self.create_rule(cr, uid, filter_pre_id=filter_draft)
69         new_lead_id = self.create_lead_test_1(cr, uid)
70         new_lead = self.model.browse(cr, uid, new_lead_id)
71         self.assertEquals(new_lead.state, 'draft')
72         self.assertEquals(new_lead.user_id.id, self.admin)
73         self.delete_rules(cr, uid)
74
75     def test_01_check_to_state_draft_post(self):
76         """
77         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         """
79         cr, uid = self.cr, self.uid
80         filter_draft = self.create_filter_draft(cr, uid)
81         self.create_rule(cr, uid, filter_id=filter_draft)
82         new_lead_id = self.create_lead_test_1(cr, uid)
83         new_lead = self.model.browse(cr, uid, new_lead_id)
84         self.assertEquals(new_lead.state, 'draft')
85         self.assertEquals(new_lead.user_id.id, self.demo)
86         self.delete_rules(cr, uid)
87
88     def test_02_check_from_draft_to_done_with_steps(self):
89         """
90         A new record will be created and will goes from draft to done state via the other states (open, pending and cancel)
91         We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
92         that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
93         verified, the responsible will stay the same
94         The responsible in that test will never change
95         """
96         cr, uid = self.cr, self.uid
97         filter_draft = self.create_filter_draft(cr, uid)
98         filter_done = self.create_filter_done(cr, uid)
99         self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done)
100         new_lead_id = self.create_lead_test_1(cr, uid)
101         new_lead = self.model.browse(cr, uid, new_lead_id)
102         self.assertEquals(new_lead.state, 'draft')
103         self.assertEquals(new_lead.user_id.id, self.admin)
104         """ change the state of new_lead to open and check that responsible doen't change"""
105         new_lead.write({'state': 'open'})
106         new_lead = self.model.browse(cr, uid, new_lead_id)
107         self.assertEquals(new_lead.state, 'open')
108         self.assertEquals(new_lead.user_id.id, self.admin)
109         """ change the state of new_lead to pending and check that responsible doen't change"""
110         new_lead.write({'state': 'pending'})
111         new_lead = self.model.browse(cr, uid, new_lead_id)
112         self.assertEquals(new_lead.state, 'pending')
113         self.assertEquals(new_lead.user_id.id, self.admin)
114         """ change the state of new_lead to cancel and check that responsible doen't change"""
115         new_lead.write({'state': 'cancel'})
116         new_lead = self.model.browse(cr, uid, new_lead_id)
117         self.assertEquals(new_lead.state, 'cancel')
118         self.assertEquals(new_lead.user_id.id, self.admin)
119         """ change the state of new_lead to done and check that responsible doen't change """
120         new_lead.write({'state': 'done'})
121         new_lead = self.model.browse(cr, uid, new_lead_id)
122         self.assertEquals(new_lead.state, 'done')
123         self.assertEquals(new_lead.user_id.id, self.admin)
124         self.delete_rules(cr, uid)
125
126     def test_02_check_from_draft_to_done_without_steps(self):
127         """
128         A new record will be created and will goes from draft to done in one operation
129         We will create a rule that says in precondition that the record must be in the "draft" state while a postcondition filter says
130         that the record will be done. If the state goes from 'draft' to 'done' the responsible will change. If those two conditions aren't
131         verified, the responsible will stay the same
132         The responsible in that test will change to user "demo"
133         """
134         cr, uid = self.cr, self.uid
135         filter_draft = self.create_filter_draft(cr, uid)
136         filter_done = self.create_filter_done(cr, uid)
137         self.create_rule(cr, uid, filter_pre_id=filter_draft, filter_id=filter_done)
138         new_lead_id = self.create_lead_test_1(cr, uid)
139         new_lead = self.model.browse(cr, uid, new_lead_id)
140         self.assertEquals(new_lead.state, 'draft')
141         self.assertEquals(new_lead.user_id.id, self.admin)
142         """ change the state of new_lead to done and check that responsible change to Demo_user"""
143         new_lead.write({'state': 'done'})
144         new_lead = self.model.browse(cr, uid, new_lead_id)
145         self.assertEquals(new_lead.state, 'done')
146         self.assertEquals(new_lead.user_id.id, self.demo)
147         self.delete_rules(cr, uid)