Add omitempty to JSON fields in alert, forecast, and observation services
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:
@@ -232,3 +232,70 @@ func TestForecastReturnsUSUnits(t *testing.T) {
|
||||
t.Fatalf("expected forecast timestamp passed to repo")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNullFieldsAreOmittedFromJSON(t *testing.T) {
|
||||
obsRepo := &fakeObservationRepo{
|
||||
summary: ports.ObservationSummaryMetric{},
|
||||
conditions: []ports.ObservationConditionMetric{
|
||||
{
|
||||
ObservedAt: time.Date(2026, 3, 17, 12, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
}
|
||||
fcRepo := &fakeForecastRepo{
|
||||
periods: []ports.ForecastPeriodMetric{
|
||||
{
|
||||
PeriodIndex: 1,
|
||||
StartTime: time.Date(2026, 3, 17, 12, 0, 0, 0, time.UTC),
|
||||
EndTime: time.Date(2026, 3, 17, 13, 0, 0, 0, time.UTC),
|
||||
ConditionCode: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
server := NewServer(
|
||||
observations.NewService(obsRepo, units.USConverter{}, constants.ObservationWindow),
|
||||
forecasts.NewService(fcRepo, units.USConverter{}, constants.ForecastQueryLimit),
|
||||
alerts.NewService(fakeAlertRepo{}),
|
||||
).Handler()
|
||||
|
||||
wObs := httptest.NewRecorder()
|
||||
server.ServeHTTP(wObs, httptest.NewRequest(http.MethodGet, "/observations/current", nil))
|
||||
if wObs.Code != http.StatusOK {
|
||||
t.Fatalf("expected observations 200, got %d", wObs.Code)
|
||||
}
|
||||
|
||||
var obsPayload map[string]any
|
||||
if err := json.Unmarshal(wObs.Body.Bytes(), &obsPayload); err != nil {
|
||||
t.Fatalf("decode observations response: %v", err)
|
||||
}
|
||||
summary := obsPayload["summary"].(map[string]any)
|
||||
if _, exists := summary["temperatureF"]; exists {
|
||||
t.Fatalf("expected summary.temperatureF to be omitted when nil")
|
||||
}
|
||||
|
||||
conditions := obsPayload["conditions"].([]any)
|
||||
firstCond := conditions[0].(map[string]any)
|
||||
if _, exists := firstCond["temperatureF"]; exists {
|
||||
t.Fatalf("expected conditions[0].temperatureF to be omitted when nil")
|
||||
}
|
||||
|
||||
wFc := httptest.NewRecorder()
|
||||
server.ServeHTTP(wFc, httptest.NewRequest(http.MethodGet, "/forecast?timestamp=2026-03-17T12:30:00Z", nil))
|
||||
if wFc.Code != http.StatusOK {
|
||||
t.Fatalf("expected forecast 200, got %d", wFc.Code)
|
||||
}
|
||||
|
||||
var fcPayload map[string]any
|
||||
if err := json.Unmarshal(wFc.Body.Bytes(), &fcPayload); err != nil {
|
||||
t.Fatalf("decode forecast response: %v", err)
|
||||
}
|
||||
periods := fcPayload["periods"].([]any)
|
||||
firstPeriod := periods[0].(map[string]any)
|
||||
if _, exists := firstPeriod["temperatureF"]; exists {
|
||||
t.Fatalf("expected periods[0].temperatureF to be omitted when nil")
|
||||
}
|
||||
if _, exists := firstPeriod["windSpeedMph"]; exists {
|
||||
t.Fatalf("expected periods[0].windSpeedMph to be omitted when nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user