8b:n blog kaatui koska test_list assertoi tarkkaa määrää vaikka testit jakavat saman tietokannan.
1.9 KiB
1.9 KiB
Generate a FastAPI project with SQLAlchemy and SQLite. Follow the REFERENCE IMPLEMENTATION exactly.
Generate these 4 files with === markers:
=== models.py === === schemas.py === === main.py === === test_main.py ===
Key patterns (copy from reference):
- class Base(DeclarativeBase): pass
- Mapped[str] = mapped_column(String(255))
- Mapped[str | None] = mapped_column(Text, default=None)
- model_config = ConfigDict(from_attributes=True)
- model_dump() not dict()
- POST 201, GET list, GET by id 404, PUT, DELETE 204
FOREIGN KEYS (when spec has relationships):
- Child entity gets parent_id field: Mapped[int] = mapped_column(ForeignKey("parents.id"))
- Import ForeignKey: from sqlalchemy import ForeignKey
- Create schema includes parent_id: int
- Test creates parent FIRST, then child with parent's id
Example FK pattern in models.py:
class Author(Base):
__tablename__ = "authors"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
name: Mapped[str] = mapped_column(String(255))
class Post(Base):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
title: Mapped[str] = mapped_column(String(255))
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"))
Example FK test pattern:
def test_create_post():
author = client.post("/authors/", json={"name": "Kirjailija"}).json()
response = client.post("/posts/", json={"title": "Artikkeli", "author_id": author["id"]})
assert response.status_code == 201
CRITICAL:
- Use ONLY fields from the JSON spec — no created_at or extra fields
- Generate EXACTLY 6 tests per entity: create, list, get_by_id, not_found, update, delete
- No search, filter, or other extra tests
- test_list: assert len(response.json()) >= 1, NEVER assert == 1 (database is shared between tests)
- test_create for child entities: create parent FIRST, use parent's id
- No markdown fences in output