Skip to content

Commit f7e4279

Browse files
committed
reintroduce eta into model
1 parent d959a7e commit f7e4279

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

‎domain_model.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def fully_allocated(self):
2424

2525
def allocate(self, stock, shipments):
2626
self.allocation = {}
27-
for source in [stock] + shipments:
27+
for source in [stock] + sorted(shipments, key=lambda x: x.eta):
2828
allocation = Allocation({
2929
sku: source
3030
for sku, quantity in self.lines.items()

‎test_allocation.py‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from domain_model import Order, Stock
2+
from datetime import date, timedelta
3+
4+
today = date.today()
5+
tomorrow = today + timedelta(days=1)
6+
later = tomorrow + timedelta(days=10)
27

38

49
def test_can_allocate_to_stock():
@@ -12,7 +17,7 @@ def test_can_allocate_to_stock():
1217

1318
def test_can_allocate_to_shipment():
1419
order = Order({'a-sku': 10})
15-
shipment = Stock({'a-sku': 1000})
20+
shipment = Stock({'a-sku': 1000}, eta=tomorrow)
1621

1722
order.allocate(stock=Stock({}), shipments=[shipment])
1823

@@ -22,7 +27,7 @@ def test_can_allocate_to_shipment():
2227
def test_ignores_irrelevant_stock():
2328
order = Order({'sku1': 10})
2429
stock = Stock({'sku2': 1000})
25-
shipment = Stock({'sku1': 1000})
30+
shipment = Stock({'sku1': 1000}, eta=tomorrow)
2631

2732
order.allocate(stock=stock, shipments=[shipment])
2833

@@ -32,8 +37,8 @@ def test_ignores_irrelevant_stock():
3237

3338
def test_can_allocate_to_correct_shipment():
3439
order = Order({'sku2': 10})
35-
shipment1 = Stock({'sku1': 1000})
36-
shipment2 = Stock({'sku2': 1000})
40+
shipment1 = Stock({'sku1': 1000}, eta=tomorrow)
41+
shipment2 = Stock({'sku2': 1000}, eta=tomorrow)
3742

3843
order.allocate(stock=Stock({}), shipments=[shipment1, shipment2])
3944

@@ -43,7 +48,7 @@ def test_can_allocate_to_correct_shipment():
4348
def test_allocates_to_stock_in_preference_to_shipment():
4449
order = Order({'sku1': 10})
4550
stock = Stock({'sku1': 1000})
46-
shipment = Stock({'sku1': 1000})
51+
shipment = Stock({'sku1': 1000}, eta=tomorrow)
4752

4853
order.allocate(stock, shipments=[shipment])
4954

@@ -61,7 +66,7 @@ def test_can_allocate_multiple_lines_to_wh():
6166

6267
def test_can_allocate_multiple_lines_to_shipment():
6368
order = Order({'sku1': 5, 'sku2': 10})
64-
shipment = Stock({'sku1': 1000, 'sku2': 1000})
69+
shipment = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
6570

6671
order.allocate(stock=Stock({}), shipments=[shipment])
6772

@@ -71,7 +76,7 @@ def test_can_allocate_multiple_lines_to_shipment():
7176

7277
def test_can_allocate_to_both():
7378
order = Order({'sku1': 5, 'sku2': 10})
74-
shipment = Stock({'sku2': 1000})
79+
shipment = Stock({'sku2': 1000}, eta=tomorrow)
7580
stock = Stock({'sku1': 1000})
7681

7782
order.allocate(stock, shipments=[shipment])
@@ -82,7 +87,7 @@ def test_can_allocate_to_both():
8287

8388
def test_can_allocate_to_both_preferring_stock():
8489
order = Order({'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4})
85-
shipment = Stock({'sku1': 1000, 'sku2': 1000, 'sku3': 1000})
90+
shipment = Stock({'sku1': 1000, 'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
8691
stock = Stock({'sku3': 1000, 'sku4': 1000})
8792

8893
order.allocate(stock, shipments=[shipment])
@@ -93,10 +98,9 @@ def test_can_allocate_to_both_preferring_stock():
9398
assert order.allocation['sku4'] == stock
9499

95100

96-
97101
def test_mixed_allocation_are_avoided_if_possible():
98102
order = Order({'sku1': 10, 'sku2': 10})
99-
shipment = Stock({'sku1': 1000, 'sku2': 1000})
103+
shipment = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
100104
stock = Stock({'sku1': 1000})
101105

102106
order.allocate(stock, shipments=[shipment])
@@ -105,26 +109,26 @@ def test_mixed_allocation_are_avoided_if_possible():
105109
assert order.allocation['sku2'] == shipment
106110

107111

108-
def test_allocated_to_first_suitable_shipment_in_list():
112+
def test_allocated_to_earliest_suitable_shipment_in_list():
109113
order = Order({'sku1': 10, 'sku2': 10})
110-
shipment1 = Stock({'sku1': 1000, 'sku2': 1000})
111-
shipment2 = Stock({'sku1': 1000, 'sku2': 1000})
114+
shipment1 = Stock({'sku1': 1000, 'sku2': 1000}, eta=today)
115+
shipment2 = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
112116
stock = Stock({})
113117

114-
order.allocate(stock, shipments=[shipment1, shipment2])
118+
order.allocate(stock, shipments=[shipment2, shipment1])
115119

116120
assert order.allocation['sku1'] == shipment1
117121
assert order.allocation['sku2'] == shipment1
118122

119123

120-
def test_still_preserves_ordering_if_split_across_shipments():
124+
def test_still_chooses_earliest_if_split_across_shipments():
121125
order = Order({'sku1': 10, 'sku2': 10, 'sku3': 10})
122-
shipment1 = Stock({'sku1': 1000})
123-
shipment2 = Stock({'sku2': 1000, 'sku3': 1000})
124-
shipment3 = Stock({'sku2': 1000, 'sku3': 1000})
126+
shipment1 = Stock({'sku1': 1000}, eta=today)
127+
shipment2 = Stock({'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
128+
shipment3 = Stock({'sku2': 1000, 'sku3': 1000}, eta=later)
125129
stock = Stock({})
126130

127-
order.allocate(stock, shipments=[shipment1, shipment2, shipment3])
131+
order.allocate(stock, shipments=[shipment3, shipment2, shipment1])
128132

129133
assert order.allocation['sku1'] == shipment1
130134
assert order.allocation['sku2'] == shipment2
@@ -156,9 +160,7 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
156160

157161
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
158162
order = Order({'a-sku': 10})
159-
shipment = Stock({
160-
'a-sku': 5,
161-
})
163+
shipment = Stock({'a-sku': 5}, eta=tomorrow)
162164

163165
order.allocate(stock=Stock({}), shipments=[shipment])
164166

0 commit comments

Comments
 (0)