Skip to content

Commit

Permalink
[IMP] testing: make MetaCase optional
Browse files Browse the repository at this point in the history
The metaclass is being removed from community because it's unnecessary complexity when `__init_subclass__` exists.

See also odoo/odoo#113461

closes #189

Signed-off-by: Christophe Simonis (chs) <[email protected]>
  • Loading branch information
xmo-odoo committed Jan 14, 2025
1 parent ea530f7 commit e38e861
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions src/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import odoo
from odoo import api, release
from odoo.modules.registry import Registry
from odoo.tests.common import BaseCase, MetaCase, TransactionCase, get_db_name
from odoo.tests.common import BaseCase, TransactionCase, get_db_name
from odoo.tools import config
from odoo.tools.parse_version import parse_version

try:
from odoo.tests.common import MetaCase
except ImportError:
MetaCase = None
try:
from unittest.mock import patch
except ImportError:
Expand Down Expand Up @@ -67,22 +71,41 @@ def decorator(func):
return decorator


def _create_meta(sequence, *tags):
class UpgradeMetaCase(MetaCase):
def __init__(self, name, bases, attrs, **kwargs):
# Setting test_tags in __init_subclass__ could work, but BaseCase will override them in __init__.
# we need to set test_tags after BaseCase __init__
super().__init__(name, bases, attrs)
self.test_sequence = sequence
self.test_tags = {"post_install", "upgrade"} | set(tags)
self.test_class = name
def _create_meta(sequence: int, *tags: str) -> type:
if MetaCase:

class UpgradeMetaCase(MetaCase):
def __init__(self, name, bases, attrs, **kwargs):
# Setting test_tags in __init_subclass__ could work, but BaseCase will override them in __init__.
# we need to set test_tags after BaseCase __init__
super().__init__(name, bases, attrs)
self.test_sequence = sequence
self.test_tags = {"post_install", "upgrade"} | set(tags)
self.test_class = name

if self.__module__.startswith("odoo.upgrade."):
self.test_module = self.__module__.split(".")[2]
elif self.__module__.startswith("odoo.addons.base.maintenance.migrations"):
self.test_module = self.__module__.split(".")[5]

return UpgradeMetaCase("UpgradeMetaCase", (), {})
else:

class UpgradeMetaCase(BaseCase):
def __init_subclass__(cls):
super().__init_subclass__()

if cls.__module__.startswith("odoo.upgrade."):
cls.test_module = cls.__module__.split(".")[2]
elif cls.__module__.startswith("odoo.addons.base.maintenance.migrations"):
cls.test_module = cls.__module__.split(".")[5]
else:
return

if self.__module__.startswith("odoo.upgrade."):
self.test_module = self.__module__.split(".")[2]
elif self.__module__.startswith("odoo.addons.base.maintenance.migrations"):
self.test_module = self.__module__.split(".")[5]
cls.test_tags = {"post_install", "upgrade"} | set(tags)
cls.test_sequence = sequence

return UpgradeMetaCase("UpgradeMetaCase", (), {})
return UpgradeMetaCase


class UnitTestCase(TransactionCase, _create_meta(10, "upgrade_unit")):
Expand Down

0 comments on commit e38e861

Please sign in to comment.