Skip to content

Commit

Permalink
added tests for context decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Dec 31, 2023
1 parent a15d9a9 commit c6199c7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
4 changes: 3 additions & 1 deletion osbot_utils/base_classes/Cache_Pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions osbot_utils/decorators/methods/context.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 1 addition & 5 deletions tests/base_classes/test_Cache_Pickle.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand Down
70 changes: 70 additions & 0 deletions tests/decorators/methods/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c6199c7

Please sign in to comment.