From 2a4b5bd0585b34836a0efe53268c0d241af612b1 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Fri, 18 Sep 2020 15:48:09 +0200 Subject: [PATCH] [10.0][IMP] stock_available_base_exclude_location: Better tests --- .../__manifest__.py | 2 +- .../tests/__init__.py | 1 - .../tests/common.py | 66 +------------------ .../tests/test_stock_exclude_location.py | 14 ++-- 4 files changed, 13 insertions(+), 70 deletions(-) diff --git a/stock_available_base_exclude_location/__manifest__.py b/stock_available_base_exclude_location/__manifest__.py index 4a539f3fb..826a25895 100644 --- a/stock_available_base_exclude_location/__manifest__.py +++ b/stock_available_base_exclude_location/__manifest__.py @@ -11,7 +11,7 @@ 'maintainers': ['rousseldenis'], 'license': 'AGPL-3', 'author': 'ACSONE SA/NV,Odoo Community Association (OCA)', - 'website': 'https://acsone.eu', + 'website': 'https://github.com/OCA/stock-logistics-warehouse', 'depends': [ "stock" ], diff --git a/stock_available_base_exclude_location/tests/__init__.py b/stock_available_base_exclude_location/tests/__init__.py index d48fb9c88..7e9fc5d74 100644 --- a/stock_available_base_exclude_location/tests/__init__.py +++ b/stock_available_base_exclude_location/tests/__init__.py @@ -1,2 +1 @@ -from . import common from . import test_stock_exclude_location diff --git a/stock_available_base_exclude_location/tests/common.py b/stock_available_base_exclude_location/tests/common.py index 4c6d2cc9c..837c8a7a1 100644 --- a/stock_available_base_exclude_location/tests/common.py +++ b/stock_available_base_exclude_location/tests/common.py @@ -1,70 +1,10 @@ # -*- coding: utf-8 -*- # Copyright 2020 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -import mock -from operator import attrgetter from odoo import models -class TestExcludeLocationMixin(object): - # Generate xmlids - # This is needed if you want to load data tied to a test model via xid. - _test_setup_gen_xid = False - # If you extend a real model (ie: res.partner) you must enable this - # to not delete the model on tear down. - _test_teardown_no_delete = False - # You can add custom fields to real models (eg: res.partner). - # In this case you must delete them to leave registry and model clean. - # This is mandatory for relational fields that link a fake model. - _test_purge_fields = [] +class ResPartner(models.Model): - @classmethod - def _test_setup_model(cls, env): - """Initialize it.""" - with mock.patch.object(env.cr, "commit"): - cls._build_model(env.registry, env.cr) - env.registry.setup_models(env.cr) - ctx = dict(env.context, update_custom_fields=True) - env.registry.init_models(env.cr, [cls._name], ctx) - - @classmethod - def _test_teardown_model(cls, env): - """Cleanup registry and real models.""" - - for fname in cls._test_purge_fields: - model = env[cls._name] - if fname in model: - model._pop_field(fname) - - if not getattr(cls, "_test_teardown_no_delete", False): - del env.registry.models[cls._name] - # here we must remove the model from list of children of inherited - # models - parents = cls._inherit - parents = ( - [parents] - if isinstance(parents, basestring) - else (parents or []) - ) - # keep a copy to be sure to not modify the original _inherit - parents = list(parents) - parents.extend(cls._inherits.keys()) - parents.append("base") - funcs = [ - attrgetter(kind + "_children") - for kind in ["_inherits", "_inherit"] - ] - for parent in parents: - for func in funcs: - children = func(env.registry[parent]) - if cls._name in children: - # at this stage our cls is referenced as children of - # parent -> must un reference it - children.remove(cls._name) - - -class ExcludeLocationModelFake(models.Model, TestExcludeLocationMixin): - - _name = "exclude.location.fake" - _inherit = "stock.exclude.location.mixin" - _description = "Exclude Location Fake" + _name = "res.partner" + _inherit = ["res.partner", "stock.exclude.location.mixin"] diff --git a/stock_available_base_exclude_location/tests/test_stock_exclude_location.py b/stock_available_base_exclude_location/tests/test_stock_exclude_location.py index f552eeea2..9b2a5e345 100644 --- a/stock_available_base_exclude_location/tests/test_stock_exclude_location.py +++ b/stock_available_base_exclude_location/tests/test_stock_exclude_location.py @@ -2,16 +2,20 @@ # Copyright 2020 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo.tests import SavepointCase -from .common import TestExcludeLocationMixin, ExcludeLocationModelFake +from odoo_test_helper import FakeModelLoader -class TestExcludeLocation(SavepointCase, TestExcludeLocationMixin): +class TestExcludeLocation(SavepointCase): @classmethod def setUpClass(cls): super(TestExcludeLocation, cls).setUpClass() - ExcludeLocationModelFake._test_setup_model(cls.env) - cls.fake = cls.env["exclude.location.fake"].create({}) + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + from .common import ResPartner + cls.loader.update_registry((ResPartner,)) + + cls.fake = cls.env["res.partner"].create({"name": "name"}) cls.location_shop = cls.env.ref("stock.stock_location_shop0") vals = { "location_id": cls.location_shop.id, @@ -23,7 +27,7 @@ class TestExcludeLocation(SavepointCase, TestExcludeLocationMixin): @classmethod def tearDownClass(cls): - ExcludeLocationModelFake._test_teardown_model(cls.env) + cls.loader.restore_registry() super(TestExcludeLocation, cls).tearDownClass() def _add_stock_to_product(self, product, location, qty):