diff --git a/bemade_utils/__init__.py b/bemade_utils/__init__.py new file mode 100644 index 0000000..91a8e75 --- /dev/null +++ b/bemade_utils/__init__.py @@ -0,0 +1,2 @@ +from odoo.addons.bemade_utils.tools import patch_test +from . import tools \ No newline at end of file diff --git a/bemade_utils/__manifest__.py b/bemade_utils/__manifest__.py new file mode 100644 index 0000000..898c3d9 --- /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': '18.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': ['base'], + '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..396c8ff --- /dev/null +++ b/bemade_utils/tests/__init__.py @@ -0,0 +1,2 @@ +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..fc45946 --- /dev/null +++ b/bemade_utils/tests/test_patching_test.py @@ -0,0 +1,17 @@ +from odoo.tests import TransactionCase, tagged +from odoo.addons.bemade_utils import patch_test + + +@tagged("-at_install", "post_install") +class TestA(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_method_a(self): + self.assertFalse(True) + + +@patch_test(TestA.test_method_a) +def test_redefining_test(self): + self.assertTrue(True) diff --git a/bemade_utils/tools/__init__.py b/bemade_utils/tools/__init__.py new file mode 100644 index 0000000..f467f1c --- /dev/null +++ b/bemade_utils/tools/__init__.py @@ -0,0 +1,2 @@ +from .test import patch_test + diff --git a/bemade_utils/tools/test.py b/bemade_utils/tools/test.py new file mode 100644 index 0000000..3f4aa18 --- /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] + + # 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