From 917fc8c01ea65ddd1f284a0de965e0ecbfa26d12 Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Tue, 12 Mar 2024 14:16:40 -0400 Subject: [PATCH] [ADD] bemade_utils: new module currently only with a decorator for patching tests. --- bemade_utils/__init__.py | 0 bemade_utils/__manifest__.py | 33 ++++++++++++++++++++++++ bemade_utils/tests/__init__.py | 1 + bemade_utils/tests/test_patching_test.py | 17 ++++++++++++ bemade_utils/tools/test.py | 28 ++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 bemade_utils/__init__.py create mode 100644 bemade_utils/__manifest__.py create mode 100644 bemade_utils/tests/__init__.py create mode 100644 bemade_utils/tests/test_patching_test.py create mode 100644 bemade_utils/tools/test.py diff --git a/bemade_utils/__init__.py b/bemade_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bemade_utils/__manifest__.py b/bemade_utils/__manifest__.py new file mode 100644 index 0000000..e330099 --- /dev/null +++ b/bemade_utils/__manifest__.py @@ -0,0 +1,33 @@ +# +# Bemade Inc. +# +# Copyright (C) 2023-June Bemade Inc. (). +# Author: Marc Durepos (Contact : marc@bemade.org) +# +# This program is under the terms of the Odoo Proprietary License v1.0 (OPL-1) +# It is forbidden to publish, distribute, sublicense, or sell copies of the Software +# or modified copies of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +{ + 'name': 'Bemade App Utilities', + 'version': '15.0.1.0.0', + 'summary': 'Utilities commonly used in Bemade modules.', + 'description': 'Adds utilities such as an annotation for patching tests when modifying Odoo behaviour.', + 'category': 'Technical', + 'author': 'Bemade Inc.', + 'website': 'http://www.bemade.org', + 'license': 'OPL-1', + 'depends': [], + 'data': [], + 'assets': {}, + 'installable': True, + 'auto_install': False +} diff --git a/bemade_utils/tests/__init__.py b/bemade_utils/tests/__init__.py new file mode 100644 index 0000000..27cd6d5 --- /dev/null +++ b/bemade_utils/tests/__init__.py @@ -0,0 +1 @@ +from . import test_patching_test diff --git a/bemade_utils/tests/test_patching_test.py b/bemade_utils/tests/test_patching_test.py new file mode 100644 index 0000000..9b4b5da --- /dev/null +++ b/bemade_utils/tests/test_patching_test.py @@ -0,0 +1,17 @@ +from odoo.tests import TransactionCase +from addons.bemade_utils.tools.test import patch_test + + +class TestA(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_method_a(self): + self.assertFalse(True) + + +class TestB(TransactionCase): + @patch_test(TestA.test_method_a) + def test_redefining_test(self): + self.assertTrue(True) diff --git a/bemade_utils/tools/test.py b/bemade_utils/tools/test.py new file mode 100644 index 0000000..29e34d3 --- /dev/null +++ b/bemade_utils/tools/test.py @@ -0,0 +1,28 @@ +import importlib +from functools import wraps + + +def patch_test(original_method): + """ + A decorator that patches an Odoo test method with a new one. + The original_method is a direct reference to the method to be patched. + """ + def decorator(new_method): + @wraps(new_method) + def wrapper(*args, **kwargs): + return new_method(*args, **kwargs) + + # Extract module and class names + module_name = original_method.__module__ + class_name = original_method.__qualname__.split('.')[0] # Assuming the method is always in a class + + # Import the module + module = importlib.import_module(module_name) + # Get the class + cls = getattr(module, class_name) + + # Replace the original method with the new one + setattr(cls, original_method.__name__, wrapper) + + return wrapper + return decorator