Files
Crussell/backend/handlers/admin/bookings_fields_test.go
T
popertotsandSisyphus 220a0ef6e8 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>
2026-06-21 19:29:24 +01:00

73 lines
2.0 KiB
Go

//go:build test
// +build test
package admin
import (
"context"
"net/http"
"testing"
"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) {
t.Parallel()
ctx, tx := testutils.SetupTestTx(t)
adminID, err := fixtures.CreateTestAdminUser(tx)
if err != nil {
t.Fatalf("failed to create admin user: %v", err)
}
userID, err := fixtures.CreateTestUser(tx)
if err != nil {
t.Fatalf("failed to create test user: %v", err)
}
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create test service: %v", err)
}
bookingID, err := fixtures.CreateTestBooking(tx, userID, serviceID)
if err != nil {
t.Fatalf("failed to create test booking: %v", err)
}
dob := "1990-01-01"
_, 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 = 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, ctx)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
}
var booking bookings.Booking
if err := parseResponseBody(w, &booking); err != nil {
t.Fatalf("failed to parse response: %v", err)
}
if booking.CreatedByName == nil {
t.Error("expected CreatedByName to be populated")
}
if booking.User == nil || booking.User.DateOfBirth == nil {
t.Error("expected User.DateOfBirth to be populated")
} else if *booking.User.DateOfBirth != dob {
t.Errorf("expected date_of_birth %s, got %s", dob, *booking.User.DateOfBirth)
}
}