Files
weatherreporter/internal/config/overrides_external_test.go

76 lines
2.3 KiB
Go

package config_test
import (
"strings"
"testing"
"gitea.maximumdirect.net/eric/weatherreporter/internal/config"
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
"gitea.maximumdirect.net/eric/weatherreporter/internal/report"
)
func TestConstructedReportOverridesUseExportedFields(t *testing.T) {
cfg := config.Defaults()
cfg.Reports = map[string]config.ReportConfig{
"daily": {
DeterministicModules: []config.ModuleConfigItem{
{ID: module.Metadata},
{ID: module.AreaForecastDiscussion},
},
Distributor: config.ReportDistributorConfig{
PathTemplates: []string{"daily/{valid_start_date}/index.md"},
},
},
}
if err := config.Validate(cfg); err != nil {
t.Fatalf("Validate() error = %v", err)
}
modules, err := cfg.ReportModuleOverrides()
if err != nil {
t.Fatalf("ReportModuleOverrides() error = %v", err)
}
if got := modules[report.Daily]; len(got) != 2 || got[0].ID != module.Metadata || got[1].ID != module.AreaForecastDiscussion {
t.Fatalf("module override = %#v, want exported daily modules", got)
}
paths, err := cfg.ReportDistributorPathOverrides()
if err != nil {
t.Fatalf("ReportDistributorPathOverrides() error = %v", err)
}
if got := paths[report.Daily]; len(got) != 1 || got[0] != "daily/{valid_start_date}/index.md" {
t.Fatalf("path override = %#v, want exported daily path", got)
}
}
func TestConstructedExplicitEmptyReportOverridesAreRejected(t *testing.T) {
tests := []struct {
name string
report config.ReportConfig
wantErr string
}{
{
name: "modules",
report: config.ReportConfig{DeterministicModules: []config.ModuleConfigItem{}},
wantErr: "reports.daily.deterministic_modules must contain at least one entry",
},
{
name: "paths",
report: config.ReportConfig{Distributor: config.ReportDistributorConfig{
PathTemplates: []string{},
}},
wantErr: "reports.daily.distributor.path_templates must contain at least one entry",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.Defaults()
cfg.Reports = map[string]config.ReportConfig{"daily": tt.report}
err := config.Validate(cfg)
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Validate() error = %v, want %q", err, tt.wantErr)
}
})
}
}