-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipes_test.py
More file actions
477 lines (382 loc) · 20.8 KB
/
Copy pathrecipes_test.py
File metadata and controls
477 lines (382 loc) · 20.8 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python3
import os
import sys
import copy
import json
import pickle
import lab
import pytest
TEST_DIRECTORY = os.path.dirname(__file__)
example_recipes = [
('compound', 'chili', [('beans', 3), ('cheese', 10), ('chili powder', 1), ('cornbread', 2), ('protein', 1)]),
('atomic', 'beans', 5),
('compound', 'cornbread', [('cornmeal', 3), ('milk', 1), ('butter', 5), ('salt', 1), ('flour', 2)]),
('atomic', 'cornmeal', 7.5),
('compound', 'burger', [('bread', 2), ('cheese', 1), ('lettuce', 1), ('protein', 1), ('ketchup', 1)]),
('compound', 'burger', [('bread', 2), ('cheese', 2), ('lettuce', 1), ('protein', 2),]),
('atomic', 'lettuce', 2),
('compound', 'butter', [('milk', 1), ('butter churn', 1)]),
('atomic', 'butter churn', 50),
('compound', 'milk', [('cow', 1), ('milking stool', 1)]),
('compound', 'cheese', [('milk', 1), ('time', 1)]),
('compound', 'cheese', [('cutting-edge laboratory', 11)]),
('atomic', 'salt', 1),
('compound', 'bread', [('yeast', 1), ('salt', 1), ('flour', 2)]),
('compound', 'protein', [('cow', 1)]),
('atomic', 'flour', 3),
('compound', 'ketchup', [('tomato', 30), ('vinegar', 5)]),
('atomic', 'chili powder', 1),
('compound', 'ketchup', [('tomato', 30), ('vinegar', 3), ('salt', 1), ('sugar', 2), ('cinnamon', 1)]), # the fancy ketchup
('atomic', 'cow', 100),
('atomic', 'milking stool', 5),
('atomic', 'cutting-edge laboratory', 1000),
('atomic', 'yeast', 2),
('atomic', 'time', 10000),
('atomic', 'vinegar', 20),
('atomic', 'sugar', 1),
('atomic', 'cinnamon', 7),
('atomic', 'tomato', 13),
]
def compare_recipe_list(expected, result):
assert len(expected) == len(result), f'Expected recipes list of length {len(expected)} but got {len(result)}'
assert type(expected) == type(result), f'Expected recipes list to be of type {type(expected)} but got {type(result)}'
for item in result:
assert isinstance(item, tuple), f'Expected all items in recipes to be a tuple but got {item}'
assert len(item) == 3, f'Expected all items in recipes to have length 3 but got {len(item)} \n for {item}'
a, b, c = item
assert isinstance(a, str) and a in {'atomic', 'compound'}, f'Expected first item in recipe tuple to be atomic or compound but got {a} in {item}'
assert isinstance(b, str), f'Expected second item in recipe tuple to be a string but got {type(b)} for {b} in {item}'
expected_type = (list, ) if a == 'compound' else (int, float)
assert isinstance(c, expected_type), f'Expected third item in recipe to be of type {expected_type} but got {type(c)} for {c} in {item}'
exp = set((a, b, tuple(c) if a =='compound' else c) for a,b,c in expected)
res = set((a, b, tuple(c) if a =='compound' else c) for a,b,c in result)
assert res == exp, f'Found {len(res.intersection(exp))} matching recipes. Additional recipes: {len(res-exp)} \n {res-exp} \n Missing Recipes: {len(exp-res)} \n {exp-res}'
def canonize_flat_recipe(recipe):
"""
Produce a nice immutable representation good for sorting and comparison.
"""
if recipe is None:
return None
assert isinstance(recipe, dict), "Each recipe should be flat, e.g. a dictionary!"
return frozenset(recipe.items())
def canonize_flat_recipes(recipes):
"""
Like above, for lists of recipes
"""
assert isinstance(recipes, list) and all(isinstance(i, dict) for i in recipes), "Recipes should be represented as a list of dictionaries!"
return frozenset((canonize_flat_recipe(recipe) for recipe in recipes))
def _load_test(n):
with open(os.path.join(TEST_DIRECTORY, 'test_recipes', f'big_recipes_{n:02d}.pickle'), 'rb') as f:
return pickle.load(f)
def _filter_graph(graph, elts):
elts = set(elts)
return [i for i in graph if i[1] not in elts]
def test_replace_item_small():
smaller_recipes = [
('compound', 'chili', [('cheese', 2), ('protein', 3), ('tomato', 2)]),
('compound', 'milk', [('cow', 1), ('milking stool', 1)]),
('compound', 'cheese', [('milk', 1), ('time', 1)]),
('compound', 'protein', [('cow', 1)]),
('atomic', 'cow', 100),
('atomic', 'tomato', 10),
('atomic', 'milking stool', 5),
('atomic', 'time', 10000),
]
orig = copy.deepcopy(smaller_recipes)
result = lab.replace_item(smaller_recipes, 'cow', 'soy')
assert smaller_recipes == orig, "be careful not to mutate the input!"
expected = [
('compound', 'chili', [('cheese', 2), ('protein', 3), ('tomato', 2)]),
('compound', 'milk', [('soy', 1), ('milking stool', 1)]),
('compound', 'cheese', [('milk', 1), ('time', 1)]),
('compound', 'protein', [('soy', 1)]),
('atomic', 'soy', 100),
('atomic', 'tomato', 10),
('atomic', 'milking stool', 5),
('atomic', 'time', 10000),
]
compare_recipe_list(expected, result)
# compare missing ingredient
result = lab.replace_item(smaller_recipes, 'chocolate', 'soy')
assert smaller_recipes == orig, "be careful not to mutate the input!"
compare_recipe_list(smaller_recipes, result)
@pytest.mark.parametrize('testnum', range(5))
def test_replace_item_big(testnum):
dbs = {}
for k in ('in', 'out'):
test_filename = os.path.join(TEST_DIRECTORY, 'test_recipes', f'replace_{k}_tests.pickle')
with open(test_filename, 'rb') as f:
dbs[k] = pickle.load(f)['replace_item']
for i in range(testnum*9, (testnum+1)*9):
inp = dbs['in'][i]
orig = copy.deepcopy(inp)
result = lab.replace_item(*inp)
assert inp == orig, 'be careful not to mutate the input!'
exp = dbs['out'][i]
compare_recipe_list(exp, result)
def test_lowest_cost_examples_all_included():
orig = copy.deepcopy(example_recipes)
# atomic food items, should just return their costs
assert lab.lowest_cost(example_recipes, 'time') == 10000
assert lab.lowest_cost(example_recipes, 'salt') == 1
assert abs(lab.lowest_cost(example_recipes, 'cornmeal') - 7.5) <= 1e-6
# compound food items, only one layer deep
assert lab.lowest_cost(example_recipes, 'protein') == 100
assert lab.lowest_cost(example_recipes, 'milk') == 105
assert lab.lowest_cost(example_recipes, 'bread') == 9
# two layers
assert lab.lowest_cost(example_recipes, 'cheese') == 10105
# more complex
assert lab.lowest_cost(example_recipes, 'burger') == 10685
assert lab.lowest_cost(example_recipes, 'chili') == 102985
assert example_recipes == orig, 'be careful not to mutate the input!'
@pytest.mark.parametrize('testnum', range(11))
def test_lowest_cost_big_all_included(testnum):
for i in range(testnum*5, (testnum+1)*5):
test_data = _load_test(i)
graph = test_data['graph']
target = test_data['target']
orig_graph = copy.deepcopy(graph)
result = lab.lowest_cost(graph, target)
assert graph == orig_graph, "be careful not the change the input!"
assert result == test_data['orig_min']
def test_lowest_cost_examples_excluded():
graph = _filter_graph(example_recipes, ('cow',))
orig = copy.deepcopy(graph)
# atomic food items, should just return their costs
assert lab.lowest_cost(graph, 'time') == 10000
assert lab.lowest_cost(graph, 'salt') == 1
assert abs(lab.lowest_cost(graph, 'cornmeal') - 7.5) <= 1e-6
# compound food items, only one layer deep
assert lab.lowest_cost(graph, 'protein') is None
assert lab.lowest_cost(graph, 'milk') is None
assert lab.lowest_cost(graph, 'bread') == 9
# two layers
assert lab.lowest_cost(graph, 'cheese') == 11000
# more complex
assert lab.lowest_cost(graph, 'burger') == None
assert lab.lowest_cost(graph, 'chili') == None
assert graph == orig, 'be careful not to mutate the input!'
def test_lowest_cost_more_examples_excluded():
with open(os.path.join(TEST_DIRECTORY, 'test_recipes', 'examples_filter.pickle'), 'rb') as f:
test_data = pickle.load(f)
for (target, filt) in test_data:
graph = _filter_graph(example_recipes, filt)
orig = copy.deepcopy(graph)
result = lab.lowest_cost(graph, target)
assert graph == orig, 'be careful not to mutate the input!'
assert result == test_data[(target, filt)][1]
@pytest.mark.parametrize('testnum', range(11))
def test_lowest_cost_big_excluded(testnum):
for i in range(testnum*5, (testnum+1)*5):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min']),
('none_filter', None),
('same_filter', test_data['orig_min']),
]:
graph = _filter_graph(test_data['graph'], test_data[filt])
orig_graph = copy.deepcopy(graph)
result = lab.lowest_cost(graph, target)
assert graph == orig_graph, "be careful not the change the input!"
assert result == expected
def test_lowest_cost_examples_forbidden():
orig = copy.deepcopy(example_recipes)
# atomic food items, should just return their costs
assert lab.lowest_cost(example_recipes, 'time', ('cow')) == 10000
assert lab.lowest_cost(example_recipes, 'salt', ('cow')) == 1
assert abs(lab.lowest_cost(example_recipes, 'cornmeal', ('cow')) - 7.5) <= 1e-6
# compound food items, only one layer deep
assert lab.lowest_cost(example_recipes, 'protein', ('cow')) is None
assert lab.lowest_cost(example_recipes, 'milk', ('cow')) is None
assert lab.lowest_cost(example_recipes, 'bread', ('cow')) == 9
# two layers
assert lab.lowest_cost(example_recipes, 'cheese', ('cow')) == 11000
# more complex
assert lab.lowest_cost(example_recipes, 'burger', ('cow')) == None
assert lab.lowest_cost(example_recipes, 'chili', ('cow')) == None
assert example_recipes == orig, 'be careful not to mutate the input!'
def test_lowest_cost_more_examples_forbidden():
with open(os.path.join(TEST_DIRECTORY, 'test_recipes', 'examples_filter.pickle'), 'rb') as f:
test_data = pickle.load(f)
for (target, filt) in test_data:
orig = copy.deepcopy(example_recipes)
result = lab.lowest_cost(example_recipes, target, filt)
assert example_recipes == orig, 'be careful not to mutate the input!'
assert result == test_data[(target, filt)][1]
@pytest.mark.parametrize('testnum', range(11))
def test_lowest_cost_big_forbidden(testnum):
for i in range(testnum*5, (testnum+1)*5):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min']),
('none_filter', None),
('same_filter', test_data['orig_min']),
]:
graph = test_data['graph']
orig_graph = copy.deepcopy(graph)
result = lab.lowest_cost(graph, target, test_data[filt])
assert graph == orig_graph, "be careful not the change the input!"
assert result == expected
@pytest.mark.parametrize('testnum', range(5))
def test_lowest_cost_big_excluded_forbidden(testnum):
for i in range(testnum*11, (testnum+1)*11):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min']),
('none_filter', None),
('same_filter', test_data['orig_min']),
]:
graph = _filter_graph(test_data['graph'], test_data[filt][::2])
orig_graph = copy.deepcopy(graph)
result = lab.lowest_cost(graph, target, test_data[filt][1::2])
assert graph == orig_graph, "be careful not the change the input!"
assert result == expected
def test_recipe_examples():
orig = copy.deepcopy(example_recipes)
# atomic food items, should just return their costs
assert lab.cheapest_flat_recipe(example_recipes, 'time') == {'time': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'salt') == {'salt': 1}
# compound food items, only one layer deep
assert lab.cheapest_flat_recipe(example_recipes, 'protein') == {'cow': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'protein', ('cow',)) is None
assert lab.cheapest_flat_recipe(example_recipes, 'milk') == {'cow': 1, 'milking stool': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'bread') == {'flour': 2, 'salt': 1, 'yeast': 1}
# two layers
assert lab.cheapest_flat_recipe(example_recipes, 'cheese') == {'cow': 1, 'milking stool': 1, 'time': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'cheese', ('milking stool',)) == {'cutting-edge laboratory': 11}
assert lab.cheapest_flat_recipe(example_recipes, 'cheese', ('milking stool', 'cutting-edge laboratory')) is None
# more complex
assert lab.cheapest_flat_recipe(example_recipes, 'burger') == {'yeast': 2, 'salt': 3, 'flour': 4, 'cow': 2, 'milking stool': 1, 'time': 1, 'lettuce': 1, 'tomato': 30, 'vinegar': 3, 'sugar': 2, 'cinnamon': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'burger', ('vinegar',)) == {'yeast': 2, 'salt': 2, 'flour': 4, 'cow': 4, 'milking stool': 2, 'time': 2, 'lettuce': 1}
assert lab.cheapest_flat_recipe(example_recipes, 'burger', ('vinegar','milk')) == {'yeast': 2, 'salt': 2, 'flour': 4, 'cutting-edge laboratory': 22, 'lettuce': 1, 'cow': 2}
assert example_recipes == orig, 'be careful not to mutate the input!'
@pytest.mark.parametrize('testnum', range(5))
def test_recipe_big_all_included(testnum):
for i in range(testnum*11, (testnum+1)*11):
test_data = _load_test(i)
target = test_data['target']
graph = test_data['graph']
orig_graph = copy.deepcopy(graph)
result = lab.cheapest_flat_recipe(graph, target)
assert graph == orig_graph, "be careful not the change the input!"
assert canonize_flat_recipe(result) == canonize_flat_recipe(test_data['orig_min_recipe'])
def test_recipe_more_examples_excluded():
with open(os.path.join(TEST_DIRECTORY, 'test_recipes', 'examples_filter.pickle'), 'rb') as f:
test_data = pickle.load(f)
for (target, filt) in test_data:
graph = _filter_graph(example_recipes, filt)
orig = copy.deepcopy(graph)
result = lab.cheapest_flat_recipe(graph, target)
assert graph == orig, 'be careful not to mutate the input!'
assert result == test_data[(target, filt)][0]
@pytest.mark.parametrize('testnum', range(5))
def test_recipe_big_excluded(testnum):
for i in range(testnum*11, (testnum+1)*11):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min_recipe']),
('none_filter', None),
('same_filter', test_data['orig_min_recipe']),
]:
graph = _filter_graph(test_data['graph'], test_data[filt])
orig_graph = copy.deepcopy(graph)
result = lab.cheapest_flat_recipe(graph, target)
assert graph == orig_graph, "be careful not the change the input!"
assert canonize_flat_recipe(result) == canonize_flat_recipe(expected)
def test_recipe_more_examples_forbidden():
with open(os.path.join(TEST_DIRECTORY, 'test_recipes', 'examples_filter.pickle'), 'rb') as f:
test_data = pickle.load(f)
for (target, filt) in test_data:
orig = copy.deepcopy(example_recipes)
result = lab.cheapest_flat_recipe(example_recipes, target, filt)
assert example_recipes == orig, 'be careful not to mutate the input!'
assert result == test_data[(target, filt)][0]
@pytest.mark.parametrize('testnum', range(5))
def test_recipe_big_forbidden(testnum):
for i in range(testnum*11, (testnum+1)*11):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min_recipe']),
('none_filter', None),
('same_filter', test_data['orig_min_recipe']),
]:
graph = test_data['graph']
orig_graph = copy.deepcopy(graph)
result = lab.cheapest_flat_recipe(graph, target, test_data[filt])
assert graph == orig_graph, "be careful not the change the input!"
assert canonize_flat_recipe(result) == canonize_flat_recipe(expected)
@pytest.mark.parametrize('testnum', range(5))
def test_recipe_big_excluded_forbidden(testnum):
for i in range(testnum*11, (testnum+1)*11):
test_data = _load_test(i)
target = test_data['target']
for filt, expected in [
('change_filter', test_data['change_min_recipe']),
('none_filter', None),
('same_filter', test_data['orig_min_recipe']),
]:
graph = _filter_graph(test_data['graph'], test_data[filt][::2])
orig_graph = copy.deepcopy(graph)
result = lab.cheapest_flat_recipe(graph, target, test_data[filt][1::2])
assert graph == orig_graph, "be careful not the change the input!"
assert canonize_flat_recipe(result) == canonize_flat_recipe(expected)
def test_all_recipes_examples():
orig = copy.deepcopy(example_recipes)
# atomic food items, should just return their costs
assert lab.all_flat_recipes(example_recipes, 'time') == [{'time': 1}]
assert lab.all_flat_recipes(example_recipes, 'salt') == [{'salt': 1}]
# compound food items, only one layer deep
assert lab.all_flat_recipes(example_recipes, 'protein') == [{'cow': 1}]
assert lab.all_flat_recipes(example_recipes, 'protein', ('cow',)) == []
assert lab.all_flat_recipes(example_recipes, 'milk') == [{'cow': 1, 'milking stool': 1}]
assert lab.all_flat_recipes(example_recipes, 'bread') == [{'flour': 2, 'salt': 1, 'yeast': 1}]
# two layers
assert canonize_flat_recipes(lab.all_flat_recipes(example_recipes, 'cheese')) == canonize_flat_recipes([{'cow': 1, 'milking stool': 1, 'time': 1}, {'cutting-edge laboratory': 11}])
assert lab.all_flat_recipes(example_recipes, 'cheese', ('milking stool',)) == [{'cutting-edge laboratory': 11}]
assert lab.all_flat_recipes(example_recipes, 'cheese', ('milking stool', 'cutting-edge laboratory')) == []
# more complex
burgers = [
{'yeast': 2, 'salt': 2, 'flour': 4, 'cow': 4, 'milking stool': 2, 'time': 2, 'lettuce': 1},
{'yeast': 2, 'salt': 2, 'flour': 4, 'cutting-edge laboratory': 22, 'lettuce': 1, 'cow': 2},
{'yeast': 2, 'salt': 3, 'flour': 4, 'cow': 2, 'milking stool': 1, 'time': 1, 'lettuce': 1, 'tomato': 30, 'vinegar': 3, 'sugar': 2, 'cinnamon': 1},
{'yeast': 2, 'salt': 2, 'flour': 4, 'cow': 2, 'milking stool': 1, 'time': 1, 'lettuce': 1, 'tomato': 30, 'vinegar': 5},
{'yeast': 2, 'salt': 3, 'flour': 4, 'cutting-edge laboratory': 11, 'lettuce': 1, 'cow': 1, 'tomato': 30, 'vinegar': 3, 'sugar': 2, 'cinnamon': 1},
{'yeast': 2, 'salt': 2, 'flour': 4, 'cutting-edge laboratory': 11, 'lettuce': 1, 'cow': 1, 'tomato': 30, 'vinegar': 5}
]
assert canonize_flat_recipes(lab.all_flat_recipes(example_recipes, 'burger')) == canonize_flat_recipes(burgers)
burgers2 = [
{'yeast': 2, 'salt': 3, 'flour': 4, 'cutting-edge laboratory': 11, 'lettuce': 1, 'cow': 1, 'tomato': 30, 'vinegar': 3, 'sugar': 2, 'cinnamon': 1},
{'yeast': 2, 'salt': 2, 'flour': 4, 'cutting-edge laboratory': 11, 'lettuce': 1, 'cow': 1, 'tomato': 30, 'vinegar': 5},
{'yeast': 2, 'salt': 2, 'flour': 4, 'cutting-edge laboratory': 22, 'lettuce': 1, 'cow': 2}
]
assert canonize_flat_recipes(lab.all_flat_recipes(example_recipes, 'burger', ('milk',))) == canonize_flat_recipes(burgers2)
assert example_recipes == orig, 'be careful not to mutate the input!'
@pytest.mark.parametrize('testnum', range(11))
def test_all_recipes_big(testnum):
for i in range(testnum*5, (testnum+1)*5):
test_data = _load_test(i)
target = test_data['target']
test_data['identity_filter'] = ()
for filt, expected in [
('identity_filter', test_data['orig_all']),
('change_filter', test_data['change_all']),
('none_filter', []),
('same_filter', test_data['same_all']),
]:
for graph, filter_ in [(_filter_graph(test_data['graph'], test_data[filt]), ()),
(test_data['graph'], test_data[filt]),
(_filter_graph(test_data['graph'], test_data[filt][1::2]), test_data[filt][::2])]:
orig_graph = copy.deepcopy(graph)
result = lab.all_flat_recipes(graph, target, filter_)
print(result)
assert graph == orig_graph, "be careful not the change the input!"
assert canonize_flat_recipes(result) == canonize_flat_recipes(expected)
if __name__ == "__main__":
import sys
res = pytest.main(["-k", " or ".join(sys.argv[1:]), "-v", __file__])