refactor(backend): replace resetTestData with SetupTestDB and add new tests

Migrate all test files from resetTestData(t) to testutils.SetupTestDB(t) for isolated per-package test databases.

- Add new feature tests: name history assertions, referral discount preview,
  time blockers, email validation, GDPR export, loyalty manual redemption
- Update existing tests to use batch queries and SetupTestDB
- Remove test_helpers.go resetTestData infrastructure
- Add comprehensive user profile tests (442 new lines)

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-20 16:57:36 +01:00
co-authored by Sisyphus
parent 9c68918c20
commit b03c4f6247
36 changed files with 1735 additions and 859 deletions
+5 -27
View File
@@ -17,39 +17,17 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"crussell/db"
"crussell/testutils"
"crussell/handlers/user"
"crussell/testutils/jwt"
"crussell/testutils/testdb"
"github.com/go-chi/chi/v5"
)
func TestMain(m *testing.M) {
pool, err := testdb.NewPool("")
if err != nil {
fmt.Println("failed to create pool:", err)
os.Exit(1)
}
testdb.Migrate(&testing.T{}, pool)
db.DB = pool
jwt.Init()
code := m.Run()
pool.Close()
os.Exit(code)
}
func resetTestData(t *testing.T) {
t.Helper()
testdb.TruncateTables(t, db.DB)
}
// createUserWithDOB creates a test user with specified date of birth
func createUserWithDOB(dob string) (string, error) {
ctx := context.Background()
@@ -117,7 +95,7 @@ func findLastSegment(path, prefix string) int {
// TestServices_ListAll verifies that listing all services returns only active services,
// filtering out inactive services from the response.
func TestServices_ListAll(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
_, err := db.DB.Exec(context.Background(), `
INSERT INTO services (name, description, price, duration_minutes, is_active, minimum_age_required)
@@ -164,7 +142,7 @@ func TestServices_ListAll(t *testing.T) {
// TestServices_EligibleForUser_AgeFilter verifies that eligible services are filtered based on the user's age,
// excluding services with minimum_age_required higher than the user's age.
func TestServices_EligibleForUser_AgeFilter(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
dob := "2006-01-01" // Age 20 in Feb 2026
userID, err := createUserWithDOB(dob)
@@ -218,7 +196,7 @@ func TestServices_EligibleForUser_AgeFilter(t *testing.T) {
// TestServices_EligibleForUser_PatchTest verifies that services requiring patch tests include a
// PatchTestStatus field set to 'ok' when the user has completed a valid patch test for that service.
func TestServices_EligibleForUser_PatchTest(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
dob := "2000-01-01"
userID, err := createUserWithDOB(dob)
@@ -312,7 +290,7 @@ func TestServices_EligibleForUser_PatchTest(t *testing.T) {
// TestContact_ReturnsInfo verifies that the contact info endpoint returns the salon's contact
// details (name, phone, email, role) from the first admin user in the database.
func TestContact_ReturnsInfo(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
_, err := db.DB.Exec(context.Background(), `
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)