|
1 | 1 | from unittest import mock |
2 | 2 | import pytest |
3 | | -from allocation import services, exceptions, repository, unit_of_work |
| 3 | +from allocation import events, exceptions, messagebus, repository, unit_of_work |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class FakeRepository(repository.AbstractRepository): |
@@ -30,49 +30,57 @@ def rollback(self): |
30 | 30 |
|
31 | 31 |
|
32 | 32 |
|
33 | | -def test_add_batch_for_new_product(): |
34 | | - uow = FakeUnitOfWork() |
35 | | - services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow) |
36 | | - assert uow.products.get("CRUNCHY-ARMCHAIR") is not None |
37 | | - assert uow.committed |
| 33 | +class TestAddBatch: |
38 | 34 |
|
| 35 | + @staticmethod |
| 36 | + def test_for_new_product(): |
| 37 | + uow = FakeUnitOfWork() |
| 38 | + messagebus.handle(events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow) |
| 39 | + assert uow.products.get("CRUNCHY-ARMCHAIR") is not None |
| 40 | + assert uow.committed |
39 | 41 |
|
40 | | -def test_add_batch_for_existing_product(): |
41 | | - uow = FakeUnitOfWork() |
42 | | - services.add_batch("b1", "GARISH-RUG", 100, None, uow) |
43 | | - services.add_batch("b2", "GARISH-RUG", 99, None, uow) |
44 | | - assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] |
| 42 | + @staticmethod |
| 43 | + def test_for_existing_product(): |
| 44 | + uow = FakeUnitOfWork() |
| 45 | + messagebus.handle(events.BatchCreated("b1", "GARISH-RUG", 100, None), uow) |
| 46 | + messagebus.handle(events.BatchCreated("b2", "GARISH-RUG", 99, None), uow) |
| 47 | + assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] |
45 | 48 |
|
46 | 49 |
|
47 | | -def test_allocate_returns_allocation(): |
48 | | - uow = FakeUnitOfWork() |
49 | | - services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow) |
50 | | - result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow) |
51 | | - assert result == "batch1" |
52 | 50 |
|
| 51 | +class TestAllocate: |
53 | 52 |
|
54 | | -def test_allocate_errors_for_invalid_sku(): |
55 | | - uow = FakeUnitOfWork() |
56 | | - services.add_batch("b1", "AREALSKU", 100, None, uow) |
| 53 | + @staticmethod |
| 54 | + def test_returns_allocation(): |
| 55 | + uow = FakeUnitOfWork() |
| 56 | + messagebus.handle(events.BatchCreated("b1", "COMPLICATED-LAMP", 100, None), uow) |
| 57 | + result = messagebus.handle(events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow) |
| 58 | + assert result == "b1" |
57 | 59 |
|
58 | | - with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"): |
59 | | - services.allocate("o1", "NONEXISTENTSKU", 10, uow) |
| 60 | + @staticmethod |
| 61 | + def test_errors_for_invalid_sku(): |
| 62 | + uow = FakeUnitOfWork() |
| 63 | + messagebus.handle(events.BatchCreated("b1", "AREALSKU", 100, None), uow) |
60 | 64 |
|
| 65 | + with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"): |
| 66 | + messagebus.handle(events.AllocationRequired("o1", "NONEXISTENTSKU", 10) , uow) |
61 | 67 |
|
62 | | -def test_allocate_commits(): |
63 | | - uow = FakeUnitOfWork() |
64 | | - services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow) |
65 | | - services.allocate("o1", "OMINOUS-MIRROR", 10, uow) |
66 | | - assert uow.committed |
67 | 68 |
|
| 69 | + @staticmethod |
| 70 | + def test_commits(): |
| 71 | + uow = FakeUnitOfWork() |
| 72 | + messagebus.handle(events.BatchCreated("b1", "OMINOUS-MIRROR", 100, None), uow) |
| 73 | + messagebus.handle(events.AllocationRequired("o1", "OMINOUS-MIRROR", 10), uow) |
| 74 | + assert uow.committed |
68 | 75 |
|
69 | | -def test_sends_email_on_out_of_stock_error(): |
70 | | - uow = FakeUnitOfWork() |
71 | | - services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow) |
| 76 | + @staticmethod |
| 77 | + def test_sends_email_on_out_of_stock_error(): |
| 78 | + uow = FakeUnitOfWork() |
| 79 | + messagebus.handle(events.BatchCreated("b1", "POPULAR-CURTAINS", 9, None), uow) |
72 | 80 |
|
73 | | - with mock.patch("allocation.email.send_mail") as mock_send_mail: |
74 | | - services.allocate("o1", "POPULAR-CURTAINS", 10, uow) |
75 | | - assert mock_send_mail.call_args == mock.call( |
76 | | - "stock@made.com", |
77 | | - f"Out of stock for POPULAR-CURTAINS", |
78 | | - ) |
| 81 | + with mock.patch("allocation.email.send") as mock_send_mail: |
| 82 | + messagebus.handle(events.AllocationRequired("o1", "POPULAR-CURTAINS", 10), uow) |
| 83 | + assert mock_send_mail.call_args == mock.call( |
| 84 | + "stock@made.com", |
| 85 | + f"Out of stock for POPULAR-CURTAINS", |
| 86 | + ) |
0 commit comments