Added support for rounding of values by default in API responses
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -26,15 +26,15 @@ func TestObservationPayloadUS(t *testing.T) {
|
||||
RelativeHumidityPercent: float64Ptr(50),
|
||||
}
|
||||
|
||||
payload := ObservationPayload(obs, UnitsUS)
|
||||
payload := ObservationPayload(obs, UnitsUS, 2)
|
||||
converted, ok := payload.(WeatherObservationUS)
|
||||
if !ok {
|
||||
t.Fatalf("expected WeatherObservationUS payload, got %T", payload)
|
||||
}
|
||||
|
||||
assertApprox(t, converted.TemperatureF, 68.0, 0.0001)
|
||||
assertApprox(t, converted.WindSpeedMph, 62.1371192237, 0.0001)
|
||||
assertApprox(t, converted.BarometricPressureInHg, 29.9212524019, 0.0001)
|
||||
assertApprox(t, converted.WindSpeedMph, 62.14, 0.0001)
|
||||
assertApprox(t, converted.BarometricPressureInHg, 29.92, 0.0001)
|
||||
assertApprox(t, converted.VisibilityMiles, 1.0, 0.0001)
|
||||
|
||||
if converted.TemperatureF == nil || converted.DewpointF == nil || converted.ApparentTemperatureF == nil {
|
||||
@@ -64,13 +64,13 @@ func TestForecastPayloadUS(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
|
||||
payload := ForecastPayload(run, UnitsUS)
|
||||
payload := ForecastPayload(run, UnitsUS, 2)
|
||||
converted, ok := payload.(WeatherForecastRunUS)
|
||||
if !ok {
|
||||
t.Fatalf("expected WeatherForecastRunUS payload, got %T", payload)
|
||||
}
|
||||
|
||||
assertApprox(t, converted.ElevationFeet, 3280.839895, 0.0001)
|
||||
assertApprox(t, converted.ElevationFeet, 3280.84, 0.0001)
|
||||
if len(converted.Periods) != 1 {
|
||||
t.Fatalf("expected 1 period, got %d", len(converted.Periods))
|
||||
}
|
||||
@@ -83,27 +83,34 @@ func TestForecastPayloadUS(t *testing.T) {
|
||||
assertApprox(t, period.SnowfallDepthIn, 2.0, 0.0001)
|
||||
}
|
||||
|
||||
func TestMetricPassthroughAndNilHandling(t *testing.T) {
|
||||
obs := &model.WeatherObservation{}
|
||||
metric := ObservationPayload(obs, UnitsMetric)
|
||||
func TestMetricCopyAndNilHandling(t *testing.T) {
|
||||
obs := &model.WeatherObservation{
|
||||
TemperatureC: float64Ptr(20.6),
|
||||
}
|
||||
metric := ObservationPayload(obs, UnitsMetric, 0)
|
||||
metricObs, ok := metric.(*model.WeatherObservation)
|
||||
if !ok {
|
||||
t.Fatalf("expected metric payload to remain model type, got %T", metric)
|
||||
}
|
||||
if metricObs != obs {
|
||||
t.Fatalf("expected metric payload to be original pointer")
|
||||
if metricObs == obs {
|
||||
t.Fatalf("expected metric payload to be copied")
|
||||
}
|
||||
assertApprox(t, metricObs.TemperatureC, 21, 0.0001)
|
||||
assertApprox(t, obs.TemperatureC, 20.6, 0.0001)
|
||||
if metricObs.TemperatureC == obs.TemperatureC {
|
||||
t.Fatalf("expected temperature pointer copy, got same pointer")
|
||||
}
|
||||
|
||||
if ObservationPayload(nil, UnitsUS) != nil {
|
||||
if ObservationPayload(nil, UnitsUS, 0) != nil {
|
||||
t.Fatalf("expected nil observation input to return nil payload")
|
||||
}
|
||||
if ForecastPayload(nil, UnitsUS) != nil {
|
||||
if ForecastPayload(nil, UnitsUS, 0) != nil {
|
||||
t.Fatalf("expected nil forecast input to return nil payload")
|
||||
}
|
||||
if AlertsPayload(nil, UnitsUS) != nil {
|
||||
t.Fatalf("expected nil alerts input to return nil payload")
|
||||
}
|
||||
if CurrentConditionsPayload(nil, UnitsUS) != nil {
|
||||
if CurrentConditionsPayload(nil, UnitsUS, 0) != nil {
|
||||
t.Fatalf("expected nil current conditions input to return nil payload")
|
||||
}
|
||||
}
|
||||
@@ -120,7 +127,7 @@ func TestCurrentConditionsPayloadMetricAndUS(t *testing.T) {
|
||||
IsDay: boolPtr(true),
|
||||
}
|
||||
|
||||
metricPayload := CurrentConditionsPayload(conditions, UnitsMetric)
|
||||
metricPayload := CurrentConditionsPayload(conditions, UnitsMetric, 2)
|
||||
metric, ok := metricPayload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse metric payload, got %T", metricPayload)
|
||||
@@ -134,13 +141,13 @@ func TestCurrentConditionsPayloadMetricAndUS(t *testing.T) {
|
||||
t.Fatalf("expected condition text Sunny, got %q", metric.ConditionText)
|
||||
}
|
||||
|
||||
usPayload := CurrentConditionsPayload(conditions, UnitsUS)
|
||||
usPayload := CurrentConditionsPayload(conditions, UnitsUS, 2)
|
||||
us, ok := usPayload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse US payload, got %T", usPayload)
|
||||
}
|
||||
assertApprox(t, us.TemperatureF, 68, 0.0001)
|
||||
assertApprox(t, us.WindSpeedMph, 62.1371192237, 0.0001)
|
||||
assertApprox(t, us.WindSpeedMph, 62.14, 0.0001)
|
||||
if us.TemperatureC != nil || us.WindSpeedKmh != nil {
|
||||
t.Fatalf("expected metric fields omitted for US payload")
|
||||
}
|
||||
@@ -151,7 +158,7 @@ func TestCurrentConditionsPayloadUsesNightConditionText(t *testing.T) {
|
||||
payload := CurrentConditionsPayload(&app.CurrentConditions{
|
||||
ConditionCode: 0,
|
||||
IsDay: &night,
|
||||
}, UnitsMetric)
|
||||
}, UnitsMetric, 0)
|
||||
|
||||
metric, ok := payload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
@@ -162,6 +169,45 @@ func TestCurrentConditionsPayloadUsesNightConditionText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentConditionsPayloadRoundsHalfAwayFromZero(t *testing.T) {
|
||||
payload := CurrentConditionsPayload(&app.CurrentConditions{
|
||||
TemperatureC: float64Ptr(-1.5),
|
||||
}, UnitsMetric, 0)
|
||||
|
||||
metric, ok := payload.(CurrentConditionsResponse)
|
||||
if !ok {
|
||||
t.Fatalf("expected CurrentConditionsResponse payload, got %T", payload)
|
||||
}
|
||||
assertApprox(t, metric.TemperatureC, -2, 0.0001)
|
||||
}
|
||||
|
||||
func TestForecastPayloadLatitudeLongitudeNotRounded(t *testing.T) {
|
||||
run := &model.WeatherForecastRun{
|
||||
Latitude: float64Ptr(38.627123),
|
||||
Longitude: float64Ptr(-90.199456),
|
||||
ElevationMeters: float64Ptr(10.499),
|
||||
Product: model.ForecastProductHourly,
|
||||
IssuedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
metricPayload := ForecastPayload(run, UnitsMetric, 0)
|
||||
metric, ok := metricPayload.(*model.WeatherForecastRun)
|
||||
if !ok {
|
||||
t.Fatalf("expected metric payload type *model.WeatherForecastRun, got %T", metricPayload)
|
||||
}
|
||||
assertApprox(t, metric.Latitude, 38.627123, 0.000001)
|
||||
assertApprox(t, metric.Longitude, -90.199456, 0.000001)
|
||||
assertApprox(t, metric.ElevationMeters, 10, 0.0001)
|
||||
|
||||
usPayload := ForecastPayload(run, UnitsUS, 0)
|
||||
us, ok := usPayload.(WeatherForecastRunUS)
|
||||
if !ok {
|
||||
t.Fatalf("expected us payload type WeatherForecastRunUS, got %T", usPayload)
|
||||
}
|
||||
assertApprox(t, us.Latitude, 38.627123, 0.000001)
|
||||
assertApprox(t, us.Longitude, -90.199456, 0.000001)
|
||||
}
|
||||
|
||||
func float64Ptr(v float64) *float64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user