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
@@ -13,23 +13,23 @@ import (
"crussell/testutils/fixtures"
)
func setupRefundTestWithDiscount(t *testing.T) (string, string, float64) {
func setupRefundTestWithDiscount(t *testing.T, ctx context.Context, q db.Querier) (string, string, float64) {
t.Helper()
userID, err := fixtures.CreateTestUser(db.DB)
userID, err := fixtures.CreateTestUser(q)
if err != nil {
t.Fatalf("failed to create user: %v", err)
}
serviceID, err := fixtures.CreateTestService(db.DB)
serviceID, err := fixtures.CreateTestService(q)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
bookingID, err := fixtures.CreateTestBookingAtTime(db.DB, userID, serviceID, time.Date(2099, 12, 31, 10, 0, 0, 0, time.UTC))
bookingID, err := fixtures.CreateTestBookingAtTime(q, userID, serviceID, time.Date(2099, 12, 31, 10, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("failed to create booking: %v", err)
}
// Insert a real cash payment of 50
_, err = db.DB.Exec(context.Background(), `
_, err = q.Exec(ctx, `
INSERT INTO payments (booking_id, payment_type, payment_method, amount, status, created_at, updated_at)
VALUES ($1, 'full', 'cash', 5000, 'completed', NOW(), NOW())
`, bookingID)
@@ -38,7 +38,7 @@ func setupRefundTestWithDiscount(t *testing.T) (string, string, float64) {
}
// Insert a discount payment record (should be excluded from refund)
_, err = db.DB.Exec(context.Background(), `
_, err = q.Exec(ctx, `
INSERT INTO payments (booking_id, payment_type, payment_method, amount, status, created_at, updated_at)
VALUES ($1, 'partial', 'discount', 500, 'completed', NOW(), NOW())
`, bookingID)
@@ -47,7 +47,7 @@ func setupRefundTestWithDiscount(t *testing.T) (string, string, float64) {
}
// Insert an on_the_house payment record (should also be excluded)
_, err = db.DB.Exec(context.Background(), `
_, err = q.Exec(ctx, `
INSERT INTO payments (booking_id, payment_type, payment_method, amount, status, created_at, updated_at)
VALUES ($1, 'partial', 'on_the_house', 1000, 'completed', NOW(), NOW())
`, bookingID)
@@ -59,14 +59,15 @@ func setupRefundTestWithDiscount(t *testing.T) (string, string, float64) {
}
func TestProcessCancellationRefund_ExcludesDiscountPayments(t *testing.T) {
testutils.SetupTestDB(t)
userID, bookingID, total := setupRefundTestWithDiscount(t)
t.Parallel()
ctx, tx := testutils.SetupTestTx(t)
userID, bookingID, total := setupRefundTestWithDiscount(t, ctx, tx)
farFuture := time.Date(2099, 12, 31, 10, 0, 0, 0, time.UTC)
now := farFuture.Add(-72 * time.Hour).Add(-1 * time.Hour) // >72h before
result, err := ProcessCancellationRefund(
context.Background(), bookingID, total, 50,
ctx, bookingID, total, 50,
farFuture, now, "client_cancelled", &userID,
)
if err != nil {
@@ -80,12 +81,13 @@ func TestProcessCancellationRefund_ExcludesDiscountPayments(t *testing.T) {
}
func TestProcessCancellationRefund_ExcludesOnTheHousePayments(t *testing.T) {
testutils.SetupTestDB(t)
userID, bookingID, total := setupRefundTestWithDiscount(t)
t.Parallel()
ctx, tx := testutils.SetupTestTx(t)
userID, bookingID, total := setupRefundTestWithDiscount(t, ctx, tx)
// Make on_the_house the only non-discount payment by marking the 50 cash as a payment that gets refunded
// but also add a pure on_the_house booking with no real money
_, err := db.DB.Exec(context.Background(), `
_, err := tx.Exec(ctx, `
INSERT INTO payments (booking_id, payment_type, payment_method, amount, status, created_at, updated_at)
VALUES ($1, 'partial', 'discount', 2500, 'completed', NOW(), NOW())
`, bookingID)
@@ -97,7 +99,7 @@ func TestProcessCancellationRefund_ExcludesOnTheHousePayments(t *testing.T) {
now := farFuture.Add(-72 * time.Hour).Add(-1 * time.Hour)
result, err := ProcessCancellationRefund(
context.Background(), bookingID, total, 50,
ctx, bookingID, total, 50,
farFuture, now, "client_cancelled", &userID,
)
if err != nil {