Skip to content

Commit

Permalink
Add database verify test
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 12, 2024
1 parent c5fa6ab commit 4b35096
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions backend/pkg/repository/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
var migrationFilePath = "../../db/migrations"

// setupTestDB sets up a Postgres test container and returns the connection and a cleanup function.
func setupTestDB(ctx context.Context) (*Postgres, func(), error) {
func setupTestDB(ctx context.Context, dbName string) (*Postgres, func(), error) {
req := testcontainers.ContainerRequest{
Image: "postgres:latest",
ExposedPorts: []string{"5432/tcp"},
Env: map[string]string{
"POSTGRES_USER": config.Cfg.PGUser,
"POSTGRES_PASSWORD": config.Cfg.PGPassword,
"POSTGRES_DB": config.Cfg.PGDBName,
"POSTGRES_DB": dbName,
},
WaitingFor: wait.ForListeningPort("5432/tcp").WithStartupTimeout(5 * time.Minute),
}
Expand Down Expand Up @@ -51,7 +51,7 @@ func setupTestDB(ctx context.Context) (*Postgres, func(), error) {
Host: host,
User: config.Cfg.PGUser,
Password: config.Cfg.PGPassword,
DBName: config.Cfg.PGDBName,
DBName: dbName,
Port: port.Port(),
SSLMode: "disable",
}
Expand All @@ -76,7 +76,7 @@ func TestPostgres_Open(t *testing.T) {
t.Parallel()
ctx := context.Background()

pg, cleanup, err := setupTestDB(ctx)
pg, cleanup, err := setupTestDB(ctx, config.Cfg.PGDBName)
if err != nil {
t.Fatalf("setupTestDB failed: %s", err)
}
Expand All @@ -103,7 +103,7 @@ func TestPostgres_RunGooseMigrations(t *testing.T) {
t.Parallel()
ctx := context.Background()

pg, cleanup, err := setupTestDB(ctx)
pg, cleanup, err := setupTestDB(ctx, config.Cfg.PGDBName)
if err != nil {
t.Fatalf("setupTestDB failed: %s", err)
}
Expand All @@ -115,3 +115,39 @@ func TestPostgres_RunGooseMigrations(t *testing.T) {

t.Log("Goose migrations ran successfully!")
}

// TestDatabaseCreation checks if the 'flamingodb' database is created.
func TestDatabaseCreation(t *testing.T) {
t.Parallel()
ctx := context.Background()

// Use a temporary database name for this test
pg, cleanup, err := setupTestDB(ctx, config.Cfg.PGDBName)
if err != nil {
t.Fatalf("setupTestDB failed: %s", err)
}
defer cleanup()

sqlDB, err := pg.DB.DB()
if err != nil {
t.Fatalf("failed to get sql.DB: %s", err)
}

if err = sqlDB.Ping(); err != nil {
t.Fatalf("failed to ping database: %s", err)
}

// Check if the database 'flamingodb' exists
var exists bool
query := fmt.Sprintf("SELECT EXISTS(SELECT datname FROM pg_catalog.pg_database WHERE datname = '%s')", config.Cfg.PGDBName)
err = sqlDB.QueryRow(query).Scan(&exists)
if err != nil {
t.Fatalf("failed to check database existence: %s", err)
}

if !exists {
t.Fatalf("database %s does not exist", config.Cfg.PGDBName)
}

t.Logf("Database %s exists", config.Cfg.PGDBName)
}

0 comments on commit 4b35096

Please sign in to comment.