Validate precipitation probabilities and ice wording
This commit is contained in:
@@ -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"`
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user