Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/example/note/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ async def create_note(body: CreateNoteBody) -> JSONResponse:
errors: list[ValidationError] = []
if not body.title.strip():
errors.append(ValidationError("title", "Title must not be empty.", "required"))
if not body.body.strip():
errors.append(ValidationError("body", "Body must not be empty.", "required"))
if errors:
raise ValidationException(errors)

Expand All @@ -76,6 +78,8 @@ async def update_note(note_id: int, body: UpdateNoteBody) -> JSONResponse:
errors: list[ValidationError] = []
if not body.title.strip():
errors.append(ValidationError("title", "Title must not be empty.", "required"))
if not body.body.strip():
errors.append(ValidationError("body", "Body must not be empty.", "required"))
if errors:
raise ValidationException(errors)

Expand Down
7 changes: 7 additions & 0 deletions tests/example/note/test_list_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def test_create_note_empty_title_returns_422(client: TestClient) -> None:
assert r.json()["errors"][0]["field"] == "title"


def test_create_note_empty_title_and_body_returns_all_fields(client: TestClient) -> None:
r = client.post("/examples/notes", json={"title": "", "body": ""})
assert r.status_code == 422
fields = {e["field"] for e in r.json()["errors"]}
assert fields == {"title", "body"}


def test_get_nonexistent_note_returns_404(client: TestClient) -> None:
r = client.get("/examples/notes/9999")
assert r.status_code == 404
Expand Down
Loading