feat: first
This commit is contained in:
193
internal/config/config_test.go
Normal file
193
internal/config/config_test.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Save original env vars
|
||||
originalGRPCPort := os.Getenv("SERVER_GRPC_PORT")
|
||||
originalHTTPPort := os.Getenv("SERVER_HTTP_PORT")
|
||||
originalLogLevel := os.Getenv("LOG_LEVEL")
|
||||
originalLogFormat := os.Getenv("LOG_FORMAT")
|
||||
originalMetricsEnabled := os.Getenv("METRICS_ENABLED")
|
||||
|
||||
// Clean up after test
|
||||
defer func() {
|
||||
os.Setenv("SERVER_GRPC_PORT", originalGRPCPort)
|
||||
os.Setenv("SERVER_HTTP_PORT", originalHTTPPort)
|
||||
os.Setenv("LOG_LEVEL", originalLogLevel)
|
||||
os.Setenv("LOG_FORMAT", originalLogFormat)
|
||||
os.Setenv("METRICS_ENABLED", originalMetricsEnabled)
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
envVars map[string]string
|
||||
expected Config
|
||||
}{
|
||||
{
|
||||
name: "default values",
|
||||
envVars: map[string]string{},
|
||||
expected: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "8080",
|
||||
HTTPPort: "8090",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
},
|
||||
Metrics: MetricsConfig{
|
||||
Enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "custom values",
|
||||
envVars: map[string]string{
|
||||
"SERVER_GRPC_PORT": "9090",
|
||||
"SERVER_HTTP_PORT": "9091",
|
||||
"LOG_LEVEL": "debug",
|
||||
"LOG_FORMAT": "text",
|
||||
"METRICS_ENABLED": "false",
|
||||
},
|
||||
expected: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "9090",
|
||||
HTTPPort: "9091",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "debug",
|
||||
Format: "text",
|
||||
},
|
||||
Metrics: MetricsConfig{
|
||||
Enabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Clear all env vars first
|
||||
os.Unsetenv("SERVER_GRPC_PORT")
|
||||
os.Unsetenv("SERVER_HTTP_PORT")
|
||||
os.Unsetenv("LOG_LEVEL")
|
||||
os.Unsetenv("LOG_FORMAT")
|
||||
os.Unsetenv("METRICS_ENABLED")
|
||||
|
||||
// Set test env vars
|
||||
for key, value := range tt.envVars {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.Server.GRPCPort != tt.expected.Server.GRPCPort {
|
||||
t.Errorf("expected GRPCPort %s, got %s", tt.expected.Server.GRPCPort, cfg.Server.GRPCPort)
|
||||
}
|
||||
if cfg.Server.HTTPPort != tt.expected.Server.HTTPPort {
|
||||
t.Errorf("expected HTTPPort %s, got %s", tt.expected.Server.HTTPPort, cfg.Server.HTTPPort)
|
||||
}
|
||||
if cfg.Log.Level != tt.expected.Log.Level {
|
||||
t.Errorf("expected LogLevel %s, got %s", tt.expected.Log.Level, cfg.Log.Level)
|
||||
}
|
||||
if cfg.Log.Format != tt.expected.Log.Format {
|
||||
t.Errorf("expected LogFormat %s, got %s", tt.expected.Log.Format, cfg.Log.Format)
|
||||
}
|
||||
if cfg.Metrics.Enabled != tt.expected.Metrics.Enabled {
|
||||
t.Errorf("expected MetricsEnabled %t, got %t", tt.expected.Metrics.Enabled, cfg.Metrics.Enabled)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config Config
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid config",
|
||||
config: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "8080",
|
||||
HTTPPort: "8090",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty grpc port",
|
||||
config: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "",
|
||||
HTTPPort: "8090",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "same ports",
|
||||
config: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "8080",
|
||||
HTTPPort: "8080",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "info",
|
||||
Format: "json",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid log level",
|
||||
config: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "8080",
|
||||
HTTPPort: "8090",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "invalid",
|
||||
Format: "json",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid log format",
|
||||
config: Config{
|
||||
Server: ServerConfig{
|
||||
GRPCPort: "8080",
|
||||
HTTPPort: "8090",
|
||||
},
|
||||
Log: LogConfig{
|
||||
Level: "info",
|
||||
Format: "invalid",
|
||||
},
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.config.Validate()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user