From f55d2644c3492d9cb6a8243c48ea067f420a1347 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Thu, 6 Mar 2025 21:39:56 +0100 Subject: [PATCH] Run integration tests with both dbs --- .../admin_handlers_integration_test.go | 30 ++++++----- .../auth_handlers_integration_test.go | 6 ++- .../file_handlers_integration_test.go | 6 ++- .../handlers/git_handlers_integration_test.go | 6 ++- server/internal/handlers/integration_test.go | 50 +++++++++++++++++-- .../user_handlers_integration_test.go | 6 ++- .../workspace_handlers_integration_test.go | 6 ++- 7 files changed, 88 insertions(+), 22 deletions(-) diff --git a/server/internal/handlers/admin_handlers_integration_test.go b/server/internal/handlers/admin_handlers_integration_test.go index ddb96f9..f53d659 100644 --- a/server/internal/handlers/admin_handlers_integration_test.go +++ b/server/internal/handlers/admin_handlers_integration_test.go @@ -15,21 +15,12 @@ import ( "github.com/stretchr/testify/require" ) -// 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 +func TestAdminHandlers_Integration(t *testing.T) { + runWithDatabases(t, testAdminHandlers) } -func TestAdminHandlers_Integration(t *testing.T) { - h := setupTestHarness(t) +func testAdminHandlers(t *testing.T, dbConfig DatabaseConfig) { + h := setupTestHarness(t, dbConfig) defer h.teardown(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) }) } + +// 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 +} diff --git a/server/internal/handlers/auth_handlers_integration_test.go b/server/internal/handlers/auth_handlers_integration_test.go index 45a2049..1f00d0f 100644 --- a/server/internal/handlers/auth_handlers_integration_test.go +++ b/server/internal/handlers/auth_handlers_integration_test.go @@ -19,7 +19,11 @@ import ( ) 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) t.Run("login", func(t *testing.T) { diff --git a/server/internal/handlers/file_handlers_integration_test.go b/server/internal/handlers/file_handlers_integration_test.go index c9a35a4..962b8f9 100644 --- a/server/internal/handlers/file_handlers_integration_test.go +++ b/server/internal/handlers/file_handlers_integration_test.go @@ -18,7 +18,11 @@ import ( ) 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) t.Run("file operations", func(t *testing.T) { diff --git a/server/internal/handlers/git_handlers_integration_test.go b/server/internal/handlers/git_handlers_integration_test.go index f458b86..23e2bf3 100644 --- a/server/internal/handlers/git_handlers_integration_test.go +++ b/server/internal/handlers/git_handlers_integration_test.go @@ -16,7 +16,11 @@ import ( ) 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) t.Run("git operations", func(t *testing.T) { diff --git a/server/internal/handlers/integration_test.go b/server/internal/handlers/integration_test.go index 43e2f64..7e9894c 100644 --- a/server/internal/handlers/integration_test.go +++ b/server/internal/handlers/integration_test.go @@ -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() diff --git a/server/internal/handlers/user_handlers_integration_test.go b/server/internal/handlers/user_handlers_integration_test.go index ff1363f..61b54f6 100644 --- a/server/internal/handlers/user_handlers_integration_test.go +++ b/server/internal/handlers/user_handlers_integration_test.go @@ -15,7 +15,11 @@ import ( ) 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) currentEmail := h.RegularTestUser.userModel.Email diff --git a/server/internal/handlers/workspace_handlers_integration_test.go b/server/internal/handlers/workspace_handlers_integration_test.go index a0fc06f..cb73efa 100644 --- a/server/internal/handlers/workspace_handlers_integration_test.go +++ b/server/internal/handlers/workspace_handlers_integration_test.go @@ -15,7 +15,11 @@ import ( ) 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) t.Run("list workspaces", func(t *testing.T) {