Files
weatherapi/internal/adapters/outbound/postgres/conditions_codes_test.go

117 lines
2.5 KiB
Go

// conditions_codes_test.go tests current-conditions WMO code selection.
// Layer: adapters/outbound/postgres conditions feature.
package postgres
import (
"testing"
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
)
func TestSelectCurrentConditionsConditionCode(t *testing.T) {
tests := []struct {
name string
candidates []currentConditionsConditionCodeCandidate
want model.WMOCode
}{
{
name: "clear cloud ranking breaks exact tie",
candidates: conditionCodeCandidates(
0,
1,
2,
),
want: 0,
},
{
name: "clear cloud family wins over thunderstorm",
candidates: conditionCodeCandidates(
1,
2,
95,
),
want: 1,
},
{
name: "tied families return unknown",
candidates: conditionCodeCandidates(
0,
95,
),
want: model.WMOUnknown,
},
{
name: "rain ranking breaks exact tie",
candidates: conditionCodeCandidates(
61,
63,
80,
),
want: 61,
},
{
name: "three tied families return unknown",
candidates: conditionCodeCandidates(
61,
95,
0,
),
want: model.WMOUnknown,
},
{
name: "single thunderstorm code wins",
candidates: conditionCodeCandidates(
95,
),
want: 95,
},
{
name: "unrecognized only returns unknown",
candidates: conditionCodeCandidates(
4,
100,
),
want: model.WMOUnknown,
},
{
name: "duplicate source only votes once",
candidates: []currentConditionsConditionCodeCandidate{
{EventSource: "source-1", ConditionCode: 95},
{EventSource: "source-1", ConditionCode: 95},
{EventSource: "source-2", ConditionCode: 0},
{EventSource: "source-3", ConditionCode: 0},
},
want: 0,
},
{
name: "exact code frequency wins before ranking",
candidates: []currentConditionsConditionCodeCandidate{
{EventSource: "source-1", ConditionCode: 61},
{EventSource: "source-2", ConditionCode: 63},
{EventSource: "source-3", ConditionCode: 63},
},
want: 63,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := selectCurrentConditionsConditionCode(tt.candidates)
if got != tt.want {
t.Fatalf("expected condition code %d, got %d", tt.want, got)
}
})
}
}
func conditionCodeCandidates(codes ...model.WMOCode) []currentConditionsConditionCodeCandidate {
candidates := make([]currentConditionsConditionCodeCandidate, 0, len(codes))
for i, code := range codes {
candidates = append(candidates, currentConditionsConditionCodeCandidate{
EventSource: string(rune('a' + i)),
ConditionCode: code,
})
}
return candidates
}