package contract import ( "context" "fmt" "io/fs" "os" "testing" "time" "github.com/jackc/pgx/v5/pgxpool" assets "shared-album" "shared-album/internal/albums" "shared-album/internal/auth" "shared-album/internal/db" "shared-album/internal/members" "shared-album/internal/notifications" "shared-album/internal/photos" "shared-album/internal/settings" "shared-album/internal/sharelinks" "shared-album/internal/social" ) // TestPostgresContract runs the shared suite against PostgreSQL inside its own // schema, so it never clashes with other packages that use the same database. func TestPostgresContract(t *testing.T) { base := os.Getenv("TEST_DATABASE_URL") if base == "" { t.Skip("TEST_DATABASE_URL not set") } ctx := context.Background() admin, err := pgxpool.New(ctx, base) if err != nil { t.Skipf("cannot connect: %v", err) } if err := admin.Ping(ctx); err != nil { t.Skipf("cannot ping: %v", err) } t.Cleanup(admin.Close) schema := fmt.Sprintf("ct_%d", time.Now().UnixNano()) if _, err := admin.Exec(ctx, "CREATE SCHEMA "+schema); err != nil { t.Fatalf("create schema: %v", err) } t.Cleanup(func() { _, _ = admin.Exec(context.Background(), "DROP SCHEMA "+schema+" CASCADE") }) dsn := base + "&options=-c%20search_path%3D" + schema migFS, err := fs.Sub(assets.Migrations, "migrations") if err != nil { t.Fatalf("migrations fs: %v", err) } if err := db.Migrate(dsn, migFS); err != nil { t.Fatalf("migrate: %v", err) } pool, err := pgxpool.New(ctx, dsn) if err != nil { t.Fatalf("pool: %v", err) } t.Cleanup(pool.Close) Run(t, Repos{ Auth: auth.NewStore(pool), Albums: albums.NewStore(pool), Members: members.NewStore(pool), Photos: photos.NewStore(pool), Social: social.NewStore(pool), Notifications: notifications.NewStore(pool), ShareLinks: sharelinks.NewStore(pool), Settings: settings.NewStore(pool), Reset: func(ctx context.Context) error { _, err := pool.Exec(ctx, `TRUNCATE users, objects, app_settings CASCADE`) return err }, }) }