go-app/pkg/config/config_test.go
2025-01-05 20:29:18 -05:00

82 lines
1.8 KiB
Go

package config
import (
"encoding/json"
"testing"
)
// Changing defaults could be a breaking change,
// if this needs to be modified to pass the test,
// an item should be added to the changelog.
//
// This should be maintained, as it is the primary
// interface between an app and the app framework.
var testDefaultConfig = &AppConfig{
Environment: "development",
Version: getVersion(),
Logging: &LogConfig{
Enabled: true,
Level: "info",
Format: LogFormatJSON,
Output: "stderr",
TimeFormat: TimeFormatLong,
},
HTTP: &HTTPConfig{
Listen: "127.0.0.1:8080",
LogRequests: false,
ReadTimeout: "10s",
WriteTimeout: "10s",
IdleTimeout: "1m",
},
OTEL: &OTELConfig{
Enabled: true,
PrometheusEnabled: true,
PrometheusPath: "/metrics",
StdoutEnabled: false,
MetricIntervalSecs: 30,
},
}
func Test_loadConfig(t *testing.T) {
type args struct {
configPath string
}
tests := []struct {
name string
args args
want *AppConfig
wantErr bool
}{
{
name: "Ensure defaults",
args: args{configPath: ""},
want: testDefaultConfig,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := loadConfig(tt.args.configPath)
if (err != nil) != tt.wantErr {
t.Errorf("loadConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Marshal both the expected and actual structs to JSON
gotJSON, err := json.Marshal(got)
if err != nil {
t.Fatalf("Failed to marshal got to JSON: %v", err)
}
wantJSON, err := json.Marshal(tt.want)
if err != nil {
t.Fatalf("Failed to marshal want to JSON: %v", err)
}
// Compare the JSON strings
if string(gotJSON) != string(wantJSON) {
t.Errorf("loadConfig() JSON = %s, want JSON = %s", string(gotJSON), string(wantJSON))
}
})
}
}