From c6199c76e94dbf5a1d5ce75e00ed7f6b889c8bbf Mon Sep 17 00:00:00 2001 From: Dinis Cruz Date: Sun, 31 Dec 2023 19:16:04 +0000 Subject: [PATCH] added tests for context decorator --- osbot_utils/base_classes/Cache_Pickle.py | 4 +- osbot_utils/decorators/methods/context.py | 6 +- tests/base_classes/test_Cache_Pickle.py | 6 +- tests/decorators/methods/test_context.py | 70 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/osbot_utils/base_classes/Cache_Pickle.py b/osbot_utils/base_classes/Cache_Pickle.py index 5036e419..7833060e 100644 --- a/osbot_utils/base_classes/Cache_Pickle.py +++ b/osbot_utils/base_classes/Cache_Pickle.py @@ -80,7 +80,9 @@ def _cache_disable(self): def _cache_path(self): class_name = self.__class__.__name__ module_name = self.__class__.__module__ - folder_name = f'{self._cache__FOLDER_CACHE_ROOT_FOLDER}/{module_name.replace(".", "/")}/{class_name}' + folder_name = f'{self._cache__FOLDER_CACHE_ROOT_FOLDER}/{module_name.replace(".", "/")}' + if not module_name.endswith(class_name): + folder_name += f'/{class_name}' return path_combine(temp_folder_current(), folder_name) def _cache_files(self): diff --git a/osbot_utils/decorators/methods/context.py b/osbot_utils/decorators/methods/context.py index a547a415..b2cf5d46 100644 --- a/osbot_utils/decorators/methods/context.py +++ b/osbot_utils/decorators/methods/context.py @@ -1,11 +1,11 @@ from contextlib import contextmanager @contextmanager -def context(target, exec_before=None, exec_after=None): +def context(target, *args, exec_before=None, exec_after=None, **kwargs): if exec_before: - exec_before() + exec_before(*args, **kwargs) try: yield target finally: if exec_after: - exec_after() + exec_after(*args, **kwargs) diff --git a/tests/base_classes/test_Cache_Pickle.py b/tests/base_classes/test_Cache_Pickle.py index 0e5d0485..c082916c 100644 --- a/tests/base_classes/test_Cache_Pickle.py +++ b/tests/base_classes/test_Cache_Pickle.py @@ -1,11 +1,9 @@ import types from unittest import TestCase - from osbot_utils.base_classes.Cache_Pickle import Cache_Pickle from osbot_utils.decorators.methods.context import context -from osbot_utils.utils.Dev import pprint from osbot_utils.utils.Files import folder_exists, current_temp_folder, pickle_load_from_file -from osbot_utils.utils.Misc import date_time_now, date_now, str_md5 + class test_Cache_Pickle(TestCase): @@ -30,8 +28,6 @@ class An_Class(Cache_Pickle): def return_42(self): return 42 - - an_class = An_Class() assert isinstance(an_class , An_Class ) assert isinstance(an_class , Cache_Pickle) diff --git a/tests/decorators/methods/test_context.py b/tests/decorators/methods/test_context.py index b0e16068..e95bc2b5 100644 --- a/tests/decorators/methods/test_context.py +++ b/tests/decorators/methods/test_context.py @@ -22,6 +22,76 @@ def exec_after(): before_mock.assert_called_once() after_mock.assert_called_once() + def test_context__with_args(self): + def the_answer(value): + assert value == 42 + return "it's 42" + def exec_before(value): + assert value == 'before_and_after' + def exec_after(value): + assert value == 'before_and_after' + + with context(the_answer, 'before_and_after', exec_before=exec_before, exec_after=exec_after) as target: # value can be passed as a param + assert target(42) == "it's 42" + args = ['before_and_after'] + with context(the_answer, *args, exec_before=exec_before, exec_after=exec_after) as target: # or as an *args object at the start + assert target(42) == "it's 42" + with context(the_answer, exec_before=exec_before, exec_after=exec_after, *args) as target: # or as an *args object at the end + assert target(42) == "it's 42" + + def test_context__with_kwargs(self): + value_before_and_after = {'before_and_after': 42} + value_target = {'target': 42} + return_value = "value was value_target" + + def an_dict(value): + assert value == value_target + return return_value + def exec_before(value): + assert value == value_before_and_after + def exec_after(value): + assert value == value_before_and_after + + with context(an_dict, value_before_and_after, exec_before=exec_before, exec_after=exec_after) as target: # value can be passed as a param + assert target(value_target) == return_value + + an_dict = {'answer': 42} + + def target(value): + assert value == an_dict + return context + + def exec_before(value): + assert value == an_dict + + def exec_after(value): + assert value == an_dict + + with context(target, an_dict, exec_before=exec_before, exec_after=exec_after) as target: # value can be passed as a param + assert target(an_dict) == context + + def test_context__with_kwargs_as_locals(self): + """ + shows an example of how to use the kwargs to pass data to the target function + """ + def target(data): + assert data == {'in_exec_before': 2, 'raw_data': 1} + data['in_target'] = 3 + return data + + def exec_before(data): + assert data == {'raw_data': 1} + data['in_exec_before'] = 2 + + def exec_after(data): + assert data == {'in_exec_before': 2, 'in_target': 3, 'raw_data': 1} + data['in_exec_after'] = 4 + + raw_data = {'raw_data': 1} + with context(target, raw_data, exec_before=exec_before, exec_after=exec_after) as target: + assert target(raw_data) == {'in_exec_before': 2, 'in_target': 3, 'raw_data': 1} + assert raw_data == {'in_exec_before': 2, 'in_target': 3, 'in_exec_after': 4, 'raw_data': 1} + def test_exec_before_and_after_called(self): before_mock = MagicMock() after_mock = MagicMock()