Run integration tests with both dbs

This commit is contained in:
2025-03-06 21:39:56 +01:00
parent 629baa9952
commit f55d2644c3
7 changed files with 88 additions and 22 deletions

View File

@@ -15,21 +15,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Helper function to check if a user exists in a slice of users func TestAdminHandlers_Integration(t *testing.T) {
func containsUser(users []*models.User, searchUser *models.User) bool { runWithDatabases(t, testAdminHandlers)
for _, u := range users {
if u.ID == searchUser.ID &&
u.Email == searchUser.Email &&
u.DisplayName == searchUser.DisplayName &&
u.Role == searchUser.Role {
return true
}
}
return false
} }
func TestAdminHandlers_Integration(t *testing.T) { func testAdminHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t) h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
t.Run("user management", func(t *testing.T) { t.Run("user management", func(t *testing.T) {
@@ -241,3 +232,16 @@ func TestAdminHandlers_Integration(t *testing.T) {
assert.Equal(t, http.StatusForbidden, rr.Code) assert.Equal(t, http.StatusForbidden, rr.Code)
}) })
} }
// Helper function to check if a user exists in a slice of users
func containsUser(users []*models.User, searchUser *models.User) bool {
for _, u := range users {
if u.ID == searchUser.ID &&
u.Email == searchUser.Email &&
u.DisplayName == searchUser.DisplayName &&
u.Role == searchUser.Role {
return true
}
}
return false
}

View File

@@ -19,7 +19,11 @@ import (
) )
func TestAuthHandlers_Integration(t *testing.T) { func TestAuthHandlers_Integration(t *testing.T) {
h := setupTestHarness(t) runWithDatabases(t, testAuthHandlers)
}
func testAuthHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
t.Run("login", func(t *testing.T) { t.Run("login", func(t *testing.T) {

View File

@@ -18,7 +18,11 @@ import (
) )
func TestFileHandlers_Integration(t *testing.T) { func TestFileHandlers_Integration(t *testing.T) {
h := setupTestHarness(t) runWithDatabases(t, testFileHandlers)
}
func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
t.Run("file operations", func(t *testing.T) { t.Run("file operations", func(t *testing.T) {

View File

@@ -16,7 +16,11 @@ import (
) )
func TestGitHandlers_Integration(t *testing.T) { func TestGitHandlers_Integration(t *testing.T) {
h := setupTestHarness(t) runWithDatabases(t, testGitHandlers)
}
func testGitHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
t.Run("git operations", func(t *testing.T) { t.Run("git operations", func(t *testing.T) {

View File

@@ -45,8 +45,13 @@ type testUser struct {
session *models.Session session *models.Session
} }
type DatabaseConfig struct {
Type db.DBType
URL string
}
// setupTestHarness creates a new test environment // setupTestHarness creates a new test environment
func setupTestHarness(t *testing.T) *testHarness { func setupTestHarness(t *testing.T, dbConfig DatabaseConfig) *testHarness {
t.Helper() t.Helper()
// Create temporary directory for test files // Create temporary directory for test files
@@ -61,10 +66,21 @@ func setupTestHarness(t *testing.T) *testHarness {
t.Fatalf("Failed to initialize secrets service: %v", err) t.Fatalf("Failed to initialize secrets service: %v", err)
} }
database, err := db.NewTestSQLiteDB(secretsSvc) var database db.TestDatabase
switch dbConfig.Type {
case db.DBTypeSQLite:
database, err = db.NewTestSQLiteDB(secretsSvc)
if err != nil { if err != nil {
t.Fatalf("Failed to initialize test database: %v", err) 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 { if err := database.Migrate(); err != nil {
t.Fatalf("Failed to run migrations: %v", err) t.Fatalf("Failed to run migrations: %v", err)
@@ -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 // 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 { func (h *testHarness) createTestUser(t *testing.T, email, password string, role models.UserRole) *testUser {
t.Helper() t.Helper()

View File

@@ -15,7 +15,11 @@ import (
) )
func TestUserHandlers_Integration(t *testing.T) { func TestUserHandlers_Integration(t *testing.T) {
h := setupTestHarness(t) runWithDatabases(t, testUserHandlers)
}
func testUserHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
currentEmail := h.RegularTestUser.userModel.Email currentEmail := h.RegularTestUser.userModel.Email

View File

@@ -15,7 +15,11 @@ import (
) )
func TestWorkspaceHandlers_Integration(t *testing.T) { func TestWorkspaceHandlers_Integration(t *testing.T) {
h := setupTestHarness(t) runWithDatabases(t, testWorkspaceHandlers)
}
func testWorkspaceHandlers(t *testing.T, dbConfig DatabaseConfig) {
h := setupTestHarness(t, dbConfig)
defer h.teardown(t) defer h.teardown(t)
t.Run("list workspaces", func(t *testing.T) { t.Run("list workspaces", func(t *testing.T) {