Skip to content

Commit 56d335a

Browse files
committed
basic uow test, uow and conftest.py changes
1 parent 668f1ec commit 56d335a

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/allocation/unit_of_work.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# pylint: disable=attribute-defined-outside-init
2+
from __future__ import annotations
3+
import abc
4+
from sqlalchemy import create_engine
5+
from sqlalchemy.orm import sessionmaker
6+
from sqlalchemy.orm.session import Session
7+
8+
from allocation import config
9+
from allocation import repository
10+
11+
12+
class AbstractUnitOfWork(abc.ABC):
13+
batches: repository.AbstractRepository
14+
15+
def __enter__(self) -> AbstractUnitOfWork:
16+
return self
17+
18+
def __exit__(self, *args):
19+
self.rollback()
20+
21+
@abc.abstractmethod
22+
def commit(self):
23+
raise NotImplementedError
24+
25+
@abc.abstractmethod
26+
def rollback(self):
27+
raise NotImplementedError
28+
29+
30+
31+
DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine(
32+
config.get_postgres_uri(),
33+
))
34+
35+
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):
36+
37+
def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
38+
self.session_factory = session_factory
39+
40+
def __enter__(self):
41+
self.session = self.session_factory() # type: Session
42+
self.batches = repository.SqlAlchemyRepository(self.session)
43+
return super().__enter__()
44+
45+
def __exit__(self, *args):
46+
super().__exit__(*args)
47+
self.session.close()
48+
49+
def commit(self):
50+
self.session.commit()
51+
52+
def rollback(self):
53+
self.session.rollback()

tests/conftest.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ def in_memory_db():
1919
metadata.create_all(engine)
2020
return engine
2121

22-
2322
@pytest.fixture
24-
def session(in_memory_db):
23+
def session_factory(in_memory_db):
2524
start_mappers()
26-
yield sessionmaker(bind=in_memory_db)()
25+
yield sessionmaker(bind=in_memory_db)
2726
clear_mappers()
2827

28+
@pytest.fixture
29+
def session(session_factory):
30+
return session_factory()
31+
2932

3033
def wait_for_postgres_to_come_up(engine):
3134
deadline = time.time() + 10

tests/integration/test_uow.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
from allocation import model
3+
from allocation import unit_of_work
4+
5+
def insert_batch(session, ref, sku, qty, eta):
6+
session.execute(
7+
'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
8+
' VALUES (:ref, :sku, :qty, :eta)',
9+
dict(ref=ref, sku=sku, qty=qty, eta=eta)
10+
)
11+
12+
def get_allocated_batch_ref(session, orderid, sku):
13+
[[orderlineid]] = session.execute(
14+
'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku',
15+
dict(orderid=orderid, sku=sku)
16+
)
17+
[[batchref]] = session.execute(
18+
'SELECT b.reference FROM allocations JOIN batches AS b ON batch_id = b.id'
19+
' WHERE orderline_id=:orderlineid',
20+
dict(orderlineid=orderlineid)
21+
)
22+
return batchref
23+
24+
25+
def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):
26+
session = session_factory()
27+
insert_batch(session, 'batch1', 'HIPSTER-WORKBENCH', 100, None)
28+
session.commit()
29+
30+
uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
31+
with uow:
32+
batch = uow.batches.get(reference='batch1')
33+
line = model.OrderLine('o1', 'HIPSTER-WORKBENCH', 10)
34+
batch.allocate(line)
35+
uow.commit()
36+
37+
batchref = get_allocated_batch_ref(session, 'o1', 'HIPSTER-WORKBENCH')
38+
assert batchref == 'batch1'

0 commit comments

Comments
 (0)