15adaebfcd
DRAFT PR : WIP working through ideas for integration * movearound * attempttwo * openapi * further sanding * fix * start on tests * runthroughsingleconfig * somechanges * reflectissue * removeerrs * mostlyremovepanic * removeenv * noncfgtests * go * repo * fix service config test * add PWD to all * test fix * fix lint * todo for later * passingunittests * alltests * testlogger * testloggername * clean
174 lines
4.3 KiB
Go
174 lines
4.3 KiB
Go
package serviceconfig
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type TestingServiceNameConfig struct {
|
|
BaseConfig
|
|
AppEnv string `env:"APP_ENV"`
|
|
BoolTest bool `env:"BOOL_TEST"`
|
|
IntTest int `env:"INT_TEST,required,notEmpty"`
|
|
}
|
|
|
|
func TestInitializeConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
envVars map[string]string
|
|
wantErr bool
|
|
errMessageContains string
|
|
}{
|
|
{
|
|
name: "valid configuration",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "true",
|
|
"INT_TEST": "42",
|
|
"DB_USER": "postgres",
|
|
"DB_PASS": "pass",
|
|
"DB_HOST": "localhost",
|
|
"DB_PORT": "5432",
|
|
"DB_NAME": "query_orchestration",
|
|
"DB_NOSSL": "true",
|
|
"SUB_FIELD1:": "value1",
|
|
"SUB_FIELD2": "42",
|
|
"PWD": "/foo",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "missing required env var",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "true",
|
|
"DB_USER": "postgres",
|
|
"DB_PASS": "pass",
|
|
"DB_HOST": "localhost",
|
|
"DB_PORT": "5432",
|
|
"DB_NAME": "query_orchestration",
|
|
"DB_NOSSL": "true",
|
|
"PWD": "/foo",
|
|
// INT_TEST intentionally omitted
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "INT_TEST",
|
|
},
|
|
{
|
|
name: "invalid boolean value",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "notabool",
|
|
"PWD": "/foo",
|
|
"INT_TEST": "42",
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "BoolTest",
|
|
},
|
|
{
|
|
name: "invalid integer value",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "true",
|
|
"PWD": "/foo",
|
|
"INT_TEST": "notanint",
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "parse error on field \"IntTest\"",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
os.Clearenv()
|
|
|
|
for k, v := range tt.envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
|
|
cfg := &TestingServiceNameConfig{}
|
|
err := InitializeConfig(cfg)
|
|
|
|
logger := cfg.GetLogger()
|
|
assert.NotNil(t, logger)
|
|
logger.Info("Logger initialized")
|
|
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("InitializeConfig() error = nil, wantErr %v", tt.wantErr)
|
|
return
|
|
}
|
|
// if we expect an error, check if the error message tt.errMessageContains is contained in the error message
|
|
if tt.errMessageContains != "" && !strings.Contains(err.Error(), tt.errMessageContains) {
|
|
t.Errorf("InitializeConfig() error = %v, want error containing %v", err, tt.errMessageContains)
|
|
}
|
|
|
|
} else {
|
|
if err != nil {
|
|
t.Errorf("InitializeConfig() unexpected error = %v", err)
|
|
return
|
|
}
|
|
|
|
// Verify the values were set correctly
|
|
if cfg.AppEnv != tt.envVars["APP_ENV"] {
|
|
t.Errorf("AppEnv = %v, want %v", cfg.AppEnv, tt.envVars["APP_ENV"])
|
|
}
|
|
if cfg.BoolTest != (tt.envVars["BOOL_TEST"] == "true") {
|
|
t.Errorf("BoolTest = %v, want %v", cfg.BoolTest, tt.envVars["BOOL_TEST"] == "true")
|
|
}
|
|
expectedInt := 42 // Known value from test cases
|
|
if cfg.IntTest != expectedInt {
|
|
t.Errorf("IntTest = %v, want %v", cfg.IntTest, expectedInt)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetBaseConfig(t *testing.T) {
|
|
assert.NotNil(t, getBaseConfig(&BaseConfig{}))
|
|
assert.Nil(t, getBaseConfig(nil))
|
|
assert.NotNil(t, getBaseConfig(&TestingServiceNameConfig{}))
|
|
}
|
|
|
|
func TestGetBasePath(t *testing.T) {
|
|
cfg := &BaseConfig{
|
|
Pwd: "pwd_path",
|
|
}
|
|
assert.Equal(t, "pwd_path", cfg.GetBasePath())
|
|
cfg.BasePath = "base_path"
|
|
assert.Equal(t, "base_path", cfg.GetBasePath())
|
|
}
|
|
|
|
func TestGetLogger(t *testing.T) {
|
|
cfg := &BaseConfig{}
|
|
assert.Nil(t, cfg.GetLogger())
|
|
cfg.Logger = slog.Default()
|
|
assert.NotNil(t, cfg.GetLogger())
|
|
}
|
|
|
|
func TestLogConfig(t *testing.T) {
|
|
cfg := &BaseConfig{}
|
|
tl := &testLogger{T: t}
|
|
cfg.Logger = slog.New(tl)
|
|
cfg.LogConfig("")
|
|
assert.Len(t, tl.Logs, 14)
|
|
assert.Equal(t, "Logger", tl.Logs[0]["key"])
|
|
}
|
|
|
|
func TestLogConfigRecursive(t *testing.T) {
|
|
cfg := &BaseConfig{}
|
|
tl := &testLogger{T: t}
|
|
cfg.Logger = slog.New(tl)
|
|
v := struct{ Example string }{Example: "examplestring"}
|
|
cfg.logConfigRecursive(reflect.ValueOf(v), "", "", make(map[reflect.Value]bool))
|
|
assert.Len(t, tl.Logs, 1)
|
|
assert.Equal(t, "Example", tl.Logs[0]["key"])
|
|
assert.Equal(t, "examp...", tl.Logs[0]["value"])
|
|
}
|