ci / go (push) Waiting to run
ci / go-db (agent) (push) Waiting to run
ci / go-db (config) (push) Waiting to run
ci / go-db (db) (push) Waiting to run
ci / go-db (evidence) (push) Waiting to run
ci / go-db (llmrec) (push) Waiting to run
ci / go-db (server) (push) Waiting to run
detections / detections (push) Waiting to run
web / web (push) Waiting to run
docs / links (push) Canceled after 0s
43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestPostgresDSNPrecedence(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cfgPath := filepath.Join(dir, "config.json")
|
|
os.WriteFile(cfgPath, []byte(`{"database":{"host":"10.1.2.3","port":6000,"user":"u","password":"p","dbname":"d","sslmode":"require"}}`), 0o644)
|
|
t.Setenv("ARTEX_CONFIG", cfgPath)
|
|
|
|
// no env DSN → built from config file fields
|
|
t.Setenv("ARTEX_PG_DSN", "")
|
|
got, _, err := PostgresDSN()
|
|
want := "postgres://u:p@10.1.2.3:6000/d?sslmode=require"
|
|
if err != nil || got != want {
|
|
t.Fatalf("from file: got %q err %v want %q", got, err, want)
|
|
}
|
|
|
|
// env wins over config file
|
|
t.Setenv("ARTEX_PG_DSN", "postgres://envwins/x")
|
|
if got, _, err := PostgresDSN(); err != nil || got != "postgres://envwins/x" {
|
|
t.Fatalf("env should win, got %q err %v", got, err)
|
|
}
|
|
|
|
// no env, no file → error (no built-in default)
|
|
t.Setenv("ARTEX_PG_DSN", "")
|
|
t.Setenv("ARTEX_CONFIG", filepath.Join(dir, "nope.json"))
|
|
if got, _, err := PostgresDSN(); err == nil {
|
|
t.Fatalf("missing config should error, got %q", got)
|
|
}
|
|
|
|
// full dsn in config file is used verbatim
|
|
os.WriteFile(cfgPath, []byte(`{"database":{"dsn":"postgres://full/dsn"}}`), 0o644)
|
|
t.Setenv("ARTEX_CONFIG", cfgPath)
|
|
if got, _, err := PostgresDSN(); err != nil || got != "postgres://full/dsn" {
|
|
t.Fatalf("file dsn verbatim, got %q err %v", got, err)
|
|
}
|
|
}
|