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