[10.0][IMP] stock_available_base_exclude_location: Better tests

This commit is contained in:
Denis Roussel
2020-09-18 15:48:09 +02:00
committed by Xavier Bouquiaux
parent 7ac5573655
commit 2a4b5bd058
4 changed files with 13 additions and 70 deletions

View File

@@ -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"
],

View File

@@ -1,2 +1 @@
from . import common
from . import test_stock_exclude_location

View File

@@ -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"]

View File

@@ -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):