Skip to content

Commit

Permalink
Check if water temperature value is current (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Bieniek <[email protected]>
  • Loading branch information
bieniu and bieniu authored Jun 6, 2024
1 parent a5b7855 commit afea399
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
20 changes: 15 additions & 5 deletions imgw_pib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Python wrapper for IMGW-PIB API."""

import logging
from datetime import UTC, datetime
from http import HTTPStatus
from typing import Any, Self

Expand Down Expand Up @@ -248,16 +249,25 @@ def _parse_hydrological_data(self: Self, data: dict[str, Any]) -> HydrologicalDa
if self._alarm_water_level is not None
else None,
)
water_temperature = data[ApiNames.WATER_TEMPERATURE]

water_temperature_measurement_date = get_datetime(
data[ApiNames.WATER_TEMPERATURE_MEASUREMENT_DATE],
"%Y-%m-%d %H:%M:%S",
)
if (
water_temperature_measurement_date is not None
and water_temperature_measurement_date.date() == datetime.now(tz=UTC).date()
):
water_temperature = data[ApiNames.WATER_TEMPERATURE]
else:
water_temperature_measurement_date = None
water_temperature = None

water_temperature_sensor = SensorData(
name="Water Temperature",
value=float(water_temperature) if water_temperature is not None else None,
unit=Units.CELSIUS.value if water_temperature is not None else None,
)
water_temperature_measurement_date = get_datetime(
data[ApiNames.WATER_TEMPERATURE_MEASUREMENT_DATE],
"%Y-%m-%d %H:%M:%S",
)

return HydrologicalData(
water_level=water_level_sensor,
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
aioresponses==0.7.6
coverage==7.5.3
freezegun==1.5.1
mypy==1.10.0
pytest-asyncio==0.23.7
pytest-cov==5.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

PROJECT_DIR = Path(__file__).parent.resolve()
README_FILE = PROJECT_DIR / "README.md"
VERSION = "1.0.2"
VERSION = "1.0.3"

setup(
name="imgw_pib",
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/test_init.ambr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# serializer version: 1
# name: test_hydrological_station
HydrologicalData(water_level=SensorData(name='Water Level', value=568.0, unit='cm'), flood_alarm_level=SensorData(name='Flood Alarm Level', value=630.0, unit='cm'), flood_warning_level=SensorData(name='Flood Warning Level', value=590.0, unit='cm'), water_temperature=SensorData(name='Water Temperature', value=8.1, unit='°C'), river='Zalew Wiślany', station_id='154190050', station='Nowe Batorowo', water_level_measurement_date=datetime.datetime(2024, 4, 22, 10, 0, tzinfo=datetime.timezone.utc), water_temperature_measurement_date=datetime.datetime(2024, 4, 22, 10, 10, tzinfo=datetime.timezone.utc), flood_alarm=False, flood_warning=False)
HydrologicalData(water_level=SensorData(name='Water Level', value=568.0, unit='cm'), flood_alarm_level=SensorData(name='Flood Alarm Level', value=630.0, unit='cm'), flood_warning_level=SensorData(name='Flood Warning Level', value=590.0, unit='cm'), water_temperature=SensorData(name='Water Temperature', value=8.1, unit='°C'), river='Zalew Wiślany', station_id='154190050', station='Nowe Batorowo', water_level_measurement_date=FakeDatetime(2024, 4, 22, 10, 0, tzinfo=datetime.timezone.utc), water_temperature_measurement_date=FakeDatetime(2024, 4, 22, 10, 10, tzinfo=datetime.timezone.utc), flood_alarm=False, flood_warning=False)
# ---
# name: test_hydrological_stations
dict({
Expand Down
38 changes: 37 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Tests for imgw-pib package."""

from datetime import UTC, datetime
from http import HTTPStatus
from typing import Any

import aiohttp
import pytest
from aioresponses import aioresponses
from freezegun import freeze_time
from syrupy import SnapshotAssertion

from imgw_pib import ImgwPib
Expand All @@ -17,6 +19,8 @@
from imgw_pib.exceptions import ApiError
from imgw_pib.model import ApiNames

TEST_TIME = datetime(2024, 4, 22, 11, 10, 32, tzinfo=UTC)


@pytest.mark.asyncio()
async def test_weather_stations(
Expand Down Expand Up @@ -109,7 +113,7 @@ async def test_hydrological_station(
"""Test weather station."""
session = aiohttp.ClientSession()

with aioresponses() as session_mock:
with aioresponses() as session_mock, freeze_time(TEST_TIME):
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(
f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station
Expand Down Expand Up @@ -369,3 +373,35 @@ async def test_hydrological_details_is_none(
await session.close()

assert str(exc_info.value) == "Invalid hydrological details format"


@pytest.mark.asyncio()
async def test_water_temperature_not_current(
hydrological_stations: list[dict[str, Any]],
hydrological_station: dict[str, Any],
hydrological_details: dict[str, Any],
) -> None:
"""Test water_temperature is not current."""
session = aiohttp.ClientSession()

hydrological_station["temperatura_wody_data_pomiaru"] = "2002-01-01 12:00:01"

with aioresponses() as session_mock, freeze_time(TEST_TIME):
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(
f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station
)
session_mock.get(
API_HYDROLOGICAL_DETAILS_ENDPOINT.format(
hydrological_station_id="154190050"
),
payload=hydrological_details,
)

imgwpib = await ImgwPib.create(session, hydrological_station_id="154190050")
result = await imgwpib.get_hydrological_data()

await session.close()

assert result.water_temperature.value is None
assert result.water_temperature_measurement_date is None

0 comments on commit afea399

Please sign in to comment.