Skip to content

Commit e3b2b13

Browse files
committed
start on prioritsing by eta
1 parent ec48906 commit e3b2b13

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

domain_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def allocate(order, stock, shipments):
4343
for line in order:
4444
line.allocation = 'STOCK'
4545
return
46-
46+
shipments.sort(key=lambda s: s.eta)
4747
for shipment in shipments:
4848
if skus(order) <= skus(shipment):
4949
for line in order:

test_allocation.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import uuid
2-
from datetime import date
2+
from datetime import date, timedelta
33

44

55
from domain_model import (
@@ -172,3 +172,54 @@ def test_mixed_allocations_are_avoided_if_possible():
172172
assert order[0].allocation == shipment.id
173173
assert order[1].allocation == shipment.id
174174

175+
176+
def test_prefer_allocating_to_earlier_shipment():
177+
sku1, sku2 = random_id(), random_id()
178+
order = [
179+
OrderLine(sku=sku1, quantity=10),
180+
OrderLine(sku=sku2, quantity=10),
181+
]
182+
shipment1 = Shipment(id=random_id(), eta=date.today(), lines=[
183+
Line(sku=sku1, quantity=1000),
184+
Line(sku=sku2, quantity=1000),
185+
])
186+
tomorrow = date.today() + timedelta(days=1)
187+
shipment2 = Shipment(id=random_id(), eta=tomorrow, lines=[
188+
Line(sku=sku1, quantity=1000),
189+
Line(sku=sku2, quantity=1000),
190+
])
191+
stock = []
192+
193+
allocate(order, stock, shipments=[shipment2, shipment1])
194+
195+
assert order[0].allocation == shipment1.id
196+
assert order[1].allocation == shipment1.id
197+
198+
199+
def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
200+
sku1, sku2, sku3 = random_id(), random_id(), random_id()
201+
order = [
202+
OrderLine(sku=sku1, quantity=10),
203+
OrderLine(sku=sku2, quantity=10),
204+
OrderLine(sku=sku3, quantity=10),
205+
]
206+
shipment1 = Shipment(id=random_id(), eta=date.today(), lines=[
207+
Line(sku=sku1, quantity=1000),
208+
])
209+
tomorrow = date.today() + timedelta(days=1)
210+
shipment2 = Shipment(id=random_id(), eta=tomorrow, lines=[
211+
Line(sku=sku2, quantity=1000),
212+
Line(sku=sku3, quantity=1000),
213+
])
214+
later = tomorrow + timedelta(days=1)
215+
shipment3 = Shipment(id=random_id(), eta=later, lines=[
216+
Line(sku=sku2, quantity=1000),
217+
Line(sku=sku3, quantity=1000),
218+
])
219+
stock = []
220+
221+
allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
222+
223+
assert order[1].allocation == shipment2.id
224+
assert order[2].allocation == shipment2.id
225+

0 commit comments

Comments
 (0)