- 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
66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
//go:build !dev
|
|
// +build !dev
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var DB *pgxpool.Pool
|
|
|
|
func Connect() error {
|
|
// Connect to Postgres on local network (127.x.x.x)
|
|
dsn := fmt.Sprintf(
|
|
"postgres://%s:%s@%s:5432/%s",
|
|
getEnv("POSTGRES_USER"),
|
|
getEnv("POSTGRES_PASSWORD"),
|
|
getEnv("POSTGRES_HOST"),
|
|
getEnv("POSTGRES_DB"),
|
|
)
|
|
|
|
pool, err := pgxpool.New(context.Background(), dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
DB = pool
|
|
|
|
err = testDB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testDB() error {
|
|
ctx := context.Background()
|
|
conn, err := DB.Acquire(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Release()
|
|
|
|
row := conn.QueryRow(ctx, "SELECT 1")
|
|
var result int
|
|
err = row.Scan(&result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getEnv(key string) string {
|
|
if val := os.Getenv(key); val != "" {
|
|
return val
|
|
}
|
|
// Return empty string instead of fatal error - allows tests to run without prod env vars
|
|
// Tests should use testdb.Pool() and set db.DB before running handler code
|
|
return ""
|
|
}
|