Skip to content

Commit

Permalink
Updated unittest for new infos in trace
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAndradeDias committed Mar 13, 2024
1 parent dd8d256 commit 3334ac0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
25 changes: 13 additions & 12 deletions tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@
import collections
import sys

from pathlib import Path
from src.outliner import Display
from unittest.mock import patch
from io import StringIO


PATH = Path(str(Path(__file__).resolve() / "data" ) + "/module_testing_1.py")

functions_flow = collections.OrderedDict(
[("test2", None), ("__init__", None), ("func1", None), ("func2", None)]
)

detail_data = collections.defaultdict(
int,
{
"test2": {"call": 1, "return": 1, "line": 2},
"__init__": {"call": 1, "return": 1, "line": 1},
"func1": {"call": 1, "return": 1, "line": 2},
"func2": {"call": 1, "return": 1, "line": 1},
"test2": {"call": 1, "return": 1, "line": 2, "file": PATH, "start_line": 13},
"__init__": {"call": 1, "return": 1, "line": 1, "file": PATH, "start_line": 2},
"func1": {"call": 1, "return": 1, "line": 2, "file": PATH, "start_line": 5},
"func2": {"call": 1, "return": 1, "line": 1, "file": PATH, "start_line": 9},
},
)

detail_data_with_exception = collections.defaultdict(
int,
{
"test2": {"call": 1, "return": 1, "line": 2},
"__init__": {"call": 1, "return": 1, "line": 1},
"func1": {"call": 1, "return": 1, "line": 2},
"func2": {"call": 1, "return": 1, "line": 1, "exception": 1},
"test2": {"call": 1, "return": 1, "line": 2, "file": PATH, "start_line": 13},
"__init__": {"call": 1, "return": 1, "line": 1, "file": PATH, "start_line": 2},
"func1": {"call": 1, "return": 1, "line": 2, "file": PATH, "start_line": 5},
"func2": {"call": 1, "return": 1, "line": 1, "exception": 1, "file": PATH, "start_line": 9},
},
)

Expand All @@ -38,9 +41,9 @@ def setUp(self):

@patch("sys.stdout", new_callable=StringIO)
def test_display_tree_positive_input(self, stdout):
expected_result = " \x1b[38;5;208mtest2\x1b[0m\n\n │──\x1b[36m1. __init__\n\x1b[0m │\n │──\x1b[36m2. func1\n\x1b[0m │\n │──\x1b[36m3. func2\n\x1b[0m"
expected_result = " \x1b[38;5;208mtest2\x1b[0m\n\n │──\x1b[36m1. __init__ module_testing_1.py 2\n\x1b[0m │\n │──\x1b[36m2. func1 module_testing_1.py 5\n\x1b[0m │\n │──\x1b[36m3. func2 module_testing_1.py 9\n\x1b[0m"
self.parser.tree()
self.assertEqual(stdout.getvalue(), expected_result)
self.assertEqual(repr(stdout.getvalue()), repr(expected_result))

@patch("src.outliner.Display")
@patch("sys.stdout", new_callable=StringIO)
Expand All @@ -54,7 +57,6 @@ def test_detailed_data_positive_input(self, stdout):
expected_result = "\nInvoked objects: 4\n\n__init__\n called: 1\n return: 1\n lines: 1\n\nfunc1\n called: 1\n return: 1\n lines: 2\n\nfunc2\n called: 1\n return: 1\n lines: 1\n\ntest2\n called: 1\n return: 1\n lines: 2\n"

self.parser.detailed_data()
# print(repr(stdout.getvalue()))
self.assertEqual(expected_result, stdout.getvalue())

@patch("src.outliner.Display")
Expand All @@ -67,7 +69,6 @@ def test_detailed_data_negative_input(self, stdout, module):
@patch("src.outliner.Display")
@patch("sys.stdout", new_callable=StringIO)
def test_func_with_exception(self, stdout, module):
expected_result = " \x1b[38;5;208mtest2\x1b[0m\n\n │──\x1b[36m1. __init__\n\x1b[0m │\n │──\x1b[36m2. func1\n\x1b[0m │\n │──\x1b[33m3. func2\n\x1b[0m"
module_instance = module()
module_instance.tree()
self.assertEqual("", stdout.getvalue())
6 changes: 4 additions & 2 deletions tests/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_trace_obj_positive_input(self):
module_obj = ModuleObject(Path(self.mock_path + "/module_testing_1.py"))
module_running_obj = RunningObject(module_obj, "test()")
self.parser.trace_obj(module_running_obj)

expected_trace = r"Trace object\nrunned objects:\n __init__ + \n func1 + \n func2 + \n "
self.assertEqual(expected_trace, repr(self.parser))

Expand All @@ -55,6 +56,7 @@ def test_trace_run_file_positive_multiple_runs(self):
"""
module_path = ModuleObject(Path(self.mock_path + "/module_testing_1.py"))
module_running_obj_1 = RunningObject(module_path, "test()")

module_running_obj_2 = RunningObject(module_path, "test2()")
self.parser.trace_obj(module_running_obj_1)
self.parser.trace_obj(module_running_obj_2)
Expand All @@ -63,7 +65,7 @@ def test_trace_run_file_positive_multiple_runs(self):

def test_trace_run_with_imported_files(self):
"""
Given a valid module path and a callable function "test",
Given a valid module path and a callable function "main",
When Trace.trace_obj is called twice with these parameters,
Then it should trace the functions individually and produce the expected trace result for each run.
"""
Expand All @@ -76,7 +78,7 @@ def test_trace_run_with_imported_files(self):
expected_trace = (
r"Trace object\nrunned objects:"
r"\n main + \n __init__ + "
r"\n calculate_area + \n "
r"\n calculate_area1 + \n calculate_area + \n "
r"load_data + \n analyze_data + \n ")

self.assertEqual(expected_trace, repr(self.parser))

0 comments on commit 3334ac0

Please sign in to comment.