Skip to content

Commit bb9c5b9

Browse files
committed
more on classes
1 parent b089c1b commit bb9c5b9

File tree

2 files changed

+84
-78
lines changed

2 files changed

+84
-78
lines changed

domain_model.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ class Order:
66
lines: dict
77
allocations: dict = None
88

9+
@property
10+
def fully_allocated(self):
11+
return set(self.allocations.keys()) == set(self.lines.keys())
12+
13+
914
def allocate(self, stock, shipments):
10-
self.allocations = {
11-
key: stock
12-
for key in self.lines
13-
}
14-
for s in shipments:
15+
self.allocations = {}
16+
for source in [stock] + shipments:
1517
self.allocations.update({
16-
key: s
18+
key: stock
1719
for key in self.lines
20+
if key in source.lines
1821
})
22+
if self.fully_allocated:
23+
return
1924

2025

2126
@dataclass

test_allocation.py

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_can_allocate_to_shipment():
1414
order = Order({'a-sku': 10})
1515
shipment = Stock({'a-sku': 1000})
1616

17-
order.allocate(stock={}, shipments=[shipment])
17+
order.allocate(stock=Stock({}), shipments=[shipment])
1818

1919
assert order.allocations['a-sku'] == shipment
2020

@@ -29,139 +29,140 @@ def test_ignores_irrelevant_stock():
2929
assert order.allocations['sku1'] == shipment
3030

3131

32-
'''
3332

3433
def test_can_allocate_to_correct_shipment():
35-
order = {'sku2': 10}
36-
shipment1 = {'sku1': 1000}
37-
shipment2 = {'sku2': 1000}
34+
order = Order({'sku2': 10})
35+
shipment1 = Stock({'sku1': 1000})
36+
shipment2 = Stock({'sku2': 1000})
3837

39-
allocations = allocate(order, stock={}, shipments=[shipment1, shipment2])
38+
order.allocate(stock=Stock({}), shipments=[shipment1, shipment2])
4039

41-
assert allocations['sku2'] == shipment2
40+
assert order.allocations['sku2'] == shipment2
4241

4342

4443
def test_allocates_to_stock_in_preference_to_shipment():
45-
order = {'sku1': 10}
46-
stock = {'sku1': 1000}
47-
shipment = {'sku1': 1000}
44+
order = Order({'sku1': 10})
45+
stock = Stock({'sku1': 1000})
46+
shipment = Stock({'sku1': 1000})
4847

49-
allocations = allocate(order, stock, shipments=[shipment])
48+
order.allocate(stock, shipments=[shipment])
5049

51-
assert allocations['sku1'] == stock
50+
assert order.allocations['sku1'] == stock
5251

5352

5453
def test_can_allocate_multiple_lines_to_wh():
55-
order = {'sku1': 5, 'sku2': 10}
56-
stock = {'sku1': 1000, 'sku2': 1000}
54+
order = Order({'sku1': 5, 'sku2': 10})
55+
stock = Stock({'sku1': 1000, 'sku2': 1000})
5756

58-
allocations = allocate(order, stock, shipments=[])
59-
assert allocations['sku1'] == stock
60-
assert allocations['sku2'] == stock
57+
order.allocate(stock, shipments=[])
58+
assert order.allocations['sku1'] == stock
59+
assert order.allocations['sku2'] == stock
6160

6261

6362
def test_can_allocate_multiple_lines_to_shipment():
64-
order = {'sku1': 5, 'sku2': 10}
65-
shipment = {'sku1': 1000, 'sku2': 1000}
63+
order = Order({'sku1': 5, 'sku2': 10})
64+
shipment = Stock({'sku1': 1000, 'sku2': 1000})
6665

67-
allocations = allocate(order, stock={}, shipments=[shipment])
66+
order.allocate(stock=Stock({}), shipments=[shipment])
6867

69-
assert allocations['sku1'] == shipment
70-
assert allocations['sku2'] == shipment
68+
assert order.allocations['sku1'] == shipment
69+
assert order.allocations['sku2'] == shipment
7170

7271

7372
def test_can_allocate_to_both():
74-
order = {'sku1': 5, 'sku2': 10}
75-
shipment = {'sku2': 1000}
76-
stock = {'sku1': 1000}
73+
order = Order({'sku1': 5, 'sku2': 10})
74+
shipment = Stock({'sku2': 1000})
75+
stock = Stock({'sku1': 1000})
7776

78-
allocations = allocate(order, stock, shipments=[shipment])
77+
order.allocate(stock, shipments=[shipment])
7978

80-
assert allocations['sku1'] == stock
81-
assert allocations['sku2'] == shipment
79+
assert order.allocations['sku1'] == stock
80+
assert order.allocations['sku2'] == shipment
8281

8382

8483
def test_can_allocate_to_both_preferring_stock():
85-
order = {'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4}
86-
shipment = {'sku1': 1000, 'sku2': 1000, 'sku3': 1000}
87-
stock = {'sku3': 1000, 'sku4': 1000}
84+
order = Order({'sku1': 1, 'sku2': 2, 'sku3': 3, 'sku4': 4})
85+
shipment = Stock({'sku1': 1000, 'sku2': 1000, 'sku3': 1000})
86+
stock = Stock({'sku3': 1000, 'sku4': 1000})
87+
88+
order.allocate(stock, shipments=[shipment])
8889

89-
allocations = allocate(order, stock, shipments=[shipment])
90+
assert order.allocations['sku1'] == shipment
91+
assert order.allocations['sku2'] == shipment
92+
assert order.allocations['sku3'] == stock
93+
assert order.allocations['sku4'] == stock
9094

91-
assert allocations['sku1'] == shipment
92-
assert allocations['sku2'] == shipment
93-
assert allocations['sku3'] == stock
94-
assert allocations['sku4'] == stock
9595

9696

9797
def test_mixed_allocations_are_avoided_if_possible():
98-
order = {'sku1': 10, 'sku2': 10}
99-
shipment = {'sku1': 1000, 'sku2': 1000}
100-
stock = {'sku1': 1000}
98+
order = Order({'sku1': 10, 'sku2': 10})
99+
shipment = Stock({'sku1': 1000, 'sku2': 1000})
100+
stock = Stock({'sku1': 1000})
101101

102-
allocations = allocate(order, stock, shipments=[shipment])
102+
order.allocate(stock, shipments=[shipment])
103103

104-
assert allocations['sku1'] == shipment
105-
assert allocations['sku2'] == shipment
104+
assert order.allocations['sku1'] == shipment
105+
assert order.allocations['sku2'] == shipment
106106

107107

108108
def test_allocated_to_first_suitable_shipment_in_list():
109-
order = {'sku1': 10, 'sku2': 10}
110-
shipment1 = {'sku1': 1000, 'sku2': 1000}
111-
shipment2 = {'sku1': 1000, 'sku2': 1000}
112-
stock = {}
109+
order = Order({'sku1': 10, 'sku2': 10})
110+
shipment1 = Stock({'sku1': 1000, 'sku2': 1000})
111+
shipment2 = Stock({'sku1': 1000, 'sku2': 1000})
112+
stock = Stock({})
113113

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

116-
assert allocations['sku1'] == shipment1
117-
assert allocations['sku2'] == shipment1
116+
assert order.allocations['sku1'] == shipment1
117+
assert order.allocations['sku2'] == shipment1
118118

119119

120120
def test_still_preserves_ordering_if_split_across_shipments():
121-
order = {'sku1': 10, 'sku2': 10, 'sku3': 10}
122-
shipment1 = {'sku1': 1000}
123-
shipment2 = {'sku2': 1000, 'sku3': 1000}
124-
shipment3 = {'sku2': 1000, 'sku3': 1000}
125-
stock = {}
121+
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})
125+
stock = Stock({})
126126

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

129-
assert allocations['sku1'] == shipment1
130-
assert allocations['sku2'] == shipment2
131-
assert allocations['sku3'] == shipment2
129+
assert order.allocations['sku1'] == shipment1
130+
assert order.allocations['sku2'] == shipment2
131+
assert order.allocations['sku3'] == shipment2
132132

133+
'''
133134
134135
def test_stock_not_quite_enough_means_we_use_shipment():
135-
order = {'sku1': 10, 'sku2': 10}
136-
stock = {'sku1': 10, 'sku2': 5}
137-
shipment = {
136+
order = Order({'sku1': 10, 'sku2': 10})
137+
stock = Stock({'sku1': 10, 'sku2': 5})
138+
shipment = Stock({)
138139
'sku1': 1000,
139140
'sku2': 1000,
140141
}
141142
142-
allocations = allocate(order, stock, shipments=[shipment])
143+
order.allocate(stock, shipments=[shipment])
143144
144-
assert allocations['sku1'] == shipment
145-
assert allocations['sku2'] == shipment
145+
assert order.allocations['sku1'] == shipment
146+
assert order.allocations['sku2'] == shipment
146147
147148
148149
def test_cannot_allocate_if_insufficent_quantity_in_stock():
149-
order = {'a-sku': 10}
150-
stock = {'a-sku': 5}
150+
order = Order({'a-sku': 10})
151+
stock = Stock({'a-sku': 5})
151152
152-
allocations = allocate(order, stock, shipments=[])
153+
order.allocate(stock, shipments=[])
153154
154-
assert 'a-sku' not in allocations
155+
assert 'a-sku' not in order.allocations
155156
156157
157158
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
158-
order = {'a-sku': 10}
159-
shipment = {
159+
order = Order({'a-sku': 10})
160+
shipment = Stock({)
160161
'a-sku': 5,
161162
}
162163
163-
allocations = allocate(order, stock={}, shipments=[shipment])
164+
order.allocate(stock=Stock({}), shipments=[shipment])
164165
165-
assert 'a-sku' not in allocations
166+
assert 'a-sku' not in order.allocations
166167
167168
'''

0 commit comments

Comments
 (0)