package config import ( "os" "path/filepath" "testing" ) func TestLoad(t *testing.T) { // Create a temporary test config file testConfig := ` database: driver: postgres dsn: postgres://user:pass@localhost:5432/db server: port: 8080 host: localhost jwt: secret: test-secret expiration: 24h logging: level: debug format: console ` tmpDir := t.TempDir() configPath := filepath.Join(tmpDir, "config.yaml") if err := os.WriteFile(configPath, []byte(testConfig), 0644); err != nil { t.Fatalf("Failed to create test config file: %v", err) } // Test successful config loading cfg, err := Load(configPath) if err != nil { t.Fatalf("Failed to load config: %v", err) } // Verify loaded values tests := []struct { name string got interface{} expected interface{} }{ {"database.driver", cfg.Database.Driver, "postgres"}, {"database.dsn", cfg.Database.DSN, "postgres://user:pass@localhost:5432/db"}, {"server.port", cfg.Server.Port, 8080}, {"server.host", cfg.Server.Host, "localhost"}, {"jwt.secret", cfg.JWT.Secret, "test-secret"}, {"jwt.expiration", cfg.JWT.Expiration, "24h"}, {"logging.level", cfg.Logging.Level, "debug"}, {"logging.format", cfg.Logging.Format, "console"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.got != tt.expected { t.Errorf("Config %s = %v, want %v", tt.name, tt.got, tt.expected) } }) } // Test loading non-existent file _, err = Load("non-existent.yaml") if err == nil { t.Error("Expected error when loading non-existent file, got nil") } // Test loading invalid YAML invalidPath := filepath.Join(tmpDir, "invalid.yaml") if err := os.WriteFile(invalidPath, []byte("invalid: yaml: content"), 0644); err != nil { t.Fatalf("Failed to create invalid config file: %v", err) } _, err = Load(invalidPath) if err == nil { t.Error("Expected error when loading invalid YAML, got nil") } }