Validate precipitation probabilities and ice wording

This commit is contained in:
2026-08-13 00:48:40 +00:00
parent 8e49ba88c7
commit 730929e2ed
10 changed files with 142 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package weatherdata
import (
"encoding/json"
"math"
"time"
)
@@ -137,6 +138,12 @@ func (p ForecastPeriod) HasUsableTimeBounds() bool {
return !p.StartTime.IsZero() && !p.EndTime.IsZero() && p.EndTime.After(p.StartTime)
}
// HasValidPrecipitationProbability reports whether an optional probability is a finite percentage.
func (p ForecastPeriod) HasValidPrecipitationProbability() bool {
value := p.ProbabilityOfPrecipitationPercent
return value == nil || !math.IsNaN(*value) && !math.IsInf(*value, 0) && *value >= 0 && *value <= 100
}
type AlertRun struct {
AsOf *time.Time `json:"asOf,omitempty"`
Alerts []json.RawMessage `json:"alerts,omitempty"`

View File

@@ -2,6 +2,7 @@ package weatherdata
import (
"encoding/json"
"math"
"testing"
"time"
)
@@ -93,6 +94,29 @@ func TestForecastPeriodHasUsableTimeBounds(t *testing.T) {
}
}
func TestForecastPeriodHasValidPrecipitationProbability(t *testing.T) {
for _, tt := range []struct {
name string
value *float64
want bool
}{
{name: "missing", want: true},
{name: "zero", value: ptrProbability(0), want: true},
{name: "one hundred", value: ptrProbability(100), want: true},
{name: "negative", value: ptrProbability(-0.1)},
{name: "above one hundred", value: ptrProbability(100.1)},
{name: "not a number", value: ptrProbability(math.NaN())},
{name: "infinite", value: ptrProbability(math.Inf(1))},
} {
t.Run(tt.name, func(t *testing.T) {
period := ForecastPeriod{ProbabilityOfPrecipitationPercent: tt.value}
if got := period.HasValidPrecipitationProbability(); got != tt.want {
t.Fatalf("HasValidPrecipitationProbability() = %t, want %t", got, tt.want)
}
})
}
}
func mustParseBundleTime(value string) time.Time {
parsed, err := time.Parse(time.RFC3339, value)
if err != nil {
@@ -100,3 +124,7 @@ func mustParseBundleTime(value string) time.Time {
}
return parsed
}
func ptrProbability(value float64) *float64 {
return &value
}