Skip to content

Commit a4a11b9

Browse files
committed
start return allocations
1 parent e29efff commit a4a11b9

File tree

2 files changed

+54
-49
lines changed

2 files changed

+54
-49
lines changed

domain_model.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,39 @@ def skus(thing):
1818
return thing.keys()
1919

2020

21+
def allocate_line(order, sku, quantity, source):
22+
if source.get(sku, 0) > quantity:
23+
source[sku] -= quantity
24+
order.allocations[sku] = getattr(source, 'id', 'STOCK')
25+
2126
def allocate_to(order, source):
2227
for sku, quantity in order.items():
23-
if source.get(sku, 0) > quantity:
24-
source[sku] -= quantity
25-
order.allocations[sku] = getattr(source, 'id', 'STOCK')
28+
allocate_line(order, sku, quantity, source)
2629

2730

2831
def allocate_to_shipments(order, shipments):
2932
for sku, quantity in order.items():
3033
if sku in order.allocations:
3134
continue
3235
for shipment in shipments:
33-
if shipment.get(sku, 0) > quantity:
34-
shipment[sku] -= quantity
35-
order.allocations[sku] = shipment.id
36+
allocate_line(order, sku, quantity, shipment)
37+
if sku in order.allocations:
3638
break
3739

3840

41+
3942
def allocate(order, stock, shipments):
4043
if skus(order) <= skus(stock):
4144
allocate_to(order, stock)
42-
return
45+
return order.allocations
4346

4447
shipments.sort(key=lambda s: s.eta)
4548

4649
for shipment in shipments:
4750
if skus(order) <= skus(shipment):
4851
allocate_to(order, shipment)
49-
return
52+
return order.allocations
5053

5154
allocate_to(order, stock)
5255
allocate_to_shipments(order, shipments)
56+
return order.allocations

test_allocation.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def test_can_allocate_to_stock():
1717
order = Order({'a-sku': 10})
1818
stock = {'a-sku': 1000}
1919

20-
allocate(order, stock, shipments=[])
20+
allocations = allocate(order, stock, shipments=[])
2121

22-
assert order.allocations['a-sku'] == 'STOCK'
22+
assert allocations['a-sku'] == 'STOCK'
2323
assert stock['a-sku'] == 990
2424

2525

@@ -29,9 +29,9 @@ def test_can_allocate_to_shipment():
2929
'a-sku': 1000
3030
})
3131

32-
allocate(order, stock=[], shipments=[shipment])
32+
allocations = allocate(order, stock=[], shipments=[shipment])
3333

34-
assert order.allocations['a-sku'] == shipment.id
34+
assert allocations['a-sku'] == shipment.id
3535
assert shipment['a-sku'] == 990
3636

3737

@@ -42,9 +42,9 @@ def test_ignores_irrelevant_stock():
4242
'sku1': 1000,
4343
})
4444

45-
allocate(order, stock=stock, shipments=[shipment])
45+
allocations = allocate(order, stock=stock, shipments=[shipment])
4646

47-
assert order.allocations['sku1'] == shipment.id
47+
assert allocations['sku1'] == shipment.id
4848
assert stock['sku2'] == 1000
4949
assert shipment['sku1'] == 990
5050

@@ -58,9 +58,9 @@ def test_can_allocate_to_correct_shipment():
5858
'sku2': 1000,
5959
})
6060

61-
allocate(order, stock=[], shipments=[shipment1, shipment2])
61+
allocations = allocate(order, stock=[], shipments=[shipment1, shipment2])
6262

63-
assert order.allocations['sku2'] == shipment2.id
63+
assert allocations['sku2'] == shipment2.id
6464
assert shipment1['sku1'] == 1000
6565
assert shipment2['sku2'] == 990
6666

@@ -72,9 +72,9 @@ def test_allocates_to_stock_in_preference_to_shipment():
7272
'sku1': 1000,
7373
})
7474

75-
allocate(order, stock, shipments=[shipment])
75+
allocations = allocate(order, stock, shipments=[shipment])
7676

77-
assert order.allocations['sku1'] == 'STOCK'
77+
assert allocations['sku1'] == 'STOCK'
7878
assert stock['sku1'] == 990
7979
assert shipment['sku1'] == 1000
8080

@@ -83,9 +83,9 @@ def test_can_allocate_multiple_lines_to_wh():
8383
order = Order({'sku1': 5, 'sku2': 10})
8484
stock = {'sku1': 1000, 'sku2': 1000}
8585

86-
allocate(order, stock, shipments=[])
87-
assert order.allocations['sku1'] == 'STOCK'
88-
assert order.allocations['sku2'] == 'STOCK'
86+
allocations = allocate(order, stock, shipments=[])
87+
assert allocations['sku1'] == 'STOCK'
88+
assert allocations['sku2'] == 'STOCK'
8989
assert stock['sku1'] == 995
9090
assert stock['sku2'] == 990
9191

@@ -97,10 +97,10 @@ def test_can_allocate_multiple_lines_to_shipment():
9797
'sku2': 1000,
9898
})
9999

100-
allocate(order, [], shipments=[shipment])
100+
allocations = allocate(order, [], shipments=[shipment])
101101

102-
assert order.allocations['sku1'] == shipment.id
103-
assert order.allocations['sku2'] == shipment.id
102+
assert allocations['sku1'] == shipment.id
103+
assert allocations['sku2'] == shipment.id
104104
assert shipment['sku1'] == 995
105105
assert shipment['sku2'] == 990
106106

@@ -112,10 +112,10 @@ def test_can_allocate_to_both():
112112
})
113113
stock = {'sku1': 1000}
114114

115-
allocate(order, stock, shipments=[shipment])
115+
allocations = allocate(order, stock, shipments=[shipment])
116116

117-
assert order.allocations['sku1'] == 'STOCK'
118-
assert order.allocations['sku2'] == shipment.id
117+
assert allocations['sku1'] == 'STOCK'
118+
assert allocations['sku2'] == shipment.id
119119
assert stock['sku1'] == 995
120120
assert shipment['sku2'] == 990
121121

@@ -129,12 +129,12 @@ def test_can_allocate_to_both_preferring_stock():
129129
})
130130
stock = {'sku3': 1000, 'sku4': 1000}
131131

132-
allocate(order, stock, shipments=[shipment])
132+
allocations = allocate(order, stock, shipments=[shipment])
133133

134-
assert order.allocations['sku1'] == shipment.id
135-
assert order.allocations['sku2'] == shipment.id
136-
assert order.allocations['sku3'] == 'STOCK'
137-
assert order.allocations['sku4'] == 'STOCK'
134+
assert allocations['sku1'] == shipment.id
135+
assert allocations['sku2'] == shipment.id
136+
assert allocations['sku3'] == 'STOCK'
137+
assert allocations['sku4'] == 'STOCK'
138138
assert shipment['sku1'] == 999
139139
assert shipment['sku2'] == 998
140140
assert shipment['sku3'] == 1000
@@ -150,10 +150,10 @@ def test_mixed_allocations_are_avoided_if_possible():
150150
})
151151
stock = {'sku1': 1000}
152152

153-
allocate(order, stock, shipments=[shipment])
153+
allocations = allocate(order, stock, shipments=[shipment])
154154

155-
assert order.allocations['sku1'] == shipment.id
156-
assert order.allocations['sku2'] == shipment.id
155+
assert allocations['sku1'] == shipment.id
156+
assert allocations['sku2'] == shipment.id
157157

158158

159159
def test_prefer_allocating_to_earlier_shipment():
@@ -169,10 +169,10 @@ def test_prefer_allocating_to_earlier_shipment():
169169
})
170170
stock = {}
171171

172-
allocate(order, stock, shipments=[shipment2, shipment1])
172+
allocations = allocate(order, stock, shipments=[shipment2, shipment1])
173173

174-
assert order.allocations['sku1'] == shipment1.id
175-
assert order.allocations['sku2'] == shipment1.id
174+
assert allocations['sku1'] == shipment1.id
175+
assert allocations['sku2'] == shipment1.id
176176

177177

178178
def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
@@ -192,20 +192,20 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
192192
})
193193
stock = {}
194194

195-
allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
195+
allocations = allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
196196

197-
assert order.allocations['sku1'] == shipment1.id
198-
assert order.allocations['sku2'] == shipment2.id
199-
assert order.allocations['sku3'] == shipment2.id
197+
assert allocations['sku1'] == shipment1.id
198+
assert allocations['sku2'] == shipment2.id
199+
assert allocations['sku3'] == shipment2.id
200200

201201

202202
def test_cannot_allocate_if_insufficent_quantity_in_stock():
203203
order = Order({'a-sku': 10})
204204
stock = {'a-sku': 5}
205205

206-
allocate(order, stock, shipments=[])
206+
allocations = allocate(order, stock, shipments=[])
207207

208-
assert 'a-sku' not in order.allocations
208+
assert 'a-sku' not in allocations
209209

210210

211211
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
@@ -214,18 +214,19 @@ def test_cannot_allocate_if_insufficent_quantity_in_shipment():
214214
'a-sku': 5,
215215
})
216216

217-
allocate(order, stock=[], shipments=[shipment])
217+
allocations = allocate(order, stock=[], shipments=[shipment])
218218

219-
assert 'a-sku' not in order.allocations
219+
assert 'a-sku' not in allocations
220220

221221

222222
def test_cannot_allocate_more_orders_than_we_have_stock_for():
223223
order1 = Order({'a-sku': 10})
224224
order2 = Order({'a-sku': 10})
225225
stock = {'a-sku': 15}
226226

227-
allocate(order1, stock, shipments=[])
228-
allocate(order2, stock, shipments=[])
227+
allocations1 = allocate(order1, stock, shipments=[])
228+
allocations2 = allocate(order2, stock, shipments=[])
229229

230-
assert 'a-sku' not in order2.allocations
230+
assert 'a-sku' in allocations1
231+
assert 'a-sku' not in allocations2
231232

0 commit comments

Comments
 (0)