|
1 | 1 | # pylint: disable=no-self-use
|
2 | 2 | from __future__ import annotations
|
| 3 | +from collections import defaultdict |
3 | 4 | from datetime import date
|
4 |
| -from unittest import mock |
| 5 | +from typing import Dict, List |
5 | 6 | import pytest
|
6 | 7 | from allocation import bootstrap
|
7 |
| -from allocation.adapters import repository |
8 | 8 | 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 |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class FakeRepository(repository.AbstractRepository):
|
@@ -39,11 +41,19 @@ def rollback(self):
|
39 | 41 | pass
|
40 | 42 |
|
41 | 43 |
|
| 44 | +class FakeNotifications(notifications.AbstractNotifications): |
| 45 | + def __init__(self): |
| 46 | + self.sent = defaultdict(list) # type: Dict[str, List[str]] |
| 47 | + |
| 48 | + def send(self, destination, message): |
| 49 | + self.sent[destination].append(message) |
| 50 | + |
| 51 | + |
42 | 52 | def bootstrap_test_app():
|
43 | 53 | return bootstrap.bootstrap(
|
44 | 54 | start_orm=False,
|
45 | 55 | uow=FakeUnitOfWork(),
|
46 |
| - send_mail=lambda *args: None, |
| 56 | + notifications=FakeNotifications(), |
47 | 57 | publish=lambda *args: None,
|
48 | 58 | )
|
49 | 59 |
|
@@ -86,21 +96,17 @@ def test_commits(self):
|
86 | 96 | assert bus.uow.committed
|
87 | 97 |
|
88 | 98 | def test_sends_email_on_out_of_stock_error(self):
|
89 |
| - emails = [] |
90 |
| - |
91 |
| - def fake_send_mail(*args): |
92 |
| - emails.append(args) |
93 |
| - |
| 99 | + fake_notifs = FakeNotifications() |
94 | 100 | bus = bootstrap.bootstrap(
|
95 | 101 | start_orm=False,
|
96 | 102 | uow=FakeUnitOfWork(),
|
97 |
| - send_mail=fake_send_mail, |
| 103 | + notifications=fake_notifs, |
98 | 104 | publish=lambda *args: None,
|
99 | 105 | )
|
100 | 106 | bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
|
101 | 107 | bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
|
102 |
| - assert emails == [ |
103 |
| - ("stock@made.com", f"Out of stock for POPULAR-CURTAINS"), |
| 108 | + assert fake_notifs.sent["stock@made.com"] == [ |
| 109 | + f"Out of stock for POPULAR-CURTAINS", |
104 | 110 | ]
|
105 | 111 |
|
106 | 112 |
|
|
0 commit comments