Skip to content

Commit dc06fe1

Browse files
committed
separate out a shipment class, remove lines attr
1 parent f7e4279 commit dc06fe1

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

domain_model.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ def allocate(self, stock, shipments):
3737
self.allocation = allocation
3838

3939

40+
class Stock(dict):
41+
42+
def can_allocate(self, sku, quantity):
43+
return sku in self and self[sku] > quantity
44+
45+
4046
@dataclass
41-
class Stock:
42-
lines: dict
43-
eta: date = None
47+
class Shipment(Stock):
48+
49+
def __init__(self, lines, eta):
50+
self.eta = eta
51+
super().__init__(lines)
4452

4553
def can_allocate(self, sku, quantity):
46-
return sku in self.lines and self.lines[sku] > quantity
54+
return sku in self and self[sku] > quantity
4755

test_allocation.py

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

44
today = date.today()
@@ -17,7 +17,7 @@ def test_can_allocate_to_stock():
1717

1818
def test_can_allocate_to_shipment():
1919
order = Order({'a-sku': 10})
20-
shipment = Stock({'a-sku': 1000}, eta=tomorrow)
20+
shipment = Shipment({'a-sku': 1000}, eta=tomorrow)
2121

2222
order.allocate(stock=Stock({}), shipments=[shipment])
2323

@@ -27,7 +27,7 @@ def test_can_allocate_to_shipment():
2727
def test_ignores_irrelevant_stock():
2828
order = Order({'sku1': 10})
2929
stock = Stock({'sku2': 1000})
30-
shipment = Stock({'sku1': 1000}, eta=tomorrow)
30+
shipment = Shipment({'sku1': 1000}, eta=tomorrow)
3131

3232
order.allocate(stock=stock, shipments=[shipment])
3333

@@ -37,8 +37,8 @@ def test_ignores_irrelevant_stock():
3737

3838
def test_can_allocate_to_correct_shipment():
3939
order = Order({'sku2': 10})
40-
shipment1 = Stock({'sku1': 1000}, eta=tomorrow)
41-
shipment2 = Stock({'sku2': 1000}, eta=tomorrow)
40+
shipment1 = Shipment({'sku1': 1000}, eta=tomorrow)
41+
shipment2 = Shipment({'sku2': 1000}, eta=tomorrow)
4242

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

@@ -48,7 +48,7 @@ def test_can_allocate_to_correct_shipment():
4848
def test_allocates_to_stock_in_preference_to_shipment():
4949
order = Order({'sku1': 10})
5050
stock = Stock({'sku1': 1000})
51-
shipment = Stock({'sku1': 1000}, eta=tomorrow)
51+
shipment = Shipment({'sku1': 1000}, eta=tomorrow)
5252

5353
order.allocate(stock, shipments=[shipment])
5454

@@ -66,7 +66,7 @@ def test_can_allocate_multiple_lines_to_wh():
6666

6767
def test_can_allocate_multiple_lines_to_shipment():
6868
order = Order({'sku1': 5, 'sku2': 10})
69-
shipment = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
69+
shipment = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
7070

7171
order.allocate(stock=Stock({}), shipments=[shipment])
7272

@@ -76,7 +76,7 @@ def test_can_allocate_multiple_lines_to_shipment():
7676

7777
def test_can_allocate_to_both():
7878
order = Order({'sku1': 5, 'sku2': 10})
79-
shipment = Stock({'sku2': 1000}, eta=tomorrow)
79+
shipment = Shipment({'sku2': 1000}, eta=tomorrow)
8080
stock = Stock({'sku1': 1000})
8181

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

8888
def test_can_allocate_to_both_preferring_stock():
8989
order = Order({'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4})
90-
shipment = Stock({'sku1': 1000, 'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
90+
shipment = Shipment({'sku1': 1000, 'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
9191
stock = Stock({'sku3': 1000, 'sku4': 1000})
9292

9393
order.allocate(stock, shipments=[shipment])
@@ -100,7 +100,7 @@ def test_can_allocate_to_both_preferring_stock():
100100

101101
def test_mixed_allocation_are_avoided_if_possible():
102102
order = Order({'sku1': 10, 'sku2': 10})
103-
shipment = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
103+
shipment = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
104104
stock = Stock({'sku1': 1000})
105105

106106
order.allocate(stock, shipments=[shipment])
@@ -111,8 +111,8 @@ def test_mixed_allocation_are_avoided_if_possible():
111111

112112
def test_allocated_to_earliest_suitable_shipment_in_list():
113113
order = Order({'sku1': 10, 'sku2': 10})
114-
shipment1 = Stock({'sku1': 1000, 'sku2': 1000}, eta=today)
115-
shipment2 = Stock({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
114+
shipment1 = Shipment({'sku1': 1000, 'sku2': 1000}, eta=today)
115+
shipment2 = Shipment({'sku1': 1000, 'sku2': 1000}, eta=tomorrow)
116116
stock = Stock({})
117117

118118
order.allocate(stock, shipments=[shipment2, shipment1])
@@ -123,9 +123,9 @@ def test_allocated_to_earliest_suitable_shipment_in_list():
123123

124124
def test_still_chooses_earliest_if_split_across_shipments():
125125
order = Order({'sku1': 10, 'sku2': 10, 'sku3': 10})
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)
126+
shipment1 = Shipment({'sku1': 1000}, eta=today)
127+
shipment2 = Shipment({'sku2': 1000, 'sku3': 1000}, eta=tomorrow)
128+
shipment3 = Shipment({'sku2': 1000, 'sku3': 1000}, eta=later)
129129
stock = Stock({})
130130

131131
order.allocate(stock, shipments=[shipment3, shipment2, shipment1])
@@ -138,10 +138,10 @@ def test_still_chooses_earliest_if_split_across_shipments():
138138
def test_stock_not_quite_enough_means_we_use_shipment():
139139
order = Order({'sku1': 10, 'sku2': 10})
140140
stock = Stock({'sku1': 10, 'sku2': 5})
141-
shipment = Stock({
141+
shipment = Shipment({
142142
'sku1': 1000,
143143
'sku2': 1000,
144-
})
144+
}, eta=tomorrow)
145145

146146
order.allocate(stock, shipments=[shipment])
147147

@@ -160,7 +160,7 @@ def test_cannot_allocate_if_insufficent_quantity_in_stock():
160160

161161
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
162162
order = Order({'a-sku': 10})
163-
shipment = Stock({'a-sku': 5}, eta=tomorrow)
163+
shipment = Shipment({'a-sku': 5}, eta=tomorrow)
164164

165165
order.allocate(stock=Stock({}), shipments=[shipment])
166166

0 commit comments

Comments
 (0)