Repository navigation
test: add player service and route unit tests #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jishnu70
wants to merge
7
commits into
nanotaboada:master
Choose a base branch
from
jishnu70:test/player-unit-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
911c877
test: add player service unit tests
jishnu70 cf88903
test: add player service unit tests
jishnu70 6368138
test: add player route unit tests
jishnu70 46558e2
test(unit): isolate player unit tests
jishnu70 3f27a63
Merge branch 'nanotaboada:master' into test/player-unit-tests
jishnu70 5f4776b
Merge branch 'master' into test/player-unit-tests
nanotaboada b7e7b20
docs(adr): add ADR-0014 unit and integration test strategy (#490)
nanotaboada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(): | ||
| """Disable database migrations for unit tests.""" | ||
| pass | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add a return annotation to the fixture.
Annotate
apply_migrationswith-> None.Proposed fix
As per path instructions,
**/*.pyrequires type hints for function parameters and return values.📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Path instructions