Skip to content

Commit 0d2c09b

Browse files
authored
Updated test error messages and fixed a couple of tests. (#3546)
1 parent 6f87ae7 commit 0d2c09b

File tree

1 file changed

+76
-35
lines changed

1 file changed

+76
-35
lines changed

exercises/concept/mecha-munch-management/dict_methods_test.py

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import pytest
3+
from collections import OrderedDict
34
from dict_methods import (add_item,
45
read_notes,
56
update_recipes,
@@ -22,10 +23,14 @@ def test_add_item(self):
2223
{'Orange': 1, 'Raspberry': 3, 'Blueberries': 11},
2324
{'Broccoli': 2, 'Banana': 3, 'Kiwi': 3, 'Melon': 1, 'Apple': 1}]
2425

25-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
26-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
27-
error_msg=f'Expected: {output_data} but got a different shopping cart.'
28-
self.assertEqual(add_item(input_data[0], input_data[1]), output_data, msg=error_msg)
26+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
27+
with self.subTest(f'variation #{variant}', input_data=input_data, expected=expected):
28+
actual_result = add_item(input_data[0], input_data[1])
29+
error_msg= (f'Called add_item({input_data[0]}, {input_data[1]}). '
30+
f'The function returned {actual_result}, but the tests '
31+
f'expected: {expected} once the item was added.')
32+
33+
self.assertEqual(actual_result, expected, msg=error_msg)
2934

3035

3136
@pytest.mark.task(taskno=2)
@@ -36,10 +41,14 @@ def test_read_notes(self):
3641
output_data = [{'Apple': 1, 'Banana': 1}, {'Orange': 1, 'Raspberry': 1, 'Blueberries': 1},
3742
{'Broccoli': 1, 'Kiwi': 1, 'Melon': 1, 'Apple': 1, 'Banana': 1}]
3843

39-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
40-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
41-
error_msg=f'Expected: {output_data} but got a different shopping cart.'
42-
self.assertEqual(read_notes(input_data), output_data, msg=error_msg)
44+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
45+
with self.subTest(f'variation #{variant}', input_data=input_data, expected=expected):
46+
actual_result = read_notes(input_data)
47+
error_msg = (f'Called read_notes({input_data}). '
48+
f'The function returned {actual_result}, but the tests '
49+
f'expected: {expected} once the notes were read.')
50+
51+
self.assertEqual(actual_result, expected, msg=error_msg)
4352

4453
@pytest.mark.task(taskno=3)
4554
def test_update_recipes(self):
@@ -72,10 +81,14 @@ def test_update_recipes(self):
7281
'Blueberry Crumble': {'Blueberries': 2, 'Whipped Creme': 2, 'Granola Topping': 2, 'Yogurt': 3}}
7382
]
7483

75-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
76-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
77-
error_msg=f'Expected: {output_data} but got a different ideas instead.'
78-
self.assertEqual(update_recipes(input_data[0], input_data[1]), output_data, msg=error_msg)
84+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
85+
with self.subTest(f'variation #{variant}', input_data=input_data, expected=expected):
86+
actual_result = update_recipes(input_data[0], input_data[1])
87+
error_msg = (f'Called update_recipes({input_data[0]}, {input_data[1]}). '
88+
f'The function returned {actual_result}, but the tests '
89+
f'expected: {expected} once the recipes were updated.')
90+
91+
self.assertEqual(actual_result, expected, msg=error_msg)
7992

8093
@pytest.mark.task(taskno=4)
8194
def test_sort_entries(self):
@@ -88,41 +101,65 @@ def test_sort_entries(self):
88101

89102
output_data = [
90103
{'Apple': 2, 'Banana': 4, 'Orange': 1, 'Pear': 12},
91-
{'Avocado': 2, 'Apple': 3, 'Banana': 1, 'Orange': 5},
92-
{'Apple': 1, 'Orange': 3, 'Banana': 2},
104+
{'Apple': 3, 'Avocado': 2, 'Banana': 1, 'Orange': 5},
105+
{'Apple': 1, 'Banana': 2, 'Orange': 3},
93106
{'Apple' : 2, 'Blueberries': 5, 'Broccoli': 2, 'Kiwi': 1, 'Melon': 4, 'Raspberry': 2}
94107
]
95108

96-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
97-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
98-
error_msg=f'Expected: {output_data} but got a different sorted list instead.'
99-
self.assertEqual(sort_entries(input_data), output_data, msg=error_msg)
109+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
110+
with self.subTest(f'variation #{variant}', input_data=input_data, expecred=expected):
111+
actual_result = sort_entries(input_data)
112+
error_msg = (f'Called sort_entries({input_data}). '
113+
f'The function returned {actual_result}, but the tests '
114+
f'expected: {expected} for the sorted entries.')
115+
116+
# Because we are asserting equal, we need to convert to an OrderedDict.
117+
# Regular dictionaries will compare equal even when they are ordered
118+
# differently from one another. See https://stackoverflow.com/a/58961124
119+
self.assertEqual(OrderedDict(actual_result), OrderedDict(expected), msg=error_msg)
100120

101121
@pytest.mark.task(taskno=5)
102122
def test_send_to_store(self):
103123
input_data = [
104124
({'Banana': 3, 'Apple': 2, 'Orange': 1, 'Milk': 2},
105-
{'Banana': ['Isle 5', False], 'Apple': ['Isle 4', False], 'Orange': ['Isle 4', False], 'Milk': ['Isle 2', True]}),
125+
{'Banana': ['Isle 5', False], 'Apple': ['Isle 4', False],
126+
'Orange': ['Isle 4', False], 'Milk': ['Isle 2', True]}),
106127

107128
({'Kiwi': 3, 'Juice': 5, 'Yoghurt': 2, 'Milk': 5},
108-
{'Kiwi': ['Isle 6', False], 'Juice': ['Isle 5', False], 'Yoghurt': ['Isle 2', True], 'Milk': ['Isle 2', True]}),
129+
{'Kiwi': ['Isle 6', False], 'Juice': ['Isle 5', False],
130+
'Yoghurt': ['Isle 2', True], 'Milk': ['Isle 2', True]}),
131+
132+
({'Apple': 2, 'Raspberry': 2, 'Blueberries': 5,
133+
'Broccoli' : 2, 'Kiwi': 1, 'Melon': 4},
109134

110-
({'Apple': 2, 'Raspberry': 2, 'Blueberries': 5, 'Broccoli' : 2, 'Kiwi': 1, 'Melon': 4},
111-
{'Apple': ['Isle 1', False], 'Raspberry': ['Isle 6', False], 'Blueberries': ['Isle 6', False],
112-
'Broccoli': ['Isle 3', False], 'Kiwi': ['Isle 6', False], 'Melon': ['Isle 6', False]})
135+
{'Apple': ['Isle 1', False], 'Raspberry': ['Isle 6', False],
136+
'Blueberries': ['Isle 6', False], 'Broccoli': ['Isle 3', False],
137+
'Kiwi': ['Isle 6', False], 'Melon': ['Isle 6', False]})
113138
]
114139

115140
output_data = [
116-
{'Orange': [1, 'Isle 4', False], 'Milk': [2, 'Isle 2', True], 'Banana': [3, 'Isle 5', False], 'Apple': [2, 'Isle 4', False]},
117-
{'Juice': [5, 'Isle 5', False], 'Yoghurt': [2, 'Isle 2', True], 'Milk': [5, 'Isle 2', True], 'Kiwi': [3, 'Isle 6', False]},
118-
{'Kiwi': [1, 'Isle 6', False], 'Melon': [4, 'Isle 6', False], 'Apple': [2, 'Isle 1', False],
119-
'Raspberry': [2, 'Isle 6', False], 'Blueberries': [5, 'Isle 6', False], 'Broccoli': [2, 'Isle 3', False]}
141+
{'Orange': [1, 'Isle 4', False], 'Milk': [2, 'Isle 2', True],
142+
'Banana': [3, 'Isle 5', False], 'Apple': [2, 'Isle 4', False]},
143+
144+
{'Yoghurt': [2, 'Isle 2', True], 'Milk': [5, 'Isle 2', True],
145+
'Kiwi': [3, 'Isle 6', False], 'Juice': [5, 'Isle 5', False]},
146+
147+
{'Raspberry': [2, 'Isle 6', False], 'Melon': [4, 'Isle 6', False],
148+
'Kiwi': [1, 'Isle 6', False], 'Broccoli': [2, 'Isle 3', False],
149+
'Blueberries': [5, 'Isle 6', False], 'Apple': [2, 'Isle 1', False]}
120150
]
121151

122-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
123-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
124-
error_msg=f'Expected: {output_data} but got a different fulfillment_cart instead.'
125-
self.assertEqual(send_to_store(input_data[0], input_data[1]), output_data, msg=error_msg)
152+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
153+
with self.subTest(f'variation #{variant}', input_data=input_data, expected=expected):
154+
actual_result = send_to_store(input_data[0], input_data[1])
155+
error_msg = (f'Called send_to_store({input_data[0]}, {input_data[1]}). '
156+
f'The function returned {actual_result}, but the tests '
157+
f'expected: {expected} as the fulfillment cart.')
158+
159+
# Because we are asserting equal, we need to convert to an OrderedDict.
160+
# Regular dictionaries will compare equal even when they are ordered
161+
# differently from one another. See https://stackoverflow.com/a/58961124
162+
self.assertEqual(OrderedDict(actual_result), OrderedDict(expected), msg=error_msg)
126163

127164
@pytest.mark.task(taskno=6)
128165
def test_update_store_inventory(self):
@@ -155,7 +192,11 @@ def test_update_store_inventory(self):
155192
'Blueberries': [5, 'Isle 6', False], 'Broccoli': [3, 'Isle 3', False]}
156193
]
157194

158-
for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
159-
with self.subTest(f'variation #{variant}', input_data=input_data, output_data=output_data):
160-
error_msg=f'Expected: {output_data} but got a different store inventory instead.'
161-
self.assertEqual(update_store_inventory(input_data[0], input_data[1]), output_data, msg=error_msg)
195+
for variant, (input_data, expected) in enumerate(zip(input_data, output_data), start=1):
196+
with self.subTest(f'variation #{variant}', input_data=input_data, expected=expected):
197+
actual_result = update_store_inventory(input_data[0], input_data[1])
198+
error_msg = (f'Called update_store_inventory({input_data[0]}, {input_data[1]}). '
199+
f'The function returned {actual_result}, but the tests '
200+
f'expected: {expected} as the store inventory.')
201+
202+
self.assertEqual(actual_result, expected, msg=error_msg)

0 commit comments

Comments
 (0)