118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDefaultConfig(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
|
|
if cfg.DBHost != "localhost" {
|
|
t.Errorf("Expected DBHost to be 'localhost', got %q", cfg.DBHost)
|
|
}
|
|
|
|
if cfg.DBPort != 5432 {
|
|
t.Errorf("Expected DBPort to be 5432, got %d", cfg.DBPort)
|
|
}
|
|
|
|
if cfg.DBSchema != "public" {
|
|
t.Errorf("Expected DBSchema to be 'public', got %q", cfg.DBSchema)
|
|
}
|
|
|
|
if cfg.DBUser != "postgres" {
|
|
t.Errorf("Expected DBUser to be 'postgres', got %q", cfg.DBUser)
|
|
}
|
|
|
|
if cfg.DBPassword != "postgres" {
|
|
t.Errorf("Expected DBPassword to be 'postgres', got %q", cfg.DBPassword)
|
|
}
|
|
}
|
|
|
|
func TestSaveAndLoad(t *testing.T) {
|
|
// Create a temporary directory and set HOME to it
|
|
tmpDir := t.TempDir()
|
|
oldHome := os.Getenv("HOME")
|
|
os.Setenv("HOME", tmpDir)
|
|
defer os.Setenv("HOME", oldHome)
|
|
|
|
configPath := filepath.Join(tmpDir, ".config", "entity-maker.toml")
|
|
|
|
// Create a test config
|
|
cfg := &Config{
|
|
DBHost: "testhost",
|
|
DBPort: 5433,
|
|
DBName: "testdb",
|
|
DBSchema: "testschema",
|
|
DBUser: "testuser",
|
|
DBPassword: "testpass",
|
|
DBTable: "testtable",
|
|
OutputDir: "/tmp/test",
|
|
EntityName: "TestEntity",
|
|
}
|
|
|
|
// Save config
|
|
err := cfg.Save()
|
|
if err != nil {
|
|
t.Fatalf("Failed to save config: %v", err)
|
|
}
|
|
|
|
// Verify file exists
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
t.Fatalf("Config file was not created: %v", err)
|
|
}
|
|
|
|
// Load config
|
|
loadedCfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
// Verify loaded values
|
|
tests := []struct {
|
|
name string
|
|
got interface{}
|
|
expected interface{}
|
|
}{
|
|
{"DBHost", loadedCfg.DBHost, cfg.DBHost},
|
|
{"DBPort", loadedCfg.DBPort, cfg.DBPort},
|
|
{"DBName", loadedCfg.DBName, cfg.DBName},
|
|
{"DBSchema", loadedCfg.DBSchema, cfg.DBSchema},
|
|
{"DBUser", loadedCfg.DBUser, cfg.DBUser},
|
|
{"DBPassword", loadedCfg.DBPassword, cfg.DBPassword},
|
|
{"DBTable", loadedCfg.DBTable, cfg.DBTable},
|
|
{"OutputDir", loadedCfg.OutputDir, cfg.OutputDir},
|
|
{"EntityName", loadedCfg.EntityName, cfg.EntityName},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
if tt.got != tt.expected {
|
|
t.Errorf("%s mismatch: expected %v, got %v", tt.name, tt.expected, tt.got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadNonExistentConfig(t *testing.T) {
|
|
// Create a temporary directory and set HOME to it
|
|
tmpDir := t.TempDir()
|
|
oldHome := os.Getenv("HOME")
|
|
os.Setenv("HOME", tmpDir)
|
|
defer os.Setenv("HOME", oldHome)
|
|
|
|
// Load should return default config when file doesn't exist
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load should not error when file doesn't exist: %v", err)
|
|
}
|
|
|
|
// Verify it's the default config
|
|
defaultCfg := DefaultConfig()
|
|
if cfg.DBHost != defaultCfg.DBHost {
|
|
t.Errorf("Expected default DBHost %q, got %q", defaultCfg.DBHost, cfg.DBHost)
|
|
}
|
|
if cfg.DBPort != defaultCfg.DBPort {
|
|
t.Errorf("Expected default DBPort %d, got %d", defaultCfg.DBPort, cfg.DBPort)
|
|
}
|
|
}
|