All checks were successful
check / check (push) Successful in 2m19s
- Create cmd/webhooker/main.go with fx dependency injection wiring - Add REPO_POLICIES.md, .editorconfig, .dockerignore - Add .gitea/workflows/check.yml for CI (docker build on push) - Rewrite Makefile with all required targets (test, lint, fmt, fmt-check, check, build, hooks, docker, clean, dev, run, deps) - Rewrite Dockerfile with sha256-pinned base images, golangci-lint installed from verified release archive, make check as build step - Fix README.md: add required sections (description, getting started, rationale, design, TODO, license, author) - Integrate TODO.md content into README.md and remove TODO.md - Move config.yaml to configs/config.yaml.example - Fix .gitignore pattern for webhooker binary - Fix static/static.go embed directive (remove empty vendor dir) - Fix database test to use in-memory config (no filesystem dependency) closes #1 closes #2
107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.eeqj.de/sneak/webhooker/internal/config"
|
|
"git.eeqj.de/sneak/webhooker/internal/globals"
|
|
"git.eeqj.de/sneak/webhooker/internal/logger"
|
|
pkgconfig "git.eeqj.de/sneak/webhooker/pkg/config"
|
|
"github.com/spf13/afero"
|
|
"go.uber.org/fx/fxtest"
|
|
)
|
|
|
|
func TestDatabaseConnection(t *testing.T) {
|
|
// Set up in-memory config so the test does not depend on config.yaml on disk
|
|
fs := afero.NewMemMapFs()
|
|
testConfigYAML := `
|
|
environments:
|
|
dev:
|
|
config:
|
|
port: 8080
|
|
debug: false
|
|
maintenanceMode: false
|
|
developmentMode: true
|
|
environment: dev
|
|
dburl: "file::memory:?cache=shared"
|
|
secrets:
|
|
sessionKey: d2ViaG9va2VyLWRldi1zZXNzaW9uLWtleS1pbnNlY3VyZSE=
|
|
sentryDSN: ""
|
|
configDefaults:
|
|
port: 8080
|
|
`
|
|
if err := afero.WriteFile(fs, "config.yaml", []byte(testConfigYAML), 0644); err != nil {
|
|
t.Fatalf("Failed to write test config: %v", err)
|
|
}
|
|
pkgconfig.SetFs(fs)
|
|
|
|
// 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)
|
|
}
|
|
}
|