Skip to content

Commit 0710aae

Browse files
committed
and finish changing order into a special dict, totally change impl
1 parent 4073e41 commit 0710aae

2 files changed

Lines changed: 63 additions & 94 deletions

File tree

‎domain_model.py‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
from dataclasses import dataclass
22

33
@dataclass
4-
class Line:
5-
sku: str
6-
quantity: int
4+
class Order(dict):
5+
def __init__(self, d):
6+
self.allocations = {}
7+
super().__init__(d)
78

89

9-
10-
11-
@dataclass
12-
class OrderLine(Line):
13-
allocation: str = None
14-
15-
16-
@dataclass
1710
class Shipment(dict):
1811
def __init__(self, id, eta, lines):
1912
self.id = id
@@ -28,32 +21,31 @@ def skus(thing):
2821
return thing.keys()
2922

3023

31-
def allocate_to(line, allocation, source):
32-
if source.get(line.sku, 0) > line.quantity:
33-
line.allocation = allocation
34-
source[line.sku] -= line.quantity
24+
def allocate_to(order, source, name):
25+
for sku, quantity in order.items():
26+
if source.get(sku, 0) > quantity:
27+
source[sku] -= quantity
28+
order.allocations[sku] = name
3529

3630

3731
def allocate_to_stock(order, stock):
38-
for line in order:
39-
allocate_to(line, 'STOCK', stock)
40-
32+
allocate_to(order, stock, 'STOCK')
4133

4234
def allocate_to_shipment(order, shipment):
43-
for line in order:
44-
allocate_to(line, shipment.id, shipment)
35+
allocate_to(order, shipment, shipment.id)
4536

4637

47-
def allocate_to_shipments(order, shipments):
48-
for line in order:
49-
_allocate_line_to_shipments(line, shipments)
5038

5139

52-
def _allocate_line_to_shipments(line, shipments):
53-
for shipment in shipments:
54-
allocate_to(line, shipment.id, shipment)
55-
if line.allocation:
56-
return
40+
def allocate_to_shipments(order, shipments):
41+
for sku, quantity in order.items():
42+
if sku in order.allocations:
43+
continue
44+
for shipment in shipments:
45+
if shipment.get(sku, 0) > quantity:
46+
shipment[sku] -= quantity
47+
order.allocations[sku] = shipment.id
48+
break
5749

5850

5951
def allocate(order, stock, shipments):
@@ -68,5 +60,5 @@ def allocate(order, stock, shipments):
6860
allocate_to_shipment(order, shipment)
6961
return
7062

71-
allocate_to_shipments(order, shipments)
7263
allocate_to_stock(order, stock)
64+
allocate_to_shipments(order, shipments)

‎test_allocation.py‎

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
from domain_model import (
6-
OrderLine,
6+
Order,
77
Shipment,
88
allocate,
99
)
@@ -16,43 +16,43 @@ def random_id():
1616

1717

1818
def test_can_allocate_to_stock():
19-
order = [OrderLine(sku='a-sku', quantity=10)]
19+
order = Order({'a-sku': 10})
2020
stock = {'a-sku': 1000}
2121

2222
allocate(order, stock, shipments=[])
2323

24-
assert order[0].allocation == 'STOCK'
24+
assert order.allocations['a-sku'] == 'STOCK'
2525
assert stock['a-sku'] == 990
2626

2727

2828
def test_can_allocate_to_shipment():
29-
order = [OrderLine(sku='a-sku', quantity=10)]
29+
order = Order({'a-sku': 10})
3030
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
3131
'a-sku': 1000
3232
})
3333

3434
allocate(order, stock=[], shipments=[shipment])
3535

36-
assert order[0].allocation == shipment.id
36+
assert order.allocations['a-sku'] == shipment.id
3737
assert shipment['a-sku'] == 990
3838

3939

4040
def test_ignores_irrelevant_stock():
41-
order = [OrderLine(sku='sku1', quantity=10), ]
41+
order = Order({'sku1': 10})
4242
stock = {'sku2': 1000}
4343
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
4444
'sku1': 1000,
4545
})
4646

4747
allocate(order, stock=stock, shipments=[shipment])
4848

49-
assert order[0].allocation == shipment.id
49+
assert order.allocations['sku1'] == shipment.id
5050
assert stock['sku2'] == 1000
5151
assert shipment['sku1'] == 990
5252

5353

5454
def test_can_allocate_to_correct_shipment():
55-
order = [OrderLine(sku='sku2', quantity=10)]
55+
order = Order({'sku2': 10})
5656
shipment1 = Shipment('shipment1', eta=date.today(), lines={
5757
'sku1': 1000,
5858
})
@@ -62,82 +62,68 @@ def test_can_allocate_to_correct_shipment():
6262

6363
allocate(order, stock=[], shipments=[shipment1, shipment2])
6464

65-
assert order[0].allocation == shipment2.id
65+
assert order.allocations['sku2'] == shipment2.id
6666
assert shipment1['sku1'] == 1000
6767
assert shipment2['sku2'] == 990
6868

6969

7070
def test_allocates_to_stock_in_preference_to_shipment():
71-
order = [OrderLine(sku='sku1', quantity=10)]
71+
order = Order({'sku1': 10})
7272
stock = {'sku1': 1000}
7373
shipment = Shipment('shipment1', eta=date.today(), lines={
7474
'sku1': 1000,
7575
})
7676

7777
allocate(order, stock, shipments=[shipment])
7878

79-
assert order[0].allocation == 'STOCK'
79+
assert order.allocations['sku1'] == 'STOCK'
8080
assert stock['sku1'] == 990
8181
assert shipment['sku1'] == 1000
8282

8383

8484
def test_can_allocate_multiple_lines_to_wh():
85-
order = [
86-
OrderLine(sku='sku1', quantity=5),
87-
OrderLine(sku='sku2', quantity=10),
88-
]
85+
order = Order({'sku1': 5, 'sku2': 10})
8986
stock = {'sku1': 1000, 'sku2': 1000}
9087

9188
allocate(order, stock, shipments=[])
92-
assert order[0].allocation == 'STOCK'
93-
assert order[1].allocation == 'STOCK'
89+
assert order.allocations['sku1'] == 'STOCK'
90+
assert order.allocations['sku2'] == 'STOCK'
9491
assert stock['sku1'] == 995
9592
assert stock['sku2'] == 990
9693

9794

9895
def test_can_allocate_multiple_lines_to_shipment():
99-
order = [
100-
OrderLine(sku='sku1', quantity=5),
101-
OrderLine(sku='sku2', quantity=10),
102-
]
96+
order = Order({'sku1': 5, 'sku2': 10})
10397
shipment = Shipment('shipment1', eta=date.today(), lines={
10498
'sku1': 1000,
10599
'sku2': 1000,
106100
})
107101

108102
allocate(order, [], shipments=[shipment])
109103

110-
assert order[0].allocation == shipment.id
111-
assert order[1].allocation == shipment.id
104+
assert order.allocations['sku1'] == shipment.id
105+
assert order.allocations['sku2'] == shipment.id
112106
assert shipment['sku1'] == 995
113107
assert shipment['sku2'] == 990
114108

115109

116110
def test_can_allocate_to_both():
117-
order = [
118-
OrderLine(sku='sku1', quantity=5),
119-
OrderLine(sku='sku2', quantity=10),
120-
]
111+
order = Order({'sku1': 5, 'sku2': 10})
121112
shipment = Shipment('shipment1', eta=date.today(), lines={
122113
'sku2': 1000,
123114
})
124115
stock = {'sku1': 1000}
125116

126117
allocate(order, stock, shipments=[shipment])
127118

128-
assert order[0].allocation == 'STOCK'
129-
assert order[1].allocation == shipment.id
119+
assert order.allocations['sku1'] == 'STOCK'
120+
assert order.allocations['sku2'] == shipment.id
130121
assert stock['sku1'] == 995
131122
assert shipment['sku2'] == 990
132123

133124

134125
def test_can_allocate_to_both_preferring_stock():
135-
order = [
136-
OrderLine(sku='sku1', quantity=1),
137-
OrderLine(sku='sku2', quantity=2),
138-
OrderLine(sku='sku3', quantity=3),
139-
OrderLine(sku='sku4', quantity=4),
140-
]
126+
order = Order({'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4})
141127
shipment = Shipment('shipment1', eta=date.today(), lines={
142128
'sku1': 1000,
143129
'sku2': 1000,
@@ -147,10 +133,10 @@ def test_can_allocate_to_both_preferring_stock():
147133

148134
allocate(order, stock, shipments=[shipment])
149135

150-
assert order[0].allocation == shipment.id
151-
assert order[1].allocation == shipment.id
152-
assert order[2].allocation == 'STOCK'
153-
assert order[3].allocation == 'STOCK'
136+
assert order.allocations['sku1'] == shipment.id
137+
assert order.allocations['sku2'] == shipment.id
138+
assert order.allocations['sku3'] == 'STOCK'
139+
assert order.allocations['sku4'] == 'STOCK'
154140
assert shipment['sku1'] == 999
155141
assert shipment['sku2'] == 998
156142
assert shipment['sku3'] == 1000
@@ -159,10 +145,7 @@ def test_can_allocate_to_both_preferring_stock():
159145

160146

161147
def test_mixed_allocations_are_avoided_if_possible():
162-
order = [
163-
OrderLine(sku='sku1', quantity=10),
164-
OrderLine(sku='sku2', quantity=10),
165-
]
148+
order = Order({'sku1': 10, 'sku2': 10})
166149
shipment = Shipment('shipment1', eta=date.today(), lines={
167150
'sku1': 1000,
168151
'sku2': 1000,
@@ -171,15 +154,12 @@ def test_mixed_allocations_are_avoided_if_possible():
171154

172155
allocate(order, stock, shipments=[shipment])
173156

174-
assert order[0].allocation == shipment.id
175-
assert order[1].allocation == shipment.id
157+
assert order.allocations['sku1'] == shipment.id
158+
assert order.allocations['sku2'] == shipment.id
176159

177160

178161
def test_prefer_allocating_to_earlier_shipment():
179-
order = [
180-
OrderLine(sku='sku1', quantity=10),
181-
OrderLine(sku='sku2', quantity=10),
182-
]
162+
order = Order({'sku1': 10, 'sku2': 10})
183163
shipment1 = Shipment('shipment1', eta=date.today(), lines={
184164
'sku1': 1000,
185165
'sku2': 1000,
@@ -193,16 +173,12 @@ def test_prefer_allocating_to_earlier_shipment():
193173

194174
allocate(order, stock, shipments=[shipment2, shipment1])
195175

196-
assert order[0].allocation == shipment1.id
197-
assert order[1].allocation == shipment1.id
176+
assert order.allocations['sku1'] == shipment1.id
177+
assert order.allocations['sku2'] == shipment1.id
198178

199179

200180
def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
201-
order = [
202-
OrderLine(sku='sku1', quantity=10),
203-
OrderLine(sku='sku2', quantity=10),
204-
OrderLine(sku='sku3', quantity=10),
205-
]
181+
order = Order({'sku1': 10, 'sku2': 10, 'sku3': 10})
206182
shipment1 = Shipment(id='shipment1', eta=date.today(), lines={
207183
'sku1': 1000,
208184
})
@@ -220,37 +196,38 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
220196

221197
allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
222198

223-
assert order[1].allocation == shipment2.id
224-
assert order[2].allocation == shipment2.id
199+
assert order.allocations['sku1'] == shipment1.id
200+
assert order.allocations['sku2'] == shipment2.id
201+
assert order.allocations['sku3'] == shipment2.id
225202

226203

227204
def test_cannot_allocate_if_insufficent_quantity_in_stock():
228-
order = [OrderLine(sku='a-sku', quantity=10)]
205+
order = Order({'a-sku': 10})
229206
stock = {'a-sku': 5}
230207

231208
allocate(order, stock, shipments=[])
232209

233-
assert order[0].allocation is None
210+
assert 'a-sku' not in order.allocations
234211

235212

236213
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
237-
order = [OrderLine(sku='a-sku', quantity=10)]
214+
order = Order({'a-sku': 10})
238215
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
239216
'a-sku': 5,
240217
})
241218

242219
allocate(order, stock=[], shipments=[shipment])
243220

244-
assert order[0].allocation is None
221+
assert 'a-sku' not in order.allocations
245222

246223

247224
def test_cannot_allocate_more_orders_than_we_have_stock_for():
248-
order1 = [OrderLine(sku='a-sku', quantity=10)]
249-
order2 = [OrderLine(sku='a-sku', quantity=10)]
225+
order1 = Order({'a-sku': 10})
226+
order2 = Order({'a-sku': 10})
250227
stock = {'a-sku': 15}
251228

252229
allocate(order1, stock, shipments=[])
253230
allocate(order2, stock, shipments=[])
254231

255-
assert order2[0].allocation is None
232+
assert 'a-sku' not in order2.allocations
256233

0 commit comments

Comments
 (0)