Port bemade_utils from 17.0 to 18.0
This commit is contained in:
parent
15f96ec795
commit
6ea4b324b3
6 changed files with 84 additions and 0 deletions
2
bemade_utils/__init__.py
Normal file
2
bemade_utils/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
from odoo.addons.bemade_utils.tools import patch_test
|
||||||
|
from . import tools
|
||||||
33
bemade_utils/__manifest__.py
Normal file
33
bemade_utils/__manifest__.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
#
|
||||||
|
# Bemade Inc.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2023-June Bemade Inc. (<https://www.bemade.org>).
|
||||||
|
# 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
|
||||||
|
}
|
||||||
2
bemade_utils/tests/__init__.py
Normal file
2
bemade_utils/tests/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
from . import test_patching_test
|
||||||
|
|
||||||
17
bemade_utils/tests/test_patching_test.py
Normal file
17
bemade_utils/tests/test_patching_test.py
Normal file
|
|
@ -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)
|
||||||
2
bemade_utils/tools/__init__.py
Normal file
2
bemade_utils/tools/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .test import patch_test
|
||||||
|
|
||||||
28
bemade_utils/tools/test.py
Normal file
28
bemade_utils/tools/test.py
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue