All checks were successful
check / check (push) Successful in 6s
Remove the Buildarch field from the globals package variable, struct, and constructor. Remove all references in main.go (including the runtime import), logger startup output, and test setup across globals, logger, database, and webhook_db_manager packages. closes #30
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"go.uber.org/fx/fxtest"
|
|
"sneak.berlin/go/webhooker/internal/config"
|
|
"sneak.berlin/go/webhooker/internal/globals"
|
|
"sneak.berlin/go/webhooker/internal/logger"
|
|
)
|
|
|
|
func TestDatabaseConnection(t *testing.T) {
|
|
// Set up test dependencies
|
|
lc := fxtest.NewLifecycle(t)
|
|
|
|
// Create globals
|
|
globals.Appname = "webhooker-test"
|
|
globals.Version = "test"
|
|
|
|
g, err := globals.New(lc)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create globals: %v", err)
|
|
}
|
|
|
|
// Create logger
|
|
l, err := logger.New(lc, logger.LoggerParams{Globals: g})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create logger: %v", err)
|
|
}
|
|
|
|
// Create config with DataDir pointing to a temp directory
|
|
c := &config.Config{
|
|
DataDir: t.TempDir(),
|
|
Environment: "dev",
|
|
}
|
|
|
|
// Create database
|
|
db, err := New(lc, DatabaseParams{
|
|
Config: c,
|
|
Logger: l,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Failed to create database: %v", err)
|
|
}
|
|
|
|
// Start lifecycle (this will trigger the connection)
|
|
ctx := context.Background()
|
|
err = lc.Start(ctx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to connect to database: %v", err)
|
|
}
|
|
defer func() {
|
|
if stopErr := lc.Stop(ctx); stopErr != nil {
|
|
t.Errorf("Failed to stop lifecycle: %v", stopErr)
|
|
}
|
|
}()
|
|
|
|
// Verify we can get the DB instance
|
|
if db.DB() == nil {
|
|
t.Error("Expected non-nil database connection")
|
|
}
|
|
|
|
// Test that we can perform a simple query
|
|
var result int
|
|
err = db.DB().Raw("SELECT 1").Scan(&result).Error
|
|
if err != nil {
|
|
t.Fatalf("Failed to execute test query: %v", err)
|
|
}
|
|
|
|
if result != 1 {
|
|
t.Errorf("Expected query result to be 1, got %d", result)
|
|
}
|
|
}
|