Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b0cc20

Browse files
committed
view now users redis, tweak tests+app. [redis_readmodel_view]
1 parent 5ddb83a commit 5b0cc20

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/allocation/entrypoints/flask_app.py

+1-2
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

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
from allocation.adapters import redis_eventpublisher
12
from allocation.service_layer import unit_of_work
23

34

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

tests/integration/test_views.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
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

68
today = date.today()
79

810

11+
@pytest.fixture
12+
def cleanup_redis():
13+
r = redis.Redis(**config.get_redis_host_and_port())
14+
yield
15+
for k in r.keys():
16+
print("cleaning up redis key", k)
17+
r.delete(k)
18+
19+
20+
pytestmark = pytest.mark.usefixtures("cleanup_redis")
21+
22+
923
def test_allocations_view(sqlite_session_factory):
1024
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
1125
messagebus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None), uow)
@@ -17,7 +31,7 @@ def test_allocations_view(sqlite_session_factory):
1731
messagebus.handle(commands.Allocate("otherorder", "sku1", 30), uow)
1832
messagebus.handle(commands.Allocate("otherorder", "sku2", 10), uow)
1933

20-
assert views.allocations("order1", uow) == [
34+
assert views.allocations("order1") == [
2135
{"sku": "sku1", "batchref": "sku1batch"},
2236
{"sku": "sku2", "batchref": "sku2batch"},
2337
]
@@ -30,6 +44,6 @@ def test_deallocation(sqlite_session_factory):
3044
messagebus.handle(commands.Allocate("o1", "sku1", 40), uow)
3145
messagebus.handle(commands.ChangeBatchQuantity("b1", 10), uow)
3246

33-
assert views.allocations("o1", uow) == [
47+
assert views.allocations("o1") == [
3448
{"sku": "sku1", "batchref": "b2"},
3549
]

0 commit comments

Comments
 (0)