Skip to content

Commit b089c1b

Browse files
committed
start on a classy version
1 parent 27b3362 commit b089c1b

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

domain_model.py

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1-
def skus(d):
2-
return set(d.keys())
1+
from dataclasses import dataclass
2+
from datetime import date
3+
4+
@dataclass
5+
class Order:
6+
lines: dict
7+
allocations: dict = None
8+
9+
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.update({
16+
key: s
17+
for key in self.lines
18+
})
19+
20+
21+
@dataclass
22+
class Stock:
23+
lines: dict
24+
eta: date = None
325

4-
def allocated_completely(order, allocation):
5-
return skus(order) == skus(allocation)
6-
7-
8-
def allocate_to(order, source):
9-
return {
10-
sku: source
11-
for sku, quantity in order.items()
12-
if sku in source
13-
and source[sku] > quantity
14-
}
15-
16-
17-
def merge(allocations):
18-
return {
19-
k: v
20-
for d in allocations
21-
for k, v in d.items()
22-
}
23-
24-
25-
26-
def allocate(order, stock, shipments):
27-
allocations = []
28-
for source in [stock] + shipments:
29-
allocation = allocate_to(order, source)
30-
if allocated_completely(order, allocation):
31-
return allocation
32-
allocations.append(allocation)
33-
34-
return merge(reversed(allocations))
3526

test_allocation.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
from domain_model import allocate
1+
from domain_model import Order, Stock
22

33

44
def test_can_allocate_to_stock():
5-
order = {'a-sku': 10}
6-
stock = {'a-sku': 1000}
5+
order = Order({'a-sku': 10})
6+
stock = Stock({'a-sku': 1000})
77

8-
allocations = allocate(order, stock, shipments=[])
8+
order.allocate(stock, shipments=[])
99

10-
assert allocations['a-sku'] == stock
10+
assert order.allocations['a-sku'] == stock
1111

1212

1313
def test_can_allocate_to_shipment():
14-
order = {'a-sku': 10}
15-
shipment = {'a-sku': 1000}
14+
order = Order({'a-sku': 10})
15+
shipment = Stock({'a-sku': 1000})
1616

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

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

2121

2222
def test_ignores_irrelevant_stock():
23-
order = {'sku1': 10}
24-
stock = {'sku2': 1000}
25-
shipment = {'sku1': 1000}
23+
order = Order({'sku1': 10})
24+
stock = Stock({'sku2': 1000})
25+
shipment = Stock({'sku1': 1000})
2626

27-
allocations = allocate(order, stock=stock, shipments=[shipment])
27+
order.allocate(stock=stock, shipments=[shipment])
28+
29+
assert order.allocations['sku1'] == shipment
2830

29-
assert allocations['sku1'] == shipment
3031

32+
'''
3133
3234
def test_can_allocate_to_correct_shipment():
3335
order = {'sku2': 10}
@@ -162,3 +164,4 @@ def test_cannot_allocate_if_insufficent_quantity_in_shipment():
162164
163165
assert 'a-sku' not in allocations
164166
167+
'''

0 commit comments

Comments
 (0)