Skip to content

Commit a2b7640

Browse files
committed
tests for notifcations
1 parent 0d24fae commit a2b7640

3 files changed

Lines changed: 62 additions & 51 deletions

File tree

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/fakes.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Dict, List
2+
from collections import defaultdict
3+
from unittest import mock
4+
from allocation.adapters import notifications, repository
5+
from allocation.service_layer import messagebus, unit_of_work
6+
7+
8+
class FakeRepository(repository.AbstractRepository):
9+
10+
def __init__(self, products):
11+
super().__init__()
12+
self._products = set(products)
13+
14+
def _add(self, product):
15+
self._products.add(product)
16+
17+
def _get(self, sku):
18+
return next((p for p in self._products if p.sku == sku), None)
19+
20+
def _get_by_batchref(self, batchref):
21+
return next((
22+
p for p in self._products for b in p.batches
23+
if b.reference == batchref
24+
), None)
25+
26+
27+
class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork):
28+
29+
def __init__(self):
30+
self.products = FakeRepository([])
31+
self.committed = False
32+
33+
def _commit(self):
34+
self.committed = True
35+
36+
def rollback(self):
37+
pass
38+
39+
40+
41+
class FakeBus(messagebus.MessageBus):
42+
def __init__(self):
43+
uow = FakeUnitOfWork()
44+
super().__init__(
45+
uow=uow,
46+
notifications=FakeNotifications(),
47+
publish=mock.Mock(),
48+
)
49+
uow.bus = self
50+
51+
52+
class FakeNotifications(notifications.AbstractNotifications):
53+
54+
def __init__(self):
55+
self.sent = defaultdict(list) # type: Dict[str, List[str]]
56+
57+
def send(self, destination, message):
58+
self.sent[destination].append(message)

tests/unit/test_handlers.py

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
from __future__ import annotations
22
from datetime import date
3-
from unittest import mock
43
import pytest
5-
from allocation.adapters import repository
64
from allocation.domain import commands
7-
from allocation.service_layer import handlers, messagebus, unit_of_work
8-
9-
10-
class FakeRepository(repository.AbstractRepository):
11-
12-
def __init__(self, products):
13-
super().__init__()
14-
self._products = set(products)
15-
16-
def _add(self, product):
17-
self._products.add(product)
18-
19-
def _get(self, sku):
20-
return next((p for p in self._products if p.sku == sku), None)
21-
22-
def _get_by_batchref(self, batchref):
23-
return next((
24-
p for p in self._products for b in p.batches
25-
if b.reference == batchref
26-
), None)
27-
28-
29-
class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork):
30-
31-
def __init__(self):
32-
self.products = FakeRepository([])
33-
self.committed = False
34-
35-
def _commit(self):
36-
self.committed = True
37-
38-
def rollback(self):
39-
pass
40-
41-
42-
43-
class FakeBus(messagebus.MessageBus):
44-
def __init__(self):
45-
uow = FakeUnitOfWork()
46-
super().__init__(
47-
uow=uow,
48-
send_mail=mock.Mock(),
49-
publish=mock.Mock(),
50-
)
51-
uow.bus = self
52-
5+
from allocation.service_layer import handlers
6+
from .fakes import FakeBus
537

548

559
class TestAddBatch:
@@ -99,10 +53,9 @@ def test_sends_email_on_out_of_stock_error():
9953
bus = FakeBus()
10054
bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
10155
bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
102-
assert bus.dependencies["send_mail"].call_args == mock.call(
103-
56+
assert bus.dependencies['notifications'].sent['[email protected]'] == [
10457
f"Out of stock for POPULAR-CURTAINS",
105-
)
58+
]
10659

10760

10861
class TestChangeBatchQuantity:

0 commit comments

Comments
 (0)