Skip to content

Commit 8bc6240

Browse files
committed
eta just got put out of scope. no more classes lol
1 parent 8f438c9 commit 8bc6240

File tree

2 files changed

+28
-75
lines changed

2 files changed

+28
-75
lines changed

domain_model.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
class Shipment(dict):
2-
def __init__(self, eta, lines):
3-
self.eta = eta
4-
super().__init__(lines)
5-
6-
71
def skus(d):
82
return set(d.keys())
93

4+
def allocated_completely(order, allocation):
5+
return skus(order) == skus(allocation)
6+
107

118
def allocate_to(order, source):
129
return {
@@ -19,15 +16,13 @@ def allocate_to(order, source):
1916

2017
def allocate(order, stock, shipments):
2118
stock_allocation = allocate_to(order, stock)
22-
if skus(stock_allocation) == skus(order):
19+
if allocated_completely(order, stock_allocation):
2320
return stock_allocation
2421

25-
shipments.sort(key=lambda s: s.eta)
26-
2722
shipment_allocations = []
2823
for shipment in shipments:
2924
shipment_allocation = allocate_to(order, shipment)
30-
if skus(shipment_allocation) == skus(order):
25+
if allocated_completely(order, shipment_allocation):
3126
return shipment_allocation
3227
shipment_allocations.append(shipment_allocation)
3328

test_allocation.py

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
from datetime import date, timedelta
33

44

5-
from domain_model import (
6-
Shipment,
7-
allocate,
8-
)
5+
from domain_model import allocate
96

107

118
def random_id():
@@ -23,9 +20,7 @@ def test_can_allocate_to_stock():
2320

2421
def test_can_allocate_to_shipment():
2522
order = {'a-sku': 10}
26-
shipment = Shipment(eta=date.today(), lines={
27-
'a-sku': 1000
28-
})
23+
shipment = {'a-sku': 1000}
2924

3025
allocations = allocate(order, stock={}, shipments=[shipment])
3126

@@ -35,9 +30,7 @@ def test_can_allocate_to_shipment():
3530
def test_ignores_irrelevant_stock():
3631
order = {'sku1': 10}
3732
stock = {'sku2': 1000}
38-
shipment = Shipment(eta=date.today(), lines={
39-
'sku1': 1000,
40-
})
33+
shipment = {'sku1': 1000}
4134

4235
allocations = allocate(order, stock=stock, shipments=[shipment])
4336

@@ -46,12 +39,8 @@ def test_ignores_irrelevant_stock():
4639

4740
def test_can_allocate_to_correct_shipment():
4841
order = {'sku2': 10}
49-
shipment1 = Shipment(eta=date.today(), lines={
50-
'sku1': 1000,
51-
})
52-
shipment2 = Shipment(eta=date.today(), lines={
53-
'sku2': 1000,
54-
})
42+
shipment1 = {'sku1': 1000}
43+
shipment2 = {'sku2': 1000}
5544

5645
allocations = allocate(order, stock={}, shipments=[shipment1, shipment2])
5746

@@ -61,9 +50,7 @@ def test_can_allocate_to_correct_shipment():
6150
def test_allocates_to_stock_in_preference_to_shipment():
6251
order = {'sku1': 10}
6352
stock = {'sku1': 1000}
64-
shipment = Shipment(eta=date.today(), lines={
65-
'sku1': 1000,
66-
})
53+
shipment = {'sku1': 1000}
6754

6855
allocations = allocate(order, stock, shipments=[shipment])
6956

@@ -81,10 +68,7 @@ def test_can_allocate_multiple_lines_to_wh():
8168

8269
def test_can_allocate_multiple_lines_to_shipment():
8370
order = {'sku1': 5, 'sku2': 10}
84-
shipment = Shipment(eta=date.today(), lines={
85-
'sku1': 1000,
86-
'sku2': 1000,
87-
})
71+
shipment = {'sku1': 1000, 'sku2': 1000}
8872

8973
allocations = allocate(order, stock={}, shipments=[shipment])
9074

@@ -94,9 +78,7 @@ def test_can_allocate_multiple_lines_to_shipment():
9478

9579
def test_can_allocate_to_both():
9680
order = {'sku1': 5, 'sku2': 10}
97-
shipment = Shipment(eta=date.today(), lines={
98-
'sku2': 1000,
99-
})
81+
shipment = {'sku2': 1000}
10082
stock = {'sku1': 1000}
10183

10284
allocations = allocate(order, stock, shipments=[shipment])
@@ -107,11 +89,7 @@ def test_can_allocate_to_both():
10789

10890
def test_can_allocate_to_both_preferring_stock():
10991
order = {'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4}
110-
shipment = Shipment(eta=date.today(), lines={
111-
'sku1': 1000,
112-
'sku2': 1000,
113-
'sku3': 1000,
114-
})
92+
shipment = {'sku1': 1000, 'sku2': 1000, 'sku3': 1000}
11593
stock = {'sku3': 1000, 'sku4': 1000}
11694

11795
allocations = allocate(order, stock, shipments=[shipment])
@@ -124,10 +102,7 @@ def test_can_allocate_to_both_preferring_stock():
124102

125103
def test_mixed_allocations_are_avoided_if_possible():
126104
order = {'sku1': 10, 'sku2': 10}
127-
shipment = Shipment(eta=date.today(), lines={
128-
'sku1': 1000,
129-
'sku2': 1000,
130-
})
105+
shipment = {'sku1': 1000, 'sku2': 1000}
131106
stock = {'sku1': 1000}
132107

133108
allocations = allocate(order, stock, shipments=[shipment])
@@ -136,43 +111,26 @@ def test_mixed_allocations_are_avoided_if_possible():
136111
assert allocations['sku2'] == shipment
137112

138113

139-
def test_prefer_allocating_to_earlier_shipment():
114+
def test_allocated_to_first_suitable_shipment_in_list():
140115
order = {'sku1': 10, 'sku2': 10}
141-
shipment1 = Shipment(eta=date.today(), lines={
142-
'sku1': 1000,
143-
'sku2': 1000,
144-
})
145-
tomorrow = date.today() + timedelta(days=1)
146-
shipment2 = Shipment(eta=tomorrow, lines={
147-
'sku1': 1000,
148-
'sku2': 1000,
149-
})
116+
shipment1 = {'sku1': 1000, 'sku2': 1000}
117+
shipment2 = {'sku1': 1000, 'sku2': 1000}
150118
stock = {}
151119

152-
allocations = allocate(order, stock, shipments=[shipment2, shipment1])
120+
allocations = allocate(order, stock, shipments=[shipment1, shipment2])
153121

154122
assert allocations['sku1'] == shipment1
155123
assert allocations['sku2'] == shipment1
156124

157125

158-
def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
126+
def test_still_preserves_ordering_if_split_across_shipments():
159127
order = {'sku1': 10, 'sku2': 10, 'sku3': 10}
160-
shipment1 = Shipment(eta=date.today(), lines={
161-
'sku1': 1000,
162-
})
163-
tomorrow = date.today() + timedelta(days=1)
164-
shipment2 = Shipment(eta=tomorrow, lines={
165-
'sku2': 1000,
166-
'sku3': 1000,
167-
})
168-
later = tomorrow + timedelta(days=1)
169-
shipment3 = Shipment(eta=later, lines={
170-
'sku2': 1000,
171-
'sku3': 1000,
172-
})
128+
shipment1 = {'sku1': 1000}
129+
shipment2 = {'sku2': 1000, 'sku3': 1000}
130+
shipment3 = {'sku2': 1000, 'sku3': 1000}
173131
stock = {}
174132

175-
allocations = allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
133+
allocations = allocate(order, stock, shipments=[shipment1, shipment2, shipment3])
176134

177135
assert allocations['sku1'] == shipment1
178136
assert allocations['sku2'] == shipment2
@@ -182,10 +140,10 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
182140
def test_stock_not_quite_enough_means_we_use_shipment():
183141
order = {'sku1': 10, 'sku2': 10}
184142
stock = {'sku1': 10, 'sku2': 5}
185-
shipment = Shipment(eta=date.today(), lines={
143+
shipment = {
186144
'sku1': 1000,
187145
'sku2': 1000,
188-
})
146+
}
189147

190148
allocations = allocate(order, stock, shipments=[shipment])
191149

@@ -204,9 +162,9 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
204162

205163
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
206164
order = {'a-sku': 10}
207-
shipment = Shipment(eta=date.today(), lines={
165+
shipment = {
208166
'a-sku': 5,
209-
})
167+
}
210168

211169
allocations = allocate(order, stock={}, shipments=[shipment])
212170

0 commit comments

Comments
 (0)