refactor(backend): update test files for PoolProxy and per-test transactions

Migrate all test files from SetupTestDB/db.DB pattern to per-test transactions:

- Replace SetupTestDB(t) with SetupTestTx(t) for context + transaction
- Replace db.DB.Query/QueryRow/Exec with tx.Query/QueryRow/Exec
- Replace context.Background() with context from SetupTestTx
- Replace defer rows.Close() pattern with explicit rows.Close()
- Add testdb.SeedBaseline(pool) to all TestMain functions
- Wire db.Conn = db.NewPoolProxy(pool) in all TestMain functions

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-21 19:29:24 +01:00
co-authored by Sisyphus
parent 3d0e2afc4c
commit 220a0ef6e8
57 changed files with 5911 additions and 6235 deletions
+6 -7
View File
@@ -7,16 +7,15 @@ import (
"net/http"
"testing"
"crussell/db"
"crussell/testutils"
"crussell/testutils/fixtures"
)
func TestPatchTests_CRUD(t *testing.T) {
testutils.SetupTestDB(t)
ctx, tx := testutils.SetupTestTx(t)
// Create a service to link
serviceID, err := fixtures.CreateTestService(db.DB)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
@@ -28,7 +27,7 @@ func TestPatchTests_CRUD(t *testing.T) {
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", req)
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", req, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d", w.Code)
}
@@ -38,7 +37,7 @@ func TestPatchTests_CRUD(t *testing.T) {
}
parseResponseBody(w, &created)
w = makeAdminRequest(http.HandlerFunc(GetPatchTests), "GET", "/api/admin/patch-tests", nil)
w = makeAdminRequest(http.HandlerFunc(GetPatchTests), "GET", "/api/admin/patch-tests", nil, ctx)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
@@ -51,12 +50,12 @@ func TestPatchTests_CRUD(t *testing.T) {
newName := "Updated Name"
updateReq := UpdatePatchTestRequest{Name: &newName}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq)
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
w = makeAdminRequest(http.HandlerFunc(DeletePatchTest), "DELETE", "/api/admin/patch-tests/"+created.ID, nil)
w = makeAdminRequest(http.HandlerFunc(DeletePatchTest), "DELETE", "/api/admin/patch-tests/"+created.ID, nil, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d", w.Code)
}