Skip to content

Commit 16b01ec

Browse files
committed
more like it, stock now allocates correctly
1 parent f815b0d commit 16b01ec

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

domain_model.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,24 @@ def to_list(fn):
3838
return lambda *a, **kw: list(fn(*a, **kw))
3939

4040

41+
def allocate_to_stock(order_id, line, stock):
42+
for stock_line in stock:
43+
if stock_line.sku == line.sku:
44+
return Allocation(order_id, line.sku, line.quantity, shipment_id=None)
45+
46+
def allocate_to_shipments(order_id, line, shipments):
47+
for shipment in shipments:
48+
for shipment_line in shipment.lines:
49+
if shipment_line.sku == line.sku:
50+
return Allocation(order_id, line.sku, line.quantity, shipment.id)
51+
4152
@to_list
4253
def allocate(order, stock, shipments):
4354
for line in order.lines:
44-
for stock_line in stock:
45-
yield Allocation(order.id, line.sku, line.quantity, shipment_id=None)
46-
for shipment in shipments:
47-
for shipment_line in shipment.lines:
48-
if shipment_line.sku == line.sku:
49-
yield Allocation(order.id, line.sku, line.quantity, shipment.id)
55+
a = allocate_to_stock(order.id, line, stock)
56+
if a:
57+
yield a
58+
else:
59+
yield allocate_to_shipments(order.id, line, shipments)
60+
5061

test_allocation.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ def test_can_allocate_to_shipment():
4646
assert allocations[0].shipment_id == shipment.id
4747
assert allocations[0].quantity == 10
4848

49+
def test_ignores_invalid_stock():
50+
sku1, sku2 = random_id(), random_id()
51+
order = Order(id=random_id(), lines=[
52+
OrderLine(sku=sku1, quantity=10),
53+
])
54+
stock = Stock(sku=sku2, quantity=1000)
55+
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
56+
OrderLine(sku=sku1, quantity=1000),
57+
])
58+
59+
allocations = allocate(order, stock=[stock], shipments=[shipment])
60+
61+
assert allocations[0].order_id == order.id
62+
assert allocations[0].sku == sku1
63+
assert allocations[0].shipment_id == shipment.id
64+
assert allocations[0].quantity == 10
65+
4966

5067
def test_can_allocate_to_correct_shipment():
5168
sku1, sku2 = random_id(), random_id()
@@ -135,4 +152,30 @@ def test_can_allocate_to_both():
135152
assert Allocation(order.id, sku2, 10, shipment_id=shipment.id) in allocations
136153

137154

155+
def test_can_allocate_to_both_preferring_stock():
156+
sku1, sku2, sku3, sku4 = [random_id() for _ in range(4)]
157+
order = Order(id=random_id(), lines=[
158+
OrderLine(sku=sku1, quantity=10),
159+
OrderLine(sku=sku2, quantity=10),
160+
OrderLine(sku=sku3, quantity=10),
161+
OrderLine(sku=sku4, quantity=10),
162+
])
163+
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
164+
OrderLine(sku=sku1, quantity=1000),
165+
OrderLine(sku=sku2, quantity=1000),
166+
OrderLine(sku=sku3, quantity=1000),
167+
])
168+
stock = [
169+
Stock(sku=sku3, quantity=1000),
170+
Stock(sku=sku4, quantity=1000),
171+
]
172+
173+
allocations = allocate(order, stock, shipments=[shipment])
174+
assert Allocation(order.id, sku1, 10, shipment_id=shipment.id) in allocations
175+
assert Allocation(order.id, sku2, 10, shipment_id=shipment.id) in allocations
176+
assert Allocation(order.id, sku3, 10, shipment_id=None) in allocations
177+
assert Allocation(order.id, sku4, 10, shipment_id=None) in allocations
178+
assert Allocation(order.id, sku3, 10, shipment_id=shipment.id) not in allocations
179+
180+
138181

0 commit comments

Comments
 (0)