|
1 | 1 | import unittest
|
2 | 2 | import pytest
|
| 3 | +from copy import deepcopy |
3 | 4 |
|
4 | 5 | from list_methods import (
|
5 | 6 | add_me_to_the_queue,
|
|
15 | 16 | class ListMethodsTest(unittest.TestCase):
|
16 | 17 | @pytest.mark.task(taskno=1)
|
17 | 18 | def test_add_me_to_the_queue(self):
|
18 |
| - data = [ |
19 |
| - ((['Tony', 'Bruce'], ['RobotGuy', 'WW'], 0, 'HawkEye'), ['RobotGuy', 'WW', 'HawkEye']), |
20 |
| - ((['Tony', 'Bruce'], ['RobotGuy', 'WW'], 1, 'RichieRich'), ['Tony', 'Bruce', 'RichieRich']), |
21 |
| - ] |
| 19 | + test_data = [(['Tony', 'Bruce'], ['RobotGuy', 'WW'], 0, 'HawkEye'), |
| 20 | + (['Tony', 'Bruce'], ['RobotGuy', 'WW'], 1, 'RichieRich')] |
| 21 | + result_data = [['RobotGuy', 'WW', 'HawkEye'], |
| 22 | + ['Tony', 'Bruce', 'RichieRich']] |
| 23 | + |
| 24 | + for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1): |
| 25 | + |
| 26 | + # Deepcopy() is needed here because the task expects the input lists to be mutated. |
| 27 | + # That mutation wrecks havoc with the verification and error messaging. |
| 28 | + express_queue, normal_queue, ticket_type, person_name = deepcopy(params) |
| 29 | + |
| 30 | + with self.subTest(f'variation #{variant}', |
| 31 | + express_queue=express_queue, |
| 32 | + normal_queue=normal_queue, |
| 33 | + ticket_type=ticket_type, |
| 34 | + person_name=person_name, |
| 35 | + expected=expected): |
22 | 36 |
|
23 |
| - error_message = 'The person was not added to the queue correctly.' |
24 |
| - for variant, (params, result) in enumerate(data, start=1): |
25 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
26 |
| - self.assertListEqual(add_me_to_the_queue(*params), result, msg=error_message) |
| 37 | + actual_result = add_me_to_the_queue(*params) |
| 38 | + error_message = (f'Called add_me_to_the_queue{express_queue, normal_queue, ticket_type, person_name}. ' |
| 39 | + f'The function returned {actual_result}, but ' |
| 40 | + f'The tests expected {expected} when adding ' |
| 41 | + f'{person_name} to the queue.') |
| 42 | + |
| 43 | + self.assertListEqual(actual_result, expected, msg=error_message) |
27 | 44 |
|
28 | 45 | @pytest.mark.task(taskno=2)
|
29 | 46 | def test_find_my_friend(self):
|
30 |
| - data = [ |
31 |
| - ((['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Natasha'), 0), |
32 |
| - ((['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Steve'), 1), |
33 |
| - ((['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Rocket'), 4), |
34 |
| - ] |
| 47 | + test_data = [(['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Natasha'), |
| 48 | + (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Steve'), |
| 49 | + (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 'Rocket')] |
| 50 | + result_data = (0,1,4) |
| 51 | + |
| 52 | + for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1): |
| 53 | + with self.subTest(f'variation #{variant}', params=params, expected=expected): |
| 54 | + actual_result = find_my_friend(*params) |
| 55 | + error_message = (f'Called find_my_friend{params}. ' |
| 56 | + f'The function returned {actual_result}, but ' |
| 57 | + f'The tests expected {expected} when looking for ' |
| 58 | + f'{params[-1]} in the queue.') |
35 | 59 |
|
36 |
| - error_message = 'The index of the friend to find is incorrect.' |
37 |
| - for variant, (params, result) in enumerate(data, start=1): |
38 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
39 |
| - self.assertIs(find_my_friend(*params), result, msg=error_message) |
| 60 | + self.assertIs(actual_result, expected, msg=error_message) |
40 | 61 |
|
41 | 62 | @pytest.mark.task(taskno=3)
|
42 | 63 | def test_add_me_with_my_friends(self):
|
43 |
| - data = [ |
44 |
| - ( |
45 |
| - (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 0, 'Bucky'), |
46 |
| - ['Bucky', 'Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'] |
47 |
| - ), |
48 |
| - ( |
| 64 | + test_data = [(['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 0, 'Bucky'), |
49 | 65 | (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 1, 'Bucky'),
|
50 |
| - ['Natasha', 'Bucky', 'Steve', 'Tchalla', 'Wanda', 'Rocket'] |
51 |
| - ), |
52 |
| - ( |
53 |
| - (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 5, 'Bucky'), |
54 |
| - ['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket', 'Bucky'] |
55 |
| - ), |
56 |
| - ] |
| 66 | + (['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], 5, 'Bucky')] |
| 67 | + |
| 68 | + result_data = [['Bucky', 'Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], |
| 69 | + ['Natasha', 'Bucky', 'Steve', 'Tchalla', 'Wanda', 'Rocket'], |
| 70 | + ['Natasha', 'Steve', 'Tchalla', 'Wanda', 'Rocket', 'Bucky']] |
| 71 | + |
| 72 | + for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1): |
| 73 | + |
| 74 | + # Deepcopy() is needed here because the task expects the input lists to be mutated. |
| 75 | + # That mutation wrecks havoc with the verification and error messaging. |
| 76 | + queue, index, person_name = deepcopy(params) |
| 77 | + |
| 78 | + with self.subTest(f'variation #{variant}', |
| 79 | + queue=queue, |
| 80 | + index=index, |
| 81 | + person_name=person_name, |
| 82 | + expected=expected): |
| 83 | + |
| 84 | + actual_result = add_me_with_my_friends(*params) |
| 85 | + error_message = (f'Called add_me_with_my_friends{queue, index, person_name}. ' |
| 86 | + f'The function returned {actual_result}, but ' |
| 87 | + f'The tests expected {expected} when adding ' |
| 88 | + f'{person_name} to position {index} in the queue.') |
57 | 89 |
|
58 |
| - error_message = 'The person was added to the wrong location in the queue or was not added at all.' |
59 |
| - for variant, (params, result) in enumerate(data, start=1): |
60 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
61 |
| - self.assertListEqual(add_me_with_my_friends(*params), result, error_message) |
| 90 | + self.assertListEqual(actual_result, expected, msg=error_message) |
62 | 91 |
|
63 | 92 | @pytest.mark.task(taskno=4)
|
64 | 93 | def test_remove_the_mean_person(self):
|
65 |
| - data = [ |
66 |
| - ( |
67 |
| - (['Natasha', 'Steve', 'Ultron', 'Wanda', 'Rocket'], 'Ultron'), |
68 |
| - ['Natasha', 'Steve', 'Wanda', 'Rocket'] |
69 |
| - ), |
70 |
| - ( |
71 |
| - (['Natasha', 'Steve', 'Wanda', 'Rocket', 'Ultron'], 'Ultron'), |
72 |
| - ['Natasha', 'Steve', 'Wanda', 'Rocket'] |
73 |
| - ), |
74 |
| - ( |
75 |
| - (['Ultron', 'Natasha', 'Steve', 'Wanda', 'Rocket'], 'Ultron'), |
76 |
| - ['Natasha', 'Steve', 'Wanda', 'Rocket'] |
77 |
| - ), |
| 94 | + test_data = [(['Natasha', 'Steve', 'Ultron', 'Wanda', 'Rocket'], 'Ultron'), |
| 95 | + (['Natasha', 'Steve', 'Wanda', 'Rocket', 'Ultron'], 'Ultron'), |
| 96 | + (['Ultron', 'Natasha', 'Steve', 'Wanda', 'Rocket'], 'Ultron'), |
| 97 | + |
78 | 98 | ]
|
| 99 | + result_data = [['Natasha', 'Steve', 'Wanda', 'Rocket'], |
| 100 | + ['Natasha', 'Steve', 'Wanda', 'Rocket'], |
| 101 | + ['Natasha', 'Steve', 'Wanda', 'Rocket']] |
| 102 | + |
| 103 | + for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1): |
| 104 | + |
| 105 | + # Deepcopy() is needed here because the task expects the input lists to be mutated. |
| 106 | + # That mutation wrecks havoc with the verification and error messaging. |
| 107 | + queue, person_name = deepcopy(params) |
79 | 108 |
|
80 |
| - error_message = 'The mean person was not removed properly.' |
81 |
| - for variant, (params, result) in enumerate(data, start=1): |
82 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
83 |
| - self.assertListEqual(remove_the_mean_person(*params), result, msg=error_message) |
| 109 | + with self.subTest(f'variation #{variant}', |
| 110 | + queue=queue, |
| 111 | + person_name=person_name, |
| 112 | + expected=expected): |
| 113 | + |
| 114 | + actual_result = remove_the_mean_person(*params) |
| 115 | + error_message = (f'Called remove_the_mean_person{queue, person_name}. ' |
| 116 | + f'The function returned {actual_result}, but ' |
| 117 | + f'The tests expected {expected} when removing ' |
| 118 | + f'{person_name} from the queue.') |
| 119 | + |
| 120 | + self.assertListEqual(actual_result, expected, msg=error_message) |
84 | 121 |
|
85 | 122 | @pytest.mark.task(taskno=5)
|
86 | 123 | def test_how_many_namefellows(self):
|
87 |
| - data = [ |
88 |
| - ((['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'], 'Bucky'), 0), |
89 |
| - ((['Natasha', 'Steve', 'Ultron', 'Rocket'], 'Natasha'), 1), |
90 |
| - ((['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'], 'Natasha'), 2), |
91 |
| - ] |
| 124 | + test_data = [(['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'], 'Bucky'), |
| 125 | + (['Natasha', 'Steve', 'Ultron', 'Rocket'], 'Natasha'), |
| 126 | + (['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'], 'Natasha')] |
92 | 127 |
|
93 |
| - error_message = 'The namefellow count is incorrect.' |
94 |
| - for variant, (params, result) in enumerate(data, start=1): |
95 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
96 |
| - self.assertIs(how_many_namefellows(*params), result, msg=error_message) |
| 128 | + result_data = (0,1,2) |
| 129 | + |
| 130 | + for variant, (params, expected) in enumerate(zip(test_data, result_data), start=1): |
| 131 | + with self.subTest(f'variation #{variant}', params=params, expected=expected): |
| 132 | + actual_result = how_many_namefellows(*params) |
| 133 | + |
| 134 | + error_message = (f'Called how_many_namefellows{params}. ' |
| 135 | + f'The function returned {actual_result}, but ' |
| 136 | + f'The tests expected {expected} when counting ' |
| 137 | + f'namefellows in the queue for {params[-1]}.') |
| 138 | + |
| 139 | + self.assertIs(actual_result, expected, msg=error_message) |
97 | 140 |
|
98 | 141 | @pytest.mark.task(taskno=6)
|
99 | 142 | def test_remove_the_last_person(self):
|
100 |
| - data = [ |
101 |
| - (['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'], 'Rocket'), |
102 |
| - ] |
| 143 | + test_data = ['Natasha', 'Steve', 'Ultron', 'Natasha', 'Rocket'] |
| 144 | + actual_result = remove_the_last_person(test_data) |
| 145 | + expected = 'Rocket' |
| 146 | + error_message = (f'Called remove_the_last_person({test_data}).' |
| 147 | + f'The function returned {actual_result}, but the tests ' |
| 148 | + f'expected {expected} as the person who was removed.') |
103 | 149 |
|
104 |
| - error_message = 'The last person was not removed properly.' |
105 |
| - for variant, (params, result) in enumerate(data, start=1): |
106 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
107 |
| - self.assertIs(remove_the_last_person(params), result, msg=error_message) |
| 150 | + self.assertIs(actual_result, expected, msg=error_message) |
108 | 151 |
|
109 | 152 | @pytest.mark.task(taskno=7)
|
110 | 153 | def test_sorted_names(self):
|
111 |
| - data = [ |
112 |
| - ( |
113 |
| - ['Steve', 'Ultron', 'Natasha', 'Rocket'], |
114 |
| - ['Natasha', 'Rocket', 'Steve', 'Ultron'] |
115 |
| - ), |
116 |
| - ] |
117 |
| - |
118 |
| - error_message = 'The queue was not properly sorted.' |
119 |
| - for variant, (params, result) in enumerate(data, start=1): |
120 |
| - with self.subTest(f'variation #{variant}', input=params, output=result): |
121 |
| - self.assertListEqual(sorted_names(params), result, msg=error_message) |
| 154 | + test_data = ['Steve', 'Ultron', 'Natasha', 'Rocket'] |
| 155 | + expected = ['Natasha', 'Rocket', 'Steve', 'Ultron'] |
| 156 | + actual_result = sorted_names(test_data) |
| 157 | + error_message = (f'Called sorted_names({test_data}).' |
| 158 | + f'The function returned {actual_result}, but the tests ' |
| 159 | + f'expected {expected}.') |
| 160 | + |
| 161 | + self.assertListEqual(actual_result, expected, msg=error_message) |
0 commit comments