Skip to content

Commit 63130e7

Browse files
committed
first cut of out of stock event [domain_event]
1 parent 08cb29f commit 63130e7

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/allocation/events.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dataclasses import dataclass
2+
3+
class Event:
4+
pass
5+
6+
@dataclass
7+
class OutOfStock(Event):
8+
sku: str
9+

src/allocation/model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33
from datetime import date
44
from typing import Optional, List, Set
5-
from allocation import exceptions
5+
from allocation import events
66

77

88
class Product:
@@ -11,6 +11,7 @@ def __init__(self, sku: str, batches: List[Batch], version_number: int = 0):
1111
self.sku = sku
1212
self.batches = batches
1313
self.version_number = version_number
14+
self.events = [] # type: List[events.Event]
1415

1516
def allocate(self, line: OrderLine) -> str:
1617
try:
@@ -21,7 +22,9 @@ def allocate(self, line: OrderLine) -> str:
2122
self.version_number += 1
2223
return batch.reference
2324
except StopIteration:
24-
raise exceptions.OutOfStock(f'Out of stock for sku {line.sku}')
25+
self.events.append(events.OutOfStock(line.sku))
26+
# raise exceptions.OutOfStock(f'Out of stock for sku {line.sku}')
27+
return None
2528

2629

2730
@dataclass(unsafe_hash=True)

tests/unit/test_product.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import date, timedelta
22
import pytest
3-
from allocation import exceptions
3+
from allocation import events, exceptions
44
from allocation.model import Product, OrderLine, Batch
55

66
today = date.today()
@@ -51,6 +51,16 @@ def test_raises_out_of_stock_exception_if_cannot_allocate():
5151
product.allocate(different_sku_line)
5252

5353

54+
def test_records_out_of_stock_event_if_cannot_allocate():
55+
sku1_batch = Batch('batch1', 'sku1', 100, eta=today)
56+
sku2_line = OrderLine('oref', 'sku2', 10)
57+
product = Product(sku='sku1', batches=[sku1_batch])
58+
59+
with pytest.raises(exceptions.OutOfStock):
60+
product.allocate(sku2_line)
61+
assert product.events[-1] == events.OutOfStock(sku='sku2')
62+
63+
5464
def test_increments_version_number():
5565
line = OrderLine('oref', "SCANDI-PEN", 10)
5666
product = Product(sku="SCANDI-PEN", batches=[Batch('b1', "SCANDI-PEN", 100, eta=None)])

0 commit comments

Comments
 (0)