Skip to content

Commit 916bcdd

Browse files
committed
strip out UoW and fake UoW, add some tips
1 parent 7526014 commit 916bcdd

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

src/allocation/service_layer/services.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@ def is_valid_sku(sku, batches):
1616

1717

1818
def add_batch(
19-
ref: str, sku: str, qty: int, eta: Optional[date],
20-
uow: unit_of_work.AbstractUnitOfWork,
19+
ref: str,
20+
sku: str,
21+
qty: int,
22+
eta: Optional[date],
23+
uow #: unit_of_work.AbstractUnitOfWork
24+
# this argument could be start_uow: AbstractUnitOfWorkStarter instead?
2125
):
26+
# and this could be with start_uow() as uow:
2227
with uow:
2328
uow.batches.add(model.Batch(ref, sku, qty, eta))
2429
uow.commit()
2530

2631

2732
def allocate(
28-
orderid: str, sku: str, qty: int,
33+
orderid: str,
34+
sku: str,
35+
qty: int,
2936
uow: unit_of_work.AbstractUnitOfWork,
3037
) -> str:
3138
line = OrderLine(orderid, sku, qty)
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint: disable=attribute-defined-outside-init
22
from __future__ import annotations
33
import abc
4+
from typing import ContextManager
45
from sqlalchemy import create_engine
56
from sqlalchemy.orm import sessionmaker
67
from sqlalchemy.orm.session import Session
@@ -10,13 +11,9 @@
1011

1112

1213
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()
14+
# should this class contain __enter__ and __exit__?
15+
# or should the context manager and the UoW be separate?
16+
# up to you!
2017

2118
@abc.abstractmethod
2219
def commit(self):
@@ -34,21 +31,14 @@ def rollback(self):
3431
)
3532

3633

37-
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):
38-
def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
39-
self.session_factory = session_factory
40-
41-
def __enter__(self):
42-
self.session = self.session_factory() # type: Session
43-
self.batches = repository.SqlAlchemyRepository(self.session)
44-
return super().__enter__()
34+
class SqlAlchemyUnitOfWork:
35+
...
4536

46-
def __exit__(self, *args):
47-
super().__exit__(*args)
48-
self.session.close()
4937

50-
def commit(self):
51-
self.session.commit()
52-
53-
def rollback(self):
54-
self.session.rollback()
38+
# One alternative would be to define a `start_uow` function,
39+
# or a UnitOfWorkStarter or UnitOfWorkManager that does the
40+
# job of context manager, leaving the UoW as a separate class
41+
# that's returned by the context manager's __enter__.
42+
#
43+
# A type like this could work?
44+
# AbstractUnitOfWorkStarter = ContextManager[AbstractUnitOfWork]

tests/unit/test_services.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,27 @@ def list(self):
1818

1919

2020
class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork):
21-
def __init__(self):
22-
self.batches = FakeRepository([])
23-
self.committed = False
24-
25-
def commit(self):
26-
self.committed = True
27-
28-
def rollback(self):
29-
pass
21+
...
3022

3123

3224
def test_add_batch():
3325
uow = FakeUnitOfWork()
34-
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
26+
# fake_uow_starter = FakeUoWContextManager(uow) ?
27+
# fake_uow_starter = contextlib.nullcontext(uow) ?
28+
# services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, fake_uow_starter)
3529
assert uow.batches.get("b1") is not None
3630
assert uow.committed
3731

3832

33+
@pytest.mark.skip("unskip and fix when ready")
3934
def test_allocate_returns_allocation():
4035
uow = FakeUnitOfWork()
4136
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
4237
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
4338
assert result == "batch1"
4439

4540

41+
@pytest.mark.skip("unskip and fix when ready")
4642
def test_allocate_errors_for_invalid_sku():
4743
uow = FakeUnitOfWork()
4844
services.add_batch("b1", "AREALSKU", 100, None, uow)
@@ -51,6 +47,7 @@ def test_allocate_errors_for_invalid_sku():
5147
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
5248

5349

50+
@pytest.mark.skip("unskip and fix when ready")
5451
def test_allocate_commits():
5552
uow = FakeUnitOfWork()
5653
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)

0 commit comments

Comments
 (0)