forked from cosmicpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_views.py
More file actions
49 lines (40 loc) · 1.73 KB
/
test_views.py
File metadata and controls
49 lines (40 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# pylint: disable=redefined-outer-name
from datetime import date
from sqlalchemy.orm import clear_mappers
from unittest import mock
import pytest
from allocation import bootstrap, views
from allocation.domain import commands
from allocation.service_layer import unit_of_work
today = date.today()
@pytest.fixture
def sqlite_bus(sqlite_session_factory):
bus = bootstrap.bootstrap(
start_orm=True,
uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory),
notifications=mock.Mock(),
publish=lambda *args: None,
)
yield bus
clear_mappers()
def test_allocations_view(sqlite_bus):
sqlite_bus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None))
sqlite_bus.handle(commands.CreateBatch("sku2batch", "sku2", 50, today))
sqlite_bus.handle(commands.Allocate("order1", "sku1", 20))
sqlite_bus.handle(commands.Allocate("order1", "sku2", 20))
# add a spurious batch and order to make sure we're getting the right ones
sqlite_bus.handle(commands.CreateBatch("sku1batch-later", "sku1", 50, today))
sqlite_bus.handle(commands.Allocate("otherorder", "sku1", 30))
sqlite_bus.handle(commands.Allocate("otherorder", "sku2", 10))
assert views.allocations("order1", sqlite_bus.uow) == [
{"sku": "sku1", "batchref": "sku1batch"},
{"sku": "sku2", "batchref": "sku2batch"},
]
def test_deallocation(sqlite_bus):
sqlite_bus.handle(commands.CreateBatch("b1", "sku1", 50, None))
sqlite_bus.handle(commands.CreateBatch("b2", "sku1", 50, today))
sqlite_bus.handle(commands.Allocate("o1", "sku1", 40))
sqlite_bus.handle(commands.ChangeBatchQuantity("b1", 10))
assert views.allocations("o1", sqlite_bus.uow) == [
{"sku": "sku1", "batchref": "b2"},
]