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:
@@ -4,11 +4,9 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"crussell/db"
|
||||
"crussell/testutils"
|
||||
)
|
||||
|
||||
@@ -16,25 +14,18 @@ func intPtr(i int) *int { return &i }
|
||||
func float64Ptr(f float64) *float64 { return &f }
|
||||
func boolPtr(b bool) *bool { return &b }
|
||||
|
||||
func seedBusinessSettings(t *testing.T) {
|
||||
t.Helper()
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
INSERT INTO business_settings (business_name, business_address, currency_code, gift_card_expiry_months, voucher_type)
|
||||
VALUES ('Test Salon', '123 Test St', 'GBP', 12, 'SPV')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed business settings: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetBusinessSettings verifies that GET /api/admin/settings returns the
|
||||
// current business settings row.
|
||||
func TestGetBusinessSettings(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
_, err := tx.Exec(ctx, `UPDATE business_settings SET business_name = 'Test Salon', business_address = '123 Test St', currency_code = 'GBP', gift_card_expiry_months = 12, voucher_type = 'SPV'`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed business settings: %v", err)
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(GetBusinessSettings)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/settings", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/settings", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -65,14 +56,13 @@ func TestGetBusinessSettings(t *testing.T) {
|
||||
// TestUpdateBusinessSettings verifies that updating a single field via
|
||||
// PUT /api/admin/settings returns 200 with the updated settings.
|
||||
func TestUpdateBusinessSettings(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
BusinessName: stringPtr("Updated Salon Name"),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -91,8 +81,7 @@ func TestUpdateBusinessSettings(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_MultipleFields verifies that updating several
|
||||
// fields at once works correctly.
|
||||
func TestUpdateBusinessSettings_MultipleFields(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
@@ -101,7 +90,7 @@ func TestUpdateBusinessSettings_MultipleFields(t *testing.T) {
|
||||
GiftCardExpiryMonths: intPtr(24),
|
||||
VoucherType: stringPtr("MPV"),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -129,14 +118,13 @@ func TestUpdateBusinessSettings_MultipleFields(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_InvalidVoucherType verifies that an invalid
|
||||
// voucher_type value returns 400.
|
||||
func TestUpdateBusinessSettings_InvalidVoucherType(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
VoucherType: stringPtr("INVALID"),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, ctx)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -149,14 +137,13 @@ func TestUpdateBusinessSettings_InvalidVoucherType(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_NegativeExpiryMonths verifies that a
|
||||
// gift_card_expiry_months value less than 1 returns 400.
|
||||
func TestUpdateBusinessSettings_NegativeExpiryMonths(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
GiftCardExpiryMonths: intPtr(0),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, ctx)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -169,15 +156,14 @@ func TestUpdateBusinessSettings_NegativeExpiryMonths(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_InvalidVATRate verifies that a default_vat_rate
|
||||
// outside the 0-100 range returns 400.
|
||||
func TestUpdateBusinessSettings_InvalidVATRate(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
DefaultVATRate: float64Ptr(-1),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, ctx)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400 for negative rate, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -188,7 +174,7 @@ func TestUpdateBusinessSettings_InvalidVATRate(t *testing.T) {
|
||||
body2 := UpdateBusinessSettingsRequest{
|
||||
DefaultVATRate: float64Ptr(101),
|
||||
}
|
||||
w2 := makeAdminRequest(handler, "PUT", "/api/admin/settings", body2)
|
||||
w2 := makeAdminRequest(handler, "PUT", "/api/admin/settings", body2, ctx)
|
||||
|
||||
if w2.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400 for rate > 100, got %d. body: %s", w2.Code, w2.Body.String())
|
||||
@@ -201,11 +187,10 @@ func TestUpdateBusinessSettings_InvalidVATRate(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_NoFields verifies that an empty request body
|
||||
// (no fields to update) returns 400.
|
||||
func TestUpdateBusinessSettings_NoFields(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", UpdateBusinessSettingsRequest{})
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", UpdateBusinessSettingsRequest{}, ctx)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -218,14 +203,18 @@ func TestUpdateBusinessSettings_NoFields(t *testing.T) {
|
||||
// TestUpdateBusinessSettings_PartialUpdate verifies that updating a single field
|
||||
// leaves other fields unchanged.
|
||||
func TestUpdateBusinessSettings_PartialUpdate(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
seedBusinessSettings(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
_, err := tx.Exec(ctx, `UPDATE business_settings SET business_name = 'Test Salon', business_address = '123 Test St', currency_code = 'GBP', gift_card_expiry_months = 12, voucher_type = 'SPV'`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to seed business settings: %v", err)
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(UpdateBusinessSettings)
|
||||
body := UpdateBusinessSettingsRequest{
|
||||
GiftCardExpiryMonths: intPtr(36),
|
||||
}
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body)
|
||||
w := makeAdminRequest(handler, "PUT", "/api/admin/settings", body, 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