Skip to content

Commit 07c4c95

Browse files
committed
Fix event endpoint tests and typing
1 parent fd0869c commit 07c4c95

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

http_app/routes/events.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import typing
2-
from typing import Any, Dict, List
2+
from typing import Dict, List
33

44
from fastapi import APIRouter, Body, Header, HTTPException
5+
from fastapi.openapi.models import Example
56
from pydantic import BaseModel
67

78
from domains.books.events import BookCreatedV1
@@ -23,7 +24,7 @@ def _event_registry() -> Dict[str, typing.Type[BaseModel]]:
2324
}
2425

2526

26-
def _event_schema_examples() -> dict[str, dict[str, Any]]:
27+
def _event_schema_examples() -> dict[str, Example]:
2728
missing_example_message = (
2829
"No example has been added to this event but you can"
2930
" still explore the event schema. (Ask the developer"
@@ -32,11 +33,11 @@ def _event_schema_examples() -> dict[str, dict[str, Any]]:
3233
)
3334

3435
return {
35-
k: {
36-
"value": getattr(v, "model_config", {})
36+
k: Example(
37+
value=getattr(v, "model_config", {})
3738
.get("json_schema_extra", {})
3839
.get("examples", [missing_example_message])[0]
39-
}
40+
)
4041
for k, v in _event_registry().items()
4142
}
4243

@@ -85,6 +86,5 @@ async def submit_event(
8586
"application/cloudevents+json; charset=UTF-8"
8687
] = Header(),
8788
) -> None:
88-
# A better approach than if/else should be used when we have multiple event types
89-
if isinstance(event_data, BookCreatedV1):
90-
await BookService().book_created_event_handler(event_data.data.book_id)
89+
# Some routing will be necessary when multiple event types will be supported
90+
await BookService().book_created_event_handler(event_data.data.book_id)

tests/http_app/routes/test_events.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from unittest.mock import patch
1+
from unittest.mock import AsyncMock, MagicMock, patch
22

33
from httpx import AsyncClient
44

5+
from domains.books import service
56
from domains.books.events import BookCreatedV1
67
from domains.common.cloudevent_base import BaseEvent
78

@@ -39,12 +40,16 @@ async def test_event_returns_204(testapp):
3940
fake_event = BookCreatedV1(
4041
data={"book_id": 0, "title": "string", "author_name": "string"},
4142
)
42-
async with AsyncClient(app=testapp, base_url="http://test") as ac:
43-
response = await ac.post(
44-
"/events",
45-
headers={"content-type": "application/cloudevents+json; charset=UTF-8"},
46-
content=fake_event.model_dump_json(),
47-
)
43+
svc = MagicMock(autospec=service.BookService)
44+
svc.book_created_event_handler = AsyncMock(return_value=None)
45+
with patch("domains.books.service.BookService.__new__", return_value=svc):
46+
async with AsyncClient(app=testapp, base_url="http://test") as ac:
47+
response = await ac.post(
48+
"/events",
49+
headers={"content-type": "application/cloudevents+json; charset=UTF-8"},
50+
content=fake_event.model_dump_json(),
51+
)
52+
svc.book_created_event_handler.assert_called_once()
4853
assert response.status_code == 204
4954

5055

0 commit comments

Comments
 (0)