TypeError: tests and solution¶
tests¶
the code in type_error/tests/test_type_error.py from TypeError
1import unittest
2import src.type_error
3
4
5class TestTypeError(unittest.TestCase):
6
7 def test_type_error_w_non_callables(self):
8 src.type_error.none()
9 src.type_error.false()
10 src.type_error.true()
11 src.type_error.a_list()
12 src.type_error.a_dictionary()
13
14 def test_type_error_w_function_signatures(self):
15 src.type_error.function_00('a')
16 src.type_error.function_01('a', 'b')
17 src.type_error.function_02('a', 'b', 'c')
18 src.type_error.function_03('a', 'b', 'c', 'd')
19
20 def test_type_error_w_objects_that_do_not_mix(self):
21 with self.assertRaises(TypeError):
22 None + 1
23 with self.assertRaises(TypeError):
24 'text' + 0.1
25 with self.assertRaises(TypeError):
26 (1, 2, 3, 'n') - {1, 2, 3, 'n'}
27
28
29# Exceptions Encountered
30# AssertionError
31# AttributeError
32# TypeError
the code in calculator/tests/test_calculator.py from TypeError
1import random
2import src.calculator
3import unittest
4
5
6def a_random_number():
7 return random.randint(-10, 10)
8
9
10class TestCalculator(unittest.TestCase):
11
12 random_x = a_random_number()
13 random_y = a_random_number()
14
15 def test_addition(self):
16 self.assertEqual(
17 src.calculator.add(self.random_x, self.random_y),
18 self.random_x+self.random_y
19 )
20
21 def test_subtraction(self):
22 self.assertEqual(
23 src.calculator.subtract(self.random_x, self.random_y),
24 self.random_x-self.random_y
25 )
26
27 def test_multiplication(self):
28 self.assertEqual(
29 src.calculator.multiply(self.random_x, self.random_y),
30 self.random_x*self.random_y
31 )
32
33 def test_division(self):
34 while self.random_y == 0:
35 with self.assertRaises(ZeroDivisionError):
36 src.calculator.divide(self.random_x, self.random_y)
37 self.random_y = a_random_number()
38 else:
39 self.assertEqual(
40 src.calculator.divide(self.random_x, self.random_y),
41 self.random_x/self.random_y
42 )
43
44 def test_calculator_raises_type_error(self):
45 self.assertEqual(
46 src.calculator.add(self.random_x, None),
47 'I only work with numbers'
48 )
49 self.assertEqual(
50 src.calculator.divide(self.random_x, None),
51 'I only work with numbers'
52 )
53 self.assertEqual(
54 src.calculator.multiply(self.random_x, None),
55 'I only work with numbers'
56 )
57 self.assertEqual(
58 src.calculator.subtract(self.random_x, None),
59 'I only work with numbers'
60 )
61
62 def test_calculator_with_strings(self):
63 self.assertEqual(
64 src.calculator.add('hello ', 'world'),
65 'I only work with numbers'
66 )
67 self.assertEqual(
68 src.calculator.divide('hello ', 'world'),
69 'I only work with numbers'
70 )
71 self.assertEqual(
72 src.calculator.multiply('hello', 'world'),
73 'I only work with numbers'
74 )
75 self.assertEqual(
76 src.calculator.subtract('hello', 'world'),
77 'I only work with numbers'
78 )
79
80
81# Exceptions Encountered
82# AssertionError
83# NameError
84# AttributeError
85# TypeError
86# ZeroDivisionError
solutions¶
the solution in type_error/src/type_error.py from TypeError
1def none():
2 return None
3
4
5def true():
6 return True
7
8
9def false():
10 return False
11
12
13def a_list():
14 return [1, 2, 3, 4]
15
16
17def a_dictionary():
18 return {'key': 'value'}
19
20
21def function_00():
22 return None
23
24
25def function_01(the_input):
26 return None
27
28
29def function_02(argument1, argument2):
30 return None
31
32
33def function_03(
34 argument1, argument2,
35 argument3
36 ):
37 return None
the solution in calculator/src/calculator.py from TypeError
1def check_input(function):
2 def wrapper(input_1, input_2):
3 error_message = 'I only work with numbers'
4 if isinstance(input_1, str) or isinstance(input_2, str):
5 return error_message
6 try:
7 return function(input_1, input_2)
8 except TypeError:
9 return error_message
10 return wrapper
11
12
13@check_input
14def subtract(input_1, input_2):
15 return input_1 - input_2
16
17
18@check_input
19def multiply(input_1, input_2):
20 return input_1 * input_2
21
22
23@check_input
24def divide(input_1, input_2):
25 return input_1 / input_2
26
27
28@check_input
29def add(input_1, input_2):
30 return input_1 + input_2