[IMP] base_automation: Adapt tests to work with/without demo data
This commit is contained in:
@@ -21,7 +21,4 @@ trigger an automatic reminder email.
|
||||
'data/base_automation_data.xml',
|
||||
'views/base_automation_view.xml',
|
||||
],
|
||||
'demo': [
|
||||
'data/base_automation_demo.xml',
|
||||
],
|
||||
}
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
|
||||
<record id="test_rule_on_create" model="base.automation">
|
||||
<field name="name">Base Automation: test rule on create</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code" eval="'records.write({\'user_id\': %s})' % ref('base.user_demo')"/>
|
||||
<field name="trigger">on_create</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="filter_domain">[('state', '=', 'draft')]</field>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_on_write" model="base.automation">
|
||||
<field name="name">Base Automation: test rule on write</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code" eval="'records.write({\'user_id\': %s})' % ref('base.user_demo')"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="filter_domain">[('state', '=', 'done')]</field>
|
||||
<field name="filter_pre_domain">[('state', '=', 'open')]</field>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_on_recompute" model="base.automation">
|
||||
<field name="name">Base Automation: test rule on recompute</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code" eval="'records.write({\'user_id\': %s})' % ref('base.user_demo')"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="filter_domain">[('employee', '=', True)]</field>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_recursive" model="base.automation">
|
||||
<field name="name">Base Automation: test recursive rule</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code">
|
||||
record = model.browse(env.context['active_id'])
|
||||
if 'partner_id' in env.context['old_values'][record.id]:
|
||||
record.write({'state': 'draft'})
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_on_line" model="base.automation">
|
||||
<field name="name">Base Automation: test rule on secondary model</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_line_test"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code" eval="'records.write({\'user_id\': %s})' % ref('base.user_demo')"/>
|
||||
<field name="trigger">on_create</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_line_test"/>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_on_write_check_context" model="base.automation">
|
||||
<field name="name">Base Automation: test rule on write check context</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="state">code</field>
|
||||
<field name="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)})
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="test_rule_with_trigger" model="base.automation">
|
||||
<field name="name">Base Automation: test rule with trigger</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="trigger_field_ids" eval="[(4,ref('base_automation.field_base_automation_lead_test__state'))]"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="state">code</field>
|
||||
<field name="code">
|
||||
record = model.browse(env.context['active_id'])
|
||||
record['name'] = record.name + 'X'
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="test_mail_template_automation" model="mail.template">
|
||||
<field name="name">Template Automation</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="body_html"><div>Email automation</div></field>
|
||||
</record>
|
||||
<record id="test_rule_on_write_recompute_send_email" model="base.automation">
|
||||
<field name="name">Base Automation: test send an email</field>
|
||||
<field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
|
||||
<field name="template_id" ref="base_automation.test_mail_template_automation"/>
|
||||
<field name="state">email</field>
|
||||
<field name="trigger_field_ids" eval="[(4,ref('base_automation.field_base_automation_lead_test__deadline'))]"/>
|
||||
<field name="trigger">on_write</field>
|
||||
<field name="active" eval="True"/>
|
||||
<field name="filter_domain">[('deadline', '!=', False)]</field>
|
||||
<field name="filter_pre_domain">[('deadline', '=', False)]</field>
|
||||
</record>
|
||||
</odoo>
|
||||
@@ -3,4 +3,3 @@
|
||||
|
||||
from . import base_automation
|
||||
from . import ir_actions
|
||||
from . import ir_demo
|
||||
|
||||
@@ -94,13 +94,14 @@ class BaseAutomation(models.Model):
|
||||
}
|
||||
}}
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
vals['usage'] = 'base_automation'
|
||||
base_automation = super(BaseAutomation, self).create(vals)
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
for vals in vals_list:
|
||||
vals['usage'] = 'base_automation'
|
||||
base_automations = super(BaseAutomation, self).create(vals_list)
|
||||
self._update_cron()
|
||||
self._update_registry()
|
||||
return base_automation
|
||||
return base_automations
|
||||
|
||||
def write(self, vals):
|
||||
res = super(BaseAutomation, self).write(vals)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class IrDemo(models.TransientModel):
|
||||
_inherit = 'ir.demo'
|
||||
|
||||
def install_demo(self):
|
||||
# Prevent the registry to reload while loading demo data, since `create` calls
|
||||
# `_update_registry`.
|
||||
self.pool.ready = False
|
||||
try:
|
||||
result = super().install_demo()
|
||||
finally:
|
||||
self.pool.ready = True
|
||||
|
||||
# Reload the registry
|
||||
self.env['base.automation']._update_registry()
|
||||
|
||||
return result
|
||||
@@ -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': """<div>Email automation</div>""",
|
||||
})
|
||||
|
||||
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.")
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
from dateutil import relativedelta
|
||||
from odoo import fields, models, api
|
||||
|
||||
|
||||
# YTI TODO: Move this brol to a new test_base_automation module
|
||||
class LeadTest(models.Model):
|
||||
_name = "base.automation.lead.test"
|
||||
_description = "Automated Rule Test"
|
||||
@@ -42,6 +42,7 @@ class LeadTest(models.Model):
|
||||
return result
|
||||
|
||||
|
||||
|
||||
class LineTest(models.Model):
|
||||
_name = "base.automation.line.test"
|
||||
_description = "Automated Rule Line Test"
|
||||
|
||||
Reference in New Issue
Block a user