|
| 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 |
|
| 9 | +@pytest.fixture |
| 10 | +def sqlite_bus(sqlite_session_factory): |
| 11 | + bus = bootstrap.bootstrap( |
| 12 | + start_orm=True, |
| 13 | + uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory), |
| 14 | + send_mail=lambda *args: None, |
| 15 | + publish=lambda *args: None, |
| 16 | + ) |
| 17 | + yield bus |
| 18 | + clear_mappers() |
6 | 19 |
|
7 |
| -def test_allocations_view(sqlite_session_factory): |
8 |
| - 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) |
| 20 | +def test_allocations_view(sqlite_bus): |
| 21 | + sqlite_bus.handle(commands.CreateBatch('sku1batch', 'sku1', 50, None)) |
| 22 | + sqlite_bus.handle(commands.CreateBatch('sku2batch', 'sku2', 50, date.today())) |
| 23 | + sqlite_bus.handle(commands.Allocate('order1', 'sku1', 20)) |
| 24 | + sqlite_bus.handle(commands.Allocate('order1', 'sku2', 20)) |
13 | 25 | # 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) |
| 26 | + sqlite_bus.handle(commands.CreateBatch('sku1batch-later', 'sku1', 50, date.today())) |
| 27 | + sqlite_bus.handle(commands.Allocate('otherorder', 'sku1', 30)) |
| 28 | + sqlite_bus.handle(commands.Allocate('otherorder', 'sku2', 10)) |
17 | 29 |
|
18 |
| - assert views.allocations('order1', uow) == [ |
| 30 | + assert views.allocations('order1', sqlite_bus.uow) == [ |
19 | 31 | {'sku': 'sku1', 'batchref': 'sku1batch'},
|
20 | 32 | {'sku': 'sku2', 'batchref': 'sku2batch'},
|
21 | 33 | ]
|
22 | 34 |
|
23 | 35 |
|
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) |
| 36 | +def test_deallocation(sqlite_bus): |
| 37 | + sqlite_bus.handle(commands.CreateBatch('b1', 'sku1', 50, None)) |
| 38 | + sqlite_bus.handle(commands.CreateBatch('b2', 'sku1', 50, date.today())) |
| 39 | + sqlite_bus.handle(commands.Allocate('o1', 'sku1', 40)) |
| 40 | + sqlite_bus.handle(commands.ChangeBatchQuantity('b1', 10)) |
30 | 41 |
|
31 |
| - assert views.allocations('o1', uow) == [ |
| 42 | + assert views.allocations('o1', sqlite_bus.uow) == [ |
32 | 43 | {'sku': 'sku1', 'batchref': 'b2'},
|
33 | 44 | ]
|
0 commit comments