Skip to content

Commit 9a890ad

Browse files
committed
bring allocate function back
1 parent 27231f4 commit 9a890ad

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

domain_model.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
class Order(dict):
2-
3-
def allocate(self, warehouse, shipments):
4-
self.allocation = self.find_allocation(warehouse, shipments)
5-
self.decrement_source_quantities()
1+
def allocate(order, warehouse, shipments):
2+
ordered_sources = [warehouse] + sorted(shipments)
3+
allocation = {}
4+
for source in reversed(ordered_sources):
5+
allocation.update(source.allocation_for(order))
6+
decrement_source_quantities(order, allocation)
7+
return allocation
68

7-
def find_allocation(self, warehouse, shipments):
8-
ordered_sources = [warehouse] + sorted(shipments)
9-
allocation = {}
10-
for source in reversed(ordered_sources):
11-
allocation.update(source.allocation_for(self))
12-
return allocation
9+
def decrement_source_quantities(order, allocation):
10+
for sku, source in allocation.items():
11+
source[sku] -= order[sku]
1312

14-
def decrement_source_quantities(self):
15-
for sku, source in self.allocation.items():
16-
source[sku] -= self[sku]
1713

14+
class Order(dict):
15+
pass
1816

1917

2018
class _Stock(dict):

test_allocation.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from domain_model import Order, Warehouse, Shipment
1+
from domain_model import allocate, Order, Warehouse, Shipment
22
from datetime import date, timedelta
33

44
today = date.today()
@@ -10,19 +10,19 @@ def test_can_allocate_to_warehouse():
1010
order = Order({'a-sku': 10})
1111
warehouse = Warehouse({'a-sku': 1000})
1212

13-
order.allocate(warehouse, shipments=[])
13+
allocation = allocate(order, warehouse, shipments=[])
1414

15-
assert order.allocation['a-sku'] == warehouse
15+
assert allocation['a-sku'] == warehouse
1616
assert warehouse['a-sku'] == 990
1717

1818

1919
def test_can_allocate_to_shipment():
2020
order = Order({'a-sku': 10})
2121
shipment = Shipment({'a-sku': 1000}, eta=tomorrow)
2222

23-
order.allocate(warehouse=Warehouse({}), shipments=[shipment])
23+
allocation = allocate(order, warehouse=Warehouse({}), shipments=[shipment])
2424

25-
assert order.allocation['a-sku'] == shipment
25+
assert allocation['a-sku'] == shipment
2626
assert shipment['a-sku'] == 990
2727

2828

@@ -31,9 +31,9 @@ def test_ignores_irrelevant_warehouse():
3131
warehouse = Warehouse({'sku2': 1000})
3232
shipment = Shipment({'sku1': 1000}, eta=tomorrow)
3333

34-
order.allocate(warehouse=warehouse, shipments=[shipment])
34+
allocation = allocate(order, warehouse=warehouse, shipments=[shipment])
3535

36-
assert order.allocation['sku1'] == shipment
36+
assert allocation['sku1'] == shipment
3737

3838

3939

@@ -42,19 +42,19 @@ def test_can_allocate_to_correct_shipment():
4242
shipment1 = Shipment({'sku1': 1000}, eta=tomorrow)
4343
shipment2 = Shipment({'sku2': 1000}, eta=tomorrow)
4444

45-
order.allocate(warehouse=Warehouse({}), shipments=[shipment1, shipment2])
45+
allocation = allocate(order, warehouse=Warehouse({}), shipments=[shipment1, shipment2])
4646

47-
assert order.allocation['sku2'] == shipment2
47+
assert allocation['sku2'] == shipment2
4848

4949

5050
def test_allocates_to_warehouse_in_preference_to_shipment():
5151
order = Order({'sku1': 10})
5252
warehouse = Warehouse({'sku1': 1000})
5353
shipment = Shipment({'sku1': 1000}, eta=tomorrow)
5454

55-
order.allocate(warehouse, shipments=[shipment])
55+
allocation = allocate(order, warehouse, shipments=[shipment])
5656

57-
assert order.allocation['sku1'] == warehouse
57+
assert allocation['sku1'] == warehouse
5858
assert warehouse['sku1'] == 990
5959
assert shipment['sku1'] == 1000
6060

@@ -63,9 +63,9 @@ def test_can_allocate_multiple_lines_to_wh():
6363
order = Order({'sku1': 5, 'sku2': 10})
6464
warehouse = Warehouse({'sku1': 1000, 'sku2': 1000})
6565

66-
order.allocate(warehouse, shipments=[])
67-
assert order.allocation['sku1'] == warehouse
68-
assert order.allocation['sku2'] == warehouse
66+
allocation = allocate(order, warehouse, shipments=[])
67+
assert allocation['sku1'] == warehouse
68+
assert allocation['sku2'] == warehouse
6969
assert warehouse['sku1'] == 995
7070
assert warehouse['sku2'] == 990
7171

@@ -74,10 +74,10 @@ def test_can_allocate_multiple_lines_to_shipment():
7474
order = Order({'sku1': 5, 'sku2': 10})
7575
shipment = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
7676

77-
order.allocate(warehouse=Warehouse({}), shipments=[shipment])
77+
allocation = allocate(order, warehouse=Warehouse({}), shipments=[shipment])
7878

79-
assert order.allocation['sku1'] == shipment
80-
assert order.allocation['sku2'] == shipment
79+
assert allocation['sku1'] == shipment
80+
assert allocation['sku2'] == shipment
8181
assert shipment['sku1'] == 995
8282
assert shipment['sku2'] == 990
8383

@@ -87,10 +87,10 @@ def test_can_allocate_to_both():
8787
shipment = Shipment({'sku2': 1000}, eta=tomorrow)
8888
warehouse = Warehouse({'sku1': 1000})
8989

90-
order.allocate(warehouse, shipments=[shipment])
90+
allocation = allocate(order, warehouse, shipments=[shipment])
9191

92-
assert order.allocation['sku1'] == warehouse
93-
assert order.allocation['sku2'] == shipment
92+
assert allocation['sku1'] == warehouse
93+
assert allocation['sku2'] == shipment
9494
assert warehouse['sku1'] == 995
9595
assert shipment['sku2'] == 990
9696

@@ -100,12 +100,12 @@ def test_can_allocate_to_both_preferring_warehouse():
100100
shipment = Shipment({'sku1': 1000, 'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
101101
warehouse = Warehouse({'sku3': 1000, 'sku4': 1000})
102102

103-
order.allocate(warehouse, shipments=[shipment])
103+
allocation = allocate(order, warehouse, shipments=[shipment])
104104

105-
assert order.allocation['sku1'] == shipment
106-
assert order.allocation['sku2'] == shipment
107-
assert order.allocation['sku3'] == warehouse
108-
assert order.allocation['sku4'] == warehouse
105+
assert allocation['sku1'] == shipment
106+
assert allocation['sku2'] == shipment
107+
assert allocation['sku3'] == warehouse
108+
assert allocation['sku4'] == warehouse
109109
assert shipment['sku1'] == 999
110110
assert shipment['sku2'] == 998
111111
assert shipment['sku3'] == 1000
@@ -119,10 +119,10 @@ def test_allocated_to_earliest_suitable_shipment_in_list():
119119
shipment2 = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
120120
warehouse = Warehouse({})
121121

122-
order.allocate(warehouse, shipments=[shipment1, shipment2])
122+
allocation = allocate(order, warehouse, shipments=[shipment1, shipment2])
123123

124-
assert order.allocation['sku1'] == shipment1
125-
assert order.allocation['sku2'] == shipment1
124+
assert allocation['sku1'] == shipment1
125+
assert allocation['sku2'] == shipment1
126126

127127

128128
def test_still_chooses_earliest_if_split_across_shipments():
@@ -132,11 +132,11 @@ def test_still_chooses_earliest_if_split_across_shipments():
132132
shipment3 = Shipment({'sku2': 1000, 'sku3': 1000}, eta=later)
133133
warehouse = Warehouse({})
134134

135-
order.allocate(warehouse, shipments=[shipment2, shipment3, shipment1])
135+
allocation = allocate(order, warehouse, shipments=[shipment2, shipment3, shipment1])
136136

137-
assert order.allocation['sku1'] == shipment1
138-
assert order.allocation['sku2'] == shipment2
139-
assert order.allocation['sku3'] == shipment2
137+
assert allocation['sku1'] == shipment1
138+
assert allocation['sku2'] == shipment2
139+
assert allocation['sku3'] == shipment2
140140

141141

142142
def test_warehouse_not_quite_enough_means_we_use_shipment():
@@ -147,26 +147,26 @@ def test_warehouse_not_quite_enough_means_we_use_shipment():
147147
'sku2': 1000,
148148
}, eta=tomorrow)
149149

150-
order.allocate(warehouse, shipments=[shipment])
150+
allocation = allocate(order, warehouse, shipments=[shipment])
151151

152-
assert order.allocation['sku1'] == shipment
153-
assert order.allocation['sku2'] == shipment
152+
assert allocation['sku1'] == shipment
153+
assert allocation['sku2'] == shipment
154154

155155

156156
def test_cannot_allocate_if_insufficent_quantity_in_warehouse():
157157
order = Order({'a-sku': 10})
158158
warehouse = Warehouse({'a-sku': 5})
159159

160-
order.allocate(warehouse, shipments=[])
160+
allocation = allocate(order, warehouse, shipments=[])
161161

162-
assert 'a-sku' not in order.allocation
162+
assert 'a-sku' not in allocation
163163

164164

165165
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
166166
order = Order({'a-sku': 10})
167167
shipment = Shipment({'a-sku': 5}, eta=tomorrow)
168168

169-
order.allocate(warehouse=Warehouse({}), shipments=[shipment])
169+
allocation = allocate(order, warehouse=Warehouse({}), shipments=[shipment])
170170

171-
assert 'a-sku' not in order.allocation
171+
assert 'a-sku' not in allocation
172172

0 commit comments

Comments
 (0)