mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Run integration tests with both dbs
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user