Files
mtusi2026lab3/cmd/server/persistence_test.go
T

67 lines
2.2 KiB
Go

package main
import (
"testing"
"time"
"go.uber.org/fx"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store/postgres"
)
// TestDatabaseProviderRejectsInvalidConfiguration verifies dependency errors propagate before startup.
func TestDatabaseProviderRejectsInvalidConfiguration(t *testing.T) {
var lifecycle fx.Lifecycle
app := fx.New(fx.NopLogger, fx.Populate(&lifecycle))
if err := app.Err(); err != nil {
t.Fatal(err)
}
if _, err := newDatabase(lifecycle, &config.Persistence{}); err == nil {
t.Fatal("invalid database settings accepted")
}
}
// TestValkeyProviderRejectsInvalidConfiguration verifies the cache constructor is a real boundary.
func TestValkeyProviderRejectsInvalidConfiguration(t *testing.T) {
var lifecycle fx.Lifecycle
app := fx.New(fx.NopLogger, fx.Populate(&lifecycle))
if err := app.Err(); err != nil {
t.Fatal(err)
}
if _, err := newValkey(lifecycle, &config.Persistence{}); err == nil {
t.Fatal("invalid cache settings accepted")
}
}
// TestReadinessStopsAtUnavailableDatabase checks that mandatory persistence failure is visible.
func TestReadinessStopsAtUnavailableDatabase(t *testing.T) {
database, err := postgres.New(postgres.Options{URL: "postgres://localhost/test", MaxConnections: 1, Timeout: time.Second})
if err != nil {
t.Fatal(err)
}
database.Close()
readiness := newReadiness(database, nil)
if err := readiness.Ping(t.Context()); err == nil {
t.Fatal("closed database reported ready")
}
}
// TestMigrationCommandRejectsMissingConfiguration fails before opening a database.
func TestMigrationCommandRejectsMissingConfiguration(t *testing.T) {
t.Setenv("DATABASE_URL", "")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
if err := run([]string{"migrate"}); err == nil {
t.Fatal("missing database configuration accepted")
}
}
// TestMigrationCommandRejectsMalformedDatabaseURL fails without logging credentials.
func TestMigrationCommandRejectsMalformedDatabaseURL(t *testing.T) {
t.Setenv("DATABASE_URL", "://invalid")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
if err := run([]string{"migrate"}); err == nil {
t.Fatal("malformed database configuration accepted")
}
}