summaryrefslogtreecommitdiff
path: root/addons/base_automation/tests/test_base_automation.py
diff options
context:
space:
mode:
authorOdoo's Mergebot <xmo+roboto@odoo.com>2019-11-07 08:37:12 +0100
committerGitHub <noreply@github.com>2019-11-07 08:37:12 +0100
commitcfc65bd1b72b6fb7833f67fa580fa9fdac28b1f5 (patch)
treec1aa353aa7e29d0ed918911dbdce456ed957b72e /addons/base_automation/tests/test_base_automation.py
parenta3f5afdd53f5f0d8d76427fe14dce6f02e064a75 (diff)
parentbc8fe668495a1d080ff883d5e61e961a128669de (diff)
[MERGE] various: Make the test suite demo data independant
Purpose ======= This is the first huge step to improve the test suite in several ways: - Make them demo data independent, allowing to modify the demo data without having to adapt the tests depending on it. It also allows to have a beautiful + convenient demo dataset to make a demonstration, and to configure complex data in the tests to manage the corner cases. - Improve the test performances by converting them into SavepointCase. Each test will be rollbacked to it's initial state, after the setUpClass call. That way the different data are not re-created at each test for nothing. - Introduce several test classes in base/tests/common.py to launch a TransactionCase, HttpCase or SavepointCase test with an available demo user, portal user or a large set of partners. - This is the first step to tend to a rolling release process, allowing to execute the tests (or a subset of the tests) on a migrated base, for example. closes odoo/odoo#39868 Taskid: 2075739 Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Diffstat (limited to 'addons/base_automation/tests/test_base_automation.py')
-rw-r--r--addons/base_automation/tests/test_base_automation.py116
1 files changed, 103 insertions, 13 deletions
diff --git a/addons/base_automation/tests/test_base_automation.py b/addons/base_automation/tests/test_base_automation.py
index 633b435a216..c768bc7a141 100644
--- a/addons/base_automation/tests/test_base_automation.py
+++ b/addons/base_automation/tests/test_base_automation.py
@@ -2,17 +2,107 @@
# # Part of Odoo. See LICENSE file for full copyright and licensing details.
from unittest.mock import patch
-from odoo.tests import common
+from odoo.addons.base.tests.common import TransactionCaseWithUserDemo
+from odoo.tests import tagged
-@common.tagged('post_install','-at_install')
-class base_automation_test(common.TransactionCase):
+@tagged('post_install', '-at_install')
+class BaseAutomationTest(TransactionCaseWithUserDemo):
def setUp(self):
- super(base_automation_test, self).setUp()
+ super(BaseAutomationTest, self).setUp()
self.user_root = self.env.ref('base.user_root')
self.user_admin = self.env.ref('base.user_admin')
- self.user_demo = self.env.ref('base.user_demo')
+
+ self.test_mail_template_automation = self.env['mail.template'].create({
+ 'name': 'Template Automation',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'body_html': """&lt;div&gt;Email automation&lt;/div&gt;""",
+ })
+
+ self.res_partner_1 = self.env['res.partner'].create({'name': 'My Partner'})
+ self.env['base.automation'].create([
+ {
+ 'name': 'Base Automation: test rule on create',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'state': 'code',
+ 'code': "records.write({'user_id': %s})" % (self.user_demo.id),
+ 'trigger': 'on_create',
+ 'active': True,
+ 'filter_domain': "[('state', '=', 'draft')]",
+ }, {
+ 'name': 'Base Automation: test rule on write',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'state': 'code',
+ 'code': "records.write({'user_id': %s})" % (self.user_demo.id),
+ 'trigger': 'on_write',
+ 'active': True,
+ 'filter_domain': "[('state', '=', 'done')]",
+ 'filter_pre_domain': "[('state', '=', 'open')]",
+ }, {
+ 'name': 'Base Automation: test rule on recompute',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'state': 'code',
+ 'code': "records.write({'user_id': %s})" % (self.user_demo.id),
+ 'trigger': 'on_write',
+ 'active': True,
+ 'filter_domain': "[('employee', '=', True)]",
+ }, {
+ 'name': 'Base Automation: test recursive rule',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'state': 'code',
+ 'code': """
+record = model.browse(env.context['active_id'])
+if 'partner_id' in env.context['old_values'][record.id]:
+ record.write({'state': 'draft'})""",
+ 'trigger': 'on_write',
+ 'active': True,
+ }, {
+ 'name': 'Base Automation: test rule on secondary model',
+ 'model_id': self.env.ref('base_automation.model_base_automation_line_test').id,
+ 'state': 'code',
+ 'code': "records.write({'user_id': %s})" % (self.user_demo.id),
+ 'trigger': 'on_create',
+ 'active': True,
+ }, {
+ 'name': 'Base Automation: test rule on write check context',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'state': 'code',
+ 'code': """
+record = model.browse(env.context['active_id'])
+if 'user_id' in env.context['old_values'][record.id]:
+ record.write({'is_assigned_to_admin': (record.user_id.id == 1)})""",
+ 'trigger': 'on_write',
+ 'active': True,
+ }, {
+ 'name': 'Base Automation: test rule with trigger',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'trigger_field_ids': [(4, self.env.ref('base_automation.field_base_automation_lead_test__state').id)],
+ 'state': 'code',
+ 'code': """
+record = model.browse(env.context['active_id'])
+record['name'] = record.name + 'X'""",
+ 'trigger': 'on_write',
+ 'active': True,
+ }, {
+ 'name': 'Base Automation: test send an email',
+ 'model_id': self.env.ref('base_automation.model_base_automation_lead_test').id,
+ 'template_id': self.test_mail_template_automation.id,
+ 'trigger_field_ids': [(4, self.env.ref('base_automation.field_base_automation_lead_test__deadline').id)],
+ 'state': 'email',
+ 'code': """
+record = model.browse(env.context['active_id'])
+record['name'] = record.name + 'X'""",
+ 'trigger': 'on_write',
+ 'active': True,
+ 'filter_domain': "[('deadline', '!=', False)]",
+ 'filter_pre_domain': "[('deadline', '=', False)]",
+ }
+ ])
+
+ def tearDown(self):
+ super().tearDown()
+ self.env['base.automation']._unregister_hook()
def create_lead(self, **kwargs):
vals = {
@@ -83,7 +173,7 @@ class base_automation_test(common.TransactionCase):
Check that a rule is executed whenever a field is recomputed after a
change on another model.
"""
- partner = self.env.ref('base.res_partner_1')
+ partner = self.res_partner_1
partner.write({'employee': False})
lead = self.create_lead(state='open', partner_id=partner.id)
self.assertFalse(lead.employee, "Customer field should updated to False")
@@ -99,7 +189,7 @@ class base_automation_test(common.TransactionCase):
Check that a rule is executed whenever a field is recomputed and the
context contains the target field
"""
- partner = self.env.ref('base.res_partner_1')
+ partner = self.res_partner_1
lead = self.create_lead(state='draft', partner_id=partner.id)
self.assertFalse(lead.deadline, 'There should not be a deadline defined')
# change priority and user; this triggers deadline recomputation, and
@@ -109,7 +199,7 @@ class base_automation_test(common.TransactionCase):
self.assertTrue(lead.is_assigned_to_admin, 'Lead should be assigned to admin')
def test_11b_recomputed_field(self):
- mail_automation = self.env.ref('base_automation.test_rule_on_write_recompute_send_email')
+ mail_automation = self.env['base.automation'].search([('name', '=', 'Base Automation: test send an email')])
send_mail_count = 0
def _patched_get_actions(*args, **kwargs):
@@ -151,7 +241,7 @@ class base_automation_test(common.TransactionCase):
self.assertEqual(lead.state, 'open')
self.assertEqual(lead.user_id, self.user_root)
# change partner; this should trigger the rule that modifies the state
- partner = self.env.ref('base.res_partner_1')
+ partner = self.res_partner_1
lead.write({'partner_id': partner.id})
self.assertEqual(lead.state, 'draft')
@@ -178,7 +268,7 @@ class base_automation_test(common.TransactionCase):
"""
lead = self.create_lead(name="X")
lead.priority = True
- partner1 = self.env.ref('base.res_partner_1')
+ partner1 = self.res_partner_1
lead.partner_id = partner1.id
self.assertEqual(lead.name, 'X', "No update until now.")
@@ -192,10 +282,10 @@ class base_automation_test(common.TransactionCase):
self.assertEqual(lead.name, 'XXXX', "One update should have happened.")
# change the rule to trigger on partner_id
- rule = self.env.ref('base_automation.test_rule_with_trigger')
- rule.trigger_field_ids = self.env.ref('base_automation.field_base_automation_lead_test__partner_id')
+ rule = self.env['base.automation'].search([('name', '=', 'Base Automation: test rule with trigger')])
+ rule.write({'trigger_field_ids': [(6, 0, [self.env.ref('base_automation.field_base_automation_lead_test__partner_id').id])]})
- partner2 = self.env.ref('base.res_partner_2')
+ partner2 = self.env['res.partner'].create({'name': 'A new partner'})
lead.name = 'X'
lead.state = 'open'
self.assertEqual(lead.name, 'X', "No update should have happened.")