From f5eb434b76f0b4268a396d67c1a66cbc1a9f4386 Mon Sep 17 00:00:00 2001
From: erl-odoo <erl@odoo.com>
Date: Thu, 25 May 2023 15:09:26 +0000
Subject: [PATCH] [IMP] core: ease testing upgrade scripts in custom modules

Upgrade (aka migration) scripts are a core part of Odoo, allowing
database manipulations for modules during version changes.

Any module, including custom ones can run upgrade scripts, even if the
`--upgrade-path` flag (and with it, the `odoo.upgrade` sub-module) is
not present. Currently only the "standard" modules benefit of easy
upgrade script testing. Any custom modules that want to run tests of
their upgrades have to import the tests in the usual `tests` folder,
which is not ideal.

Therefore, to allow TDD and programmatic testing of upgrade scripts in
custom modules, the test discovery is here modified to also parse the
module's `migrations` and `upgrades` sub-modules for tests.

closes odoo/odoo#136175

X-original-commit: e8683726b2c1a6dfaa831587e0f1645e6aa3713a
Signed-off-by: Vranckx Florian (flvr) <flvr@odoo.com>
Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
---
 odoo/tests/loader.py | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/odoo/tests/loader.py b/odoo/tests/loader.py
index 0da120d990f6..99ea7cf0f69a 100644
--- a/odoo/tests/loader.py
+++ b/odoo/tests/loader.py
@@ -18,13 +18,7 @@ def get_test_modules(module):
     feed unittest.TestLoader.loadTestsFromModule() """
     # Try to import the module
     results = _get_tests_modules('odoo.addons', module)
-
-    try:
-        importlib.import_module('odoo.upgrade.%s' % module)
-    except ImportError:
-        pass
-    else:
-        results += list(_get_upgrade_test_modules(module))
+    results += list(_get_upgrade_test_modules(module))
 
     return results
 
@@ -53,16 +47,26 @@ def _get_tests_modules(path, module):
     return result
 
 def _get_upgrade_test_modules(module):
-    upg = importlib.import_module("odoo.upgrade")
-    for path in map(Path, upg.__path__):
-        for test in (path / module / "tests").glob("test_*.py"):
-            spec = importlib.util.spec_from_file_location(f"odoo.upgrade.{module}.tests.{test.stem}", test)
-            if not spec:
-                continue
-            pymod = importlib.util.module_from_spec(spec)
-            sys.modules[spec.name] = pymod
-            spec.loader.exec_module(pymod)
-            yield pymod
+    upgrade_modules = (
+        f"odoo.upgrade.{module}",
+        f"odoo.addons.{module}.migrations",
+        f"odoo.addons.{module}.upgrades",
+    )
+    for module_name in upgrade_modules:
+        try:
+            upg = importlib.import_module(module_name)
+        except ImportError:
+            continue
+
+        for path in map(Path, upg.__path__):
+            for test in path.glob("tests/test_*.py"):
+                spec = importlib.util.spec_from_file_location(f"{upg.__name__}.tests.{test.stem}", test)
+                if not spec:
+                    continue
+                pymod = importlib.util.module_from_spec(spec)
+                sys.modules[spec.name] = pymod
+                spec.loader.exec_module(pymod)
+                yield pymod
 
 
 def make_suite(module_names, position='at_install'):
-- 
GitLab