step one
This commit is contained in:
parent
7a8d5d13fa
commit
9e663db9dc
68 changed files with 5647 additions and 2958 deletions
76
internal/config/config_test.go
Normal file
76
internal/config/config_test.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadDefaults(t *testing.T) {
|
||||
t.Setenv("PREDICTOR_DATA_DIR", "")
|
||||
t.Setenv("PREDICTOR_PORT", "")
|
||||
t.Setenv("PREDICTOR_CONFIG_FILE", "")
|
||||
|
||||
cfg, err := Load(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.HTTP.Port != 8080 {
|
||||
t.Errorf("default port = %d, want 8080", cfg.HTTP.Port)
|
||||
}
|
||||
if cfg.Download.Parallel != 8 {
|
||||
t.Errorf("default parallel = %d, want 8", cfg.Download.Parallel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadEnvOverridesDefaults(t *testing.T) {
|
||||
t.Setenv("PREDICTOR_PORT", "9090")
|
||||
t.Setenv("PREDICTOR_UPDATE_INTERVAL", "30m")
|
||||
|
||||
cfg, err := Load(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.HTTP.Port != 9090 {
|
||||
t.Errorf("env port = %d, want 9090", cfg.HTTP.Port)
|
||||
}
|
||||
if cfg.Download.UpdateInterval != 30*time.Minute {
|
||||
t.Errorf("env update interval = %v, want 30m", cfg.Download.UpdateInterval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFlagsOverrideEnv(t *testing.T) {
|
||||
t.Setenv("PREDICTOR_PORT", "9090")
|
||||
cfg, err := Load([]string{"-port", "7777"})
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.HTTP.Port != 7777 {
|
||||
t.Errorf("flag should override env: port = %d, want 7777", cfg.HTTP.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileOverridesDefaults(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "predictor.yml")
|
||||
if err := os.WriteFile(path, []byte("http:\n port: 12345\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg, err := Load([]string{"-config", path})
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.HTTP.Port != 12345 {
|
||||
t.Errorf("file port = %d, want 12345", cfg.HTTP.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
cfg := Defaults()
|
||||
cfg.Data.Dir = ""
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("expected validation error for empty data dir")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue