Add timezone support to /forecast/hourly endpoint
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:
@@ -394,6 +394,148 @@ func TestForecastPrecisionTwo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastTimezoneOffsetUppercaseTZConvertsAllTimes(t *testing.T) {
|
||||
issuedAt := time.Date(2026, 7, 10, 15, 0, 0, 0, time.UTC)
|
||||
updatedAt := issuedAt.Add(30 * time.Minute)
|
||||
periodStart := issuedAt.Add(time.Hour)
|
||||
periodEnd := periodStart.Add(time.Hour)
|
||||
|
||||
h := newHandler(t, &fakeService{
|
||||
forecast: &model.WeatherForecastRun{
|
||||
Product: model.ForecastProductHourly,
|
||||
IssuedAt: issuedAt,
|
||||
UpdatedAt: &updatedAt,
|
||||
Periods: []model.WeatherForecastPeriod{{
|
||||
StartTime: periodStart,
|
||||
EndTime: periodEnd,
|
||||
ConditionCode: model.WMOUnknown,
|
||||
}},
|
||||
},
|
||||
}, "/forecast/hourly")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/forecast/hourly?TZ=-5", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
payload := decodeForecastTimePayload(t, w)
|
||||
assertOffsetSeconds(t, payload.Data.IssuedAt, -5*60*60)
|
||||
if payload.Data.UpdatedAt == nil {
|
||||
t.Fatalf("expected updatedAt in payload")
|
||||
}
|
||||
assertOffsetSeconds(t, *payload.Data.UpdatedAt, -5*60*60)
|
||||
assertOffsetSeconds(t, payload.Data.Periods[0].StartTime, -5*60*60)
|
||||
assertOffsetSeconds(t, payload.Data.Periods[0].EndTime, -5*60*60)
|
||||
if !payload.Data.IssuedAt.UTC().Equal(issuedAt) {
|
||||
t.Fatalf("expected issuedAt instant to be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastTimezoneAbbreviationCDT(t *testing.T) {
|
||||
issuedAt := time.Date(2026, 1, 10, 12, 0, 0, 0, time.UTC)
|
||||
h := newHandler(t, &fakeService{
|
||||
forecast: &model.WeatherForecastRun{
|
||||
Product: model.ForecastProductHourly,
|
||||
IssuedAt: issuedAt,
|
||||
Periods: []model.WeatherForecastPeriod{{
|
||||
StartTime: issuedAt,
|
||||
EndTime: issuedAt.Add(time.Hour),
|
||||
ConditionCode: model.WMOUnknown,
|
||||
}},
|
||||
},
|
||||
}, "/forecast/hourly")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/forecast/hourly?tz=CDT", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
payload := decodeForecastTimePayload(t, w)
|
||||
assertOffsetSeconds(t, payload.Data.IssuedAt, -5*60*60)
|
||||
assertOffsetSeconds(t, payload.Data.Periods[0].StartTime, -5*60*60)
|
||||
}
|
||||
|
||||
func TestForecastTimezoneCityAliasChicago(t *testing.T) {
|
||||
issuedAt := time.Date(2026, 7, 10, 12, 0, 0, 0, time.UTC)
|
||||
h := newHandler(t, &fakeService{
|
||||
forecast: &model.WeatherForecastRun{
|
||||
Product: model.ForecastProductHourly,
|
||||
IssuedAt: issuedAt,
|
||||
Periods: []model.WeatherForecastPeriod{{
|
||||
StartTime: issuedAt,
|
||||
EndTime: issuedAt.Add(time.Hour),
|
||||
ConditionCode: model.WMOUnknown,
|
||||
}},
|
||||
},
|
||||
}, "/forecast/hourly")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/forecast/hourly?tz=Chicago", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
payload := decodeForecastTimePayload(t, w)
|
||||
assertOffsetSeconds(t, payload.Data.IssuedAt, -5*60*60)
|
||||
assertOffsetSeconds(t, payload.Data.Periods[0].StartTime, -5*60*60)
|
||||
}
|
||||
|
||||
func TestForecastTimezoneInvalidValueRejected(t *testing.T) {
|
||||
h := newHandler(t, &fakeService{
|
||||
forecast: &model.WeatherForecastRun{Product: model.ForecastProductHourly, IssuedAt: time.Now().UTC()},
|
||||
}, "/forecast/hourly")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/forecast/hourly?tz=not-a-timezone", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d", w.Code)
|
||||
}
|
||||
|
||||
var env apierrors.Envelope
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode error envelope: %v", err)
|
||||
}
|
||||
if env.Error == nil || env.Error.Code != apierrors.CodeInvalidParameter {
|
||||
t.Fatalf("expected invalid_parameter code, got %+v", env.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastTimezoneConflictingKeyValuesRejected(t *testing.T) {
|
||||
h := newHandler(t, &fakeService{
|
||||
forecast: &model.WeatherForecastRun{Product: model.ForecastProductHourly, IssuedAt: time.Now().UTC()},
|
||||
}, "/forecast/hourly")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/forecast/hourly?tz=CDT&TZ=EST", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentConditionsRejectTimezoneQueryParameter(t *testing.T) {
|
||||
h := newHandler(t, &fakeService{}, "/conditions/current")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/conditions/current?tz=CDT", nil)
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentConditionsNoDataReturnsNullEnvelopeData(t *testing.T) {
|
||||
h := newHandler(t, &fakeService{}, "/conditions/current")
|
||||
|
||||
@@ -629,3 +771,35 @@ func testRenderers(t *testing.T) *render.Registry {
|
||||
func float64Ptr(v float64) *float64 {
|
||||
return &v
|
||||
}
|
||||
|
||||
type forecastTimePayload struct {
|
||||
Data struct {
|
||||
IssuedAt time.Time `json:"issuedAt"`
|
||||
UpdatedAt *time.Time `json:"updatedAt"`
|
||||
Periods []struct {
|
||||
StartTime time.Time `json:"startTime"`
|
||||
EndTime time.Time `json:"endTime"`
|
||||
} `json:"periods"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func decodeForecastTimePayload(t *testing.T, w *httptest.ResponseRecorder) forecastTimePayload {
|
||||
t.Helper()
|
||||
|
||||
var payload forecastTimePayload
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode forecast payload: %v", err)
|
||||
}
|
||||
if len(payload.Data.Periods) == 0 {
|
||||
t.Fatalf("expected non-empty periods")
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func assertOffsetSeconds(t *testing.T, ts time.Time, want int) {
|
||||
t.Helper()
|
||||
_, got := ts.Zone()
|
||||
if got != want {
|
||||
t.Fatalf("expected offset %d, got %d for %s", want, got, ts.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user