Skip to content
Open
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ This project uses famous football coaches as release codenames, following an A-Z
- ADR-0011: Use Coach-Themed Semantic Versioning
- ADR-0012: Adopt AI-Assisted Development Workflow
- ADR-0013: Adopt Spec-Driven Development (SDD)
- ADR-0014: Unit and Integration Test Strategy (supersedes ADR-0007) (#490)
- `.claude/skills/create-issue/SKILL.md` and `.claude/skills/add-endpoint/SKILL.md`:
extracted from `CLAUDE.md` as on-demand skills, so the SDD issue templates and
the endpoint/schema-change recipes load only when invoked instead of every
session; each starts with a top-level heading per `markdownlint` MD041
- `tests/unit/`: isolated unit tests for `services/player_service.py` and
`routes/player_route.py` (`test_player_service.py`, `test_player_route.py`,
`conftest.py`), covering happy paths, error/rollback branches (`SQLAlchemyError`
and HTTP 500 responses), and cache hit/miss behavior without database migrations
(#490)

### Changed

Expand Down Expand Up @@ -81,6 +87,9 @@ This project uses famous football coaches as release codenames, following an A-Z
finishing touch and `knowledge_base.code_guidelines` to reference `CLAUDE.md`;
added `alembic/versions/**/*.py` path instruction enforcing append-only
migration policy (#590)
- `services/player_service.py` and `routes/player_route.py`: removed
`# pragma: no cover` markers from error-handling branches now covered by unit
tests (#490)

### Fixed

Expand Down
11 changes: 8 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ used by the Docker entrypoint.
(not 422 — it is a semantic error, not a validation failure)
- **Logging**: `logging` module only; never `print()`
- **Tests**: integration tests against the real SQLite DB (seeded via
Alembic migrations) via `TestClient` — no mocking. Naming pattern
`test_request_{method}_{resource}_{context}_response_{outcome}`;
Alembic migrations) via `TestClient` — no mocking. Unit tests in
`tests/unit/` isolate dependencies with mocks and do not require database
migrations (see ADR-0014). Naming pattern
`test_request_{method}_{resource}_{context}_response_{outcome}` (routes);
`test_{function}_{context}_returns_{outcome}` (service unit tests);
docstrings single-line, concise; `tests/player_fake.py` for test data;
`tests/conftest.py` provides a `function`-scoped `client` fixture for
isolation; `tests/test_main.py` excluded from Black;
Expand Down Expand Up @@ -107,6 +110,8 @@ a release tag with a coach name not in that table.
docstring updates that don't change the API contract)
- Tests in `tests/` — maintain async patterns, naming convention, and
integration-test approach (no mocking)
- Tests in `tests/unit/` — maintain async patterns, naming convention, and
unit-test isolation (mock dependencies as needed)
- Documentation and docstring updates
- Lint/format fixes
- Refactoring within existing architectural patterns
Expand Down Expand Up @@ -160,6 +165,6 @@ confirms.

## Architecture Decision Records

Significant architectural decisions are documented in `docs/adr/` (ADR-0001–0013).
Significant architectural decisions are documented in `docs/adr/` (ADR-0001–0014).
Load these before proposing structural changes. When a proposal would change an
accepted decision, create a new ADR rather than editing the existing one.
3 changes: 1 addition & 2 deletions docs/adr/0007-integration-only-test-strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Date: 2026-03-21

## Status

Accepted. Unit tests with mocking for the service and route layers are
under consideration — tracked in issue #490.
Superseded by [ADR-0014](0014-unit-and-integration-test-strategy.md).

## Context

Expand Down
99 changes: 99 additions & 0 deletions docs/adr/0014-unit-and-integration-test-strategy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# ADR-0014: Unit and Integration Test Strategy

Date: 2026-10-26

## Status

Accepted. Supersedes [ADR-0007](0007-integration-only-test-strategy.md).

## Context

ADR-0007 established integration tests only: every test exercises the
full stack via `TestClient` against the real SQLite database, with no
mocking. Its consequences already flagged two gaps, tracked in issue
#490:

- **Unreachable error paths** — the `SQLAlchemyError` handlers in
`services/player_service.py` and the HTTP 500 branches in
`routes/player_route.py` cannot be triggered against a healthy
SQLite database. They are excluded from coverage with
`# pragma: no cover`, so the reported coverage says nothing about
them.
- **No layer isolation** — an integration test failure does not
pinpoint whether the route, the service, or the database is at
fault.

`unittest.mock.AsyncMock` with `spec=AsyncSession` produces a mock
whose async methods (`commit`, `refresh`, `execute`, `delete`) are
awaitable and whose sync methods (`add`) are not, matching the real
`AsyncSession` interface. Route functions are plain `async def`
callables and can be invoked directly with their dependencies passed
as arguments, bypassing FastAPI's dependency injection.

## Alternatives Considered

- **Keep integration-only (ADR-0007)** — the error paths stay
untested and excluded from coverage. Simulating database failures
against SQLite (corrupting the file, revoking permissions) is
fragile and platform-dependent.
- **Fault injection via dependency overrides** — override
`generate_async_session` in `TestClient` with a session that raises
on `commit`. This covers the error paths through HTTP but couples
the tests to the full stack anyway and still offers no layer
isolation.
- **Replace integration tests with unit tests** — fast and isolated,
but mocks can silently diverge from real behavior; ADR-0007's
argument for exercising the real database still holds for the happy
paths and the HTTP contract.

## Decision

We will maintain two test tiers:

- **Integration tests** in `tests/` (`test_main.py`,
`test_migrations.py`) remain the authority for the API contract:
status codes, headers, response shapes, and real database
behavior. No mocking.
- **Unit tests** in `tests/unit/` test the route and service layers in
isolation. Service tests mock `AsyncSession`
(`AsyncMock(spec=AsyncSession)`); route tests mock the
`player_service` functions and the cache. Unit tests do not apply
Alembic migrations (`tests/unit/conftest.py` overrides the
session-scoped `apply_migrations` fixture).

Naming conventions:

- Route unit tests follow the integration pattern
`test_request_{method}_{resource}_{context}_response_{outcome}`,
since they verify the same HTTP-level outcomes.
- Service unit tests use `test_{function}_{context}_returns_{outcome}`
(e.g. `test_create_async_commit_error_returns_none`), since there is
no HTTP request involved.

Error branches covered by unit tests must not carry
`# pragma: no cover`; the marker is reserved for code that no test
tier can reach.

This keeps the integration suite as the source of truth for
observable behavior while giving the error paths real, counted
coverage.

## Consequences

**Positive:**

- Error-handling paths (rollback on `SQLAlchemyError`, HTTP 500
responses) are exercised and counted in coverage.
- Unit test failures pinpoint the failing layer.
- Unit tests need no database or migrations and run fast.

**Negative:**

- Mocks encode assumptions about `AsyncSession` and the service API;
if either changes, unit tests may keep passing while integration
tests fail, or the reverse. Both tiers must be updated together.
- Two naming conventions and two fixture setups to maintain.
- Route unit tests call route functions directly, so they do not
verify FastAPI wiring (path parameters, dependency injection,
response model serialization). That remains the integration suite's
job.
3 changes: 2 additions & 1 deletion docs/adr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ one and the three-part test.
| [0004](0004-squad-number-as-mutation-key.md) | Squad Number as the Mutation Key | Accepted | 2026-03-21 |
| [0005](0005-full-replace-put-no-patch.md) | Full Replace PUT, No PATCH | Accepted | 2026-03-21 |
| [0006](0006-in-memory-caching-with-aiocache.md) | In-Memory Caching with aiocache | Accepted | 2026-03-21 |
| [0007](0007-integration-only-test-strategy.md) | Integration-Only Test Strategy | Accepted | 2026-03-21 |
| [0007](0007-integration-only-test-strategy.md) | Integration-Only Test Strategy | Superseded | 2026-03-21 |
| [0008](0008-layered-architecture.md) | Layered Architecture with FastAPI Dependency Injection | Accepted | 2026-04-02 |
| [0009](0009-docker-and-compose-strategy.md) | Docker and Compose Strategy | Accepted | 2026-04-02 |
| [0010](0010-alembic-migration-based-schema-management.md) | Alembic Migration-Based Schema Management | Accepted | 2026-04-09 |
| [0011](0011-coach-themed-versioning.md) | Use Coach-Themed Semantic Versioning | Accepted | 2026-06-10 |
| [0012](0012-ai-assisted-development-workflow.md) | Adopt AI-Assisted Development Workflow | Accepted | 2026-06-10 |
| [0013](0013-spec-driven-development.md) | Adopt Spec-Driven Development (SDD) | Accepted | 2026-06-10 |
| [0014](0014-unit-and-integration-test-strategy.md) | Unit and Integration Test Strategy | Accepted | 2026-10-26 |
11 changes: 6 additions & 5 deletions routes/player_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

from typing import Annotated, List
from uuid import UUID
from fastapi import APIRouter, Body, Depends, HTTPException, status, Path, Response
from sqlalchemy.ext.asyncio import AsyncSession

from aiocache import SimpleMemoryCache
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Response, status
from sqlalchemy.ext.asyncio import AsyncSession

from databases.player_database import generate_async_session
from models.player_model import PlayerRequestModel, PlayerResponseModel
Expand Down Expand Up @@ -79,7 +80,7 @@ async def post_async(
detail="A Player with this squad number already exists.",
)
player = await player_service.create_async(async_session, player_model)
if player is None: # pragma: no cover
if player is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create the Player due to a database error.",
Expand Down Expand Up @@ -231,7 +232,7 @@ async def put_async(
updated = await player_service.update_by_squad_number_async(
async_session, squad_number, player_model
)
if not updated: # pragma: no cover
if not updated:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update the Player due to a database error.",
Expand Down Expand Up @@ -271,7 +272,7 @@ async def delete_async(
deleted = await player_service.delete_by_squad_number_async(
async_session, squad_number
)
if not deleted: # pragma: no cover
if not deleted:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to delete the Player due to a database error.",
Expand Down
12 changes: 6 additions & 6 deletions services/player_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from uuid import UUID

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession

from models.player_model import PlayerRequestModel
from schemas.player_schema import Player
Expand Down Expand Up @@ -52,7 +52,7 @@ async def create_async(
await async_session.commit()
await async_session.refresh(player)
return player
except SQLAlchemyError as error: # pragma: no cover
except SQLAlchemyError as error:
logger.exception("Error trying to create the Player: %s", error)
await async_session.rollback()
return None
Expand Down Expand Up @@ -133,7 +133,7 @@ async def update_by_squad_number_async(
True if the Player was updated successfully, False otherwise.
"""
player = await retrieve_by_squad_number_async(async_session, squad_number)
if player is None: # pragma: no cover
if player is None:
logger.error("Player not found for update: squad_number=%s", squad_number)
return False
player.first_name = player_model.first_name
Expand All @@ -149,7 +149,7 @@ async def update_by_squad_number_async(
try:
await async_session.commit()
return True
except SQLAlchemyError as error: # pragma: no cover
except SQLAlchemyError as error:
logger.exception("Error trying to update the Player: %s", error)
await async_session.rollback()
return False
Expand All @@ -172,14 +172,14 @@ async def delete_by_squad_number_async(
True if the Player was deleted successfully, False otherwise.
"""
player = await retrieve_by_squad_number_async(async_session, squad_number)
if player is None: # pragma: no cover
if player is None:
logger.error("Player not found for delete: squad_number=%s", squad_number)
return False
try:
await async_session.delete(player)
await async_session.commit()
return True
except SQLAlchemyError as error: # pragma: no cover
except SQLAlchemyError as error:
logger.exception("Error trying to delete the Player: %s", error)
await async_session.rollback()
return False
Empty file added tests/unit/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Fixtures for the unit tests.
"""

import pytest


@pytest.fixture(scope="session", autouse=True)
def apply_migrations() -> None:
"""Disable database migrations for unit tests."""
pass
Loading