CodeBench: code-small — FK update-testiesimerkki (author_id mukana PUT:ssa)

8b:n test_update_article palautti 422 koska author_id puuttui.
Lisätty konkreettinen test_update_post esimerkki FK-kentän kanssa.
This commit is contained in:
2026-04-14 14:15:09 +03:00
parent 06089a58b2
commit 8e9fbc5422

View File

@@ -35,12 +35,18 @@ class Post(Base):
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id")) author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"))
``` ```
Example FK test pattern: Example FK test patterns:
``` ```
def test_create_post(): def test_create_post():
author = client.post("/authors/", json={"name": "Kirjailija"}).json() author = client.post("/authors/", json={"name": "Kirjailija"}).json()
response = client.post("/posts/", json={"title": "Artikkeli", "author_id": author["id"]}) response = client.post("/posts/", json={"title": "Artikkeli", "author_id": author["id"]})
assert response.status_code == 201 assert response.status_code == 201
def test_update_post():
author = client.post("/authors/", json={"name": "Päivittäjä"}).json()
created = client.post("/posts/", json={"title": "Vanha", "author_id": author["id"]}).json()
response = client.put(f"/posts/{created['id']}", json={"title": "Uusi", "author_id": author["id"]})
assert response.status_code == 201
``` ```
CRITICAL: CRITICAL: