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