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
+18 -17
View File
@@ -25,6 +25,7 @@ import (
"testing"
"crussell/db"
"crussell/testutils"
"crussell/mw"
"crussell/testutils/fixtures"
@@ -74,7 +75,7 @@ func makeCustomServiceRequest(handler http.Handler, method, path string, body in
// TestCustomServices_List verifies that an admin can list all custom services
// with pagination metadata.
func TestCustomServices_List(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -118,7 +119,7 @@ func TestCustomServices_List(t *testing.T) {
// TestCustomServices_List_Search verifies search filtering via the q parameter,
// including case-insensitive matching and no-results scenarios.
func TestCustomServices_List_Search(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -182,7 +183,7 @@ func TestCustomServices_List_Search(t *testing.T) {
// TestCustomServices_List_Popular verifies the popular flag returns custom services
// ordered by usage_count, limited to the specified number.
func TestCustomServices_List_Popular(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -236,7 +237,7 @@ func TestCustomServices_List_Popular(t *testing.T) {
// TestCustomServices_List_Pagination verifies page and per_page query parameters.
func TestCustomServices_List_Pagination(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -295,7 +296,7 @@ func TestCustomServices_List_Pagination(t *testing.T) {
// TestCustomServices_Create verifies that an admin can create a new custom service
// with name, description, price, duration, minimum age, and notes.
func TestCustomServices_Create(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -353,7 +354,7 @@ func TestCustomServices_Create(t *testing.T) {
// TestCustomServices_Create_Validation verifies that validation errors return
// HTTP 400 for various invalid inputs.
func TestCustomServices_Create_Validation(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -434,7 +435,7 @@ func TestCustomServices_Create_Validation(t *testing.T) {
// TestCustomServices_Get verifies that an admin can retrieve a single custom service by ID.
func TestCustomServices_Get(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -476,7 +477,7 @@ func TestCustomServices_Get(t *testing.T) {
// TestCustomServices_Get_NotFound verifies that requesting a non-existent custom service returns 404.
func TestCustomServices_Get_NotFound(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -499,7 +500,7 @@ func TestCustomServices_Get_NotFound(t *testing.T) {
// TestCustomServices_Update verifies that an admin can update a custom service's fields.
func TestCustomServices_Update(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -550,7 +551,7 @@ func TestCustomServices_Update(t *testing.T) {
// TestCustomServices_Update_NotFound verifies that updating a non-existent custom service returns 404.
func TestCustomServices_Update_NotFound(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -575,7 +576,7 @@ func TestCustomServices_Update_NotFound(t *testing.T) {
// TestCustomServices_Update_NoFields verifies that sending an update with no fields
// returns 400 Bad Request.
func TestCustomServices_Update_NoFields(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -607,7 +608,7 @@ func TestCustomServices_Update_NoFields(t *testing.T) {
// TestCustomServices_Promote verifies that promoting a custom service creates a
// regular service, migrates data, and deletes the original custom service.
func TestCustomServices_Promote(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -670,7 +671,7 @@ func TestCustomServices_Promote(t *testing.T) {
// TestCustomServices_Promote_NotFound verifies that promoting a non-existent custom service returns 404.
func TestCustomServices_Promote_NotFound(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -693,7 +694,7 @@ func TestCustomServices_Promote_NotFound(t *testing.T) {
// TestCustomServices_Delete verifies that an admin can delete an unused custom service.
func TestCustomServices_Delete(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -737,7 +738,7 @@ func TestCustomServices_Delete(t *testing.T) {
// TestCustomServices_Delete_NotFound verifies that deleting a non-existent custom service returns 404.
func TestCustomServices_Delete_NotFound(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -757,7 +758,7 @@ func TestCustomServices_Delete_NotFound(t *testing.T) {
// TestCustomServices_Delete_Conflict verifies that deleting a custom service with
// usage_count > 0 returns 409 Conflict.
func TestCustomServices_Delete_Conflict(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
adminID, err := fixtures.CreateTestAdminUser(db.DB)
if err != nil {
@@ -794,7 +795,7 @@ func TestCustomServices_Delete_Conflict(t *testing.T) {
// TestCustomServices_NonAdmin verifies that non-admin users receive HTTP 403
// Forbidden when attempting to access any admin custom services endpoint.
func TestCustomServices_NonAdmin(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
_, err := fixtures.CreateTestUser(db.DB)
if err != nil {