Concise rules for writing and maintaining tests. For comprehensive examples and patterns, see TESTING.md.
- Use
test_util.GetUniqueID()for all database record IDs - Use
test_util.GetUniqueUserID()for all user IDs - Use
test_util.NonExistentIDfor 404 test scenarios - Use
test_util.NoDBTestIDfor tests that skip database operations - Never hardcode numeric IDs or user ID strings in tests
Every test package with database access must have a TestMain that:
- Creates a timestamped SQLite database file (e.g.,
{timestamp}-dashboard-template.db) - Resets ID generators:
test_util.ResetIDGenerator(),test_util.ResetUserIDGenerator() - Reserves special IDs:
test_util.ReserveID(test_util.NoDBTestID),test_util.ReserveID(test_util.NonExistentID) - Reserves hardcoded user IDs used in legacy tests (e.g.,
"user-123","different-user") - Cleans up the database file after tests complete
- One test file per feature:
get_widgets_test.go,update_widget_test.go,copy_widget_test.go - Common setup functions live in
server_test.go:setupRouter(),withIdentityContext() - Use table-driven tests (
t.Runsubtests) for multiple scenarios of the same operation - Test all paths: success, not-found, unauthorized, validation failure, edge cases
test_util.GenerateIdentityStruct()creates a default test identitytest_util.GenerateIdentityStructFromTemplate()creates identity with specific user IDwithIdentityContext(req)injects default identity into request contextwithCustomIdentityContext(req, identity)injects specific identity- Always test authorization: verify users cannot access other users' templates
test_util.MockDashboardTemplate()creates a template with unique IDstest_util.MockDashboardTemplateWithSpecificUser(userID)creates for a specific usertest_util.MockDashboardTemplateWithUniqueID()creates with guaranteed unique ID
- Tests must not depend on execution order
- Tests must not share mutable state
- Each test creates its own data with unique IDs
- Test databases are per-run (timestamped filenames), preventing cross-run interference
make test # All tests with coverage
go test ./pkg/server -v # Verbose server tests
go test ./pkg/server -race # Race condition detection
go test ./pkg/server -count=3 # Reliability check- Run
make test- all tests must pass - Check for leftover
.dbfiles:find . -name "*-dashboard-template.db" - Remove any debug
t.Logforfmt.Printlnadded during development