85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoad(t *testing.T) {
|
|
// Create a temporary test config file
|
|
content := []byte(`
|
|
database:
|
|
driver: postgres
|
|
dsn: postgres://user:pass@localhost:5432/dbname
|
|
server:
|
|
port: 8080
|
|
host: localhost
|
|
jwt:
|
|
secret: test-secret
|
|
expiration: 24h
|
|
storage:
|
|
type: local
|
|
local:
|
|
root_dir: /tmp/storage
|
|
upload:
|
|
max_size: 10485760
|
|
logging:
|
|
level: info
|
|
format: json
|
|
`)
|
|
|
|
tmpDir := t.TempDir()
|
|
configPath := filepath.Join(tmpDir, "config.yaml")
|
|
if err := os.WriteFile(configPath, content, 0644); err != nil {
|
|
t.Fatalf("Failed to write test config: %v", err)
|
|
}
|
|
|
|
// Test loading config
|
|
cfg, err := Load(configPath)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
|
|
// Verify loaded values
|
|
tests := []struct {
|
|
name string
|
|
got interface{}
|
|
want interface{}
|
|
errorMsg string
|
|
}{
|
|
{"Database Driver", cfg.Database.Driver, "postgres", "incorrect database driver"},
|
|
{"Server Port", cfg.Server.Port, 8080, "incorrect server port"},
|
|
{"JWT Secret", cfg.JWT.Secret, "test-secret", "incorrect JWT secret"},
|
|
{"Storage Type", cfg.Storage.Type, "local", "incorrect storage type"},
|
|
{"Logging Level", cfg.Logging.Level, "info", "incorrect logging level"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.got != tt.want {
|
|
t.Errorf("%s = %v, want %v", tt.name, tt.got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadError(t *testing.T) {
|
|
// Test loading non-existent file
|
|
_, err := Load("non-existent-file.yaml")
|
|
if err == nil {
|
|
t.Error("Load() error = nil, want error for non-existent file")
|
|
}
|
|
|
|
// Test loading invalid YAML
|
|
tmpDir := t.TempDir()
|
|
invalidPath := filepath.Join(tmpDir, "invalid.yaml")
|
|
if err := os.WriteFile(invalidPath, []byte("invalid: }{yaml"), 0644); err != nil {
|
|
t.Fatalf("Failed to write invalid config: %v", err)
|
|
}
|
|
|
|
_, err = Load(invalidPath)
|
|
if err == nil {
|
|
t.Error("Load() error = nil, want error for invalid YAML")
|
|
}
|
|
}
|