Skip to content

Commit b037c86

Browse files
committed
uow now does messagebus magically. breaks tests [uow_has_messagebus]
1 parent c035987 commit b037c86

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

src/allocation/repository.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ class SqlAlchemyRepository(AbstractRepository):
1717

1818
def __init__(self, session):
1919
self.session = session
20+
self.seen = set()
2021

2122
def add(self, product):
23+
self.seen.add(product)
2224
self.session.add(product)
2325

2426
def get(self, sku):
25-
return self.session.query(model.Product).filter_by(sku=sku).first()
27+
p = self.session.query(model.Product).filter_by(sku=sku).first()
28+
if p:
29+
self.seen.add(p)
30+
return p
2631

src/allocation/services.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional
33
from datetime import date
44

5-
from allocation import exceptions, messagebus, model, unit_of_work
5+
from allocation import exceptions, model, unit_of_work
66
from allocation.model import OrderLine
77

88

@@ -28,9 +28,6 @@ def allocate(
2828
product = uow.products.get(sku=line.sku)
2929
if product is None:
3030
raise exceptions.InvalidSku(f'Invalid sku {line.sku}')
31-
try:
32-
batchref = product.allocate(line)
33-
uow.commit()
34-
return batchref
35-
finally:
36-
messagebus.handle(product.events)
31+
batchref = product.allocate(line)
32+
uow.commit()
33+
return batchref

src/allocation/unit_of_work.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from sqlalchemy.orm import sessionmaker
55
from sqlalchemy.orm.session import Session
66

7-
from allocation import config
8-
from allocation import repository
7+
from allocation import config, messagebus, repository
8+
99

1010

1111
class AbstractUnitOfWork(abc.ABC):
@@ -16,8 +16,13 @@ def __enter__(self):
1616
def __exit__(self, *args):
1717
self.rollback()
1818

19-
@abc.abstractmethod
2019
def commit(self):
20+
self._commit()
21+
for obj in self.products.seen:
22+
messagebus.handle(obj.events)
23+
24+
@abc.abstractmethod
25+
def _commit(self):
2126
raise NotImplementedError
2227

2328
@abc.abstractmethod
@@ -44,9 +49,8 @@ def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
4449
self.session = session_factory() # type: Session
4550
self.init_repositories(repository.SqlAlchemyRepository(self.session))
4651

47-
def commit(self):
52+
def _commit(self):
4853
self.session.commit()
4954

5055
def rollback(self):
5156
self.session.rollback()
52-

tests/unit/test_services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self):
2121
self.init_repositories(FakeRepository([]))
2222
self.committed = False
2323

24-
def commit(self):
24+
def _commit(self):
2525
self.committed = True
2626

2727
def rollback(self):

0 commit comments

Comments
 (0)