saveinmed/backend/internal/repository/postgres/postgres_test.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

58 lines
1.3 KiB
Go

package postgres
import (
"context"
"os"
"strings"
"testing"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/saveinmed/backend-go/internal/config"
)
func TestDatabaseConnection(t *testing.T) {
if os.Getenv("SKIP_DB_TEST") != "" {
t.Skip("Skipping database tests")
}
// Simple .env loader for testing purposes
// Try loading from project root (3 levels up from this file)
if content, err := os.ReadFile("../../../.env"); err == nil {
for _, line := range strings.Split(string(content), "\n") {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
os.Setenv(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
}
}
cfg, err := config.Load()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
db, err := sqlx.ConnectContext(ctx, "pgx", cfg.DatabaseURL)
if err != nil {
t.Fatalf("Failed to connect to database: %v", err)
}
defer db.Close()
if err := db.PingContext(ctx); err != nil {
t.Fatalf("Failed to ping database: %v", err)
}
var result int
if err := db.QueryRowContext(ctx, "SELECT 1").Scan(&result); err != nil {
t.Fatalf("Failed to execute query: %v", err)
}
if result != 1 {
t.Errorf("Expected 1, got %d", result)
}
}