Skip to content

Commit 4ffbb6e

Browse files
committed
view now users redis, tweak tests+app. [redis_readmodel_view]
1 parent 7263d99 commit 4ffbb6e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/allocation/entrypoints/flask_app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def allocate_endpoint():
4040

4141
@app.route("/allocations/<orderid>", methods=['GET'])
4242
def allocations_view_endpoint(orderid):
43-
uow = unit_of_work.SqlAlchemyUnitOfWork()
44-
result = views.allocations(orderid, uow)
43+
result = views.allocations(orderid)
4544
if not result:
4645
return 'not found', 404
4746
return jsonify(result), 200

src/allocation/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from allocation.adapters import redis_eventpublisher
12
from allocation.service_layer import unit_of_work
23

3-
def allocations(orderid: str, uow: unit_of_work.SqlAlchemyUnitOfWork):
4-
with uow:
5-
results = list(uow.session.execute(
6-
'SELECT sku, batchref FROM allocations_view WHERE orderid = :orderid',
7-
dict(orderid=orderid)
8-
))
9-
return [dict(r) for r in results]
4+
def allocations(orderid):
5+
batches = redis_eventpublisher.get_readmodel(orderid)
6+
return [
7+
{'batchref': b.decode(), 'sku': s.decode()}
8+
for s, b in batches.items()
9+
]

tests/integration/test_views.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
from datetime import date
2-
from allocation import views
2+
import pytest
3+
import redis
4+
from allocation import config, views
35
from allocation.domain import commands
46
from allocation.service_layer import messagebus, unit_of_work
57

8+
@pytest.fixture
9+
def cleanup_redis():
10+
r = redis.Redis(**config.get_redis_host_and_port())
11+
yield
12+
for k in r.keys():
13+
print('cleaning up redis key', k)
14+
r.delete(k)
15+
16+
pytestmark = pytest.mark.usefixtures('cleanup_redis')
17+
618

719
def test_allocations_view(sqlite_session_factory):
820
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
@@ -15,7 +27,7 @@ def test_allocations_view(sqlite_session_factory):
1527
messagebus.handle(commands.Allocate('otherorder', 'sku1', 30), uow)
1628
messagebus.handle(commands.Allocate('otherorder', 'sku2', 10), uow)
1729

18-
assert views.allocations('order1', uow) == [
30+
assert views.allocations('order1') == [
1931
{'sku': 'sku1', 'batchref': 'sku1batch'},
2032
{'sku': 'sku2', 'batchref': 'sku2batch'},
2133
]
@@ -28,6 +40,6 @@ def test_deallocation(sqlite_session_factory):
2840
messagebus.handle(commands.Allocate('o1', 'sku1', 40), uow)
2941
messagebus.handle(commands.ChangeBatchQuantity('b1', 10), uow)
3042

31-
assert views.allocations('o1', uow) == [
43+
assert views.allocations('o1') == [
3244
{'sku': 'sku1', 'batchref': 'b2'},
3345
]

0 commit comments

Comments
 (0)