Files
Crussell/backend/internal/dav/service_dev.go
T
popertots df3439bd70 fix: improve test infrastructure and add ID validation
- Add TestMain to set test env vars and testdb.TruncateTables for test
  isolation
- Add chi routing context to test helpers for path parameter extraction
- Fix SQL error handling to use errors.Is() instead of ==
- Add validators package with ID validation
- Fix admin test middleware chain (RequireAdmin wrapper)
- Update test user inserts to include phone and date_of_birth fields
- Update service delete test to check soft-delete (is_active=false)
- Update holiday hours test to use new schema (weekday, is_open)
- Add phone number validation tests for UK mobile numbers
2026-02-23 00:59:32 +00:00

59 lines
1.1 KiB
Go

//go:build dev
// +build dev
package dav
import (
"context"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v5/pgxpool"
)
var Service *BaseService
func init() {
if err := connect(); err != nil {
// Don't fatal in test mode - tests will use testdb instead
if os.Getenv("GO_TESTING") != "" {
return
}
log.Fatalf("failed to initialize dev service: %v", err)
}
}
func connect() error {
dsn := fmt.Sprintf(
"postgres://%s:%s@localhost:5432/%s?sslmode=disable",
getEnv("POSTGRES_USER"),
getEnv("POSTGRES_PASSWORD"),
getEnv("POSTGRES_DB"),
)
pool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
return err
}
Service = newBaseService(pool)
return testDB(pool)
}
func testDB(pool *pgxpool.Pool) error {
var n int
err := pool.QueryRow(context.Background(), "SELECT 1").Scan(&n)
if err != nil || n != 1 {
return fmt.Errorf("db test failed: %w", err)
}
return nil
}
func getEnv(key string) string {
if v := os.Getenv(key); v != "" {
return v
}
// Return empty string instead of fatal error - allows tests to run without prod env vars
return ""
}