Skip to content

Commit cde82ba

Browse files
committed
check allocation actually decrements quantities
1 parent 4472358 commit cde82ba

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎domain_model.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def for_(order, source):
1212
if source.can_allocate(sku, quantity)
1313
})
1414

15-
1615
def supplement_with(self, allocation):
1716
for sku, quantity in allocation.items():
1817
if sku in self:
@@ -24,7 +23,6 @@ def fully_allocates(self, order):
2423

2524

2625

27-
2826
class Order(dict):
2927

3028
@property
@@ -41,15 +39,26 @@ def allocate(self, stock, shipments):
4139
source_allocation = Allocation.for_(self, source)
4240
if source_allocation.fully_allocates(self):
4341
self.allocation = source_allocation
42+
self.apply_allocation()
4443
return
4544
self.allocation.supplement_with(source_allocation)
45+
self.apply_allocation()
46+
47+
def apply_allocation(self):
48+
for sku, source in self.allocation.items():
49+
print('decrementing source {source} by {self[sku]} for {sku}')
50+
source[sku] -= self[sku]
51+
4652

4753

4854
class Stock(dict):
4955

5056
def can_allocate(self, sku, quantity):
5157
return sku in self and self[sku] > quantity
5258

59+
def allocate(self, sku, quantity):
60+
self[sku] -= quantity
61+
5362

5463
class Shipment(Stock):
5564

‎test_allocation.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def test_can_allocate_to_stock():
1313
order.allocate(stock, shipments=[])
1414

1515
assert order.allocation['a-sku'] == stock
16+
assert stock['a-sku'] == 990
1617

1718

1819
def test_can_allocate_to_shipment():
@@ -22,6 +23,7 @@ def test_can_allocate_to_shipment():
2223
order.allocate(stock=Stock({}), shipments=[shipment])
2324

2425
assert order.allocation['a-sku'] == shipment
26+
assert shipment['a-sku'] == 990
2527

2628

2729
def test_ignores_irrelevant_stock():
@@ -53,6 +55,8 @@ def test_allocates_to_stock_in_preference_to_shipment():
5355
order.allocate(stock, shipments=[shipment])
5456

5557
assert order.allocation['sku1'] == stock
58+
assert stock['sku1'] == 990
59+
assert shipment['sku1'] == 1000
5660

5761

5862
def test_can_allocate_multiple_lines_to_wh():
@@ -62,6 +66,8 @@ def test_can_allocate_multiple_lines_to_wh():
6266
order.allocate(stock, shipments=[])
6367
assert order.allocation['sku1'] == stock
6468
assert order.allocation['sku2'] == stock
69+
assert stock['sku1'] == 995
70+
assert stock['sku2'] == 990
6571

6672

6773
def test_can_allocate_multiple_lines_to_shipment():
@@ -72,6 +78,8 @@ def test_can_allocate_multiple_lines_to_shipment():
7278

7379
assert order.allocation['sku1'] == shipment
7480
assert order.allocation['sku2'] == shipment
81+
assert shipment['sku1'] == 995
82+
assert shipment['sku2'] == 990
7583

7684

7785
def test_can_allocate_to_both():
@@ -83,6 +91,8 @@ def test_can_allocate_to_both():
8391

8492
assert order.allocation['sku1'] == stock
8593
assert order.allocation['sku2'] == shipment
94+
assert stock['sku1'] == 995
95+
assert shipment['sku2'] == 990
8696

8797

8898
def test_can_allocate_to_both_preferring_stock():
@@ -96,6 +106,11 @@ def test_can_allocate_to_both_preferring_stock():
96106
assert order.allocation['sku2'] == shipment
97107
assert order.allocation['sku3'] == stock
98108
assert order.allocation['sku4'] == stock
109+
assert shipment['sku1'] == 999
110+
assert shipment['sku2'] == 998
111+
assert shipment['sku3'] == 1000
112+
assert stock['sku3'] == 997
113+
assert stock['sku4'] == 996
99114

100115

101116
def test_mixed_allocation_are_avoided_if_possible():

0 commit comments

Comments
 (0)