4.1 KiB
Backend Testing Plan
Overview
Add comprehensive tests for the training-tracker backend following Go best practices and the CLAUDE.md guidelines (table-driven tests).
Scope: Both unit tests (handlers with mocks) AND integration tests (repositories with testcontainers)
Plan storage: Copy this plan to training-tracker/plans/testing-plan.md
Current State
- No existing tests - greenfield testing
- 3 packages to test:
api,storage,models - Dependencies: handlers → repositories → database
Testing Strategy
1. Unit Tests for API Handlers
Test HTTP handlers with mocked repositories.
Files to create:
internal/api/exercises_test.gointernal/api/plans_test.gointernal/api/sessions_test.go
Approach:
- Define repository interfaces
- Create mock implementations
- Use
httptest.NewRecorder()andhttptest.NewRequest() - Table-driven tests for each endpoint
Test cases per handler:
- List: empty result, multiple results
- GetByID: found, not found
- Create: valid input, missing required fields, invalid type
- Update: found, not found, validation errors
- Delete: found, not found
2. Integration Tests for Storage Layer
Test repositories against real PostgreSQL using testcontainers.
Files to create:
internal/storage/exercises_test.gointernal/storage/plans_test.gointernal/storage/sessions_test.gointernal/storage/db_test.go
Approach:
- Use
testcontainers-goto spin up PostgreSQL - Run migrations before tests
- Test CRUD operations end-to-end
- Test transaction rollback scenarios
Test cases:
- CRUD operations for each entity
- Cascading deletes
- Transaction handling in plans (create/update)
- NULL field handling
- Foreign key constraints
3. Test File Structure
backend/internal/
├── api/
│ ├── exercises_test.go
│ ├── plans_test.go
│ ├── sessions_test.go
│ └── mocks_test.go # Shared mock implementations
├── storage/
│ ├── testhelpers_test.go # Shared test database setup
│ ├── exercises_test.go
│ ├── plans_test.go
│ └── sessions_test.go
└── models/
└── (no tests needed - pure data structures)
Implementation Steps
-
Create plans folder and store this plan:
- Create
training-tracker/plans/ - Copy this plan to
training-tracker/plans/testing-plan.md
- Create
-
Add test dependencies to go.mod:
github.com/testcontainers/testcontainers-go- Use standard library for assertions (no testify)
-
Create repository interfaces in
internal/api/:ExerciseRepositoryinterfacePlanRepositoryinterfaceSessionRepositoryinterface
-
Create mock implementations for unit testing handlers
-
Create test helpers for integration tests:
- Shared PostgreSQL container setup
- Database cleanup between tests
-
Write handler unit tests (table-driven):
- All CRUD operations
- Validation error paths
- Not found scenarios
-
Write storage integration tests (table-driven):
- All repository methods
- Edge cases and error conditions
-
Update CLAUDE.md with testing instructions
Files to Modify
backend/go.mod- add test dependenciesbackend/internal/api/router.go- extract interfaces for testabilitybackend/CLAUDE.md- add testing instructions
Files to Create
plans/testing-plan.md- this plan documentbackend/internal/api/interfaces.go- repository interfacesbackend/internal/api/mocks_test.go- mock implementationsbackend/internal/api/exercises_test.gobackend/internal/api/plans_test.gobackend/internal/api/sessions_test.gobackend/internal/storage/testhelpers_test.gobackend/internal/storage/exercises_test.gobackend/internal/storage/plans_test.gobackend/internal/storage/sessions_test.go
Verification
cd backend
# Run all tests
go test ./...
# Run with verbose output
go test -v ./...
# Run with coverage
go test -cover ./...
# Run only unit tests (fast)
go test -short ./...
# Run specific package
go test -v ./internal/api/...