-
Notifications
You must be signed in to change notification settings - Fork 9
/
weather_test.go
381 lines (363 loc) · 15.5 KB
/
weather_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package climacell
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// not real weather data (which is why the sample is from the geographically
// impossible coordinates 91 degrees north and 181 degrees west), but the same
// format as a real API response
var everyWeatherField = []byte(`
{
"lat": 91.128,
"lon": -181.250,
"temp": {"value": 10, "units": "C"},
"feels_like": {"value": 10, "units": "C"},
"dewpoint": {"value": -0.50, "units": "C"},
"wind_speed": {"value": 5, "units": "beaufort"},
"wind_gust": {"value": 10, "units": "beaufort"},
"baro_pressure": {"value": 20.71, "units": "inHg"},
"visibility": {"value": 6.21371, "units": "mi"},
"humidity": {"value": 20.71, "units": "%"},
"wind_direction": {"value": 250.25, "units": "degrees"},
"precipitation": {"value": 10, "units": "mm/hr"},
"precipitation_type": {"value": "rain"},
"cloud_cover": {"value": 12.8, "units": "%"},
"cloud_ceiling": {"value": 6000, "units": "ft"},
"cloud_base": {"value": 5000, "units": "ft"},
"surface_shortwave_radiation": {"value": 250.1123, "units": "w/sqm"},
"fire_index": {"value": 3.6},
"sunrise": {"value": "2020-04-12T12:34:56.789Z"},
"sunset": {"value": "2020-04-12T23:45:56.789Z"},
"moon_phase": {"value": "waning_gibbous"},
"weather_code": {"value": "mostly_clear"},
"road_risk": {"value": "low_risk"},
"road_risk_score": {"value": "Low Risk"},
"road_risk_confidence": {"value": 100 },
"road_risk_conditions": {"value": "Low visibility"},
"epa_aqi": {"value": 25},
"epa_primary_pollutant": {"value": "pm25"},
"china_aqi": {"value": 12},
"china_primary_pollutant": {"value": "pm25"},
"pm25": {"value": 10, "units": "µg/m3"},
"pm10": {"value": 15, "units": "µg/m3"},
"o3": {"value": 8, "units": "ppb"},
"no2": {"value": 40, "units": "ppb"},
"co": {"value": 3, "units": "ppm"},
"so2": {"value": 1, "units": "ppb"},
"epa_health_concern": {"value": "Good"},
"china_health_concern": {"value": "Good"},
"observation_time": {"value": "2020-04-12T12:00:00.000Z"}
}`)
// TestDeserializeWeatherWithAllFields validates that if we try to deserialize
// a Weather sample with all fields non-null, the deserialization succeeds.
func TestDeserializeWeatherWithAllFields(t *testing.T) {
var w HourlyForecast
require.NoError(t, json.Unmarshal(everyWeatherField, &w))
assert.Equal(t, 91.128, w.Lat)
assert.Equal(t, -181.250, w.Lon)
expObservationTime, err := time.Parse(time.RFC3339, "2020-04-12T12:00:00.000Z")
require.NoError(t, err)
assert.EqualValues(t, expObservationTime, w.ObservationTime.Value)
if temp, ok := w.Temp.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 10, temp)
}
if feelsLike, ok := w.FeelsLike.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 10, feelsLike)
}
if dewPoint, ok := w.DewPoint.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, -0.50, dewPoint)
}
if windSpeed, ok := w.WindSpeed.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 5, windSpeed)
}
if windGust, ok := w.WindGust.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 10, windGust)
}
if baroPressure, ok := w.BaroPressure.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 20.71, baroPressure)
}
if visibility, ok := w.Visibility.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 6.21371, visibility)
}
if humidity, ok := w.Humidity.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 20.71, humidity)
}
if windDirection, ok := w.WindDirection.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 250.25, windDirection)
}
if precipitation, ok := w.Precipitation.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 10, precipitation)
}
if precipitationType, ok := w.PrecipitationType.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "rain", precipitationType)
}
if cloudCover, ok := w.CloudCover.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 12.8, cloudCover)
}
if cloudCeiling, ok := w.CloudCeiling.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 6000, cloudCeiling)
}
if cloudBase, ok := w.CloudBase.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 5000, cloudBase)
}
if ssw, ok := w.SurfaceShortwaveRadiation.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 250.1123, ssw)
}
if fireIndex, ok := w.FireIndex.GetValue(); ok {
assert.EqualValues(t, 3.6, fireIndex)
}
expSunrise, err := time.Parse(time.RFC3339, "2020-04-12T12:34:56.789Z")
require.NoError(t, err)
if sunrise, ok := w.Sunrise.GetValue(); assert.True(t, ok) {
assert.WithinDuration(t, expSunrise, sunrise, time.Second)
}
expSunset, err := time.Parse(time.RFC3339, "2020-04-12T23:45:56.789Z")
require.NoError(t, err)
if sunset, ok := w.Sunset.GetValue(); assert.True(t, ok) {
assert.WithinDuration(t, expSunset, sunset, time.Second)
}
if moonPhase, ok := w.MoonPhase.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "waning_gibbous", moonPhase)
}
if weatherCode, ok := w.WeatherCode.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "mostly_clear", weatherCode)
}
if roadRisk, ok := w.RoadRisk.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "low_risk", roadRisk)
}
if roadRiskScore, ok := w.RoadRiskScore.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "Low Risk", roadRiskScore)
}
if roadRiskConfidence, ok := w.RoadRiskConfidence.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 100, roadRiskConfidence)
}
if roadRiskConditions, ok := w.RoadRiskConditions.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "Low visibility", roadRiskConditions)
}
if epaAQI, ok := w.EpaAQI.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 25, epaAQI)
}
if epaPrimaryPollutant, ok := w.EPAPrimaryPollutant.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "pm25", epaPrimaryPollutant)
}
if epaHealthConcern, ok := w.EPAHealthConcern.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "Good", epaHealthConcern)
}
if chinaAQI, ok := w.ChinaAQI.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 12, chinaAQI)
}
if chinaPrimaryPollutant, ok := w.ChinaPrimaryPollutant.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "pm25", chinaPrimaryPollutant)
}
if chinaHealthConcern, ok := w.ChinaHealthConcern.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, "Good", chinaHealthConcern)
}
if pmTwoPointFive, ok := w.PMTwoPointFive.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 10, pmTwoPointFive)
}
if pmTen, ok := w.PMTen.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 15, pmTen)
}
if o3, ok := w.O3.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 8, o3)
}
if no2, ok := w.NO2.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 40, no2)
}
if co, ok := w.CO.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 3, co)
}
if so2, ok := w.SO2.GetValue(); assert.True(t, ok) {
assert.EqualValues(t, 1, so2)
}
}
// not real weather data (which is why the sample is from the geographically
// impossible coordinates 91 degrees north and 181 degrees west), but the same
// format as a real API response
var minimalWeatherData = []byte(`
{
"lat": 91.128,
"lon": -181.250,
"observation_time": {"value": "2020-04-12T12:00:00.000Z"}
}`)
// TestDeserializeWeatherWithAllFieldsAbsent validates that if we try to
// deserialize a Weather sample with all nullable fields absent, the
// deserialization succeeds, with nil values for any absent fields.
func TestDeserializeWeatherWithAllFieldsAbsent(t *testing.T) {
var w HourlyForecast
require.NoError(t, json.Unmarshal(minimalWeatherData, &w))
assert.Equal(t, 91.128, w.Lat)
assert.Equal(t, -181.250, w.Lon)
expObservationTime, err := time.Parse(time.RFC3339, "2020-04-12T12:00:00.000Z")
require.NoError(t, err)
assert.EqualValues(t, expObservationTime, w.ObservationTime.Value)
assert.Nil(t, w.Temp)
assert.Nil(t, w.FeelsLike)
assert.Nil(t, w.DewPoint)
assert.Nil(t, w.WindSpeed)
assert.Nil(t, w.WindGust)
assert.Nil(t, w.BaroPressure)
assert.Nil(t, w.Visibility)
assert.Nil(t, w.Humidity)
assert.Nil(t, w.WindDirection)
assert.Nil(t, w.Precipitation)
assert.Nil(t, w.PrecipitationType)
assert.Nil(t, w.CloudCover)
assert.Nil(t, w.CloudCeiling)
assert.Nil(t, w.CloudBase)
assert.Nil(t, w.SurfaceShortwaveRadiation)
assert.Nil(t, w.FireIndex)
assert.Nil(t, w.Sunrise)
assert.Nil(t, w.Sunset)
assert.Nil(t, w.MoonPhase)
assert.Nil(t, w.WeatherCode)
assert.Nil(t, w.RoadRisk)
assert.Nil(t, w.RoadRiskScore)
assert.Nil(t, w.RoadRiskConfidence)
assert.Nil(t, w.RoadRiskConditions)
assert.Nil(t, w.EpaAQI)
assert.Nil(t, w.EPAPrimaryPollutant)
assert.Nil(t, w.EPAHealthConcern)
assert.Nil(t, w.ChinaAQI)
assert.Nil(t, w.ChinaPrimaryPollutant)
assert.Nil(t, w.ChinaHealthConcern)
assert.Nil(t, w.PMTwoPointFive)
assert.Nil(t, w.PMTen)
assert.Nil(t, w.O3)
assert.Nil(t, w.NO2)
assert.Nil(t, w.CO)
assert.Nil(t, w.SO2)
}
// not real weather data (which is why the sample is from the geographically
// impossible coordinates 91 degrees north and 181 degrees west), but the same
// format as a real API response
var everyWeatherFieldNull = []byte(`
{
"lat": 91.128,
"lon": -181.250,
"temp": {"value": null, "units": "C"},
"feels_like": {"value": null, "units": "C"},
"dewpoint": {"value": null, "units": "C"},
"wind_speed": {"value": null, "units": "beaufort"},
"wind_gust": {"value": null, "units": "beaufort"},
"baro_pressure": {"value": null, "units": "inHg"},
"visibility": {"value": null, "units": "mi"},
"humidity": {"value": null, "units": "%"},
"wind_direction": {"value": null, "units": "degrees"},
"precipitation": {"value": null, "units": "mm/hr"},
"precipitation_type": {"value": null},
"cloud_cover": {"value": null, "units": "%"},
"cloud_ceiling": {"value": null, "units": "ft"},
"cloud_base": {"value": null, "units": "ft"},
"surface_shortwave_radiation": {"value": null, "units": "w/sqm"},
"fire_index": {"value": null},
"sunrise": {"value": null},
"sunset": {"value": null},
"moon_phase": {"value": null},
"weather_code": {"value": null},
"road_risk": {"value": null},
"road_risk_score": {"value": null},
"road_risk_confidence": {"value": null},
"road_risk_conditions": {"value": null},
"epa_aqi": {"value": null},
"epa_primary_pollutant": {"value": null},
"china_aqi": {"value": null},
"china_primary_pollutant": {"value": null},
"pm25": {"value": null, "units": "µg/m3"},
"pm10": {"value": null, "units": "µg/m3"},
"o3": {"value": null, "units": "ppb"},
"no2": {"value": null, "units": "ppb"},
"co": {"value": null, "units": "ppm"},
"so2": {"value": null, "units": "ppb"},
"epa_health_concern": {"value": null},
"china_health_concern": {"value": null},
"observation_time": {"value": "2020-04-12T12:00:00.000Z"}
}`)
// TestDeserializeWeatherWithAllFieldsNull validates that if we try to
// deserialize a Weather sample with almost all nullable fields present but
// with null values, the deserialization succeeds, and for each field's
// GetValue method, a false ok value is returned.
func TestDeserializeWeatherWithAllFieldsNull(t *testing.T) {
var w HourlyForecast
require.NoError(t, json.Unmarshal(minimalWeatherData, &w))
assert.Equal(t, 91.128, w.Lat)
assert.Equal(t, -181.250, w.Lon)
expObservationTime, err := time.Parse(time.RFC3339, "2020-04-12T12:00:00.000Z")
require.NoError(t, err)
assert.EqualValues(t, expObservationTime, w.ObservationTime.Value)
_, ok := w.Temp.GetValue()
assert.False(t, ok, "Temp was present")
_, ok = w.FeelsLike.GetValue()
assert.False(t, ok, "FeelsLike was present")
_, ok = w.DewPoint.GetValue()
assert.False(t, ok, "DewPoint was present")
_, ok = w.WindSpeed.GetValue()
assert.False(t, ok, "WindSpeed was present")
_, ok = w.WindGust.GetValue()
assert.False(t, ok, "WindGust was present")
_, ok = w.BaroPressure.GetValue()
assert.False(t, ok, "BaroPressure was present")
_, ok = w.Visibility.GetValue()
assert.False(t, ok, "Visibility was present")
_, ok = w.Humidity.GetValue()
assert.False(t, ok, "Humidity was present")
_, ok = w.WindDirection.GetValue()
assert.False(t, ok, "WindDirection was present")
_, ok = w.Precipitation.GetValue()
assert.False(t, ok, "Precipitation was present")
_, ok = w.PrecipitationType.GetValue()
assert.False(t, ok, "PrecipitationType was present")
_, ok = w.CloudCover.GetValue()
assert.False(t, ok, "CloudCoder was present")
_, ok = w.CloudCeiling.GetValue()
assert.False(t, ok, "CloudCeiling was present")
_, ok = w.CloudBase.GetValue()
assert.False(t, ok, "CloudBase was present")
_, ok = w.SurfaceShortwaveRadiation.GetValue()
assert.False(t, ok, "SurfaceShortwaveRadiation was present")
_, ok = w.FireIndex.GetValue()
assert.False(t, ok, "FireIndex was present")
_, ok = w.Sunrise.GetValue()
assert.False(t, ok, "Sumrise was present")
_, ok = w.Sunset.GetValue()
assert.False(t, ok, "Sunset was present")
_, ok = w.MoonPhase.GetValue()
assert.False(t, ok, "MoonPhase was present")
_, ok = w.WeatherCode.GetValue()
assert.False(t, ok, "WeatherCode was present")
_, ok = w.RoadRisk.GetValue()
assert.False(t, ok, "road risk was present")
_, ok = w.RoadRiskScore.GetValue()
assert.False(t, ok, "road risk score was present")
_, ok = w.RoadRiskConfidence.GetValue()
assert.False(t, ok, "road risk confidence was present")
_, ok = w.RoadRiskConditions.GetValue()
assert.False(t, ok, "road risk conditions was present")
_, ok = w.EpaAQI.GetValue()
assert.False(t, ok, "EpaAQI was present")
_, ok = w.EPAPrimaryPollutant.GetValue()
assert.False(t, ok, "EPAPrimaryPollutant was present")
_, ok = w.EPAHealthConcern.GetValue()
assert.False(t, ok, "EPAHealthConcern was present")
_, ok = w.ChinaAQI.GetValue()
assert.False(t, ok, "ChinaAQI was present")
_, ok = w.ChinaPrimaryPollutant.GetValue()
assert.False(t, ok, "ChinaPrimaryPollutant was present")
_, ok = w.ChinaHealthConcern.GetValue()
assert.False(t, ok, "ChinaHealthConcern was present")
_, ok = w.PMTwoPointFive.GetValue()
assert.False(t, ok, "PMTwoPointFive was present")
_, ok = w.PMTen.GetValue()
assert.False(t, ok, "PMTen was present")
_, ok = w.O3.GetValue()
assert.False(t, ok, "O3 was present")
_, ok = w.NO2.GetValue()
assert.False(t, ok, "NO2 was present")
_, ok = w.CO.GetValue()
assert.False(t, ok, "CO was present")
_, ok = w.SO2.GetValue()
assert.False(t, ok, "SO2 was present")
}