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:
@@ -8,53 +8,48 @@ import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"crussell/db"
|
||||
"crussell/testutils"
|
||||
"crussell/handlers/bookings"
|
||||
"crussell/testutils"
|
||||
"crussell/testutils/fixtures"
|
||||
)
|
||||
|
||||
// TestAdminBookings_Get_EnrichedFields verifies that CreatedByName and User.DateOfBirth are populated.
|
||||
func TestAdminBookings_Get_EnrichedFields(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
|
||||
adminID, err := fixtures.CreateTestAdminUser(db.DB)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
adminID, err := fixtures.CreateTestAdminUser(tx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create admin user: %v", err)
|
||||
}
|
||||
defer fixtures.DeleteUser(db.DB, adminID)
|
||||
|
||||
userID, err := fixtures.CreateTestUser(db.DB)
|
||||
userID, err := fixtures.CreateTestUser(tx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test user: %v", err)
|
||||
}
|
||||
defer fixtures.DeleteUser(db.DB, userID)
|
||||
|
||||
serviceID, err := fixtures.CreateTestService(db.DB)
|
||||
serviceID, err := fixtures.CreateTestService(tx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test service: %v", err)
|
||||
}
|
||||
defer fixtures.DeleteService(db.DB, serviceID)
|
||||
|
||||
bookingID, err := fixtures.CreateTestBooking(db.DB, userID, serviceID)
|
||||
bookingID, err := fixtures.CreateTestBooking(tx, userID, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test booking: %v", err)
|
||||
}
|
||||
defer fixtures.DeleteBooking(db.DB, bookingID)
|
||||
|
||||
dob := "1990-01-01"
|
||||
_, err = db.DB.Exec(context.Background(), "UPDATE users SET date_of_birth = $1 WHERE id = $2", dob, userID)
|
||||
_, err = tx.Exec(context.Background(), "UPDATE users SET date_of_birth = $1 WHERE id = $2", dob, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to update user dob: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(context.Background(), "UPDATE bookings SET created_by = $1 WHERE id = $2", adminID, bookingID)
|
||||
_, err = tx.Exec(context.Background(), "UPDATE bookings SET created_by = $1 WHERE id = $2", adminID, bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to update booking created_by: %v", err)
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(bookings.GetAdminBookingHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/bookings/"+bookingID, nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/bookings/"+bookingID, nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
|
||||
Reference in New Issue
Block a user