Skip to content

Commit 0808c35

Browse files
committed
basic uow test, uow and conftest.py changes
1 parent 81a089f commit 0808c35

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

src/allocation/unit_of_work.py

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

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)