From f1d94d6590b3eb89354b22f41822f5b18f6cba49 Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Sat, 3 May 2025 11:38:31 +0200 Subject: [PATCH] Refactor tests --- tests/test_coordinator.py | 323 ------------------ ...her_sensors.py => test_current_weather.py} | 79 +++-- tests/test_daily_forecast.py | 106 ++++++ tests/test_hourly_forecast.py | 91 +++++ tests/test_radar_forecast.py | 80 +++++ tests/test_warning.py | 24 ++ 6 files changed, 361 insertions(+), 342 deletions(-) delete mode 100644 tests/test_coordinator.py rename tests/{test_current_weather_sensors.py => test_current_weather.py} (74%) create mode 100644 tests/test_daily_forecast.py create mode 100644 tests/test_hourly_forecast.py create mode 100644 tests/test_radar_forecast.py create mode 100644 tests/test_warning.py diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py deleted file mode 100644 index 7b3b5c2..0000000 --- a/tests/test_coordinator.py +++ /dev/null @@ -1,323 +0,0 @@ -from datetime import datetime -from zoneinfo import ZoneInfo - -from freezegun import freeze_time - -from irm_kmi_api.data import (CurrentWeatherData, Forecast, IrmKmiForecast, - IrmKmiRadarForecast) -from tests.conftest import get_api_with_data -from tests.const import (ATTR_CONDITION_CLOUDY, ATTR_CONDITION_PARTLYCLOUDY, - ATTR_CONDITION_RAINY) - - -@freeze_time(datetime.fromisoformat('2024-01-12T07:10:00+00:00')) -async def test_warning_data() -> None: - api = get_api_with_data("be_forecast_warning.json") - - result = api.get_warnings(lang='en') - - assert isinstance(result, list) - assert len(result) == 2 - - first = result[0] - assert first.get('starts_at').replace(tzinfo=None) < datetime.now() - assert first.get('ends_at').replace(tzinfo=None) > datetime.now() - - assert first.get('slug') == 'fog' - assert first.get('friendly_name') == 'Fog' - assert first.get('id') == 7 - assert first.get('level') == 1 - - -@freeze_time(datetime.fromisoformat('2023-12-26T17:30:00+00:00')) -async def test_current_weather_be() -> None: - api = get_api_with_data("forecast.json") - tz = ZoneInfo("Europe/Brussels") - result = await api.get_current_weather(tz) - - expected = CurrentWeatherData( - condition=ATTR_CONDITION_CLOUDY, - temperature=7, - wind_speed=5, - wind_gust_speed=None, - wind_bearing=248, - pressure=1020, - uv_index=.7 - ) - - assert result == expected - - -@freeze_time(datetime.fromisoformat("2023-12-28T15:30:00")) -async def test_current_weather_nl() -> None: - api = get_api_with_data("forecast_nl.json") - tz = ZoneInfo("Europe/Brussels") - result = await api.get_current_weather(tz) - - expected = CurrentWeatherData( - condition=ATTR_CONDITION_CLOUDY, - temperature=11, - wind_speed=40, - wind_gust_speed=None, - wind_bearing=225, - pressure=1008, - uv_index=1 - ) - - assert expected == result - - -@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00+01:00')) -async def test_daily_forecast() -> None: - api = get_api_with_data("forecast.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_daily_forecast(tz, 'fr') - - assert isinstance(result, list) - assert len(result) == 8 - assert result[0]['datetime'] == '2023-12-26' - assert not result[0]['is_daytime'] - expected = IrmKmiForecast( - datetime='2023-12-27', - condition=ATTR_CONDITION_PARTLYCLOUDY, - native_precipitation=0, - native_temperature=9, - native_templow=4, - native_wind_gust_speed=50, - native_wind_speed=20, - precipitation_probability=0, - wind_bearing=180, - is_daytime=True, - text='Bar', - sunrise="2023-12-27T08:44:00+01:00", - sunset="2023-12-27T16:43:00+01:00" - ) - - assert result[1] == expected - - -@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00+01:00')) -async def test_hourly_forecast() -> None: - api = get_api_with_data("forecast.json") - tz = ZoneInfo("Europe/Brussels") - result = await api.get_hourly_forecast(tz) - - assert isinstance(result, list) - assert len(result) == 49 - - expected = Forecast( - datetime='2023-12-27T02:00:00+01:00', - condition=ATTR_CONDITION_RAINY, - native_precipitation=.98, - native_temperature=8, - native_templow=None, - native_wind_gust_speed=None, - native_wind_speed=15, - precipitation_probability=70, - wind_bearing=180, - native_pressure=1020, - is_daytime=False - ) - - assert result[8] == expected - - -@freeze_time(datetime.fromisoformat('2024-05-31T01:50:00+02:00')) -async def test_hourly_forecast_bis() -> None: - api = get_api_with_data("no-midnight-bug-31-05-2024T01-55.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_hourly_forecast(tz) - - assert isinstance(result, list) - - times = ['2024-05-31T01:00:00+02:00', '2024-05-31T02:00:00+02:00', '2024-05-31T03:00:00+02:00', - '2024-05-31T04:00:00+02:00', '2024-05-31T05:00:00+02:00', '2024-05-31T06:00:00+02:00', - '2024-05-31T07:00:00+02:00', '2024-05-31T08:00:00+02:00', '2024-05-31T09:00:00+02:00'] - - actual = [f['datetime'] for f in result[:9]] - - assert actual == times - - -@freeze_time(datetime.fromisoformat('2024-05-31T00:10:00+02:00')) -async def test_hourly_forecast_midnight_bug() -> None: - # Related to https://github.com/jdejaegh/irm-kmi-ha/issues/38 - api = get_api_with_data("midnight-bug-31-05-2024T00-13.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_hourly_forecast(tz) - - assert isinstance(result, list) - - first = Forecast( - datetime='2024-05-31T00:00:00+02:00', - condition=ATTR_CONDITION_CLOUDY, - native_precipitation=0, - native_temperature=14, - native_templow=None, - native_wind_gust_speed=None, - native_wind_speed=10, - precipitation_probability=0, - wind_bearing=293, - native_pressure=1010, - is_daytime=False - ) - - assert result[0] == first - - times = ['2024-05-31T00:00:00+02:00', '2024-05-31T01:00:00+02:00', '2024-05-31T02:00:00+02:00', - '2024-05-31T03:00:00+02:00', '2024-05-31T04:00:00+02:00', '2024-05-31T05:00:00+02:00', - '2024-05-31T06:00:00+02:00', '2024-05-31T07:00:00+02:00', '2024-05-31T08:00:00+02:00'] - - actual = [f['datetime'] for f in result[:9]] - - assert actual == times - - assert result[24]['datetime'] == '2024-06-01T00:00:00+02:00' - - -@freeze_time(datetime.fromisoformat('2024-05-31T00:10:00+02:00')) -async def test_daily_forecast_midnight_bug() -> None: - api = get_api_with_data("midnight-bug-31-05-2024T00-13.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_daily_forecast(tz, 'en') - - assert result[0]['datetime'] == '2024-05-31' - assert not result[0]['is_daytime'] - - assert result[1]['datetime'] == '2024-05-31' - assert result[1]['is_daytime'] - - assert result[2]['datetime'] == '2024-06-01' - assert result[2]['is_daytime'] - - assert result[3]['datetime'] == '2024-06-02' - assert result[3]['is_daytime'] - - -def test_radar_forecast() -> None: - api = get_api_with_data("forecast.json") - result = api.get_radar_forecast() - - expected = [ - IrmKmiRadarForecast(datetime="2023-12-26T17:00:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T17:10:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T17:20:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T17:30:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T17:40:00+01:00", native_precipitation=0.1, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T17:50:00+01:00", native_precipitation=0.01, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T18:00:00+01:00", native_precipitation=0.12, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T18:10:00+01:00", native_precipitation=1.2, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T18:20:00+01:00", native_precipitation=2, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T18:30:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), - IrmKmiRadarForecast(datetime="2023-12-26T18:40:00+01:00", native_precipitation=0, might_rain=False, - rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min') - ] - - assert expected == result - - -def test_radar_forecast_rain_interval() -> None: - api = get_api_with_data('forecast_with_rain_on_radar.json') - result = api.get_radar_forecast() - - _12 = IrmKmiRadarForecast( - datetime='2024-05-30T18:00:00+02:00', - native_precipitation=0.89, - might_rain=True, - rain_forecast_max=1.12, - rain_forecast_min=0.50, - unit='mm/10min' - ) - - _13 = IrmKmiRadarForecast( - datetime="2024-05-30T18:10:00+02:00", - native_precipitation=0.83, - might_rain=True, - rain_forecast_max=1.09, - rain_forecast_min=0.64, - unit='mm/10min' - ) - - assert result[12] == _12 - assert result[13] == _13 - - -@freeze_time("2024-06-09T13:40:00+00:00") -async def test_datetime_daily_forecast_nl() -> None: - api = get_api_with_data("forecast_ams_no_ww.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_daily_forecast(tz, 'en') - - assert result[0]['datetime'] == '2024-06-09' - assert result[0]['is_daytime'] - - assert result[1]['datetime'] == '2024-06-10' - assert not result[1]['is_daytime'] - - assert result[2]['datetime'] == '2024-06-10' - assert result[2]['is_daytime'] - - -@freeze_time("2024-06-09T13:40:00+00:00") -async def test_current_condition_forecast_nl() -> None: - api = get_api_with_data("forecast_ams_no_ww.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_current_weather(tz) - - expected = CurrentWeatherData( - condition=ATTR_CONDITION_PARTLYCLOUDY, - temperature=15, - wind_speed=26, - wind_gust_speed=None, - wind_bearing=270, - pressure=1010, - uv_index=6 - ) - assert result == expected - - -@freeze_time("2024-06-09T13:40:00+00:00") -async def test_sunrise_sunset_nl() -> None: - api = get_api_with_data("forecast_ams_no_ww.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_daily_forecast(tz, 'en') - - assert result[0]['sunrise'] == '2024-06-09T05:19:28+02:00' - assert result[0]['sunset'] == '2024-06-09T22:01:09+02:00' - - assert result[1]['sunrise'] is None - assert result[1]['sunset'] is None - - assert result[2]['sunrise'] == '2024-06-10T05:19:08+02:00' - assert result[2]['sunset'] == '2024-06-10T22:01:53+02:00' - - -@freeze_time("2023-12-26T18:30:00+01:00") -async def test_sunrise_sunset_be() -> None: - api = get_api_with_data("forecast.json") - tz = ZoneInfo("Europe/Brussels") - - result = await api.get_daily_forecast(tz, 'en') - - assert result[1]['sunrise'] == '2023-12-27T08:44:00+01:00' - assert result[1]['sunset'] == '2023-12-27T16:43:00+01:00' - - assert result[2]['sunrise'] == '2023-12-28T08:45:00+01:00' - assert result[2]['sunset'] == '2023-12-28T16:43:00+01:00' diff --git a/tests/test_current_weather_sensors.py b/tests/test_current_weather.py similarity index 74% rename from tests/test_current_weather_sensors.py rename to tests/test_current_weather.py index 66fe399..6a9c0e5 100644 --- a/tests/test_current_weather_sensors.py +++ b/tests/test_current_weather.py @@ -4,7 +4,66 @@ from zoneinfo import ZoneInfo import pytest from freezegun import freeze_time +from irm_kmi_api.data import CurrentWeatherData from tests.conftest import get_api_data, get_api_with_data +from tests.const import ATTR_CONDITION_CLOUDY, ATTR_CONDITION_PARTLYCLOUDY + + +@freeze_time(datetime.fromisoformat('2023-12-26T17:30:00+00:00')) +async def test_current_weather_be() -> None: + api = get_api_with_data("forecast.json") + tz = ZoneInfo("Europe/Brussels") + result = await api.get_current_weather(tz) + + expected = CurrentWeatherData( + condition=ATTR_CONDITION_CLOUDY, + temperature=7, + wind_speed=5, + wind_gust_speed=None, + wind_bearing=248, + pressure=1020, + uv_index=.7 + ) + + assert result == expected + + +@freeze_time(datetime.fromisoformat("2023-12-28T15:30:00")) +async def test_current_weather_nl() -> None: + api = get_api_with_data("forecast_nl.json") + tz = ZoneInfo("Europe/Brussels") + result = await api.get_current_weather(tz) + + expected = CurrentWeatherData( + condition=ATTR_CONDITION_CLOUDY, + temperature=11, + wind_speed=40, + wind_gust_speed=None, + wind_bearing=225, + pressure=1008, + uv_index=1 + ) + + assert expected == result + + +@freeze_time("2024-06-09T13:40:00+00:00") +async def test_current_condition_forecast_nl() -> None: + api = get_api_with_data("forecast_ams_no_ww.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_current_weather(tz) + + expected = CurrentWeatherData( + condition=ATTR_CONDITION_PARTLYCLOUDY, + temperature=15, + wind_speed=26, + wind_gust_speed=None, + wind_bearing=270, + pressure=1010, + uv_index=6 + ) + assert result == expected @pytest.mark.parametrize("sensor,expected,filename", @@ -63,7 +122,7 @@ from tests.conftest import get_api_data, get_api_with_data ('pressure', 1010, 'midnight-bug-31-05-2024T00-13.json'), ('pressure', 1010, 'no-midnight-bug-31-05-2024T01-55.json') ]) -async def test_current_weather_sensors( +async def test_current_weather_attributes( sensor: str, expected: int | float | None, filename: str @@ -81,21 +140,3 @@ async def test_current_weather_sensors( assert r == expected_ await run(sensor, expected) - - -@pytest.mark.parametrize("expected,filename", - [ - ('mm/h', 'forecast_ams_no_ww.json'), - ('mm/10min', 'forecast_out_of_benelux.json'), - ('mm/10min', 'forecast_with_rain_on_radar.json'), - ]) -async def test_current_rainfall_unit( - expected, - filename -) -> None: - api = get_api_with_data(filename) - - radar_forecast=api.get_radar_forecast() - - for r in radar_forecast: - assert r.get('unit') == expected diff --git a/tests/test_daily_forecast.py b/tests/test_daily_forecast.py new file mode 100644 index 0000000..ae6716a --- /dev/null +++ b/tests/test_daily_forecast.py @@ -0,0 +1,106 @@ +from datetime import datetime +from zoneinfo import ZoneInfo + +from freezegun import freeze_time + +from irm_kmi_api.data import IrmKmiForecast +from tests.conftest import get_api_with_data +from tests.const import ATTR_CONDITION_PARTLYCLOUDY + + +@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00+01:00')) +async def test_daily_forecast() -> None: + api = get_api_with_data("forecast.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_daily_forecast(tz, 'fr') + + assert isinstance(result, list) + assert len(result) == 8 + assert result[0]['datetime'] == '2023-12-26' + assert not result[0]['is_daytime'] + expected = IrmKmiForecast( + datetime='2023-12-27', + condition=ATTR_CONDITION_PARTLYCLOUDY, + native_precipitation=0, + native_temperature=9, + native_templow=4, + native_wind_gust_speed=50, + native_wind_speed=20, + precipitation_probability=0, + wind_bearing=180, + is_daytime=True, + text='Bar', + sunrise="2023-12-27T08:44:00+01:00", + sunset="2023-12-27T16:43:00+01:00" + ) + + assert result[1] == expected + + +@freeze_time(datetime.fromisoformat('2024-05-31T00:10:00+02:00')) +async def test_daily_forecast_midnight_bug() -> None: + api = get_api_with_data("midnight-bug-31-05-2024T00-13.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_daily_forecast(tz, 'en') + + assert result[0]['datetime'] == '2024-05-31' + assert not result[0]['is_daytime'] + + assert result[1]['datetime'] == '2024-05-31' + assert result[1]['is_daytime'] + + assert result[2]['datetime'] == '2024-06-01' + assert result[2]['is_daytime'] + + assert result[3]['datetime'] == '2024-06-02' + assert result[3]['is_daytime'] + + +@freeze_time("2024-06-09T13:40:00+00:00") +async def test_datetime_daily_forecast_nl() -> None: + api = get_api_with_data("forecast_ams_no_ww.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_daily_forecast(tz, 'en') + + assert result[0]['datetime'] == '2024-06-09' + assert result[0]['is_daytime'] + + assert result[1]['datetime'] == '2024-06-10' + assert not result[1]['is_daytime'] + + assert result[2]['datetime'] == '2024-06-10' + assert result[2]['is_daytime'] + + +@freeze_time("2024-06-09T13:40:00+00:00") +async def test_sunrise_sunset_nl() -> None: + api = get_api_with_data("forecast_ams_no_ww.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_daily_forecast(tz, 'en') + + assert result[0]['sunrise'] == '2024-06-09T05:19:28+02:00' + assert result[0]['sunset'] == '2024-06-09T22:01:09+02:00' + + assert result[1]['sunrise'] is None + assert result[1]['sunset'] is None + + assert result[2]['sunrise'] == '2024-06-10T05:19:08+02:00' + assert result[2]['sunset'] == '2024-06-10T22:01:53+02:00' + + +@freeze_time("2023-12-26T18:30:00+01:00") +async def test_sunrise_sunset_be() -> None: + api = get_api_with_data("forecast.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_daily_forecast(tz, 'en') + + assert result[1]['sunrise'] == '2023-12-27T08:44:00+01:00' + assert result[1]['sunset'] == '2023-12-27T16:43:00+01:00' + + assert result[2]['sunrise'] == '2023-12-28T08:45:00+01:00' + assert result[2]['sunset'] == '2023-12-28T16:43:00+01:00' diff --git a/tests/test_hourly_forecast.py b/tests/test_hourly_forecast.py new file mode 100644 index 0000000..4f14629 --- /dev/null +++ b/tests/test_hourly_forecast.py @@ -0,0 +1,91 @@ +from datetime import datetime +from zoneinfo import ZoneInfo + +from freezegun import freeze_time + +from irm_kmi_api.data import Forecast +from tests.conftest import get_api_with_data +from tests.const import ATTR_CONDITION_CLOUDY, ATTR_CONDITION_RAINY + + +@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00+01:00')) +async def test_hourly_forecast() -> None: + api = get_api_with_data("forecast.json") + tz = ZoneInfo("Europe/Brussels") + result = await api.get_hourly_forecast(tz) + + assert isinstance(result, list) + assert len(result) == 49 + + expected = Forecast( + datetime='2023-12-27T02:00:00+01:00', + condition=ATTR_CONDITION_RAINY, + native_precipitation=.98, + native_temperature=8, + native_templow=None, + native_wind_gust_speed=None, + native_wind_speed=15, + precipitation_probability=70, + wind_bearing=180, + native_pressure=1020, + is_daytime=False + ) + + assert result[8] == expected + + +@freeze_time(datetime.fromisoformat('2024-05-31T01:50:00+02:00')) +async def test_hourly_forecast_bis() -> None: + api = get_api_with_data("no-midnight-bug-31-05-2024T01-55.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_hourly_forecast(tz) + + assert isinstance(result, list) + + times = ['2024-05-31T01:00:00+02:00', '2024-05-31T02:00:00+02:00', '2024-05-31T03:00:00+02:00', + '2024-05-31T04:00:00+02:00', '2024-05-31T05:00:00+02:00', '2024-05-31T06:00:00+02:00', + '2024-05-31T07:00:00+02:00', '2024-05-31T08:00:00+02:00', '2024-05-31T09:00:00+02:00'] + + actual = [f['datetime'] for f in result[:9]] + + assert actual == times + + +@freeze_time(datetime.fromisoformat('2024-05-31T00:10:00+02:00')) +async def test_hourly_forecast_midnight_bug() -> None: + # Related to https://github.com/jdejaegh/irm-kmi-ha/issues/38 + api = get_api_with_data("midnight-bug-31-05-2024T00-13.json") + tz = ZoneInfo("Europe/Brussels") + + result = await api.get_hourly_forecast(tz) + + assert isinstance(result, list) + + first = Forecast( + datetime='2024-05-31T00:00:00+02:00', + condition=ATTR_CONDITION_CLOUDY, + native_precipitation=0, + native_temperature=14, + native_templow=None, + native_wind_gust_speed=None, + native_wind_speed=10, + precipitation_probability=0, + wind_bearing=293, + native_pressure=1010, + is_daytime=False + ) + + assert result[0] == first + + times = ['2024-05-31T00:00:00+02:00', '2024-05-31T01:00:00+02:00', '2024-05-31T02:00:00+02:00', + '2024-05-31T03:00:00+02:00', '2024-05-31T04:00:00+02:00', '2024-05-31T05:00:00+02:00', + '2024-05-31T06:00:00+02:00', '2024-05-31T07:00:00+02:00', '2024-05-31T08:00:00+02:00'] + + actual = [f['datetime'] for f in result[:9]] + + assert actual == times + + assert result[24]['datetime'] == '2024-06-01T00:00:00+02:00' + + diff --git a/tests/test_radar_forecast.py b/tests/test_radar_forecast.py new file mode 100644 index 0000000..7502b7d --- /dev/null +++ b/tests/test_radar_forecast.py @@ -0,0 +1,80 @@ +import pytest + +from irm_kmi_api.data import IrmKmiRadarForecast +from tests.conftest import get_api_with_data + + +def test_radar_forecast() -> None: + api = get_api_with_data("forecast.json") + result = api.get_radar_forecast() + + expected = [ + IrmKmiRadarForecast(datetime="2023-12-26T17:00:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T17:10:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T17:20:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T17:30:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T17:40:00+01:00", native_precipitation=0.1, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T17:50:00+01:00", native_precipitation=0.01, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T18:00:00+01:00", native_precipitation=0.12, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T18:10:00+01:00", native_precipitation=1.2, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T18:20:00+01:00", native_precipitation=2, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T18:30:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), + IrmKmiRadarForecast(datetime="2023-12-26T18:40:00+01:00", native_precipitation=0, might_rain=False, + rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min') + ] + + assert expected == result + + +def test_radar_forecast_rain_interval() -> None: + api = get_api_with_data('forecast_with_rain_on_radar.json') + result = api.get_radar_forecast() + + _12 = IrmKmiRadarForecast( + datetime='2024-05-30T18:00:00+02:00', + native_precipitation=0.89, + might_rain=True, + rain_forecast_max=1.12, + rain_forecast_min=0.50, + unit='mm/10min' + ) + + _13 = IrmKmiRadarForecast( + datetime="2024-05-30T18:10:00+02:00", + native_precipitation=0.83, + might_rain=True, + rain_forecast_max=1.09, + rain_forecast_min=0.64, + unit='mm/10min' + ) + + assert result[12] == _12 + assert result[13] == _13 + + +@pytest.mark.parametrize("expected,filename", + [ + ('mm/h', 'forecast_ams_no_ww.json'), + ('mm/10min', 'forecast_out_of_benelux.json'), + ('mm/10min', 'forecast_with_rain_on_radar.json'), + ]) +async def test_current_rainfall_unit( + expected, + filename +) -> None: + api = get_api_with_data(filename) + + radar_forecast=api.get_radar_forecast() + + for r in radar_forecast: + assert r.get('unit') == expected diff --git a/tests/test_warning.py b/tests/test_warning.py new file mode 100644 index 0000000..1da98c6 --- /dev/null +++ b/tests/test_warning.py @@ -0,0 +1,24 @@ +from datetime import datetime + +from freezegun import freeze_time + +from tests.conftest import get_api_with_data + + +@freeze_time(datetime.fromisoformat('2024-01-12T07:10:00+00:00')) +async def test_warning_data() -> None: + api = get_api_with_data("be_forecast_warning.json") + + result = api.get_warnings(lang='en') + + assert isinstance(result, list) + assert len(result) == 2 + + first = result[0] + assert first.get('starts_at').replace(tzinfo=None) < datetime.now() + assert first.get('ends_at').replace(tzinfo=None) > datetime.now() + + assert first.get('slug') == 'fog' + assert first.get('friendly_name') == 'Fog' + assert first.get('id') == 7 + assert first.get('level') == 1