|
4 | 4 | from allocation.service_layer import services, unit_of_work |
5 | 5 |
|
6 | 6 |
|
7 | | -class FakeRepository(repository.AbstractRepository): |
| 7 | +class FakeRepository: |
8 | 8 |
|
9 | 9 | def __init__(self, products): |
10 | | - super().__init__() |
11 | | - self._products = set(products) |
| 10 | + self.products = set(products) |
12 | 11 |
|
13 | | - def _add(self, product): |
14 | | - self._products.add(product) |
| 12 | + def add(self, product): |
| 13 | + self.products.add(product) |
15 | 14 |
|
16 | | - def _get(self, sku): |
17 | | - return next((p for p in self._products if p.sku == sku), None) |
| 15 | + def get(self, sku): |
| 16 | + return next((p for p in self.products if p.sku == sku), None) |
18 | 17 |
|
19 | 18 |
|
20 | | -class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork): |
| 19 | + |
| 20 | +class FakeUnitOfWork: |
21 | 21 |
|
22 | 22 | def __init__(self): |
23 | | - self.products = FakeRepository([]) |
| 23 | + self.products = repository.TrackingRepository(FakeRepository([])) |
24 | 24 | self.committed = False |
25 | 25 |
|
26 | | - def _commit(self): |
| 26 | + def __enter__(self): |
| 27 | + return self |
| 28 | + |
| 29 | + def __exit__(self, *args): |
| 30 | + pass |
| 31 | + |
| 32 | + def commit(self): |
27 | 33 | self.committed = True |
28 | 34 |
|
29 | 35 | def rollback(self): |
30 | 36 | pass |
31 | 37 |
|
32 | 38 |
|
33 | 39 |
|
| 40 | +def get_uow(): |
| 41 | + return unit_of_work.EventPublishingUoW( |
| 42 | + unit_of_work.AutoRollbackUoW( |
| 43 | + FakeUnitOfWork() |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + |
34 | 48 | def test_add_batch_for_new_product(): |
35 | | - uow = FakeUnitOfWork() |
| 49 | + uow = get_uow() |
36 | 50 | services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow) |
37 | 51 | assert uow.products.get("CRUNCHY-ARMCHAIR") is not None |
38 | 52 | assert uow.committed |
39 | 53 |
|
40 | 54 |
|
41 | 55 | def test_add_batch_for_existing_product(): |
42 | | - uow = FakeUnitOfWork() |
| 56 | + uow = get_uow() |
43 | 57 | services.add_batch("b1", "GARISH-RUG", 100, None, uow) |
44 | 58 | services.add_batch("b2", "GARISH-RUG", 99, None, uow) |
45 | 59 | assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] |
46 | 60 |
|
47 | 61 |
|
48 | 62 | def test_allocate_returns_allocation(): |
49 | | - uow = FakeUnitOfWork() |
| 63 | + uow = get_uow() |
50 | 64 | services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow) |
51 | 65 | result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow) |
52 | 66 | assert result == "batch1" |
53 | 67 |
|
54 | 68 |
|
55 | 69 | def test_allocate_errors_for_invalid_sku(): |
56 | | - uow = FakeUnitOfWork() |
| 70 | + uow = get_uow() |
57 | 71 | services.add_batch("b1", "AREALSKU", 100, None, uow) |
58 | 72 |
|
59 | 73 | with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"): |
60 | 74 | services.allocate("o1", "NONEXISTENTSKU", 10, uow) |
61 | 75 |
|
62 | 76 |
|
63 | 77 | def test_allocate_commits(): |
64 | | - uow = FakeUnitOfWork() |
| 78 | + uow = get_uow() |
65 | 79 | services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow) |
66 | 80 | services.allocate("o1", "OMINOUS-MIRROR", 10, uow) |
67 | 81 | assert uow.committed |
68 | 82 |
|
69 | 83 |
|
70 | 84 | def test_sends_email_on_out_of_stock_error(): |
71 | | - uow = FakeUnitOfWork() |
| 85 | + uow = get_uow() |
72 | 86 | services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow) |
73 | 87 |
|
74 | 88 | with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail: |
|
0 commit comments