Skip to content

Commit e17c944

Browse files
committed
change_batch_quantity on product, needed change to batch, also emit allocated event. [change_batch_model_layer]
1 parent ec4e4a9 commit e17c944

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/allocation/model.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@ def allocate(self, line: OrderLine) -> str:
1919
b for b in sorted(self.batches) if b.can_allocate(line)
2020
)
2121
batch.allocate(line)
22+
self.events.append(events.Allocated(
23+
line.orderid, line.sku, line.qty, batch.reference
24+
))
2225
self.version_number += 1
2326
return batch.reference
2427
except StopIteration:
2528
self.events.append(events.OutOfStock(line.sku))
2629
return None
2730

31+
def change_batch_quantity(self, ref: str, qty: int):
32+
batch = next(b for b in self.batches if b.reference == ref)
33+
batch._purchased_quantity = qty
34+
while batch.available_quantity < 0:
35+
line = batch.deallocate_one()
36+
self.events.append(
37+
events.Deallocated(line.orderid, line.sku, line.qty)
38+
)
2839

2940
@dataclass(unsafe_hash=True)
3041
class OrderLine:
@@ -65,9 +76,8 @@ def allocate(self, line: OrderLine):
6576
if self.can_allocate(line):
6677
self._allocations.add(line)
6778

68-
def deallocate(self, line: OrderLine):
69-
if line in self._allocations:
70-
self._allocations.remove(line)
79+
def deallocate_one(self) -> OrderLine:
80+
return self._allocations.pop()
7181

7282
@property
7383
def allocated_quantity(self) -> int:

tests/unit/test_batches.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,3 @@ def test_allocation_is_idempotent():
3939
batch.allocate(line)
4040
batch.allocate(line)
4141
assert batch.available_quantity == 18
42-
43-
def test_deallocate():
44-
batch, line = make_batch_and_line("EXPENSIVE-FOOTSTOOL", 20, 2)
45-
batch.allocate(line)
46-
batch.deallocate(line)
47-
assert batch.available_quantity == 20
48-
49-
def test_can_only_deallocate_allocated_lines():
50-
batch, unallocated_line = make_batch_and_line("DECORATIVE-TRINKET", 20, 2)
51-
batch.deallocate(unallocated_line)
52-
assert batch.available_quantity == 20
53-

0 commit comments

Comments
 (0)