8e66e55119
DRAFT PR: Prototype for serviceconfig * in progress * in progress * extract common methods * Merge remote-tracking branch 'origin/feature/serviceconfig' into feature/serviceconfig * fix types * cleanup * add tests * Merged main into feature/serviceconfig Approved-by: Michael McGuinness
127 lines
3.1 KiB
Go
127 lines
3.1 KiB
Go
package serviceconfig
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
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",
|
|
},
|
|
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",
|
|
// INT_TEST intentionally omitted
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "INT_TEST",
|
|
},
|
|
{
|
|
name: "invalid boolean value",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "notabool",
|
|
"INT_TEST": "42",
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "BoolTest",
|
|
},
|
|
{
|
|
name: "invalid integer value",
|
|
envVars: map[string]string{
|
|
"APP_ENV": "testing",
|
|
"BOOL_TEST": "true",
|
|
"INT_TEST": "notanint",
|
|
},
|
|
wantErr: true,
|
|
errMessageContains: "parse error on field \"IntTest\"",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Clear environment before each test
|
|
os.Clearenv()
|
|
|
|
// Set environment variables for the test
|
|
for k, v := range tt.envVars {
|
|
os.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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|