|
| 1 | +# pylint: disable=redefined-outer-name |
1 | 2 | from datetime import date |
| 3 | +from unittest import mock |
| 4 | +import pytest |
2 | 5 | from allocation import views |
3 | 6 | from allocation.domain import commands |
4 | 7 | from allocation.service_layer import messagebus, unit_of_work |
5 | 8 |
|
6 | 9 |
|
7 | | -def test_allocations_view(sqlite_session_factory): |
| 10 | +@pytest.fixture |
| 11 | +def sqlite_bus(sqlite_session_factory): |
8 | 12 | uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) |
9 | | - messagebus.handle(commands.CreateBatch('sku1batch', 'sku1', 50, None), uow) |
10 | | - messagebus.handle(commands.CreateBatch('sku2batch', 'sku2', 50, date.today()), uow) |
11 | | - messagebus.handle(commands.Allocate('order1', 'sku1', 20), uow) |
12 | | - messagebus.handle(commands.Allocate('order1', 'sku2', 20), uow) |
| 13 | + bus = messagebus.MessageBus( |
| 14 | + uow=uow, |
| 15 | + send_mail=mock.Mock(), |
| 16 | + publish=mock.Mock(), |
| 17 | + ) |
| 18 | + uow.bus = bus |
| 19 | + return bus |
| 20 | + |
| 21 | + |
| 22 | +def test_allocations_view(sqlite_bus): |
| 23 | + sqlite_bus.handle(commands.CreateBatch('sku1batch', 'sku1', 50, None)) |
| 24 | + sqlite_bus.handle(commands.CreateBatch('sku2batch', 'sku2', 50, date.today())) |
| 25 | + sqlite_bus.handle(commands.Allocate('order1', 'sku1', 20)) |
| 26 | + sqlite_bus.handle(commands.Allocate('order1', 'sku2', 20)) |
13 | 27 | # add a spurious batch and order to make sure we're getting the right ones |
14 | | - messagebus.handle(commands.CreateBatch('sku1batch-later', 'sku1', 50, date.today()), uow) |
15 | | - messagebus.handle(commands.Allocate('otherorder', 'sku1', 30), uow) |
16 | | - messagebus.handle(commands.Allocate('otherorder', 'sku2', 10), uow) |
| 28 | + sqlite_bus.handle(commands.CreateBatch('sku1batch-later', 'sku1', 50, date.today())) |
| 29 | + sqlite_bus.handle(commands.Allocate('otherorder', 'sku1', 30)) |
| 30 | + sqlite_bus.handle(commands.Allocate('otherorder', 'sku2', 10)) |
17 | 31 |
|
18 | | - assert views.allocations('order1', uow) == [ |
| 32 | + assert views.allocations('order1', sqlite_bus.uow) == [ |
19 | 33 | {'sku': 'sku1', 'batchref': 'sku1batch'}, |
20 | 34 | {'sku': 'sku2', 'batchref': 'sku2batch'}, |
21 | 35 | ] |
22 | 36 |
|
23 | 37 |
|
24 | | -def test_deallocation(sqlite_session_factory): |
25 | | - uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) |
26 | | - messagebus.handle(commands.CreateBatch('b1', 'sku1', 50, None), uow) |
27 | | - messagebus.handle(commands.CreateBatch('b2', 'sku1', 50, date.today()), uow) |
28 | | - messagebus.handle(commands.Allocate('o1', 'sku1', 40), uow) |
29 | | - messagebus.handle(commands.ChangeBatchQuantity('b1', 10), uow) |
| 38 | +def test_deallocation(sqlite_bus): |
| 39 | + sqlite_bus.handle(commands.CreateBatch('b1', 'sku1', 50, None)) |
| 40 | + sqlite_bus.handle(commands.CreateBatch('b2', 'sku1', 50, date.today())) |
| 41 | + sqlite_bus.handle(commands.Allocate('o1', 'sku1', 40)) |
| 42 | + sqlite_bus.handle(commands.ChangeBatchQuantity('b1', 10)) |
30 | 43 |
|
31 | | - assert views.allocations('o1', uow) == [ |
| 44 | + assert views.allocations('o1', sqlite_bus.uow) == [ |
32 | 45 | {'sku': 'sku1', 'batchref': 'b2'}, |
33 | 46 | ] |
0 commit comments