forked from egrfly/git-exception-testing-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_handlers.py
136 lines (106 loc) · 6.74 KB
/
test_handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from unittest import TestCase, mock, main
from io import StringIO
import os
import handlers
# Helper function to get most recently printed line
def get_last_line(out):
return out.getvalue().strip().splitlines()[-1]
# Helper function for FileNotFoundError tests
def remove_file_or_directory_if_exists(filename):
try:
os.remove(filename) # Remove file if exists
except OSError:
try:
os.rmdir(filename) # Remove directory (folder) if exists
except OSError:
pass
# Test to make sure exceptions aren't arising directly from raise statements
class TestHandlersFileContent(TestCase):
def test_raise_keyword_not_used(self):
with open('handlers.py', 'r') as exceptions_file:
exceptions_file_content = exceptions_file.read()
raise_keyword_detected = 'raise' in exceptions_file_content.split()
self.assertFalse(raise_keyword_detected, msg="Raise keyword detected in handlers.py")
# Tests to make sure correct outputs arise from valid inputs
class TestHandlersWithValidInput(TestCase):
def test_handle_key_error_with_valid_input(self):
with mock.patch('builtins.input', return_value='orange'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_key_error()
self.assertEqual(get_last_line(out), "That will be 1.24, please!")
def test_handle_index_error_with_valid_input(self):
with mock.patch('builtins.input', return_value='0'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_index_error()
self.assertEqual(get_last_line(out), "You selected: Purchase some fruit.")
def test_handle_value_error_with_valid_input(self):
with mock.patch('builtins.input', return_value='2'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_value_error()
self.assertEqual(get_last_line(out), "That will be 2.48, please!")
def test_handle_zero_division_error_with_valid_input(self):
with mock.patch('builtins.input', return_value='5'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_zero_division_error()
self.assertEqual(get_last_line(out), "In that case, each of you should receive 200 oranges.")
def test_handle_file_not_found_error_with_valid_input(self):
this_file = os.path.basename(__file__)
with mock.patch('builtins.input', return_value=this_file), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_file_not_found_error()
self.assertEqual(get_last_line(out), "Thanks, order received!")
def test_handle_unicode_encode_error_with_valid_input(self):
remove_file_or_directory_if_exists('orange')
with mock.patch('builtins.input', return_value='E'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_unicode_encode_error()
self.assertEqual(get_last_line(out), "Your orange is ready. Look around in your current folder!")
with open('orange', 'r') as orange_file:
self.assertEqual(orange_file.readlines(), [" .oOOOo. \n",
"d8 8b\n",
"88 E 88\n",
"88 88\n",
" ^8bod8^ "])
# Tests to make sure correct outputs arise from invalid inputs
class TestHandlersWithInvalidInput(TestCase):
def test_handle_key_error_with_invalid_input(self):
with mock.patch('builtins.input', return_value='banana'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_key_error()
self.assertEqual(get_last_line(out), "Sorry, we don't stock that fruit.")
def test_handle_index_error_with_out_of_range_input(self):
with mock.patch('builtins.input', return_value='5'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_index_error()
self.assertEqual(get_last_line(out), "Sorry, that isn't one of the available options.")
def test_handle_value_error_with_invalid_input(self):
with mock.patch('builtins.input', return_value='4.8'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_value_error()
self.assertEqual(get_last_line(out), "Sorry, that isn't a whole number of oranges.")
def test_handle_zero_division_error_with_zero_input(self):
with mock.patch('builtins.input', return_value='0'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_zero_division_error()
self.assertEqual(get_last_line(out), "Sorry, we can't split the oranges between no people.")
def test_handle_file_not_found_error_with_invalid_input(self):
remove_file_or_directory_if_exists('donkey.txt')
with mock.patch('builtins.input', return_value='donkey.txt'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_file_not_found_error()
self.assertEqual(get_last_line(out), "Sorry, we couldn't open your order file.")
def test_handle_unicode_encode_error_with_valid_input(self):
with mock.patch('builtins.input', return_value='£'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_unicode_encode_error()
self.assertEqual(get_last_line(out), "Sorry, the character you chose wasn't ASCII.")
# Bonus tests to make sure correct outputs arise from invalid inputs
class TestHandlersWithBonusInvalidInput(TestCase):
def test_handle_index_error_with_non_numeric_input(self):
with mock.patch('builtins.input', return_value='Bungled'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_index_error()
self.assertEqual(get_last_line(out), "Sorry, that isn't one of the available options.")
def test_handle_zero_division_error_with_non_numeric_input(self):
with mock.patch('builtins.input', return_value='Donkeys'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_zero_division_error()
self.assertEqual(get_last_line(out), "Sorry, that isn't a whole number of people.")
def test_handle_file_not_found_error_with_directory_input(self):
remove_file_or_directory_if_exists('dingo')
# Make directory (folder) instead of file and perform test
try:
os.mkdir('dingo')
with mock.patch('builtins.input', return_value='dingo'), mock.patch('sys.stdout', new=StringIO()) as out:
handlers.handle_file_not_found_error()
self.assertEqual(get_last_line(out), "Sorry, we couldn't open your order file.")
finally:
os.rmdir('dingo')
if __name__ == '__main__':
main()