Skip to content

Commit bce4c15

Browse files
committed
stock is now a dict
1 parent 93d5afa commit bce4c15

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

test_allocation.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
from domain_model import (
6-
Line,
76
OrderLine,
87
Shipment,
98
allocate,
@@ -18,12 +17,12 @@ def random_id():
1817

1918
def test_can_allocate_to_stock():
2019
order = [OrderLine(sku='a-sku', quantity=10)]
21-
stock = [Line(sku='a-sku', quantity=1000)]
20+
stock = {'a-sku': 1000}
2221

2322
allocate(order, stock, shipments=[])
2423

2524
assert order[0].allocation == 'STOCK'
26-
assert stock[0].quantity == 990
25+
assert stock['a-sku'] == 990
2726

2827

2928
def test_can_allocate_to_shipment():
@@ -38,17 +37,17 @@ def test_can_allocate_to_shipment():
3837
assert shipment['a-sku'] == 990
3938

4039

41-
def test_ignores_invalid_stock():
40+
def test_ignores_irrelevant_stock():
4241
order = [OrderLine(sku='sku1', quantity=10), ]
43-
stock = [Line(sku='sku2', quantity=1000)]
42+
stock = {'sku2': 1000}
4443
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
4544
'sku1': 1000,
4645
})
4746

4847
allocate(order, stock=stock, shipments=[shipment])
4948

5049
assert order[0].allocation == shipment.id
51-
assert stock[0].quantity == 1000
50+
assert stock['sku2'] == 1000
5251
assert shipment['sku1'] == 990
5352

5453

@@ -70,15 +69,15 @@ def test_can_allocate_to_correct_shipment():
7069

7170
def test_allocates_to_stock_in_preference_to_shipment():
7271
order = [OrderLine(sku='sku1', quantity=10)]
73-
stock = [Line(sku='sku1', quantity=1000)]
72+
stock = {'sku1': 1000}
7473
shipment = Shipment('shipment1', eta=date.today(), lines={
7574
'sku1': 1000,
7675
})
7776

7877
allocate(order, stock, shipments=[shipment])
7978

8079
assert order[0].allocation == 'STOCK'
81-
assert stock[0].quantity == 990
80+
assert stock['sku1'] == 990
8281
assert shipment['sku1'] == 1000
8382

8483

@@ -87,16 +86,13 @@ def test_can_allocate_multiple_lines_to_wh():
8786
OrderLine(sku='sku1', quantity=5),
8887
OrderLine(sku='sku2', quantity=10),
8988
]
90-
stock = [
91-
Line(sku='sku1', quantity=1000),
92-
Line(sku='sku2', quantity=1000),
93-
]
89+
stock = {'sku1': 1000, 'sku2': 1000}
9490

9591
allocate(order, stock, shipments=[])
9692
assert order[0].allocation == 'STOCK'
9793
assert order[1].allocation == 'STOCK'
98-
assert stock[0].quantity == 995
99-
assert stock[1].quantity == 990
94+
assert stock['sku1'] == 995
95+
assert stock['sku2'] == 990
10096

10197

10298
def test_can_allocate_multiple_lines_to_shipment():
@@ -125,13 +121,13 @@ def test_can_allocate_to_both():
125121
shipment = Shipment('shipment1', eta=date.today(), lines={
126122
'sku2': 1000,
127123
})
128-
stock = [Line(sku='sku1', quantity=1000)]
124+
stock = {'sku1': 1000}
129125

130126
allocate(order, stock, shipments=[shipment])
131127

132128
assert order[0].allocation == 'STOCK'
133129
assert order[1].allocation == shipment.id
134-
assert stock[0].quantity == 995
130+
assert stock['sku1'] == 995
135131
assert shipment['sku2'] == 990
136132

137133

@@ -147,10 +143,7 @@ def test_can_allocate_to_both_preferring_stock():
147143
'sku2': 1000,
148144
'sku3': 1000,
149145
})
150-
stock = [
151-
Line(sku='sku3', quantity=1000),
152-
Line(sku='sku4', quantity=1000),
153-
]
146+
stock = {'sku3': 1000, 'sku4': 1000}
154147

155148
allocate(order, stock, shipments=[shipment])
156149

@@ -161,8 +154,8 @@ def test_can_allocate_to_both_preferring_stock():
161154
assert shipment['sku1'] == 999
162155
assert shipment['sku2'] == 998
163156
assert shipment['sku3'] == 1000
164-
assert stock[0].quantity == 997
165-
assert stock[1].quantity == 996
157+
assert stock['sku3'] == 997
158+
assert stock['sku4'] == 996
166159

167160

168161
def test_mixed_allocations_are_avoided_if_possible():
@@ -174,7 +167,7 @@ def test_mixed_allocations_are_avoided_if_possible():
174167
'sku1': 1000,
175168
'sku2': 1000,
176169
})
177-
stock = [Line(sku='sku1', quantity=1000)]
170+
stock = {'sku1': 1000}
178171

179172
allocate(order, stock, shipments=[shipment])
180173

@@ -196,7 +189,7 @@ def test_prefer_allocating_to_earlier_shipment():
196189
'sku1': 1000,
197190
'sku2': 1000,
198191
})
199-
stock = []
192+
stock = {}
200193

201194
allocate(order, stock, shipments=[shipment2, shipment1])
202195

@@ -233,7 +226,7 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
233226

234227
def test_cannot_allocate_if_insufficent_quantity_in_stock():
235228
order = [OrderLine(sku='a-sku', quantity=10)]
236-
stock = [Line(sku='a-sku', quantity=5)]
229+
stock = {'a-sku': 5}
237230

238231
allocate(order, stock, shipments=[])
239232

@@ -254,7 +247,7 @@ def test_cannot_allocate_if_insufficent_quantity_in_shipment():
254247
def test_cannot_allocate_more_orders_than_we_have_stock_for():
255248
order1 = [OrderLine(sku='a-sku', quantity=10)]
256249
order2 = [OrderLine(sku='a-sku', quantity=10)]
257-
stock = [Line(sku='a-sku', quantity=15)]
250+
stock = {'a-sku': 15}
258251

259252
allocate(order1, stock, shipments=[])
260253
allocate(order2, stock, shipments=[])

0 commit comments

Comments
 (0)