Add config helper tests
This commit is contained in:
parent
6ed08fbbe7
commit
8588d07388
1 changed files with 112 additions and 0 deletions
|
|
@ -198,3 +198,115 @@ func TestSwaggerSchemesTrimmed(t *testing.T) {
|
||||||
t.Errorf("expected SwaggerSchemes [https http], got %v", cfg.SwaggerSchemes)
|
t.Errorf("expected SwaggerSchemes [https http], got %v", cfg.SwaggerSchemes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetEnv(t *testing.T) {
|
||||||
|
t.Setenv("TEST_CONFIG_STRING", "value")
|
||||||
|
|
||||||
|
if got := getEnv("TEST_CONFIG_STRING", "fallback"); got != "value" {
|
||||||
|
t.Errorf("expected getEnv to return value, got %q", got)
|
||||||
|
}
|
||||||
|
if got := getEnv("MISSING_CONFIG_STRING", "fallback"); got != "fallback" {
|
||||||
|
t.Errorf("expected getEnv to return fallback, got %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetEnvInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
fallback int
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{name: "valid integer", value: "42", fallback: 10, expected: 42},
|
||||||
|
{name: "invalid integer", value: "not-a-number", fallback: 10, expected: 10},
|
||||||
|
{name: "empty value", value: "", fallback: 7, expected: 7},
|
||||||
|
{name: "negative integer", value: "-5", fallback: 3, expected: -5},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Setenv("TEST_CONFIG_INT", tt.value)
|
||||||
|
if got := getEnvInt("TEST_CONFIG_INT", tt.fallback); got != tt.expected {
|
||||||
|
t.Errorf("expected %d, got %d", tt.expected, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetEnvDuration(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
fallback time.Duration
|
||||||
|
expected time.Duration
|
||||||
|
}{
|
||||||
|
{name: "valid duration", value: "45m", fallback: time.Hour, expected: 45 * time.Minute},
|
||||||
|
{name: "invalid duration", value: "not-a-duration", fallback: time.Minute, expected: time.Minute},
|
||||||
|
{name: "empty value", value: "", fallback: 30 * time.Second, expected: 30 * time.Second},
|
||||||
|
{name: "complex duration", value: "1h30m", fallback: time.Minute, expected: 90 * time.Minute},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Setenv("TEST_CONFIG_DURATION", tt.value)
|
||||||
|
if got := getEnvDuration("TEST_CONFIG_DURATION", tt.fallback); got != tt.expected {
|
||||||
|
t.Errorf("expected %v, got %v", tt.expected, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetEnvFloat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
fallback float64
|
||||||
|
expected float64
|
||||||
|
}{
|
||||||
|
{name: "valid float", value: "3.14", fallback: 1.2, expected: 3.14},
|
||||||
|
{name: "valid integer string", value: "2", fallback: 1.2, expected: 2},
|
||||||
|
{name: "invalid float", value: "invalid", fallback: 1.2, expected: 1.2},
|
||||||
|
{name: "empty value", value: "", fallback: 2.5, expected: 2.5},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Setenv("TEST_CONFIG_FLOAT", tt.value)
|
||||||
|
if got := getEnvFloat("TEST_CONFIG_FLOAT", tt.fallback); got != tt.expected {
|
||||||
|
t.Errorf("expected %v, got %v", tt.expected, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetEnvStringSlice(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
fallback []string
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{name: "comma separated", value: "a,b,c", fallback: []string{"fallback"}, expected: []string{"a", "b", "c"}},
|
||||||
|
{name: "single value", value: "solo", fallback: []string{"fallback"}, expected: []string{"solo"}},
|
||||||
|
{name: "trim spaces", value: " a , b ", fallback: []string{"fallback"}, expected: []string{"a", "b"}},
|
||||||
|
{name: "empty entries", value: "a,,b,", fallback: []string{"fallback"}, expected: []string{"a", "b"}},
|
||||||
|
{name: "trailing spaces", value: "one ,two ", fallback: []string{"fallback"}, expected: []string{"one", "two"}},
|
||||||
|
{name: "only separators", value: " , , ", fallback: []string{"fallback"}, expected: []string{"fallback"}},
|
||||||
|
{name: "empty value", value: "", fallback: []string{"fallback"}, expected: []string{"fallback"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Setenv("TEST_CONFIG_SLICE", tt.value)
|
||||||
|
got := getEnvStringSlice("TEST_CONFIG_SLICE", tt.fallback)
|
||||||
|
if len(got) != len(tt.expected) {
|
||||||
|
t.Fatalf("expected %v, got %v", tt.expected, got)
|
||||||
|
}
|
||||||
|
for i, expected := range tt.expected {
|
||||||
|
if got[i] != expected {
|
||||||
|
t.Fatalf("expected %v, got %v", tt.expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue