Skip to content

Commit

Permalink
Added new test util called: Patch_Print
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Jan 11, 2024
1 parent a62ed2a commit a362b06
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
7 changes: 6 additions & 1 deletion osbot_utils/testing/Duration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from datetime import timedelta
from functools import wraps
from osbot_utils.utils.Misc import date_time_now, time_delta_to_str

Expand Down Expand Up @@ -48,4 +49,8 @@ def end(self):
print(f"{self.prefix} {time_delta_to_str(self.duration)}")

def seconds(self):
return self.duration.total_seconds()
return self.duration.total_seconds()

def set_duration(self,seconds:int):
self.duration = timedelta(seconds=seconds)
return self
49 changes: 49 additions & 0 deletions osbot_utils/testing/Patch_Print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from unittest.mock import patch, MagicMock, _patch

from osbot_utils.utils.Dev import pprint

from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self


class Patch_Print(Kwargs_To_Self):
enabled : bool = True
expected_calls : list
mocked_print : MagicMock
patched_print : _patch
print_calls : bool


def __init__(self, **kwargs):
super().__init__(**kwargs)

def __enter__(self):
if self.enabled:
self.patched_print = patch('builtins.print')
self.mocked_print = self.patched_print.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self.enabled:
self.patched_print.stop()

if self.print_calls:
pprint(self.calls())
#print(self.mocked_print.call_args_list)

if self.expected_calls:
assert self.calls() == self.expected_calls

def calls(self):
calls_data = []
if self.mocked_print:
for call in self.mocked_print.call_args_list:
if len(call.args) == 0 and call.kwargs == {}:
call_data = ''
elif len(call.args) == 1 and call.kwargs == {}:
call_data = call.args[0]
else:
call_data = (call.args, call.kwargs)
calls_data.append(call_data)
return calls_data


39 changes: 39 additions & 0 deletions tests/testing/test_Patch_Print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest import TestCase

from osbot_utils.utils.Objects import obj_info

from osbot_utils.utils.Dev import pprint

from osbot_utils.testing.Patch_Print import Patch_Print


class test_Patch_Print(TestCase):

def setUp(self):
self.patch_print = Patch_Print()

def test__init__(self):
assert self.patch_print.__locals__() == {'enabled' : True ,
'expected_calls': [] ,
'mocked_print' : self.patch_print.mocked_print ,
'patched_print' : None ,
'print_calls' : False }


def test__enter__exit__(self):
with self.patch_print as _:
print('in test__enter__exit__',12,1)
print("aaaa", end='__end__')

assert _.calls() == [(('in test__enter__exit__', 12, 1), {}), (('aaaa',), {'end': '__end__'})]

def test_calls(self):
with self.patch_print as _:
print('simple print')
print('with only text')

assert _.calls() == ['simple print', 'with only text']




29 changes: 19 additions & 10 deletions tests/utils/trace/test_Trace_Call.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import TestCase
from unittest.mock import patch, call

from osbot_utils.testing.Patch_Print import Patch_Print
from osbot_utils.utils.Python_Logger import Python_Logger

from osbot_utils.utils.Misc import list_set, in_github_action, ansi_text_visible_length, wait_for
Expand Down Expand Up @@ -206,17 +207,25 @@ def an_temp_file():
def test__trace_up_to_a_level(self):
with self.config as _:
_.all()
_.up_to_depth(1)
_.up_to_depth(2)
_.trace_show_internals = True
_.duration(bigger_than=0, padding=100)


with self.trace_call as _:
logger = Python_Logger()
wait_for(0.1)
logger.add_memory_logger()
ansi_text_visible_length("some text")
_.view_data()
_.show_method_class = False

expected_calls = [ '' ,
'--------- CALL TRACER ----------' ,
'Here are the 6 traces captured\n' ,
'\x1b[1m📦 Trace Session\x1b[0m' ,
'\x1b[1m│ ├── 🔗️ Python_Logger.__init__\x1b[0m' ,
'\x1b[1m│ │ ├── 🧩️ random_string\x1b[0m' ,
'\x1b[1m│ │ └── 🧩️ set_config\x1b[0m' ,
'\x1b[1m│ └── 🔗️ add_memory_logger\x1b[0m' ,
'\x1b[1m└────────── 🧩️ add_handler_memory\x1b[0m' ]

with Patch_Print(expected_calls=expected_calls, print_calls=False, enabled=True):
with self.trace_call as _:
logger = Python_Logger()
logger.add_memory_logger()
#ansi_text_visible_length("some text")



Expand Down

0 comments on commit a362b06

Please sign in to comment.