Skip to content

Commit 93d5afa

Browse files
committed
start refactoring towards dicts
1 parent a6a3501 commit 93d5afa

File tree

2 files changed

+83
-74
lines changed

2 files changed

+83
-74
lines changed

domain_model.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,40 @@ class Line:
66
quantity: int
77

88

9+
10+
911
@dataclass
1012
class OrderLine(Line):
1113
allocation: str = None
1214

1315

1416
@dataclass
15-
class Shipment(list):
17+
class Shipment(dict):
1618
def __init__(self, id, eta, lines):
1719
self.id = id
1820
self.eta = eta
1921
super().__init__(lines)
2022

2123

2224
def skus(thing):
23-
return {line.sku for line in thing}
25+
try:
26+
return {line.sku for line in thing}
27+
except:
28+
return thing.keys()
2429

2530

2631
def allocate_to(line, allocation, source):
27-
for source_line in source:
28-
if source_line.sku == line.sku and source_line.quantity > line.quantity:
32+
try:
33+
if source.get(line.sku, 0) > line.quantity:
2934
line.allocation = allocation
30-
source_line.quantity -= line.quantity
31-
return
35+
source[line.sku] -= line.quantity
36+
37+
except AttributeError:
38+
for source_line in source:
39+
if source_line.sku == line.sku and source_line.quantity > line.quantity:
40+
line.allocation = allocation
41+
source_line.quantity -= line.quantity
42+
return
3243

3344
def allocate_to_stock(order, stock):
3445
for line in order:

test_allocation.py

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,59 +28,58 @@ def test_can_allocate_to_stock():
2828

2929
def test_can_allocate_to_shipment():
3030
order = [OrderLine(sku='a-sku', quantity=10)]
31-
shipment = Shipment(id='shipment-id', eta=date.today(), lines=[
32-
Line(sku='a-sku', quantity=1000),
33-
])
31+
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
32+
'a-sku': 1000
33+
})
3434

3535
allocate(order, stock=[], shipments=[shipment])
3636

3737
assert order[0].allocation == shipment.id
38-
assert shipment[0].quantity == 990
38+
assert shipment['a-sku'] == 990
3939

4040

4141
def test_ignores_invalid_stock():
4242
order = [OrderLine(sku='sku1', quantity=10), ]
4343
stock = [Line(sku='sku2', quantity=1000)]
44-
shipment = Shipment(id='shipment-id', eta=date.today(), lines=[
45-
Line(sku='sku1', quantity=1000),
46-
])
44+
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
45+
'sku1': 1000,
46+
})
4747

4848
allocate(order, stock=stock, shipments=[shipment])
4949

5050
assert order[0].allocation == shipment.id
5151
assert stock[0].quantity == 1000
52-
assert shipment[0].quantity == 990
52+
assert shipment['sku1'] == 990
5353

5454

5555
def test_can_allocate_to_correct_shipment():
5656
order = [OrderLine(sku='sku2', quantity=10)]
57-
shipment1 = Shipment('shipment1', eta=date.today(), lines=[
58-
Line(sku='sku1', quantity=1000),
59-
])
60-
shipment2 = Shipment('shipment2', eta=date.today(), lines=[
61-
Line(sku='sku2', quantity=1000),
62-
])
57+
shipment1 = Shipment('shipment1', eta=date.today(), lines={
58+
'sku1': 1000,
59+
})
60+
shipment2 = Shipment('shipment2', eta=date.today(), lines={
61+
'sku2': 1000,
62+
})
6363

6464
allocate(order, stock=[], shipments=[shipment1, shipment2])
6565

6666
assert order[0].allocation == shipment2.id
67-
assert shipment1[0].quantity == 1000
68-
assert shipment2[0].quantity == 990
67+
assert shipment1['sku1'] == 1000
68+
assert shipment2['sku2'] == 990
6969

7070

7171
def test_allocates_to_stock_in_preference_to_shipment():
72-
sku = random_id()
73-
order = [OrderLine(sku=sku, quantity=10)]
74-
stock = [Line(sku=sku, quantity=1000)]
75-
shipment = Shipment('shipment1', eta=date.today(), lines=[
76-
Line(sku=sku, quantity=1000),
77-
])
72+
order = [OrderLine(sku='sku1', quantity=10)]
73+
stock = [Line(sku='sku1', quantity=1000)]
74+
shipment = Shipment('shipment1', eta=date.today(), lines={
75+
'sku1': 1000,
76+
})
7877

7978
allocate(order, stock, shipments=[shipment])
8079

8180
assert order[0].allocation == 'STOCK'
8281
assert stock[0].quantity == 990
83-
assert shipment[0].quantity == 1000
82+
assert shipment['sku1'] == 1000
8483

8584

8685
def test_can_allocate_multiple_lines_to_wh():
@@ -105,35 +104,35 @@ def test_can_allocate_multiple_lines_to_shipment():
105104
OrderLine(sku='sku1', quantity=5),
106105
OrderLine(sku='sku2', quantity=10),
107106
]
108-
shipment = Shipment('shipment1', eta=date.today(), lines=[
109-
Line(sku='sku1', quantity=1000),
110-
Line(sku='sku2', quantity=1000),
111-
])
107+
shipment = Shipment('shipment1', eta=date.today(), lines={
108+
'sku1': 1000,
109+
'sku2': 1000,
110+
})
112111

113112
allocate(order, [], shipments=[shipment])
114113

115114
assert order[0].allocation == shipment.id
116115
assert order[1].allocation == shipment.id
117-
assert shipment[0].quantity == 995
118-
assert shipment[1].quantity == 990
116+
assert shipment['sku1'] == 995
117+
assert shipment['sku2'] == 990
119118

120119

121120
def test_can_allocate_to_both():
122121
order = [
123122
OrderLine(sku='sku1', quantity=5),
124123
OrderLine(sku='sku2', quantity=10),
125124
]
126-
shipment = Shipment('shipment1', eta=date.today(), lines=[
127-
Line(sku='sku2', quantity=1000),
128-
])
125+
shipment = Shipment('shipment1', eta=date.today(), lines={
126+
'sku2': 1000,
127+
})
129128
stock = [Line(sku='sku1', quantity=1000)]
130129

131130
allocate(order, stock, shipments=[shipment])
132131

133132
assert order[0].allocation == 'STOCK'
134133
assert order[1].allocation == shipment.id
135134
assert stock[0].quantity == 995
136-
assert shipment[0].quantity == 990
135+
assert shipment['sku2'] == 990
137136

138137

139138
def test_can_allocate_to_both_preferring_stock():
@@ -143,11 +142,11 @@ def test_can_allocate_to_both_preferring_stock():
143142
OrderLine(sku='sku3', quantity=3),
144143
OrderLine(sku='sku4', quantity=4),
145144
]
146-
shipment = Shipment('shipment1', eta=date.today(), lines=[
147-
Line(sku='sku1', quantity=1000),
148-
Line(sku='sku2', quantity=1000),
149-
Line(sku='sku3', quantity=1000),
150-
])
145+
shipment = Shipment('shipment1', eta=date.today(), lines={
146+
'sku1': 1000,
147+
'sku2': 1000,
148+
'sku3': 1000,
149+
})
151150
stock = [
152151
Line(sku='sku3', quantity=1000),
153152
Line(sku='sku4', quantity=1000),
@@ -159,10 +158,9 @@ def test_can_allocate_to_both_preferring_stock():
159158
assert order[1].allocation == shipment.id
160159
assert order[2].allocation == 'STOCK'
161160
assert order[3].allocation == 'STOCK'
162-
assert shipment[0].quantity == 999
163-
assert shipment[1].quantity == 998
164-
assert shipment[2].quantity == 1000
165-
assert shipment[3].quantity == 1000
161+
assert shipment['sku1'] == 999
162+
assert shipment['sku2'] == 998
163+
assert shipment['sku3'] == 1000
166164
assert stock[0].quantity == 997
167165
assert stock[1].quantity == 996
168166

@@ -172,10 +170,10 @@ def test_mixed_allocations_are_avoided_if_possible():
172170
OrderLine(sku='sku1', quantity=10),
173171
OrderLine(sku='sku2', quantity=10),
174172
]
175-
shipment = Shipment('shipment1', eta=date.today(), lines=[
176-
Line(sku='sku1', quantity=1000),
177-
Line(sku='sku2', quantity=1000),
178-
])
173+
shipment = Shipment('shipment1', eta=date.today(), lines={
174+
'sku1': 1000,
175+
'sku2': 1000,
176+
})
179177
stock = [Line(sku='sku1', quantity=1000)]
180178

181179
allocate(order, stock, shipments=[shipment])
@@ -189,15 +187,15 @@ def test_prefer_allocating_to_earlier_shipment():
189187
OrderLine(sku='sku1', quantity=10),
190188
OrderLine(sku='sku2', quantity=10),
191189
]
192-
shipment1 = Shipment('shipment1', eta=date.today(), lines=[
193-
Line(sku='sku1', quantity=1000),
194-
Line(sku='sku2', quantity=1000),
195-
])
190+
shipment1 = Shipment('shipment1', eta=date.today(), lines={
191+
'sku1': 1000,
192+
'sku2': 1000,
193+
})
196194
tomorrow = date.today() + timedelta(days=1)
197-
shipment2 = Shipment('shipment2', eta=tomorrow, lines=[
198-
Line(sku='sku1', quantity=1000),
199-
Line(sku='sku2', quantity=1000),
200-
])
195+
shipment2 = Shipment('shipment2', eta=tomorrow, lines={
196+
'sku1': 1000,
197+
'sku2': 1000,
198+
})
201199
stock = []
202200

203201
allocate(order, stock, shipments=[shipment2, shipment1])
@@ -212,19 +210,19 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
212210
OrderLine(sku='sku2', quantity=10),
213211
OrderLine(sku='sku3', quantity=10),
214212
]
215-
shipment1 = Shipment(id='shipment1', eta=date.today(), lines=[
216-
Line(sku='sku1', quantity=1000),
217-
])
213+
shipment1 = Shipment(id='shipment1', eta=date.today(), lines={
214+
'sku1': 1000,
215+
})
218216
tomorrow = date.today() + timedelta(days=1)
219-
shipment2 = Shipment(id='shipment2', eta=tomorrow, lines=[
220-
Line(sku='sku2', quantity=1000),
221-
Line(sku='sku3', quantity=1000),
222-
])
217+
shipment2 = Shipment(id='shipment2', eta=tomorrow, lines={
218+
'sku2': 1000,
219+
'sku3': 1000,
220+
})
223221
later = tomorrow + timedelta(days=1)
224-
shipment3 = Shipment(id='shipment3', eta=later, lines=[
225-
Line(sku='sku2', quantity=1000),
226-
Line(sku='sku3', quantity=1000),
227-
])
222+
shipment3 = Shipment(id='shipment3', eta=later, lines={
223+
'sku2': 1000,
224+
'sku3': 1000,
225+
})
228226
stock = []
229227

230228
allocate(order, stock, shipments=[shipment3, shipment2, shipment1])
@@ -244,9 +242,9 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
244242

245243
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
246244
order = [OrderLine(sku='a-sku', quantity=10)]
247-
shipment = Shipment(id='shipment-id', eta=date.today(), lines=[
248-
Line(sku='a-sku', quantity=5),
249-
])
245+
shipment = Shipment(id='shipment-id', eta=date.today(), lines={
246+
'a-sku': 5,
247+
})
250248

251249
allocate(order, stock=[], shipments=[shipment])
252250

0 commit comments

Comments
 (0)