From 32fec0e2fdcc478acce26952572607c722320f30 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/README.rst | 4 +- partner_identification/__init__.py | 3 - partner_identification/__manifest__.py | 4 +- partner_identification/models/__init__.py | 3 - partner_identification/models/res_partner.py | 2 - .../models/res_partner_id_category.py | 2 - .../models/res_partner_id_number.py | 2 - partner_identification/tests/__init__.py | 3 - partner_identification/tests/fake_models.py | 47 ++++++++++++ .../tests/test_partner_identification.py | 19 +++-- .../tests/test_res_partner.py | 71 +++++-------------- 11 files changed, 81 insertions(+), 79 deletions(-) create mode 100644 partner_identification/tests/fake_models.py diff --git a/partner_identification/README.rst b/partner_identification/README.rst index b2476c9c2..32ea80896 100644 --- a/partner_identification/README.rst +++ b/partner_identification/README.rst @@ -1,4 +1,4 @@ -.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 @@ -21,7 +21,7 @@ and vary from country to country. Installation ============ -No specific installation step required +No specific installation step required. Configuration diff --git a/partner_identification/__init__.py b/partner_identification/__init__.py index a77a6fcbc..0650744f6 100644 --- a/partner_identification/__init__.py +++ b/partner_identification/__init__.py @@ -1,4 +1 @@ -# -*- coding: utf-8 -*- -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import models diff --git a/partner_identification/__manifest__.py b/partner_identification/__manifest__.py index e10cabebb..25cc490c0 100644 --- a/partner_identification/__manifest__.py +++ b/partner_identification/__manifest__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright 2004-2010 Tiny SPRL http://tiny.be # Copyright 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH # http://www.camptocamp.at @@ -28,5 +26,5 @@ 'ACSONE SA/NV,' 'LasLabs,' 'Odoo Community Association (OCA)', - 'website': 'https://odoo-community.org/', + 'website': 'https://github.com/OCA/partner-contact', } diff --git a/partner_identification/models/__init__.py b/partner_identification/models/__init__.py index 699c4c59d..e0e6e4fc1 100644 --- a/partner_identification/models/__init__.py +++ b/partner_identification/models/__init__.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import res_partner_id_number from . import res_partner_id_category from . import res_partner diff --git a/partner_identification/models/res_partner.py b/partner_identification/models/res_partner.py index 522d46206..06ae55741 100644 --- a/partner_identification/models/res_partner.py +++ b/partner_identification/models/res_partner.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright 2004-2010 Tiny SPRL http://tiny.be # Copyright 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH # http://www.camptocamp.at diff --git a/partner_identification/models/res_partner_id_category.py b/partner_identification/models/res_partner_id_category.py index b9cfca7ad..e73815598 100644 --- a/partner_identification/models/res_partner_id_category.py +++ b/partner_identification/models/res_partner_id_category.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright 2004-2010 Tiny SPRL http://tiny.be # Copyright 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH # http://www.camptocamp.at diff --git a/partner_identification/models/res_partner_id_number.py b/partner_identification/models/res_partner_id_number.py index ddb146b1f..2276f39ca 100644 --- a/partner_identification/models/res_partner_id_number.py +++ b/partner_identification/models/res_partner_id_number.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # Copyright 2004-2010 Tiny SPRL http://tiny.be # Copyright 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH # http://www.camptocamp.at diff --git a/partner_identification/tests/__init__.py b/partner_identification/tests/__init__.py index dd49771a4..e4a2a266e 100644 --- a/partner_identification/tests/__init__.py +++ b/partner_identification/tests/__init__.py @@ -1,5 +1,2 @@ -# -*- coding: utf-8 -*- -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import test_partner_identification from . import test_res_partner diff --git a/partner_identification/tests/fake_models.py b/partner_identification/tests/fake_models.py new file mode 100644 index 000000000..8438dfa55 --- /dev/null +++ b/partner_identification/tests/fake_models.py @@ -0,0 +1,47 @@ +# Copyright 2017 LasLabs Inc. +# Copyright 2018 ACSONE +# Copyright 2018 Camptocamp +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo import fields, models + + +def setup_test_model(env, model_cls): + """Pass a test model class and initialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + model_cls._build_model(env.registry, env.cr) + env.registry.setup_models(env.cr) + env.registry.init_models( + env.cr, [model_cls._name], + dict(env.context, update_custom_fields=True) + ) + + +def teardown_test_model(env, model_cls): + """Pass a test model class and deinitialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + if not getattr(model_cls, '_teardown_no_delete', False): + del env.registry.models[model_cls._name] + env.registry.setup_models(env.cr) + + +class ResPartner(models.Model): + _name = 'res.partner' + _inherit = 'res.partner' + _teardown_no_delete = True + + 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 9804eb5a7..d999de565 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -1,16 +1,14 @@ -# -*- coding: utf-8 -*- -# Copyright 2016 ACSONE SA/NV () +# Copyright 2016 ACSONE SA/NV () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). 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 +16,22 @@ class TestPartnerIdentificationBase(common.TransactionCase): 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 e0f295442..693196f8c 100644 --- a/partner_identification/tests/test_res_partner.py +++ b/partner_identification/tests/test_res_partner.py @@ -1,80 +1,45 @@ -# -*- coding: utf-8 -*- # 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 - ), - ) +from .fake_models import ResPartner, setup_test_model, teardown_test_model 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 + at_install = False + post_install = True @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({ + super().setUpClass() + setup_test_model(cls.env, ResPartner) + 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, }) + @classmethod + def tearDownClass(cls): + teardown_test_model(cls.env, ResPartner) + super().tearDownClass() + def test_compute_identification(self): """ It should set the proper field to the proper ID name. """ self.partner._compute_identification('name', 'id_code')