Skip to content

Commit af61179

Browse files
author
Pipeline
committed
t - Added tests for EventLoggingManager and updated tests for TipsManager
1 parent 3e40cfc commit af61179

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

Infrastructure/EventLoggingManager.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
class EventLoggingManager:
55

66
def __init__(self, file_utility):
7-
file_path = file_utility.get_root_path() + "\\MobTimerEvents.log"
8-
if not file_utility.file_exists(file_path):
9-
file_utility.create_file(file_path)
7+
self.file_path = file_utility.get_root_path() + "\\MobTimerEvents.log"
8+
self.file_utility = file_utility
9+
if not self.file_utility.file_exists(self.file_path):
10+
self.file_utility.create_file(self.file_path)
11+
12+
def log(self, data):
13+
self.file_utility.append(self.file_path, '\n' + data)

Infrastructure/FileUtilities.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ def file_exists(self, file_path):
2222
def create_file(self, file_path):
2323
f = open(file_path, "w")
2424
f.close()
25+
26+
def append(self, file_path, data):
27+
f = open(file_path, "w+")
28+
f.write(data)
29+
f.close()

Infrastructure/TipsManager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ def __init__(self, seed=None, root_directory=sys.argv[0]):
1212
random.seed(seed)
1313

1414
def go_up_dir(self, root_directory):
15-
return "/".join(root_directory.split('\\')[:-1])
15+
if '\\' in root_directory:
16+
return "/".join(root_directory.split('\\')[:-1])
17+
else:
18+
return "/".join(root_directory.split('/')[:-1])
19+
1620

1721
def get_random_tip(self):
1822
tips_folder = self.root_directory + "/Tips"

tests/Infrastructure/EventLoggingManager/test_EventLoggingManager.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from unittest.mock import MagicMock
2+
from unittest.mock import call, MagicMock
33

44
from Infrastructure.EventLoggingManager import EventLoggingManager
55
from Infrastructure.FileUtilities import FileUtilities
@@ -17,6 +17,35 @@ def test_log_file_should_be_created_if_doesnt_exist(self):
1717

1818
file_utility.create_file.assert_called_with(file_utility.get_root_path() + '\\MobTimerEvents.log')
1919

20+
def test_log_file_should_not_create_if_file_exists(self):
21+
file_utility = FileUtilities()
22+
file_utility.get_root_path = MagicMock(return_value='beginning of file path')
23+
file_utility.file_exists = MagicMock(return_value=True)
24+
file_utility.create_file = MagicMock(return_value=True)
25+
26+
EventLoggingManager(file_utility)
27+
28+
file_utility.create_file.assert_not_called()
29+
30+
def test_log_file_should_append_to_log(self):
31+
file_utility = FileUtilities()
32+
file_utility.get_root_path = MagicMock(return_value='beginning of file path')
33+
file_utility.file_exists = MagicMock(return_value=True)
34+
file_utility.create_file = MagicMock(return_value=True)
35+
file_utility.append = MagicMock()
36+
logger = EventLoggingManager(file_utility)
37+
38+
test_data = ["Hello world 1", "Hello world 2"]
39+
40+
for entry in test_data:
41+
logger.log(entry)
42+
calls = []
43+
44+
for entry in test_data:
45+
calls.append(call('beginning of file path\\MobTimerEvents.log', '\n'+entry))
46+
47+
file_utility.append.assert_has_calls(calls)
48+
2049

2150
if __name__ == '__main__':
2251
unittest.main()

tests/Infrastructure/TipsManager/test_TipsManager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def test_random_tip_from_file_second(self):
2727
result = tips_manager.get_random_tip()
2828
self.assertEqual(result, 'TestTips.txt: Customer collaboration over contract negotiation\n')
2929

30+
def test_random_tip_from_file_second_alternate_slashes(self):
31+
seed = 1
32+
dirname = os.path.dirname(__file__)
33+
path = self.go_two_dirs_up(dirname) + "\\Tips"
34+
path = path.replace("\\", "/")
35+
tips_manager = TipsManager(seed, path)
36+
result = tips_manager.get_random_tip()
37+
self.assertEqual(result, 'TestTips.txt: Customer collaboration over contract negotiation\n')
38+
3039

3140
if __name__ == '__main__':
3241
unittest.main()

0 commit comments

Comments
 (0)