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:
@@ -18,10 +18,9 @@ import (
|
||||
"crussell/mw"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func makeExtendedAdminRequest(handler http.Handler, method, path string, body interface{}) *httptest.ResponseRecorder {
|
||||
func makeExtendedAdminRequest(handler http.Handler, method, path string, body interface{}, ctx context.Context) *httptest.ResponseRecorder {
|
||||
var req *http.Request
|
||||
if body != nil {
|
||||
bodyBytes, _ := json.Marshal(body)
|
||||
@@ -35,7 +34,7 @@ func makeExtendedAdminRequest(handler http.Handler, method, path string, body in
|
||||
if id, _ := extractIDFromPath(path); id != "" {
|
||||
rctx.URLParams.Add("id", id)
|
||||
}
|
||||
ctx := context.WithValue(req.Context(), chi.RouteCtxKey, rctx)
|
||||
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
|
||||
ctx = context.WithValue(ctx, mw.UserIDKey, "admin001")
|
||||
ctx = context.WithValue(ctx, mw.UserRoleKey, "admin")
|
||||
req = req.WithContext(ctx)
|
||||
@@ -45,10 +44,10 @@ func makeExtendedAdminRequest(handler http.Handler, method, path string, body in
|
||||
return w
|
||||
}
|
||||
|
||||
func createTestUser(t *testing.T) string {
|
||||
func createTestUser(t *testing.T, ctx context.Context, q db.Querier) string {
|
||||
t.Helper()
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := q.QueryRow(ctx, `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'test@test.com', '+447700900000', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -59,7 +58,7 @@ func createTestUser(t *testing.T) string {
|
||||
return userID
|
||||
}
|
||||
|
||||
func createNotification(t *testing.T, reason, userID string, acknowledged bool) string {
|
||||
func createNotification(t *testing.T, ctx context.Context, q db.Querier, reason, userID string, acknowledged bool) string {
|
||||
t.Helper()
|
||||
var notificationID string
|
||||
if userID == "" {
|
||||
@@ -67,7 +66,7 @@ func createNotification(t *testing.T, reason, userID string, acknowledged bool)
|
||||
if acknowledged {
|
||||
query = `INSERT INTO admin_notifications (reason, acknowledged_at) VALUES ($1, NOW()) RETURNING id`
|
||||
}
|
||||
err := db.DB.QueryRow(context.Background(), query, reason).Scan(¬ificationID)
|
||||
err := q.QueryRow(ctx, query, reason).Scan(¬ificationID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create notification: %v", err)
|
||||
}
|
||||
@@ -76,7 +75,7 @@ func createNotification(t *testing.T, reason, userID string, acknowledged bool)
|
||||
if acknowledged {
|
||||
query = `INSERT INTO admin_notifications (reason, user_id, acknowledged_at) VALUES ($1, $2, NOW()) RETURNING id`
|
||||
}
|
||||
err := db.DB.QueryRow(context.Background(), query, reason, userID).Scan(¬ificationID)
|
||||
err := q.QueryRow(ctx, query, reason, userID).Scan(¬ificationID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create notification: %v", err)
|
||||
}
|
||||
@@ -89,14 +88,15 @@ func createNotification(t *testing.T, reason, userID string, acknowledged bool)
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_IncludeAcknowledged_Default(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, "cancelled_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "cancelled_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -109,14 +109,15 @@ func TestNotifications_IncludeAcknowledged_Default(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_IncludeAcknowledged_True(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, "cancelled_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "cancelled_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -129,13 +130,14 @@ func TestNotifications_IncludeAcknowledged_True(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_IncludeAcknowledged_ResponseHasAcknowledgedAt(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -156,8 +158,9 @@ func TestNotifications_IncludeAcknowledged_ResponseHasAcknowledgedAt(t *testing.
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_PriorityOrdering(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
// Create notifications in reverse priority order
|
||||
reasons := []string{
|
||||
@@ -167,11 +170,11 @@ func TestNotifications_PriorityOrdering(t *testing.T) {
|
||||
"cancelled_booking",
|
||||
}
|
||||
for _, reason := range reasons {
|
||||
createNotification(t, reason, userID, false)
|
||||
createNotification(t, ctx, tx, reason, userID, false)
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -192,16 +195,17 @@ func TestNotifications_PriorityOrdering(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_PriorityOrdering_OldestFirstWithinPriority(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
// Create two pending_booking notifications with a time gap
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -219,15 +223,16 @@ func TestNotifications_PriorityOrdering_OldestFirstWithinPriority(t *testing.T)
|
||||
}
|
||||
|
||||
func TestNotifications_AllNotifications_NewestFirst(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
createNotification(t, "cancelled_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "cancelled_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -249,15 +254,16 @@ func TestNotifications_AllNotifications_NewestFirst(t *testing.T) {
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_UnreadCount(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, "cancelled_booking", userID, false)
|
||||
createNotification(t, "affiliate_claim", userID, true)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "cancelled_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "affiliate_claim", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetUnreadCount)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
@@ -274,13 +280,14 @@ func TestNotifications_UnreadCount(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_UnreadCount_Zero(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(GetUnreadCount)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil, ctx)
|
||||
|
||||
var resp map[string]int
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -293,10 +300,11 @@ func TestNotifications_UnreadCount_Zero(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_UnreadCount_Empty(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
t.Parallel()
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(GetUnreadCount)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil, ctx)
|
||||
|
||||
var resp map[string]int
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -313,13 +321,14 @@ func TestNotifications_UnreadCount_Empty(t *testing.T) {
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_NewBookingReason(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "new_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "new_booking", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -336,13 +345,14 @@ func TestNotifications_NewBookingReason(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_EditRequestedReason(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "edit_requested", userID, false)
|
||||
createNotification(t, ctx, tx, "edit_requested", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -359,14 +369,15 @@ func TestNotifications_EditRequestedReason(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_Priority_NewBookingBelowPendingBooking(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "new_booking", userID, false)
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "new_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -387,15 +398,16 @@ func TestNotifications_Priority_NewBookingBelowPendingBooking(t *testing.T) {
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_IncludeAcknowledgedWithReasonFilter(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
createNotification(t, "pending_booking", userID, false)
|
||||
createNotification(t, "pending_booking", userID, true)
|
||||
createNotification(t, "cancelled_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
createNotification(t, ctx, tx, "pending_booking", userID, true)
|
||||
createNotification(t, ctx, tx, "cancelled_booking", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true&reason=pending_booking", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications?include_acknowledged=true&reason=pending_booking", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -412,11 +424,12 @@ func TestNotifications_IncludeAcknowledgedWithReasonFilter(t *testing.T) {
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create a service
|
||||
var serviceID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Test service', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -427,7 +440,7 @@ func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
|
||||
|
||||
// Create a user
|
||||
var userID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Alice', 'Smith', 'alice@test.com', '+447700900001', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -438,7 +451,7 @@ func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
|
||||
|
||||
// Create a booking
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status)
|
||||
VALUES ($1, NOW() + INTERVAL '3 days', 'pending')
|
||||
RETURNING id
|
||||
@@ -448,10 +461,10 @@ func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
|
||||
}
|
||||
|
||||
// Create notification with both user_id and booking_id
|
||||
createNotificationWithBooking(t, "pending_booking", userID, bookingID, false)
|
||||
createNotificationWithBooking(t, ctx, tx, "pending_booking", userID, bookingID, false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -472,13 +485,14 @@ func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_ResponseEnriched_NoUserOrBooking(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create notification without user_id or booking_id
|
||||
createNotification(t, "1_week_no_pay", "", false)
|
||||
createNotification(t, ctx, tx, "1_week_no_pay", "", false)
|
||||
|
||||
handler := http.HandlerFunc(GetNotifications)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil)
|
||||
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
|
||||
var resp AdminNotificationListResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
@@ -503,13 +517,14 @@ func TestNotifications_ResponseEnriched_NoUserOrBooking(t *testing.T) {
|
||||
// =============================================================================
|
||||
|
||||
func TestNotifications_Acknowledge_ViaExtendedHandler(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
notifID := createNotification(t, "pending_booking", userID, false)
|
||||
notifID := createNotification(t, ctx, tx, "pending_booking", userID, false)
|
||||
|
||||
handler := http.HandlerFunc(AcknowledgeNotification)
|
||||
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil)
|
||||
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -517,7 +532,7 @@ func TestNotifications_Acknowledge_ViaExtendedHandler(t *testing.T) {
|
||||
|
||||
// Verify acknowledged
|
||||
var ackTime *time.Time
|
||||
err := db.DB.QueryRow(context.Background(),
|
||||
err := tx.QueryRow(ctx,
|
||||
"SELECT acknowledged_at FROM admin_notifications WHERE id = $1", notifID).Scan(&ackTime)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query notification: %v", err)
|
||||
@@ -528,13 +543,14 @@ func TestNotifications_Acknowledge_ViaExtendedHandler(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotifications_Acknowledge_AlreadyAcknowledged_Extended(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
userID := createTestUser(t)
|
||||
t.Parallel()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
userID := createTestUser(t, ctx, tx)
|
||||
|
||||
notifID := createNotification(t, "pending_booking", userID, true)
|
||||
notifID := createNotification(t, ctx, tx, "pending_booking", userID, true)
|
||||
|
||||
handler := http.HandlerFunc(AcknowledgeNotification)
|
||||
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil)
|
||||
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil, ctx)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected status 404 for already acknowledged, got %d", w.Code)
|
||||
@@ -542,19 +558,18 @@ func TestNotifications_Acknowledge_AlreadyAcknowledged_Extended(t *testing.T) {
|
||||
}
|
||||
|
||||
// createNotificationWithBooking creates a notification with both user_id and booking_id
|
||||
func createNotificationWithBooking(t *testing.T, reason, userID, bookingID string, acknowledged bool) string {
|
||||
func createNotificationWithBooking(t *testing.T, ctx context.Context, q db.Querier, reason, userID, bookingID string, acknowledged bool) string {
|
||||
t.Helper()
|
||||
var notificationID string
|
||||
query := `INSERT INTO admin_notifications (reason, user_id, booking_id) VALUES ($1, $2, $3) RETURNING id`
|
||||
if acknowledged {
|
||||
query = `INSERT INTO admin_notifications (reason, user_id, booking_id, acknowledged_at) VALUES ($1, $2, $3, NOW()) RETURNING id`
|
||||
}
|
||||
err := db.DB.QueryRow(context.Background(), query, reason, userID, bookingID).Scan(¬ificationID)
|
||||
err := q.QueryRow(ctx, query, reason, userID, bookingID).Scan(¬ificationID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create notification: %v", err)
|
||||
}
|
||||
return notificationID
|
||||
}
|
||||
|
||||
// Ensure test compilation
|
||||
var _ = func() *pgxpool.Pool { return nil }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user