From 04a5bb7280238da000fd2dd764215cf257e904c8 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Wed, 29 Nov 2017 16:11:17 +0100 Subject: [PATCH] partner_identification: fix/imp tests on res.partner --- partner_identification/models/__init__.py | 1 + partner_identification/models/test_models.py | 25 ++++++++ .../tests/test_partner_identification.py | 16 +++-- .../tests/test_res_partner.py | 60 +++---------------- 4 files changed, 46 insertions(+), 56 deletions(-) create mode 100644 partner_identification/models/test_models.py diff --git a/partner_identification/models/__init__.py b/partner_identification/models/__init__.py index 699c4c59d77..9b71c97a494 100644 --- a/partner_identification/models/__init__.py +++ b/partner_identification/models/__init__.py @@ -4,3 +4,4 @@ from . import res_partner_id_number from . import res_partner_id_category from . import res_partner +from . import test_models diff --git a/partner_identification/models/test_models.py b/partner_identification/models/test_models.py new file mode 100644 index 00000000000..fd9a3e8cd5e --- /dev/null +++ b/partner_identification/models/test_models.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 LasLabs Inc. +# Copyright 2017 Camptocamp +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo import fields, models, tools +import os + +testing = tools.config.get('test_enable') or os.environ.get('ODOO_TEST_ENABLE') + +if testing: + class ResPartner(models.Model): + _inherit = 'res.partner' + + social_security = fields.Char( + compute=lambda s: s._compute_identification( + 'social_security', 'SSN', + ), + inverse=lambda s: s._inverse_identification( + 'social_security', 'SSN', + ), + search=lambda s, *a: s._search_identification( + 'SSN', *a + ), + ) diff --git a/partner_identification/tests/test_partner_identification.py b/partner_identification/tests/test_partner_identification.py index 9804eb5a725..5e6fe6ead7e 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -4,13 +4,12 @@ from psycopg2._psycopg import IntegrityError from odoo.tests import common from odoo.exceptions import ValidationError +from odoo.tools import mute_logger class TestPartnerIdentificationBase(common.TransactionCase): - def test_base_functionalities(self): - """Dummy CRUD test - """ + def test_create_id_category(self): partner_id_category = self.env['res.partner.id_category'].create({ 'code': 'id_code', 'name': 'id_name', @@ -18,13 +17,22 @@ def test_base_functionalities(self): self.assertEqual(partner_id_category.name, 'id_name') self.assertEqual(partner_id_category.code, 'id_code') + @mute_logger('odoo.sql_db') + def test_update_partner_with_no_category(self): partner_1 = self.env.ref('base.res_partner_1') self.assertEqual(len(partner_1.id_numbers), 0) # create without required category - with self.assertRaises(IntegrityError), self.cr.savepoint(): + with self.assertRaises(IntegrityError): partner_1.write({'id_numbers': [(0, 0, { 'name': '1234', })]}) + + def test_update_partner_with_category(self): + partner_1 = self.env.ref('base.res_partner_1') + partner_id_category = self.env['res.partner.id_category'].create({ + 'code': 'new_code', + 'name': 'new_name', + }) # successful creation partner_1.write({'id_numbers': [(0, 0, { 'name': '1234', diff --git a/partner_identification/tests/test_res_partner.py b/partner_identification/tests/test_res_partner.py index e0f29544288..8394078e5ce 100644 --- a/partner_identification/tests/test_res_partner.py +++ b/partner_identification/tests/test_res_partner.py @@ -2,77 +2,33 @@ # Copyright 2017 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import fields, models from odoo.tests import common from odoo.exceptions import ValidationError -class ResPartner(models.Model): - _inherit = 'res.partner' - - social_security = fields.Char( - compute=lambda s: s._compute_identification( - 'social_security', 'SSN', - ), - inverse=lambda s: s._inverse_identification( - 'social_security', 'SSN', - ), - search=lambda s, *a: s._search_identification( - 'SSN', *a - ), - ) - - class TestResPartner(common.SavepointCase): - @classmethod - def _init_test_model(cls, model_cls): - """ Build a model from model_cls in order to test abstract models. - Note that this does not actually create a table in the database, so - there may be some unidentified edge cases. - Args: - model_cls (openerp.models.BaseModel): Class of model to initialize - Returns: - model_cls: Instance - """ - registry = cls.env.registry - cr = cls.env.cr - inst = model_cls._build_model(registry, cr) - model = cls.env[model_cls._inherit].with_context(todo=[]) - model._prepare_setup() - model._setup_base() - model._setup_fields() - model._setup_complete() - model._auto_init() - model.init() - return inst - @classmethod def setUpClass(cls): super(TestResPartner, cls).setUpClass() - cls.env.registry.enter_test_mode() - cls._init_test_model(ResPartner) - - def setUp(self): - super(TestResPartner, self).setUp() - bad_cat = self.env['res.partner.id_category'].create({ + bad_cat = cls.env['res.partner.id_category'].create({ 'code': 'another_code', 'name': 'another_name', }) - self.env['res.partner.id_number'].create({ + cls.env['res.partner.id_number'].create({ 'name': 'Bad ID', 'category_id': bad_cat.id, - 'partner_id': self.env.user.partner_id.id, + 'partner_id': cls.env.user.partner_id.id, }) - self.partner_id_category = self.env['res.partner.id_category'].create({ + cls.partner_id_category = cls.env['res.partner.id_category'].create({ 'code': 'id_code', 'name': 'id_name', }) - self.partner = self.env.user.partner_id - self.partner_id = self.env['res.partner.id_number'].create({ + cls.partner = cls.env.user.partner_id + cls.partner_id = cls.env['res.partner.id_number'].create({ 'name': 'Good ID', - 'category_id': self.partner_id_category.id, - 'partner_id': self.partner.id, + 'category_id': cls.partner_id_category.id, + 'partner_id': cls.partner.id, }) def test_compute_identification(self):