Skip to content

Commit bfdd979

Browse files
committed
uow now does messagebus magically. breaks tests [uow_has_messagebus]
1 parent 3415f60 commit bfdd979

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/allocation/repository.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import abc
2+
from typing import Set
23
from allocation import model
34

5+
46
class AbstractRepository(abc.ABC):
57

68
@abc.abstractmethod
@@ -17,9 +19,14 @@ class SqlAlchemyRepository(AbstractRepository):
1719

1820
def __init__(self, session):
1921
self.session = session
22+
self.seen = set() # type: Set[model.Product]
2023

2124
def add(self, product):
25+
self.seen.add(product)
2226
self.session.add(product)
2327

2428
def get(self, sku):
25-
return self.session.query(model.Product).filter_by(sku=sku).first()
29+
product = self.session.query(model.Product).filter_by(sku=sku).first()
30+
if product:
31+
self.seen.add(product)
32+
return product

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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from sqlalchemy.orm import sessionmaker
66
from sqlalchemy.orm.session import Session
77

8-
from allocation import config
9-
from allocation import repository
8+
from allocation import config, messagebus, repository
9+
1010

1111

1212
class AbstractUnitOfWork(abc.ABC):
@@ -18,8 +18,18 @@ def __enter__(self) -> AbstractUnitOfWork:
1818
def __exit__(self, *args):
1919
self.rollback()
2020

21-
@abc.abstractmethod
2221
def commit(self):
22+
self._commit()
23+
self.publish_events()
24+
25+
def publish_events(self):
26+
for product in self.products.seen:
27+
while product.events:
28+
event = product.events.pop(0)
29+
messagebus.handle(event)
30+
31+
@abc.abstractmethod
32+
def _commit(self):
2333
raise NotImplementedError
2434

2535
@abc.abstractmethod
@@ -47,7 +57,7 @@ def __exit__(self, *args):
4757
super().__exit__(*args)
4858
self.session.close()
4959

50-
def commit(self):
60+
def _commit(self):
5161
self.session.commit()
5262

5363
def rollback(self):

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.products = 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)