Skip to content

Commit ec093e5

Browse files
committed
go all the way. e2e should be broken but they arent
1 parent 272b65c commit ec093e5

2 files changed

Lines changed: 78 additions & 32 deletions

File tree

‎src/allocation/service_layer/unit_of_work.py‎

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pylint: disable=attribute-defined-outside-init
22
from __future__ import annotations
3-
import abc
3+
from typing import Protocol
44
from sqlalchemy import create_engine
55
from sqlalchemy.orm import sessionmaker
66
from sqlalchemy.orm.session import Session
@@ -12,17 +12,49 @@
1212

1313

1414

15-
class AbstractUnitOfWork(abc.ABC):
16-
products: repository.AbstractRepository
15+
class AbstractUnitOfWork(Protocol):
16+
products: repository.TrackingRepository
1717

1818
def __enter__(self) -> AbstractUnitOfWork:
19-
return self
19+
...
20+
21+
def __exit__(self, *args):
22+
...
23+
24+
def commit(self):
25+
...
26+
27+
def rollback(self):
28+
...
29+
30+
31+
32+
class AutoRollbackUoW:
33+
34+
def __init__(self, uow: AbstractUnitOfWork):
35+
self._uow = uow
36+
self.products = self._uow.products
37+
38+
def __enter__(self):
39+
return self._uow.__enter__()
2040

2141
def __exit__(self, *args):
22-
self.rollback()
42+
self._uow.rollback()
43+
return self._uow.__exit__(*args)
44+
45+
def __getattr__(self, name):
46+
return getattr(self._uow, name)
47+
48+
49+
50+
class EventPublishingUoW:
51+
52+
def __init__(self, uow: AutoRollbackUoW):
53+
self._uow = uow
54+
self.products = self._uow.products
2355

2456
def commit(self):
25-
self._commit()
57+
self._uow.commit()
2658
self.publish_events()
2759

2860
def publish_events(self):
@@ -31,13 +63,14 @@ def publish_events(self):
3163
event = product.events.pop(0)
3264
messagebus.handle(event)
3365

34-
@abc.abstractmethod
35-
def _commit(self):
36-
raise NotImplementedError
3766

38-
@abc.abstractmethod
39-
def rollback(self):
40-
raise NotImplementedError
67+
def __enter__(self):
68+
return self._uow.__enter__()
69+
def __exit__(self, *args):
70+
return self._uow.__exit__(*args)
71+
72+
def __getattr__(self, name):
73+
return getattr(self._uow, name)
4174

4275

4376

@@ -46,7 +79,7 @@ def rollback(self):
4679
isolation_level="SERIALIZABLE",
4780
))
4881

49-
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):
82+
class SqlAlchemyUnitOfWork:
5083

5184
def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
5285
self.session_factory = session_factory
@@ -56,13 +89,12 @@ def __enter__(self):
5689
self.products = repository.TrackingRepository(
5790
repository.SqlAlchemyRepository(self.session)
5891
)
59-
return super().__enter__()
92+
return self
6093

6194
def __exit__(self, *args):
62-
super().__exit__(*args)
6395
self.session.close()
6496

65-
def _commit(self):
97+
def commit(self):
6698
self.session.commit()
6799

68100
def rollback(self):

‎tests/unit/test_services.py‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,85 @@
44
from allocation.service_layer import services, unit_of_work
55

66

7-
class FakeRepository(repository.AbstractRepository):
7+
class FakeRepository:
88

99
def __init__(self, products):
10-
super().__init__()
11-
self._products = set(products)
10+
self.products = set(products)
1211

13-
def _add(self, product):
14-
self._products.add(product)
12+
def add(self, product):
13+
self.products.add(product)
1514

16-
def _get(self, sku):
17-
return next((p for p in self._products if p.sku == sku), None)
15+
def get(self, sku):
16+
return next((p for p in self.products if p.sku == sku), None)
1817

1918

20-
class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork):
19+
20+
class FakeUnitOfWork:
2121

2222
def __init__(self):
23-
self.products = FakeRepository([])
23+
self.products = repository.TrackingRepository(FakeRepository([]))
2424
self.committed = False
2525

26-
def _commit(self):
26+
def __enter__(self):
27+
return self
28+
29+
def __exit__(self, *args):
30+
pass
31+
32+
def commit(self):
2733
self.committed = True
2834

2935
def rollback(self):
3036
pass
3137

3238

3339

40+
def get_uow():
41+
return unit_of_work.EventPublishingUoW(
42+
unit_of_work.AutoRollbackUoW(
43+
FakeUnitOfWork()
44+
)
45+
)
46+
47+
3448
def test_add_batch_for_new_product():
35-
uow = FakeUnitOfWork()
49+
uow = get_uow()
3650
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
3751
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
3852
assert uow.committed
3953

4054

4155
def test_add_batch_for_existing_product():
42-
uow = FakeUnitOfWork()
56+
uow = get_uow()
4357
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
4458
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
4559
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
4660

4761

4862
def test_allocate_returns_allocation():
49-
uow = FakeUnitOfWork()
63+
uow = get_uow()
5064
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
5165
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
5266
assert result == "batch1"
5367

5468

5569
def test_allocate_errors_for_invalid_sku():
56-
uow = FakeUnitOfWork()
70+
uow = get_uow()
5771
services.add_batch("b1", "AREALSKU", 100, None, uow)
5872

5973
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
6074
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
6175

6276

6377
def test_allocate_commits():
64-
uow = FakeUnitOfWork()
78+
uow = get_uow()
6579
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
6680
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
6781
assert uow.committed
6882

6983

7084
def test_sends_email_on_out_of_stock_error():
71-
uow = FakeUnitOfWork()
85+
uow = get_uow()
7286
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
7387

7488
with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail:

0 commit comments

Comments
 (0)