refactor: Add config validation, defaults, and unit tests
This commit is contained in:
63
config/config_test.go
Normal file
63
config/config_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetConfig(t *testing.T) {
|
||||
// Set up test environment variables
|
||||
envVars := map[string]string{
|
||||
EnvLogLevel: "debug",
|
||||
EnvRootPath: ".",
|
||||
EnvNumWorkers: "5",
|
||||
EnvWorkerBatchSize: "100",
|
||||
EnvMaxResponseSize: "1048576",
|
||||
EnvResponseTimeout: "30",
|
||||
}
|
||||
|
||||
for k, v := range envVars {
|
||||
os.Setenv(k, v)
|
||||
defer os.Unsetenv(k)
|
||||
}
|
||||
|
||||
// Get configuration
|
||||
config := GetConfig()
|
||||
|
||||
// Assert configuration values
|
||||
assert.Equal(t, zerolog.DebugLevel, config.LogLevel)
|
||||
assert.Equal(t, ".", config.RootPath)
|
||||
assert.Equal(t, 5, config.NumOfWorkers)
|
||||
assert.Equal(t, 100, config.WorkerBatchSize)
|
||||
assert.Equal(t, 1048576, config.MaxResponseSize)
|
||||
assert.Equal(t, 30, config.ResponseTimeout)
|
||||
}
|
||||
|
||||
func TestParsePositiveInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
wantErr bool
|
||||
}{
|
||||
{"valid positive", "42", 42, false},
|
||||
{"zero", "0", 0, true},
|
||||
{"negative", "-1", 0, true},
|
||||
{"invalid", "abc", 0, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parsePositiveInt(tt.input)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user