81e7223560
Text Extraction * bases * go * splitting * structure * movetoasync * movetoasync * settinguptrigger * reorder * storevent * standardisepollingvalidation * unittests * fixlint * fixlint * awscfg * generatesample * followthrough * tests * clena * store * externalidcleanup * clientid * local * baseunittests * putobjecttests * tests
148 lines
4.0 KiB
Go
148 lines
4.0 KiB
Go
package serviceconfig
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Struct specifically for initialize config testing
|
|
type initializeConfigTestStruct 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",
|
|
"PGUSER": "postgres",
|
|
"PGPASSWORD": "pass",
|
|
"PGHOST": "localhost",
|
|
"PGPORT": "5432",
|
|
"PGDATABASE": "query_orchestration",
|
|
"DB_NOSSL": "true",
|
|
"AWS_ACCESS_KEY_ID": "key",
|
|
"AWS_SECRET_ACCESS_KEY": "secret",
|
|
"AWS_REGION": "region",
|
|
"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",
|
|
"PGUSER": "postgres",
|
|
"PGPASSWORD": "pass",
|
|
"PGHOST": "localhost",
|
|
"PGPORT": "5432",
|
|
"PGDATABASE": "query_orchestration",
|
|
"DB_NOSSL": "true",
|
|
"AWS_ACCESS_KEY_ID": "key",
|
|
"AWS_SECRET_ACCESS_KEY": "secret",
|
|
"AWS_REGION": "region",
|
|
"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) {
|
|
// Clear environment at the start of each subtest
|
|
os.Clearenv()
|
|
|
|
// Set environment variables for this test case
|
|
for k, v := range tt.envVars {
|
|
t.Setenv(k, v)
|
|
}
|
|
|
|
// Use the test-specific config struct
|
|
cfg := &initializeConfigTestStruct{}
|
|
err := initializeConfigPrivate(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
|
|
}
|
|
// Check error message contains expected string
|
|
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
os.Clearenv()
|
|
|
|
}
|
|
|
|
func TestGetBaseConfig(t *testing.T) {
|
|
assert.NotNil(t, getBaseConfig(&BaseConfig{}))
|
|
assert.Nil(t, getBaseConfig(nil))
|
|
assert.NotNil(t, getBaseConfig(&initializeConfigTestStruct{}))
|
|
os.Clearenv()
|
|
}
|