1
1
import unittest
2
2
import pytest
3
+ from collections import OrderedDict
3
4
from dict_methods import (add_item ,
4
5
read_notes ,
5
6
update_recipes ,
@@ -22,10 +23,14 @@ def test_add_item(self):
22
23
{'Orange' : 1 , 'Raspberry' : 3 , 'Blueberries' : 11 },
23
24
{'Broccoli' : 2 , 'Banana' : 3 , 'Kiwi' : 3 , 'Melon' : 1 , 'Apple' : 1 }]
24
25
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 )
29
34
30
35
31
36
@pytest .mark .task (taskno = 2 )
@@ -36,10 +41,14 @@ def test_read_notes(self):
36
41
output_data = [{'Apple' : 1 , 'Banana' : 1 }, {'Orange' : 1 , 'Raspberry' : 1 , 'Blueberries' : 1 },
37
42
{'Broccoli' : 1 , 'Kiwi' : 1 , 'Melon' : 1 , 'Apple' : 1 , 'Banana' : 1 }]
38
43
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 )
43
52
44
53
@pytest .mark .task (taskno = 3 )
45
54
def test_update_recipes (self ):
@@ -72,10 +81,14 @@ def test_update_recipes(self):
72
81
'Blueberry Crumble' : {'Blueberries' : 2 , 'Whipped Creme' : 2 , 'Granola Topping' : 2 , 'Yogurt' : 3 }}
73
82
]
74
83
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 )
79
92
80
93
@pytest .mark .task (taskno = 4 )
81
94
def test_sort_entries (self ):
@@ -88,41 +101,65 @@ def test_sort_entries(self):
88
101
89
102
output_data = [
90
103
{'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 },
93
106
{'Apple' : 2 , 'Blueberries' : 5 , 'Broccoli' : 2 , 'Kiwi' : 1 , 'Melon' : 4 , 'Raspberry' : 2 }
94
107
]
95
108
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 )
100
120
101
121
@pytest .mark .task (taskno = 5 )
102
122
def test_send_to_store (self ):
103
123
input_data = [
104
124
({'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 ]}),
106
127
107
128
({'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 },
109
134
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 ]})
113
138
]
114
139
115
140
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 ]}
120
150
]
121
151
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 )
126
163
127
164
@pytest .mark .task (taskno = 6 )
128
165
def test_update_store_inventory (self ):
@@ -155,7 +192,11 @@ def test_update_store_inventory(self):
155
192
'Blueberries' : [5 , 'Isle 6' , False ], 'Broccoli' : [3 , 'Isle 3' , False ]}
156
193
]
157
194
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