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:
@@ -19,25 +19,14 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// SetupTestDB initializes a test database and returns a cleanup function
|
||||
// Replaces the global db.DB with a test pool
|
||||
// SetupTestDB resets test data by truncating tables
|
||||
// Assumes db.DB is already set by TestMain
|
||||
func SetupTestDB(t *testing.T) func() {
|
||||
t.Helper()
|
||||
|
||||
pool := testdb.Pool(t)
|
||||
testdb.Migrate(t, pool)
|
||||
testdb.TruncateTables(t, db.DB)
|
||||
|
||||
// Replace global db.DB with test pool
|
||||
originalDB := db.DB
|
||||
db.DB = pool
|
||||
|
||||
// Initialize JWT for tests
|
||||
jwt.Init()
|
||||
|
||||
return func() {
|
||||
db.DB = originalDB
|
||||
pool.Close()
|
||||
}
|
||||
return func() {}
|
||||
}
|
||||
|
||||
// MakeRequest makes an HTTP request to a handler with optional JWT token
|
||||
|
||||
@@ -204,6 +204,7 @@ func TruncateTables(t *testing.T, pool *pgxpool.Pool) {
|
||||
"booking_services",
|
||||
"payments",
|
||||
"bookings",
|
||||
"booking_edit_requests",
|
||||
"user_patch_tests",
|
||||
"patch_tests",
|
||||
"services",
|
||||
@@ -212,8 +213,10 @@ func TruncateTables(t *testing.T, pool *pgxpool.Pool) {
|
||||
"user_notification_preferences",
|
||||
"time_blockers",
|
||||
"working_hours",
|
||||
"exceptional_group_applications",
|
||||
"exceptional_working_hours",
|
||||
"exceptional_working_hours_groups",
|
||||
"business_settings",
|
||||
"users",
|
||||
"images",
|
||||
"tags",
|
||||
|
||||
Reference in New Issue
Block a user