refactor(backend): migrate test infrastructure to isolated databases

Add CreateTestDatabase function for parallel isolated test databases per package.

- Add CreateTestDatabase() for isolated test DBs (parallel-safe)
- Move all TestMain functions to per-package testmain_test.go files
- Remove old TestMain from handlers_test.go, jwt_test.go, main_test.go
- Add JWT init guard in main.go to skip when -test.* flags detected
- Update testdb.go with admin DSN and proper cleanup
- Rename test database to crussell_test_db for consistency
- Replace testdb.NewPool + testdb.Migrate pattern with CreateTestDatabase

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-20 16:56:57 +01:00
co-authored by Sisyphus
parent 482958584a
commit 9c68918c20
20 changed files with 403 additions and 244 deletions
+4 -7
View File
@@ -15,7 +15,7 @@ func resetEnv() {
os.Setenv("POSTGRES_USER", "myuser")
os.Setenv("POSTGRES_PASSWORD", "mypassword")
os.Setenv("POSTGRES_HOST", "localhost")
os.Setenv("POSTGRES_DB", "crussell_test")
os.Setenv("POSTGRES_DB", "crussell_test_db")
}
func closePool() {
@@ -25,6 +25,8 @@ func closePool() {
}
}
var testDBName = "crussell_test_db"
// =============================================================================
// Happy path — connect + ping
// =============================================================================
@@ -177,9 +179,4 @@ func TestGetEnv_ReturnsEmptyWhenUnset(t *testing.T) {
// Clean-up — restore env after all tests
// =============================================================================
func TestMain(m *testing.M) {
resetEnv()
code := m.Run()
closePool()
os.Exit(code)
}
+21
View File
@@ -0,0 +1,21 @@
//go:build test
// +build test
package db
import (
"os"
"testing"
"crussell/testutils/testdb"
)
func TestMain(m *testing.M) {
pool := testdb.CreateTestDatabase(testDBName)
pool.Close() // db.Connect() creates its own pool
resetEnv()
code := m.Run()
closePool()
testdb.DestroyTestDatabase(nil, testDBName)
os.Exit(code)
}