|
| 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 | + |
| 24 | +def test_allocations_view(sqlite_bus): |
| 25 | + sqlite_bus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None)) |
| 26 | + sqlite_bus.handle(commands.CreateBatch("sku2batch", "sku2", 50, today)) |
| 27 | + sqlite_bus.handle(commands.Allocate("order1", "sku1", 20)) |
| 28 | + sqlite_bus.handle(commands.Allocate("order1", "sku2", 20)) |
15 | 29 | # 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) |
| 30 | + sqlite_bus.handle(commands.CreateBatch("sku1batch-later", "sku1", 50, today)) |
| 31 | + sqlite_bus.handle(commands.Allocate("otherorder", "sku1", 30)) |
| 32 | + sqlite_bus.handle(commands.Allocate("otherorder", "sku2", 10)) |
19 | 33 |
|
20 |
| - assert views.allocations("order1", uow) == [ |
| 34 | + assert views.allocations("order1", sqlite_bus.uow) == [ |
21 | 35 | {"sku": "sku1", "batchref": "sku1batch"},
|
22 | 36 | {"sku": "sku2", "batchref": "sku2batch"},
|
23 | 37 | ]
|
24 | 38 |
|
25 | 39 |
|
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) |
| 40 | +def test_deallocation(sqlite_bus): |
| 41 | + sqlite_bus.handle(commands.CreateBatch("b1", "sku1", 50, None)) |
| 42 | + sqlite_bus.handle(commands.CreateBatch("b2", "sku1", 50, today)) |
| 43 | + sqlite_bus.handle(commands.Allocate("o1", "sku1", 40)) |
| 44 | + sqlite_bus.handle(commands.ChangeBatchQuantity("b1", 10)) |
32 | 45 |
|
33 |
| - assert views.allocations("o1", uow) == [ |
| 46 | + assert views.allocations("o1", sqlite_bus.uow) == [ |
34 | 47 | {"sku": "sku1", "batchref": "b2"},
|
35 | 48 | ]
|
0 commit comments