mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Run integration tests with both dbs
This commit is contained in:
@@ -45,8 +45,13 @@ type testUser struct {
|
||||
session *models.Session
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Type db.DBType
|
||||
URL string
|
||||
}
|
||||
|
||||
// setupTestHarness creates a new test environment
|
||||
func setupTestHarness(t *testing.T) *testHarness {
|
||||
func setupTestHarness(t *testing.T, dbConfig DatabaseConfig) *testHarness {
|
||||
t.Helper()
|
||||
|
||||
// Create temporary directory for test files
|
||||
@@ -61,9 +66,20 @@ func setupTestHarness(t *testing.T) *testHarness {
|
||||
t.Fatalf("Failed to initialize secrets service: %v", err)
|
||||
}
|
||||
|
||||
database, err := db.NewTestSQLiteDB(secretsSvc)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize test database: %v", err)
|
||||
var database db.TestDatabase
|
||||
switch dbConfig.Type {
|
||||
case db.DBTypeSQLite:
|
||||
database, err = db.NewTestSQLiteDB(secretsSvc)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize test database: %v", err)
|
||||
}
|
||||
case db.DBTypePostgres:
|
||||
database, err = db.NewPostgresTestDB(dbConfig.URL, secretsSvc)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize test database: %v", err)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("Unsupported database type: %s", dbConfig.Type)
|
||||
}
|
||||
|
||||
if err := database.Migrate(); err != nil {
|
||||
@@ -156,6 +172,32 @@ func (h *testHarness) teardown(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// runWithDatabases runs a test function with both SQLite and PostgreSQL databases
|
||||
func runWithDatabases(t *testing.T, testFn func(*testing.T, DatabaseConfig)) {
|
||||
// Get PostgreSQL connection URL from environment variable
|
||||
postgresURL := os.Getenv("LEMMA_TEST_POSTGRES_URL")
|
||||
|
||||
// Always run with SQLite in-memory
|
||||
t.Run("SQLite", func(t *testing.T) {
|
||||
testFn(t, DatabaseConfig{
|
||||
Type: db.DBTypeSQLite,
|
||||
URL: "sqlite://:memory:",
|
||||
})
|
||||
})
|
||||
|
||||
// Run with PostgreSQL if connection URL is provided
|
||||
if postgresURL != "" {
|
||||
t.Run("PostgreSQL", func(t *testing.T) {
|
||||
testFn(t, DatabaseConfig{
|
||||
Type: db.DBTypePostgres,
|
||||
URL: postgresURL,
|
||||
})
|
||||
})
|
||||
} else {
|
||||
t.Log("Skipping PostgreSQL tests, LEMMA_TEST_POSTGRES_URL environment variable not set")
|
||||
}
|
||||
}
|
||||
|
||||
// createTestUser creates a test user and returns the user and access token
|
||||
func (h *testHarness) createTestUser(t *testing.T, email, password string, role models.UserRole) *testUser {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user