Merge pull request #54 from rede5/codex/add-10-percent-more-tests

Add tests for config helper functions
This commit is contained in:
Tiago Yamamoto 2026-01-01 15:43:50 -03:00 committed by GitHub
commit f8148c428e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)
}
}
})
}
}