webhooker/internal/database/database_test.go
clawbot 49852e7506
All checks were successful
check / check (push) Successful in 1m0s
refactor: remove file-based configuration, use env vars only
Remove the entire pkg/config package (Viper-based YAML config file
loader) and simplify internal/config to read all settings directly from
environment variables via os.Getenv(). This eliminates the spurious
"Failed to load config" log messages that appeared when no config.yaml
file was present.

- Delete pkg/config/ (YAML loader, resolver, manager, tests)
- Delete configs/config.yaml.example
- Simplify internal/config helper functions to use os.Getenv() with
  defaults instead of falling back to pkgconfig
- Update tests to set env vars directly instead of creating in-memory
  YAML config files via afero
- Remove afero, cloud.google.com/*, aws-sdk-go dependencies from go.mod
- Update README: document env-var-only configuration, remove YAML/Viper
  references
- Keep godotenv/autoload for .env file convenience in local development

closes #27
2026-03-01 23:04:49 -08:00

87 lines
1.9 KiB
Go

package database
import (
"context"
"os"
"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 DBURL env var for config loading
os.Setenv("DBURL", "file::memory:?cache=shared")
defer os.Unsetenv("DBURL")
// Set up test dependencies
lc := fxtest.NewLifecycle(t)
// Create globals
globals.Appname = "webhooker-test"
globals.Version = "test"
globals.Buildarch = "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
c, err := config.New(lc, config.ConfigParams{
Globals: g,
Logger: l,
})
if err != nil {
t.Fatalf("Failed to create config: %v", err)
}
// Override DBURL to use a temp file-based SQLite (in-memory doesn't persist across connections)
c.DBURL = "file:" + t.TempDir() + "/test.db?cache=shared&mode=rwc"
// 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)
}
}