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
+18 -9
View File
@@ -7,16 +7,27 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"crussell/db"
"crussell/testutils"
"crussell/testutils/testdb"
)
func TestMain(m *testing.M) {
pool, err := testdb.NewPool("")
if err != nil {
panic(err)
}
testdb.Migrate(&testing.T{}, pool)
db.DB = pool
code := m.Run()
pool.Close()
os.Exit(code)
}
func TestHealthCheck_OK(t *testing.T) {
// Setup test database
cleanup := testutils.SetupTestDB(t)
defer cleanup()
testdb.TruncateTables(t, db.DB)
// Create request and recorder
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
@@ -62,10 +73,9 @@ func TestHealthCheck_OK(t *testing.T) {
}
func TestHealthCheck_Degraded(t *testing.T) {
// Setup test database
cleanup := testutils.SetupTestDB(t)
testdb.TruncateTables(t, db.DB)
// Save original db.DB and set to nil to simulate degraded state
// Set db.DB to nil to simulate degraded state
originalDB := db.DB
db.DB = nil
@@ -105,7 +115,6 @@ func TestHealthCheck_Degraded(t *testing.T) {
t.Errorf("expected services.database 'error', got '%v'", services["database"])
}
// Restore original db.DB and cleanup
// Restore original db.DB
db.DB = originalDB
cleanup()
}