Skip to content

Commit dbbf48d

Browse files
committed
tests for notifcations [notifications_unit_tests]
1 parent 51a465b commit dbbf48d

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

tests/integration/test_views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint: disable=redefined-outer-name
22
from datetime import date
33
from sqlalchemy.orm import clear_mappers
4+
from unittest import mock
45
import pytest
56
from allocation import bootstrap, views
67
from allocation.domain import commands
@@ -11,7 +12,7 @@ def sqlite_bus(sqlite_session_factory):
1112
bus = bootstrap.bootstrap(
1213
start_orm=True,
1314
uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory),
14-
send_mail=lambda *args: None,
15+
notifications=mock.Mock(),
1516
publish=lambda *args: None,
1617
)
1718
yield bus

tests/unit/test_handlers.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# pylint: disable=no-self-use
22
from __future__ import annotations
3+
from collections import defaultdict
34
from datetime import date
4-
from unittest import mock
5+
from typing import Dict, List
56
import pytest
67
from allocation import bootstrap
7-
from allocation.adapters import repository
88
from allocation.domain import commands
9-
from allocation.service_layer import handlers, unit_of_work
9+
from allocation.service_layer import handlers
10+
from allocation.adapters import notifications, repository
11+
from allocation.service_layer import unit_of_work
1012

1113

1214
class FakeRepository(repository.AbstractRepository):
@@ -41,11 +43,21 @@ def rollback(self):
4143
pass
4244

4345

46+
class FakeNotifications(notifications.AbstractNotifications):
47+
48+
def __init__(self):
49+
self.sent = defaultdict(list) # type: Dict[str, List[str]]
50+
51+
def send(self, destination, message):
52+
self.sent[destination].append(message)
53+
54+
55+
4456
def bootstrap_test_app():
4557
return bootstrap.bootstrap(
4658
start_orm=False,
4759
uow=FakeUnitOfWork(),
48-
send_mail=lambda *args: None,
60+
notifications=FakeNotifications(),
4961
publish=lambda *args: None,
5062
)
5163

@@ -92,19 +104,17 @@ def test_commits(self):
92104

93105

94106
def test_sends_email_on_out_of_stock_error(self):
95-
emails = []
96-
def fake_send_mail(*args):
97-
emails.append(args)
107+
fake_notifs = FakeNotifications()
98108
bus = bootstrap.bootstrap(
99109
start_orm=False,
100110
uow=FakeUnitOfWork(),
101-
send_mail=fake_send_mail,
111+
notifications=fake_notifs,
102112
publish=lambda *args: None,
103113
)
104114
bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
105115
bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
106-
assert emails == [
107-
("[email protected]", f"Out of stock for POPULAR-CURTAINS"),
116+
assert fake_notifs.sent['[email protected]'] == [
117+
f"Out of stock for POPULAR-CURTAINS",
108118
]
109119

110120

0 commit comments

Comments
 (0)