Add tests
This commit is contained in:
81
pkg/config/config_test.go
Normal file
81
pkg/config/config_test.go
Normal file
@ -0,0 +1,81 @@
|
||||
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))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
43
pkg/config/ctx_test.go
Normal file
43
pkg/config/ctx_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFromCtx(t *testing.T) {
|
||||
app := &AppConfig{
|
||||
Name: "testapp",
|
||||
}
|
||||
appCtx := app.AddToCtx(context.Background())
|
||||
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Unprepared app context",
|
||||
args: args{ctx: context.Background()},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Prepared app context",
|
||||
args: args{ctx: appCtx},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := FromCtx(tt.args.ctx)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FromCtx() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user