Skip to content

Commit

Permalink
view now users redis, tweak tests+app. [redis_readmodel_view]
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Feb 24, 2021
1 parent 856ec5f commit ec9fb19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/allocation/entrypoints/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def allocate_endpoint():

@app.route("/allocations/<orderid>", methods=["GET"])
def allocations_view_endpoint(orderid):
uow = unit_of_work.SqlAlchemyUnitOfWork()
result = views.allocations(orderid, uow)
result = views.allocations(orderid)
if not result:
return "not found", 404
return jsonify(result), 200
16 changes: 7 additions & 9 deletions src/allocation/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from allocation.adapters import redis_eventpublisher
from allocation.service_layer import unit_of_work


def allocations(orderid: str, uow: unit_of_work.SqlAlchemyUnitOfWork):
with uow:
results = uow.session.execute(
"""
SELECT sku, batchref FROM allocations_view WHERE orderid = :orderid
""",
dict(orderid=orderid),
)
return [dict(r) for r in results]
def allocations(orderid: str):
batches = redis_eventpublisher.get_readmodel(orderid)
return [
{"batchref": b.decode(), "sku": s.decode()}
for s, b in batches.items()
]
20 changes: 17 additions & 3 deletions tests/integration/test_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
from datetime import date
from allocation import views
import pytest
import redis
from allocation import config, views
from allocation.domain import commands
from allocation.service_layer import messagebus, unit_of_work

today = date.today()


@pytest.fixture
def cleanup_redis():
r = redis.Redis(**config.get_redis_host_and_port())
yield
for k in r.keys():
print("cleaning up redis key", k)
r.delete(k)


pytestmark = pytest.mark.usefixtures("cleanup_redis")


def test_allocations_view(sqlite_session_factory):
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
messagebus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None), uow)
Expand All @@ -17,7 +31,7 @@ def test_allocations_view(sqlite_session_factory):
messagebus.handle(commands.Allocate("otherorder", "sku1", 30), uow)
messagebus.handle(commands.Allocate("otherorder", "sku2", 10), uow)

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

assert views.allocations("o1", uow) == [
assert views.allocations("o1") == [
{"sku": "sku1", "batchref": "b2"},
]

0 comments on commit ec9fb19

Please sign in to comment.