Skip to content

Commit c892ffe

Browse files
committed
store allocation on order lines
1 parent efae48a commit c892ffe

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

domain_model.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Order:
1111
class OrderLine:
1212
sku: str
1313
quantity: int
14+
allocation: str = None
1415

1516

1617
@dataclass
@@ -34,28 +35,20 @@ class Allocation:
3435
shipment_id: str
3536

3637

37-
def to_list(fn):
38-
return lambda *a, **kw: list(fn(*a, **kw))
39-
40-
4138
def allocate_to_stock(order_id, line, stock):
4239
for stock_line in stock:
4340
if stock_line.sku == line.sku:
44-
return Allocation(order_id, line.sku, line.quantity, shipment_id=None)
41+
line.allocation = 'warehouse'
4542

4643
def allocate_to_shipments(order_id, line, shipments):
4744
for shipment in shipments:
4845
for shipment_line in shipment.lines:
4946
if shipment_line.sku == line.sku:
50-
return Allocation(order_id, line.sku, line.quantity, shipment.id)
47+
line.allocation = shipment.id
5148

52-
@to_list
5349
def allocate(order, stock, shipments):
5450
for line in order.lines:
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-
51+
allocate_to_stock(order.id, line, stock)
52+
if not line.allocation:
53+
allocate_to_shipments(order.id, line, shipments)
6154

test_allocation.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44

55
from domain_model import (
6-
Allocation,
76
Order,
87
OrderLine,
98
Shipment,
109
Stock,
11-
allocate as allocate_
10+
allocate,
1211
)
1312

1413

15-
allocate = lambda *a, **kw: list(allocate_(*a, **kw))
1614

1715

1816
def random_id():
@@ -26,12 +24,9 @@ def test_can_allocate_to_warehouse_stock():
2624
])
2725
stock = Stock(sku=sku, quantity=1000)
2826

29-
allocations = allocate(order, [stock], shipments=[])
27+
allocate(order, [stock], shipments=[])
3028

31-
assert allocations[0].order_id == order.id
32-
assert allocations[0].sku == sku
33-
assert allocations[0].shipment_id is None
34-
assert allocations[0].quantity == 10
29+
assert order.lines[0].allocation == 'warehouse'
3530

3631

3732
def test_can_allocate_to_shipment():
@@ -43,12 +38,9 @@ def test_can_allocate_to_shipment():
4338
OrderLine(sku=sku, quantity=1000),
4439
])
4540

46-
allocations = allocate(order, stock=[], shipments=[shipment])
41+
allocate(order, stock=[], shipments=[shipment])
4742

48-
assert allocations[0].order_id == order.id
49-
assert allocations[0].sku == sku
50-
assert allocations[0].shipment_id == shipment.id
51-
assert allocations[0].quantity == 10
43+
assert order.lines[0].allocation == shipment.id
5244

5345

5446
def test_ignores_invalid_stock():
@@ -61,12 +53,9 @@ def test_ignores_invalid_stock():
6153
OrderLine(sku=sku1, quantity=1000),
6254
])
6355

64-
allocations = allocate(order, stock=[stock], shipments=[shipment])
56+
allocate(order, stock=[stock], shipments=[shipment])
6557

66-
assert allocations[0].order_id == order.id
67-
assert allocations[0].sku == sku1
68-
assert allocations[0].shipment_id == shipment.id
69-
assert allocations[0].quantity == 10
58+
assert order.lines[0].allocation == shipment.id
7059

7160

7261
def test_can_allocate_to_correct_shipment():
@@ -81,12 +70,9 @@ def test_can_allocate_to_correct_shipment():
8170
OrderLine(sku=sku2, quantity=1000),
8271
])
8372

84-
allocations = allocate(order, stock=[], shipments=[shipment1, shipment2])
73+
allocate(order, stock=[], shipments=[shipment1, shipment2])
8574

86-
assert allocations[0].order_id == order.id
87-
assert allocations[0].sku == sku2
88-
assert allocations[0].shipment_id == shipment2.id
89-
assert allocations[0].quantity == 10
75+
assert order.lines[0].allocation == shipment2.id
9076

9177

9278
def test_allocates_to_warehouse_stock_in_preference_to_shipment():
@@ -99,12 +85,9 @@ def test_allocates_to_warehouse_stock_in_preference_to_shipment():
9985
OrderLine(sku=sku, quantity=1000),
10086
])
10187

102-
allocations = allocate(order, [stock], shipments=[shipment])
88+
allocate(order, [stock], shipments=[shipment])
10389

104-
assert allocations[0].order_id == order.id
105-
assert allocations[0].sku == sku
106-
assert allocations[0].shipment_id is None
107-
assert allocations[0].quantity == 10
90+
assert order.lines[0].allocation == 'warehouse'
10891

10992

11093
def test_can_allocate_multiple_lines_to_wh():
@@ -118,9 +101,9 @@ def test_can_allocate_multiple_lines_to_wh():
118101
Stock(sku=sku2, quantity=1000),
119102
]
120103

121-
allocations = allocate(order, stock, shipments=[])
122-
assert Allocation(order.id, sku1, 10, shipment_id=None) in allocations
123-
assert Allocation(order.id, sku2, 10, shipment_id=None) in allocations
104+
allocate(order, stock, shipments=[])
105+
assert order.lines[0].allocation == 'warehouse'
106+
assert order.lines[1].allocation == 'warehouse'
124107

125108

126109
def test_can_allocate_multiple_lines_to_shipment():
@@ -134,9 +117,10 @@ def test_can_allocate_multiple_lines_to_shipment():
134117
OrderLine(sku=sku2, quantity=1000),
135118
])
136119

137-
allocations = allocate(order, [], shipments=[shipment])
138-
assert Allocation(order.id, sku1, 10, shipment_id=shipment.id) in allocations
139-
assert Allocation(order.id, sku2, 10, shipment_id=shipment.id) in allocations
120+
allocate(order, [], shipments=[shipment])
121+
122+
assert order.lines[0].allocation == shipment.id
123+
assert order.lines[1].allocation == shipment.id
140124

141125

142126
def test_can_allocate_to_both():
@@ -152,9 +136,10 @@ def test_can_allocate_to_both():
152136
Stock(sku=sku1, quantity=1000),
153137
]
154138

155-
allocations = allocate(order, stock, shipments=[shipment])
156-
assert Allocation(order.id, sku1, 10, shipment_id=None) in allocations
157-
assert Allocation(order.id, sku2, 10, shipment_id=shipment.id) in allocations
139+
allocate(order, stock, shipments=[shipment])
140+
141+
assert order.lines[0].allocation == 'warehouse'
142+
assert order.lines[1].allocation == shipment.id
158143

159144

160145
def test_can_allocate_to_both_preferring_stock():
@@ -175,11 +160,10 @@ def test_can_allocate_to_both_preferring_stock():
175160
Stock(sku=sku4, quantity=1000),
176161
]
177162

178-
allocations = allocate(order, stock, shipments=[shipment])
179-
assert Allocation(order.id, sku1, 10, shipment_id=shipment.id) in allocations
180-
assert Allocation(order.id, sku2, 10, shipment_id=shipment.id) in allocations
181-
assert Allocation(order.id, sku3, 10, shipment_id=None) in allocations
182-
assert Allocation(order.id, sku4, 10, shipment_id=None) in allocations
183-
assert Allocation(order.id, sku3, 10, shipment_id=shipment.id) not in allocations
163+
allocate(order, stock, shipments=[shipment])
184164

165+
assert order.lines[0].allocation == shipment.id
166+
assert order.lines[1].allocation == shipment.id
167+
assert order.lines[2].allocation == 'warehouse'
168+
assert order.lines[3].allocation == 'warehouse'
185169

0 commit comments

Comments
 (0)