1+ import importlib
2+ import io
3+ import os
4+ import re
5+ import sys
6+ from pathlib import Path
7+ from unittest import TestCase , skipIf
8+ from unittest .mock import patch
9+
10+ file_path = Path (__file__ ).absolute ().parent / 'main.py'
11+ is_file_empty = os .stat (file_path ).st_size == 0
12+
13+
14+ @skipIf (is_file_empty , 'Empty file. Test 022 Skipped' )
15+ class TestNextDay (TestCase ):
16+
17+ def setUp (self ) -> None :
18+ self .module_name = 'projects.m1.022-admission-price.python.main'
19+
20+ @patch ('builtins.input' )
21+ def test_ok (self , mock_inputs ):
22+ """
23+ Check if return the correct result
24+ """
25+
26+ mock_inputs .side_effect = [1.5 , 2 , 3 , 50 , '' ]
27+
28+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
29+ sys .modules .pop (self .module_name , None )
30+ importlib .import_module (name = self .module_name , package = 'files' )
31+
32+ result = re .findall (r'\d+\.\d+\d+' , mock_print .getvalue ())[- 1 ]
33+ self .assertEqual ('37.00' , result , 'Total cost not correct' )
34+
35+ @patch ('builtins.input' )
36+ def test_ok_2 (self , mock_inputs ):
37+ """
38+ Check if return the correct result
39+ """
40+
41+ mock_inputs .side_effect = [0 , 78 , 3 , 45 , '' ]
42+
43+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
44+ sys .modules .pop (self .module_name , None )
45+ importlib .import_module (name = self .module_name , package = 'files' )
46+
47+ result = re .findall (r'\d+\.\d+\d+' , mock_print .getvalue ())[- 1 ]
48+ self .assertEqual ('55.00' , result , 'Total cost not correct' )
49+
50+ @patch ('builtins.input' )
51+ def test_ok_3 (self , mock_inputs ):
52+ """
53+ Check if return the correct result
54+ """
55+
56+ mock_inputs .side_effect = [0 , '' ]
57+
58+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
59+ sys .modules .pop (self .module_name , None )
60+ importlib .import_module (name = self .module_name , package = 'files' )
61+
62+ result = re .findall (r'\d+\.\d+\d+' , mock_print .getvalue ())[- 1 ]
63+ self .assertEqual ('0.00' , result , 'Total cost not correct' )
64+
65+ @patch ('builtins.input' )
66+ def test_ok_no_inputs (self , mock_inputs ):
67+ """
68+ Check if return the correct result
69+ """
70+
71+ mock_inputs .side_effect = ['' ]
72+
73+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
74+ sys .modules .pop (self .module_name , None )
75+ importlib .import_module (name = self .module_name , package = 'files' )
76+
77+ result = re .findall (r'\d+\.\d+\d+' , mock_print .getvalue ())[- 1 ]
78+ self .assertEqual ('0.00' , result , 'Total cost not correct' )
0 commit comments