From 5871f5dfce3c63daae072acedc9149a9ce1467a1 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Thu, 28 Dec 2017 18:01:56 +0100 Subject: [PATCH] Refactor tests of module 'base_exception'. Defines a new mechanism to build odoo classes that are only defined during testing. --- base_exception/__init__.py | 3 - base_exception/__manifest__.py | 1 - base_exception/models/base_exception.py | 28 +------ .../security/tmp_test_model_access_rule.xml | 8 -- base_exception/tests/__init__.py | 3 +- base_exception/tests/common.py | 14 ++++ base_exception/tests/purchase_test.py | 73 ++++++++++++++++++ base_exception/tests/test_base_exception.py | 35 +++++---- base_exception/tests/test_tmp_model.py | 77 ------------------- 9 files changed, 109 insertions(+), 133 deletions(-) delete mode 100644 base_exception/security/tmp_test_model_access_rule.xml create mode 100644 base_exception/tests/common.py create mode 100644 base_exception/tests/purchase_test.py delete mode 100644 base_exception/tests/test_tmp_model.py diff --git a/base_exception/__init__.py b/base_exception/__init__.py index 7f16dfdc2..75a695531 100644 --- a/base_exception/__init__.py +++ b/base_exception/__init__.py @@ -1,5 +1,2 @@ -# Copyright 2011 Raphaƫl Valyi, Renato Lima, Guewen Baconnier, Sodexis -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from . import wizard, models -from .tests import test_tmp_model diff --git a/base_exception/__manifest__.py b/base_exception/__manifest__.py index a7539eef7..35e2c0be2 100644 --- a/base_exception/__manifest__.py +++ b/base_exception/__manifest__.py @@ -18,7 +18,6 @@ 'data': [ 'security/base_exception_security.xml', 'security/ir.model.access.csv', - 'security/tmp_test_model_access_rule.xml', 'wizard/base_exception_confirm_view.xml', 'views/base_exception_view.xml', ], diff --git a/base_exception/models/base_exception.py b/base_exception/models/base_exception.py index c62fba6c8..e263ff7a7 100644 --- a/base_exception/models/base_exception.py +++ b/base_exception/models/base_exception.py @@ -5,8 +5,7 @@ import time from functools import wraps -import os -from odoo import api, fields, models, tools, _ +from odoo import api, fields, models, _ import logging from odoo.exceptions import UserError, ValidationError @@ -252,28 +251,3 @@ class BaseException(models.AbstractModel): def _default_get_lines(self): return [] - - @api.model - def _import_acl_for_tmp_test_model(self): - # import the access rule of temp test model only if testing - testing = tools.config.get('test_enable')\ - or os.environ.get('ODOO_TEST_ENABLE') - if testing: - header = ['id', 'name', 'model_id:id', 'group_id:id', - 'perm_read', 'perm_write', - 'perm_create', 'perm_unlink'] - IrModelAccess = self.env['ir.model.access'] - acl_data = [ - ['access_base_exception_test_purchase', - 'access_base_exception_test_purchase', - 'base_exception.model_base_exception_test_purchase', - 'base.group_system', '1', '1', '1', '1'], - ['access_base_exception_test_purchase_line', - 'access_base_exception_test_purchase_line', - 'base_exception.model_base_exception_test_purchase_line', - 'base.group_system', '1', '1', '1', '1'] - ] - - result = IrModelAccess.load(header, acl_data) - if result['messages']: - _logger.warning(result['messages']) diff --git a/base_exception/security/tmp_test_model_access_rule.xml b/base_exception/security/tmp_test_model_access_rule.xml deleted file mode 100644 index d104ea432..000000000 --- a/base_exception/security/tmp_test_model_access_rule.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/base_exception/tests/__init__.py b/base_exception/tests/__init__.py index e817e312c..874f27874 100644 --- a/base_exception/tests/__init__.py +++ b/base_exception/tests/__init__.py @@ -1,3 +1,4 @@ -from . import test_tmp_model +from . import common +from . import purchase_test from . import test_base_exception diff --git a/base_exception/tests/common.py b/base_exception/tests/common.py new file mode 100644 index 000000000..fc52552f8 --- /dev/null +++ b/base_exception/tests/common.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 ACSONE SA/NV () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +def setup_test_model(env, model_clses): + for model_cls in model_clses: + model_cls._build_model(env.registry, env.cr) + + env.registry.setup_models(env.cr) + env.registry.init_models( + env.cr, [model_cls._name for model_cls in model_clses], + dict(env.context, update_custom_fields=True) + ) diff --git a/base_exception/tests/purchase_test.py b/base_exception/tests/purchase_test.py new file mode 100644 index 000000000..715b200bc --- /dev/null +++ b/base_exception/tests/purchase_test.py @@ -0,0 +1,73 @@ +# Copyright 2016 Akretion Mourad EL HADJ MIMOUNE +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import api, fields, models + + +class PurchaseTest(models.Model): + _inherit = 'base.exception' + _name = "base.exception.test.purchase" + _description = "Base Ecxeption Test Model" + + rule_group = fields.Selection( + selection_add=[('test_base', 'test')], + default='test_base', + ) + name = fields.Char(required=True) + user_id = fields.Many2one('res.users', string='Responsible') + state = fields.Selection( + [('draft', 'New'), ('cancel', 'Cancelled'), + ('purchase', 'Purchase'), + ('to approve', 'To approve'), ('done', 'Done')], + string="Status", readonly=True, default='draft') + active = fields.Boolean(default=True) + partner_id = fields.Many2one('res.partner', string='Partner') + line_ids = fields.One2many( + 'base.exception.test.purchase.line', 'lead_id') + amount_total = fields.Float( + compute='_compute_amount_total', store=True) + + @api.depends('line_ids') + def _compute_amount_total(cls): + for record in cls: + for line in record.line_ids: + record.amount_total += line.amount * line.qty + + @api.constrains('ignore_exception', 'line_ids', 'state') + def test_purchase_check_exception(cls): + orders = cls.filtered(lambda s: s.state == 'purchase') + if orders: + orders._check_exception() + + @api.multi + def button_approve(cls, force=False): + cls.write({'state': 'to approve'}) + return {} + + @api.multi + def button_draft(cls): + cls.write({'state': 'draft'}) + return {} + + @api.multi + def button_confirm(cls): + cls.write({'state': 'purchase'}) + return True + + @api.multi + def button_cancel(cls): + cls.write({'state': 'cancel'}) + + def test_base_get_lines(cls): + cls.ensure_one() + return cls.line_ids + + +class LineTest(models.Model): + _name = "base.exception.test.purchase.line" + _description = "Base Exception Test Model Line" + + name = fields.Char() + lead_id = fields.Many2one('base.exception.test.purchase', + ondelete='cascade') + qty = fields.Float() + amount = fields.Float() diff --git a/base_exception/tests/test_base_exception.py b/base_exception/tests/test_base_exception.py index e8f57d751..33755133d 100644 --- a/base_exception/tests/test_base_exception.py +++ b/base_exception/tests/test_base_exception.py @@ -2,35 +2,38 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from odoo.tests import common - +from .common import setup_test_model +from .purchase_test import PurchaseTest, LineTest import logging _logger = logging.getLogger(__name__) -# @common.at_install(False) -# @common.post_install(True) -class TestBaseException(common.TransactionCase): +class TestBaseException(common.SavepointCase): + + # pylint: disable=missing-return - def setUp(self): - super(TestBaseException, self).setUp() + @classmethod + def setUpClass(cls): + super(TestBaseException, cls).setUpClass() + setup_test_model(cls.env, [PurchaseTest, LineTest]) - self.base_exception = self.env['base.exception'] - self.exception_rule = self.env['exception.rule'] - self.exception_confirm = self.env['exception.rule.confirm'] + cls.base_exception = cls.env['base.exception'] + cls.exception_rule = cls.env['exception.rule'] + cls.exception_confirm = cls.env['exception.rule.confirm'] - self.exception_rule._fields['rule_group'].selection.append( + cls.exception_rule._fields['rule_group'].selection.append( ('test_base', 'test base exception') ) - self.exception_rule._fields['model'].selection.append( + cls.exception_rule._fields['model'].selection.append( ('base.exception.test.purchase', 'base.exception.test.purchase') ) - self.exception_rule._fields['model'].selection.append( + cls.exception_rule._fields['model'].selection.append( ('base.exception.test.purchase.line', 'base.exception.test.purchase.line') ) - self.exceptionnozip = self.env['exception.rule'].create({ + cls.exceptionnozip = cls.env['exception.rule'].create({ 'name': "No ZIP code on destination", 'sequence': 10, 'rule_group': "test_base", @@ -38,7 +41,7 @@ class TestBaseException(common.TransactionCase): 'code': """if not test_base.partner_id.zip: failed=True""", }) - self.exceptionno_minorder = self.env['exception.rule'].create({ + cls.exceptionno_minorder = cls.env['exception.rule'].create({ 'name': "Min order except", 'sequence': 10, 'rule_group': "test_base", @@ -47,7 +50,7 @@ class TestBaseException(common.TransactionCase): failed=True""", }) - self.exceptionno_lineqty = self.env['exception.rule'].create({ + cls.exceptionno_lineqty = cls.env['exception.rule'].create({ 'name': "Qty > 0", 'sequence': 10, 'rule_group': "test_base", @@ -55,7 +58,7 @@ class TestBaseException(common.TransactionCase): 'code': """if test_base_line.qty <= 0: failed=True"""}) - def test_sale_order_exception(self): + def test_purchase_order_exception(self): partner = self.env.ref('base.res_partner_1') partner.zip = False potest1 = self.env['base.exception.test.purchase'].create({ diff --git a/base_exception/tests/test_tmp_model.py b/base_exception/tests/test_tmp_model.py deleted file mode 100644 index 8612b6d05..000000000 --- a/base_exception/tests/test_tmp_model.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2017 Akretion (http://www.akretion.com) -# Mourad EL HADJ MIMOUNE -import os -from odoo import api, fields, models, tools -import logging - -_logger = logging.getLogger(__name__) - -testing = tools.config.get('test_enable') or os.environ.get('ODOO_TEST_ENABLE') -if testing: - class PurchaseTest(models.Model): - _inherit = 'base.exception' - _name = "base.exception.test.purchase" - _description = "Base Ecxeption Test Model" - - rule_group = fields.Selection( - selection_add=[('test_base', 'test')], - default='test_base', - ) - name = fields.Char(required=True) - user_id = fields.Many2one('res.users', string='Responsible') - state = fields.Selection( - [('draft', 'New'), ('cancel', 'Cancelled'), - ('purchase', 'Purchase'), - ('to approve', 'To approve'), ('done', 'Done')], - string="Status", readonly=True, default='draft') - active = fields.Boolean(default=True) - partner_id = fields.Many2one('res.partner', string='Partner') - line_ids = fields.One2many( - 'base.exception.test.purchase.line', 'lead_id') - amount_total = fields.Float( - compute='_compute_amount_total', store=True) - - @api.depends('line_ids') - def _compute_amount_total(self): - for record in self: - for line in record.line_ids: - record.amount_total += line.amount * line.qty - - @api.constrains('ignore_exception', 'line_ids', 'state') - def test_purchase_check_exception(self): - orders = self.filtered(lambda s: s.state == 'purchase') - if orders: - orders._check_exception() - - @api.multi - def button_approve(self, force=False): - self.write({'state': 'to approve'}) - return {} - - @api.multi - def button_draft(self): - self.write({'state': 'draft'}) - return {} - - @api.multi - def button_confirm(self): - self.write({'state': 'purchase'}) - return True - - @api.multi - def button_cancel(self): - self.write({'state': 'cancel'}) - - def test_base_get_lines(self): - self.ensure_one() - return self.line_ids - - class LineTest(models.Model): - _name = "base.exception.test.purchase.line" - _description = "Base Ecxeption Test Model Line" - - name = fields.Char() - lead_id = fields.Many2one('base.exception.test.purchase', - ondelete='cascade') - qty = fields.Float() - amount = fields.Float()