Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor initialize #5

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions imgw_pib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,60 @@ def hydrological_stations(self: Self) -> dict[str, str]:
async def initialize(self: Self) -> None:
"""Initialize."""
_LOGGER.debug("Initializing IMGW-PIB")
self._weather_station_list = await self.get_weather_stations()
self._hydrological_station_list = await self.get_hydrological_stations()

if (
self.weather_station_id is not None
and self.weather_station_id not in self.weather_stations
):
msg = f"Invalid weather station ID: {self.weather_station_id}"
raise ApiError(msg)
if (
self.hydrological_station_id is not None
and self.hydrological_station_id not in self.hydrological_stations
):
msg = f"Invalid hydrological station ID: {self.hydrological_station_id}"
raise ApiError(msg)

async def get_weather_stations(self: Self) -> dict[str, str]:
"""Get list of weather stations."""
if self.weather_station_id is not None:
await self.update_weather_stations()

if self.weather_station_id not in self.weather_stations:
msg = f"Invalid weather station ID: {self.weather_station_id}"
raise ApiError(msg)

if self.hydrological_station_id is not None:
await self.update_hydrological_stations()

if self.hydrological_station_id not in self.hydrological_stations:
msg = f"Invalid hydrological station ID: {self.hydrological_station_id}"
raise ApiError(msg)

async def update_weather_stations(self: Self) -> None:
"""Update list of weather stations."""
url = API_WEATHER_ENDPOINT

stations_data = await self._http_request(url)

return {
self._weather_station_list = {
station[API_STATION_ID]: station[API_STATION] for station in stations_data
}

async def get_weather_data(self: Self) -> WeatherData:
"""Get weather data."""
if self.weather_station_id is None:
msg = "Weather station ID is not set"
raise ApiError(msg)

url = f"{API_WEATHER_ENDPOINT}/id/{self.weather_station_id}"

weather_data = await self._http_request(url)

return self._parse_weather_data(weather_data)

async def get_hydrological_stations(self: Self) -> dict[str, str]:
"""Get list of hydrological stations."""
async def update_hydrological_stations(self: Self) -> None:
"""Update list of hydrological stations."""
url = API_HYDROLOGICAL_ENDPOINT

stations_data = await self._http_request(url)

return {
self._hydrological_station_list = {
station[API_STATION_ID]: f"{station[API_STATION]} ({station[API_RIVER]})"
for station in stations_data
}

async def get_hydrological_data(self: Self) -> HydrologicalData:
"""Get hydrological data."""
if self.hydrological_station_id is None:
msg = "Hydrological station ID is not set"
raise ApiError(msg)

url = f"{API_HYDROLOGICAL_ENDPOINT}/id/{self.hydrological_station_id}"

hydrological_data = await self._http_request(url)
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 = "0.0.2"
VERSION = "0.0.3"

setup(
name="imgw_pib",
Expand Down
113 changes: 101 additions & 12 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from syrupy import SnapshotAssertion

from imgw_pib import ImgwPib
from imgw_pib.const import API_HYDROLOGICAL_ENDPOINT, API_WEATHER_ENDPOINT
from imgw_pib.const import (
API_HYDROLOGICAL_ENDPOINT,
API_WATER_LEVEL,
API_WATER_LEVEL_MEASUREMENT_DATE,
API_WEATHER_ENDPOINT,
)
from imgw_pib.exceptions import ApiError


Expand All @@ -20,14 +25,19 @@ async def test_weather_stations(
"""Test weather stations."""
session = aiohttp.ClientSession()

imgwpib = await ImgwPib.create(session)

assert len(imgwpib.weather_stations) == 0

with aioresponses() as session_mock:
session_mock.get(API_WEATHER_ENDPOINT, payload=weather_stations)
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=[])

imgwpib = await ImgwPib.create(session)
await imgwpib.update_weather_stations()

await session.close()

assert len(imgwpib.weather_stations)

assert imgwpib.weather_stations == snapshot


Expand All @@ -43,9 +53,8 @@ async def test_weather_station(
with aioresponses() as session_mock:
session_mock.get(API_WEATHER_ENDPOINT, payload=weather_stations)
session_mock.get(f"{API_WEATHER_ENDPOINT}/id/12295", payload=weather_station)
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=[])

imgwpib = await ImgwPib.create(session, "12295")
imgwpib = await ImgwPib.create(session, weather_station_id="12295")
weather_data = await imgwpib.get_weather_data()

await session.close()
Expand All @@ -60,10 +69,9 @@ async def test_wrong_weather_station_id(weather_stations: list[dict[str, Any]])

with aioresponses() as session_mock:
session_mock.get(API_WEATHER_ENDPOINT, payload=weather_stations)
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=[])

with pytest.raises(ApiError) as exc_info:
await ImgwPib.create(session, "abcd1234")
await ImgwPib.create(session, weather_station_id="abcd1234")

await session.close()

Expand All @@ -77,11 +85,14 @@ async def test_hydrological_stations(
"""Test hydrological stations."""
session = aiohttp.ClientSession()

imgwpib = await ImgwPib.create(session)

assert len(imgwpib.hydrological_stations) == 0

with aioresponses() as session_mock:
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(API_WEATHER_ENDPOINT, payload=[])

imgwpib = await ImgwPib.create(session)
await imgwpib.update_hydrological_stations()

await session.close()

Expand All @@ -99,7 +110,6 @@ async def test_hydrological_station(

with aioresponses() as session_mock:
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(API_WEATHER_ENDPOINT, payload=[])
session_mock.get(
f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station
)
Expand All @@ -121,7 +131,6 @@ async def test_wrong_weather_hydrological_id(

with aioresponses() as session_mock:
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(API_WEATHER_ENDPOINT, payload=[])

with pytest.raises(ApiError) as exc_info:
await ImgwPib.create(session, hydrological_station_id="abcd1234")
Expand All @@ -144,8 +153,88 @@ async def test_api_error() -> None:
)

with pytest.raises(ApiError) as exc:
await ImgwPib.create(session)
await ImgwPib.create(session, weather_station_id="12345")

await session.close()

assert str(exc.value) == "Invalid response: 400"


@pytest.mark.asyncio()
async def test_get_weather_data_without_station_id() -> None:
"""Test get_weather_data() without station ID."""
session = aiohttp.ClientSession()

imgwpib = await ImgwPib.create(session)

with pytest.raises(ApiError) as exc:
await imgwpib.get_weather_data()

await session.close()

assert str(exc.value) == "Weather station ID is not set"


@pytest.mark.asyncio()
async def test_get_hydrological_data_without_station_id() -> None:
"""Test get_hydrological_data() without station ID."""
session = aiohttp.ClientSession()

imgwpib = await ImgwPib.create(session)

with pytest.raises(ApiError) as exc:
await imgwpib.get_hydrological_data()

await session.close()

assert str(exc.value) == "Hydrological station ID is not set"


@pytest.mark.asyncio()
async def test_invalid_water_level_value(
hydrological_stations: list[dict[str, Any]],
hydrological_station: dict[str, Any],
) -> None:
"""Test invalid water level value."""
session = aiohttp.ClientSession()

hydrological_station[API_WATER_LEVEL] = None

with aioresponses() as session_mock:
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(
f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station
)

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

with pytest.raises(ApiError) as exc:
await imgwpib.get_hydrological_data()

await session.close()

assert str(exc.value) == "Invalid water level value"


@pytest.mark.asyncio()
async def test_invalid_date(
hydrological_stations: list[dict[str, Any]],
hydrological_station: dict[str, Any],
) -> None:
"""Test invalid water level value."""
session = aiohttp.ClientSession()

hydrological_station[API_WATER_LEVEL_MEASUREMENT_DATE] = None

with aioresponses() as session_mock:
session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations)
session_mock.get(
f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station
)

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

await session.close()

assert hydrological_data.water_level_measurement_date is None