|
1 | 1 | from __future__ import annotations |
2 | 2 | from datetime import date |
3 | | -from unittest import mock |
4 | 3 | import pytest |
5 | | -from allocation.adapters import repository |
6 | 4 | 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 |
53 | 7 |
|
54 | 8 |
|
55 | 9 | class TestAddBatch: |
@@ -99,10 +53,9 @@ def test_sends_email_on_out_of_stock_error(): |
99 | 53 | bus = FakeBus() |
100 | 54 | bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None)) |
101 | 55 | 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]'] == [ |
104 | 57 | f"Out of stock for POPULAR-CURTAINS", |
105 | | - ) |
| 58 | + ] |
106 | 59 |
|
107 | 60 |
|
108 | 61 | class TestChangeBatchQuantity: |
|
0 commit comments