Add test postgres db connection

This commit is contained in:
2025-03-06 19:23:24 +01:00
parent 4766a166df
commit 629baa9952
8 changed files with 77 additions and 13 deletions

View File

@@ -4,7 +4,10 @@ package db
import (
"database/sql"
"fmt"
"lemma/internal/secrets"
"log"
"time"
)
type TestDatabase interface {
@@ -12,19 +15,80 @@ type TestDatabase interface {
TestDB() *sql.DB
}
func NewTestDB(secretsService secrets.Service) (TestDatabase, error) {
func NewTestSQLiteDB(secretsService secrets.Service) (TestDatabase, error) {
db, err := Init(DBTypeSQLite, ":memory:", secretsService)
if err != nil {
return nil, err
}
return &testDatabase{db.(*database)}, nil
return &testSQLiteDatabase{db.(*database)}, nil
}
type testDatabase struct {
type testSQLiteDatabase struct {
*database
}
func (td *testDatabase) TestDB() *sql.DB {
func (td *testSQLiteDatabase) TestDB() *sql.DB {
return td.DB
}
// NewPostgresTestDB creates a test database using PostgreSQL
func NewPostgresTestDB(dbURL string, secretsSvc secrets.Service) (TestDatabase, error) {
if dbURL == "" {
return nil, fmt.Errorf("postgres URL cannot be empty")
}
db, err := sql.Open("postgres", dbURL)
if err != nil {
return nil, fmt.Errorf("failed to open postgres database: %w", err)
}
if err := db.Ping(); err != nil {
db.Close()
return nil, fmt.Errorf("failed to ping postgres database: %w", err)
}
// Create a unique schema name for this test run to avoid conflicts
schemaName := fmt.Sprintf("lemma_test_%d", time.Now().UnixNano())
_, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", schemaName))
if err != nil {
db.Close()
return nil, fmt.Errorf("failed to create schema: %w", err)
}
// Set search path to use our schema
_, err = db.Exec(fmt.Sprintf("SET search_path TO %s", schemaName))
if err != nil {
db.Close()
return nil, fmt.Errorf("failed to set search path: %w", err)
}
// Create database instance
database := &postgresTestDatabase{
database: &database{DB: db, secretsService: secretsSvc, dbType: DBTypePostgres},
schemaName: schemaName,
}
return database, nil
}
// postgresTestDatabase extends the regular postgres database to add test-specific cleanup
type postgresTestDatabase struct {
*database
schemaName string
}
// Close closes the database connection and drops the test schema
func (db *postgresTestDatabase) Close() error {
_, err := db.TestDB().Exec(fmt.Sprintf("DROP SCHEMA %s CASCADE", db.schemaName))
if err != nil {
log.Printf("Failed to drop schema %s: %v", db.schemaName, err)
}
return db.TestDB().Close()
}
// TestDB returns the underlying *sql.DB instance
func (db *postgresTestDatabase) TestDB() *sql.DB {
return db.DB
}