File: test_forecast.py

package info (click to toggle)
python-datapoint 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 584 kB
  • sloc: python: 3,062; makefile: 18
file content (414 lines) | stat: -rw-r--r-- 14,934 bytes parent folder | download
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import datetime

import geojson
import pytest

import tests.reference_data.reference_data_test_forecast as reference_data_test_forecast
from datapoint import Forecast
from datapoint.exceptions import APIException

# TODO look into pytest-cases. Should reduce the amount of stored data structures


@pytest.fixture
def load_hourly_json():
    with open("./tests/reference_data/hourly_api_data.json") as f:
        my_json = geojson.load(f)
    return my_json


@pytest.fixture
def load_daily_json():
    with open("./tests/reference_data/daily_api_data.json") as f:
        my_json = geojson.load(f)
    return my_json


@pytest.fixture
def load_three_hourly_json():
    with open("./tests/reference_data/three_hourly_api_data.json") as f:
        my_json = geojson.load(f)
    return my_json


@pytest.fixture
def daily_forecast(load_daily_json):
    return Forecast.Forecast("daily", load_daily_json, convert_weather_code=True)


@pytest.fixture
def daily_forecast_raw_weather_code(load_daily_json):
    return Forecast.Forecast("daily", load_daily_json, convert_weather_code=False)


@pytest.fixture
def twice_daily_forecast(load_daily_json):
    return Forecast.Forecast("twice-daily", load_daily_json, convert_weather_code=True)


@pytest.fixture
def twice_daily_forecast_raw_weather_code(load_daily_json):
    return Forecast.Forecast("twice-daily", load_daily_json, convert_weather_code=False)


@pytest.fixture
def hourly_forecast(load_hourly_json):
    return Forecast.Forecast("hourly", load_hourly_json, convert_weather_code=True)


@pytest.fixture
def hourly_forecast_raw_weather_code(load_hourly_json):
    return Forecast.Forecast("hourly", load_hourly_json, convert_weather_code=False)


@pytest.fixture
def three_hourly_forecast(load_three_hourly_json):
    return Forecast.Forecast(
        "three-hourly", load_three_hourly_json, convert_weather_code=True
    )


@pytest.fixture
def hourly_first_forecast_and_parameters(load_hourly_json):
    parameters = load_hourly_json["parameters"][0]
    forecast = load_hourly_json["features"][0]["properties"]["timeSeries"][0]
    return (forecast, parameters)


@pytest.fixture
def three_hourly_first_forecast_and_parameters(load_three_hourly_json):
    parameters = load_three_hourly_json["parameters"][0]
    forecast = load_three_hourly_json["features"][0]["properties"]["timeSeries"][0]
    return (forecast, parameters)


@pytest.fixture
def expected_first_hourly_timestep():
    return reference_data_test_forecast.EXPECTED_FIRST_HOURLY_TIMESTEP


@pytest.fixture
def expected_first_hourly_timestep_raw_weather_code():
    return reference_data_test_forecast.EXPECTED_FIRST_HOURLY_TIMESTEP_RAW_WEATHER_CODE


@pytest.fixture
def expected_at_datetime_hourly_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_HOURLY_TIMESTEP


@pytest.fixture
def expected_at_datetime_hourly_final_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_HOURLY_FINAL_TIMESTEP


@pytest.fixture
def expected_first_daily_timestep():
    return reference_data_test_forecast.EXPECTED_FIRST_DAILY_TIMESTEP


@pytest.fixture
def expected_first_daily_timestep_raw_weather_code():
    return reference_data_test_forecast.EXPECTED_FIRST_DAILY_TIMESTEP_RAW_WEATHER_CODE


@pytest.fixture
def expected_at_datetime_daily_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_DAILY_TIMESTEP


@pytest.fixture
def expected_at_datetime_daily_final_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_DAILY_FINAL_TIMESTEP


@pytest.fixture
def expected_first_twice_daily_timestep():
    return reference_data_test_forecast.EXPECTED_FIRST_TWICE_DAILY_TIMESTEP


@pytest.fixture
def expected_first_twice_daily_timestep_raw_weather_code():
    return (
        reference_data_test_forecast.EXPECTED_FIRST_TWICE_DAILY_TIMESTEP_RAW_WEATHER_CODE
    )


@pytest.fixture
def expected_at_datetime_twice_daily_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_TWICE_DAILY_TIMESTEP


@pytest.fixture
def expected_at_datetime_twice_daily_final_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_TWICE_DAILY_FINAL_TIMESTEP


@pytest.fixture
def expected_first_three_hourly_timestep():
    return reference_data_test_forecast.EXPECTED_FIRST_THREE_HOURLY_TIMESTEP


@pytest.fixture
def expected_at_datetime_three_hourly_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_THREE_HOURLY_TIMESTEP


@pytest.fixture
def expected_at_datetime_three_hourly_final_timestep():
    return reference_data_test_forecast.EXPECTED_AT_DATETIME_THREE_HOURLY_FINAL_TIMESTEP


class TestHourlyForecast:
    def test_forecast_frequency(self, hourly_forecast):
        assert hourly_forecast.frequency == "hourly"

    def test_forecast_location_name(self, hourly_forecast):
        assert hourly_forecast.name == "Sheffield Park"

    def test_forecast_location_latitude(self, hourly_forecast):
        assert hourly_forecast.forecast_latitude == 50.9992

    def test_forecast_location_longitude(self, hourly_forecast):
        assert hourly_forecast.forecast_longitude == 0.0154

    def test_forecast_distance_from_request(self, hourly_forecast):
        assert hourly_forecast.distance_from_requested_location == 1081.5349

    def test_forecast_elevation(self, hourly_forecast):
        assert hourly_forecast.elevation == 37.0

    def test_forecast_first_timestep(
        self, hourly_forecast, expected_first_hourly_timestep
    ):
        assert hourly_forecast.timesteps[0] == expected_first_hourly_timestep

    def test_build_timestep(
        self,
        hourly_forecast,
        hourly_first_forecast_and_parameters,
        expected_first_hourly_timestep,
    ):
        built_timestep = hourly_forecast._build_timestep(
            hourly_first_forecast_and_parameters[0],
            hourly_first_forecast_and_parameters[1],
        )

        assert built_timestep == expected_first_hourly_timestep

    def test_at_datetime(self, hourly_forecast, expected_at_datetime_hourly_timestep):
        ts = hourly_forecast.at_datetime(datetime.datetime(2024, 2, 16, 19, 15))
        assert ts == expected_at_datetime_hourly_timestep

    def test_at_datetime_final_timestamp(
        self, hourly_forecast, expected_at_datetime_hourly_final_timestep
    ):
        ts = hourly_forecast.at_datetime(datetime.datetime(2024, 2, 17, 19, 20))
        assert ts == expected_at_datetime_hourly_final_timestep

    def test_requested_time_too_early(self, hourly_forecast):
        with pytest.raises(APIException):
            hourly_forecast.at_datetime(datetime.datetime(2024, 2, 15, 18, 25))

    def test_requested_time_too_late(self, hourly_forecast):
        with pytest.raises(APIException):
            hourly_forecast.at_datetime(datetime.datetime(2024, 2, 17, 19, 35))

    def test_forecast_first_timestep_raw_weather_code(
        self,
        hourly_forecast_raw_weather_code,
        expected_first_hourly_timestep_raw_weather_code,
    ):
        assert (
            hourly_forecast_raw_weather_code.timesteps[0]
            == expected_first_hourly_timestep_raw_weather_code
        )


class TestDailyForecast:
    def test_forecast_frequency(self, daily_forecast):
        assert daily_forecast.frequency == "daily"

    def test_forecast_location_name(self, daily_forecast):
        assert daily_forecast.name == "Sheffield Park"

    def test_forecast_location_latitude(self, daily_forecast):
        assert daily_forecast.forecast_latitude == 50.9992

    def test_forecast_location_longitude(self, daily_forecast):
        assert daily_forecast.forecast_longitude == 0.0154

    def test_forecast_distance_from_request(self, daily_forecast):
        assert daily_forecast.distance_from_requested_location == 1081.5349

    def test_forecast_elevation(self, daily_forecast):
        assert daily_forecast.elevation == 37.0

    def test_forecast_first_timestep(
        self, daily_forecast, expected_first_daily_timestep
    ):
        assert daily_forecast.timesteps[0] == expected_first_daily_timestep

    # Need a new test_build_timestep function here
    def test_build_timesteps(
        self, daily_forecast, load_daily_json, expected_first_daily_timestep
    ):
        timestep = daily_forecast._build_timestep(
            load_daily_json["features"][0]["properties"]["timeSeries"][0],
            load_daily_json["parameters"][0],
        )
        assert timestep == expected_first_daily_timestep

    def test_at_datetime(self, daily_forecast, expected_at_datetime_daily_timestep):
        ts = daily_forecast.at_datetime(datetime.datetime(2024, 2, 16, 19, 15))
        assert ts == expected_at_datetime_daily_timestep

    def test_at_datetime_final_timestamp(
        self, daily_forecast, expected_at_datetime_daily_final_timestep
    ):
        ts = daily_forecast.at_datetime(datetime.datetime(2024, 2, 17, 17))
        assert ts == expected_at_datetime_daily_final_timestep

    def test_requested_time_too_early(self, daily_forecast):
        with pytest.raises(APIException):
            daily_forecast.at_datetime(datetime.datetime(2024, 2, 15, 17))

    def test_requested_time_too_late(self, daily_forecast):
        with pytest.raises(APIException):
            daily_forecast.at_datetime(datetime.datetime(2024, 2, 23, 19))

    def test_forecast_first_timestep_raw_weather_code(
        self,
        daily_forecast_raw_weather_code,
        expected_first_daily_timestep_raw_weather_code,
    ):
        assert (
            daily_forecast_raw_weather_code.timesteps[0]
            == expected_first_daily_timestep_raw_weather_code
        )


class TestTwiceDailyForecast:
    def test_forecast_frequency(self, twice_daily_forecast):
        assert twice_daily_forecast.frequency == "twice-daily"

    def test_forecast_location_name(self, twice_daily_forecast):
        assert twice_daily_forecast.name == "Sheffield Park"

    def test_forecast_location_latitude(self, twice_daily_forecast):
        assert twice_daily_forecast.forecast_latitude == 50.9992

    def test_forecast_location_longitude(self, twice_daily_forecast):
        assert twice_daily_forecast.forecast_longitude == 0.0154

    def test_forecast_distance_from_request(self, twice_daily_forecast):
        assert twice_daily_forecast.distance_from_requested_location == 1081.5349

    def test_forecast_elevation(self, twice_daily_forecast):
        assert twice_daily_forecast.elevation == 37.0

    def test_forecast_first_timestep(
        self, twice_daily_forecast, expected_first_twice_daily_timestep
    ):
        assert twice_daily_forecast.timesteps[0] == expected_first_twice_daily_timestep

    # twice-daily forecasts take daily data from DataHub
    def test_build_twice_daily_timesteps(
        self, twice_daily_forecast, load_daily_json, expected_first_twice_daily_timestep
    ):
        timesteps = twice_daily_forecast._build_twice_daily_timesteps(
            load_daily_json["features"][0]["properties"]["timeSeries"],
            load_daily_json["parameters"][0],
        )
        assert timesteps[0] == expected_first_twice_daily_timestep

    def test_at_datetime(
        self, twice_daily_forecast, expected_at_datetime_twice_daily_timestep
    ):
        ts = twice_daily_forecast.at_datetime(datetime.datetime(2024, 2, 16, 19, 15))
        assert ts == expected_at_datetime_twice_daily_timestep

    def test_at_datetime_final_timestep(
        self, twice_daily_forecast, expected_at_datetime_twice_daily_final_timestep
    ):
        ts = twice_daily_forecast.at_datetime(datetime.datetime(2024, 2, 17, 17))
        assert ts == expected_at_datetime_twice_daily_final_timestep

    def test_requested_time_too_early(self, twice_daily_forecast):
        with pytest.raises(APIException):
            twice_daily_forecast.at_datetime(datetime.datetime(2024, 2, 15, 17))

    def test_requested_time_too_late(self, twice_daily_forecast):
        with pytest.raises(APIException):
            twice_daily_forecast.at_datetime(datetime.datetime(2024, 2, 23, 19))

    def test_forecast_first_timestep_raw_weather_code(
        self,
        twice_daily_forecast_raw_weather_code,
        expected_first_twice_daily_timestep_raw_weather_code,
    ):
        print(twice_daily_forecast_raw_weather_code.timesteps[0])
        assert (
            twice_daily_forecast_raw_weather_code.timesteps[0]
            == expected_first_twice_daily_timestep_raw_weather_code
        )


class TestThreeHourlyForecast:
    def test_forecast_frequency(self, three_hourly_forecast):
        assert three_hourly_forecast.frequency == "three-hourly"

    def test_forecast_location_name(self, three_hourly_forecast):
        assert three_hourly_forecast.name == "Sheffield Park"

    def test_forecast_location_latitude(self, three_hourly_forecast):
        assert three_hourly_forecast.forecast_latitude == 50.9992

    def test_forecast_location_longitude(self, three_hourly_forecast):
        assert three_hourly_forecast.forecast_longitude == 0.0154

    def test_forecast_distance_from_request(self, three_hourly_forecast):
        assert three_hourly_forecast.distance_from_requested_location == 1081.5349

    def test_forecast_elevation(self, three_hourly_forecast):
        assert three_hourly_forecast.elevation == 37.0

    def test_forecast_first_timestep(
        self, three_hourly_forecast, expected_first_three_hourly_timestep
    ):
        assert (
            three_hourly_forecast.timesteps[0] == expected_first_three_hourly_timestep
        )

    def test_build_timestep(
        self,
        three_hourly_forecast,
        three_hourly_first_forecast_and_parameters,
        expected_first_three_hourly_timestep,
    ):
        built_timestep = three_hourly_forecast._build_timestep(
            three_hourly_first_forecast_and_parameters[0],
            three_hourly_first_forecast_and_parameters[1],
        )

        assert built_timestep == expected_first_three_hourly_timestep

    def test_at_datetime(
        self, three_hourly_forecast, expected_at_datetime_three_hourly_timestep
    ):
        ts = three_hourly_forecast.at_datetime(datetime.datetime(2024, 2, 22, 19, 15))
        assert ts == expected_at_datetime_three_hourly_timestep

    def test_at_datetime_final_timestamp(
        self, three_hourly_forecast, expected_at_datetime_three_hourly_final_timestep
    ):
        ts = three_hourly_forecast.at_datetime(datetime.datetime(2024, 2, 24, 16))
        assert ts == expected_at_datetime_three_hourly_final_timestep

    def test_requested_time_too_early(self, three_hourly_forecast):
        with pytest.raises(APIException):
            three_hourly_forecast.at_datetime(datetime.datetime(2024, 2, 17, 13, 20))

    def test_requested_time_too_late(self, three_hourly_forecast):
        with pytest.raises(APIException):
            three_hourly_forecast.at_datetime(datetime.datetime(2024, 2, 24, 17))