refactor: optimize test DB setup — TestMain per package, truncate-only between tests

- Add TestMain to all 10 test packages (schema DROP+CREATE runs once per package)
- Convert per-test setupTestDB to resetTestData (TRUNCATE only, ~60% faster)
- Add 3 missing tables to TruncateTables (booking_edit_requests, exceptional_group_applications, business_settings)
- Remove dead truncateDiscountTables helper
- Consolidate discount_test.go into package bookings (was external test package)
- Update testutils.SetupTestDB to truncate-only
- Fix unused imports across user, bookings, and handlers packages
- Verify: 286 passing, 2 skipped, 0 failures with -count=2 (no state leakage)
This commit is contained in:
2026-05-10 17:27:51 +01:00
parent 83c62ffb97
commit e73c96b653
26 changed files with 554 additions and 920 deletions
+10 -20
View File
@@ -29,8 +29,7 @@ import (
// TestAdminUsers_List verifies that an admin can list all users in the
// system with their details including account role and type.
func TestAdminUsers_List(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test users
_, err := db.DB.Exec(context.Background(), `
@@ -68,8 +67,7 @@ func TestAdminUsers_List(t *testing.T) {
// TestAdminUsers_Get tests that an admin can retrieve detailed information
// about a specific user including their profile and account settings.
func TestAdminUsers_Get(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string
@@ -106,8 +104,7 @@ func TestAdminUsers_Get(t *testing.T) {
// TestAdminUsers_Get_NotFound verifies that requesting details for a
// non-existent user returns HTTP 404 Not Found.
func TestAdminUsers_Get_NotFound(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
handler := http.HandlerFunc(user.GetAdminUserHandler)
// Use 12-char or less ID to avoid CHAR(12) constraint error
@@ -122,8 +119,7 @@ func TestAdminUsers_Get_NotFound(t *testing.T) {
// identifies which services require patch tests and returns only those services
// the user is eligible for based on age requirements.
func TestAdminUsers_PatchTests_Eligible(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string
@@ -199,8 +195,7 @@ func TestAdminUsers_PatchTests_Eligible(t *testing.T) {
// user already has a valid patch test on file, that service is filtered out
// from the eligible list (since they've already completed it).
func TestAdminUsers_PatchTests_Eligible_WithExisting(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string
@@ -286,8 +281,7 @@ func TestAdminUsers_PatchTests_Eligible_WithExisting(t *testing.T) {
// TestAdminUsers_AddPatchTest verifies that an admin can record a patch
// test completion for a user, creating a user_patch_tests record.
func TestAdminUsers_AddPatchTest(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string
@@ -346,8 +340,7 @@ func TestAdminUsers_AddPatchTest(t *testing.T) {
}
func TestAdminUsers_AddPatchTest_InvalidPatchTest(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string
@@ -374,8 +367,7 @@ func TestAdminUsers_AddPatchTest_InvalidPatchTest(t *testing.T) {
// TestAdminUsers_NonAdmin verifies that non-admin users receive HTTP 403
// Forbidden when attempting to list users, get user details, or manage patch tests.
func TestAdminUsers_NonAdmin(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create regular user in DB
_, err := db.DB.Exec(context.Background(), `
@@ -429,8 +421,7 @@ func TestAdminUsers_NonAdmin(t *testing.T) {
// TestAdminUsers_Get_Success is an additional test verifying admin can
// retrieve user details including ID, name, email, and account role.
func TestAdminUsers_Get_Success(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create a test user
var userID string
@@ -478,8 +469,7 @@ func TestAdminUsers_Get_Success(t *testing.T) {
// TestAdminUsers_AddPatchTest_Duplicate verifies that recording the same patch test
// twice updates the tested_at timestamp (upsert behavior).
func TestAdminUsers_AddPatchTest_Duplicate(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
resetTestData(t)
// Create test user
var userID string