Skip to content

Commit

Permalink
adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 committed Feb 11, 2024
1 parent 41d78cb commit 441014b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
6 changes: 2 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

import json

import aiohttp
import pytest
from aioresponses import aioresponses

from aiopegelonline import PegelOnline
from aiopegelonline.const import BASE_URL

from .const import MOCK_DATA


@pytest.fixture()
def mock_pegelonline():
def mock_response():
"""Fixture that the PegelOnline is used with mocked response."""
with aioresponses() as mock_resp:
for path, data in MOCK_DATA.items():
Expand All @@ -24,4 +22,4 @@ def mock_pegelonline():
body=json.dumps(data["body"]),
exception=data.get("exception"),
)
yield PegelOnline(aiohttp.ClientSession())
yield mock_resp
35 changes: 18 additions & 17 deletions tests/test_aiopegelonline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from __future__ import annotations

import pytest
from aiohttp import ClientError
from aiohttp import ClientError, ClientSession

from aiopegelonline import PegelOnline
from aiopegelonline.exceptions import PegelonlineDataError
from aiopegelonline.models import Station, StationMeasurements


@pytest.mark.asyncio
async def test_get_all_stations(mock_pegelonline):
async def test_get_all_stations(mock_response):
"""Test async_get_all_stations."""
# with mock_response:
stations = await mock_pegelonline.async_get_all_stations()
stations = await PegelOnline(ClientSession()).async_get_all_stations()
assert len(stations) == 2

station = stations["70272185-xxxx-xxxx-xxxx-43bea330dcae"]
Expand Down Expand Up @@ -44,9 +44,9 @@ async def test_get_all_stations(mock_pegelonline):


@pytest.mark.asyncio
async def test_get_nearby_stations(mock_pegelonline):
async def test_get_nearby_stations(mock_response):
"""Test async_get_nearby_stations."""
stations = await mock_pegelonline.async_get_nearby_stations(13, 51, 25)
stations = await PegelOnline(ClientSession()).async_get_nearby_stations(13, 51, 25)
assert len(stations) == 1

station = stations["70272185-xxxx-xxxx-xxxx-43bea330dcae"]
Expand All @@ -65,16 +65,16 @@ async def test_get_nearby_stations(mock_pegelonline):


@pytest.mark.asyncio
async def test_get_nearby_stations_no_stations(mock_pegelonline):
async def test_get_nearby_stations_no_stations(mock_response):
"""Test async_get_nearby_stations."""
stations = await mock_pegelonline.async_get_nearby_stations(10, 45, 25)
stations = await PegelOnline(ClientSession()).async_get_nearby_stations(10, 45, 25)
assert len(stations) == 0


@pytest.mark.asyncio
async def test_get_station_details(mock_pegelonline):
async def test_get_station_details(mock_response):
"""Test async_get_station_details."""
station = await mock_pegelonline.async_get_station_details(
station = await PegelOnline(ClientSession()).async_get_station_details(
"70272185-xxxx-xxxx-xxxx-43bea330dcae"
)
assert isinstance(station, Station)
Expand All @@ -92,23 +92,24 @@ async def test_get_station_details(mock_pegelonline):


@pytest.mark.asyncio
async def test_get_station_details_invalid(mock_pegelonline):
async def test_get_station_details_invalid(mock_response):
"""Test async_get_station_details with invalid uuid."""
with pytest.raises(PegelonlineDataError):
await mock_pegelonline.async_get_station_details("INVALID_UUID")
await PegelOnline(ClientSession()).async_get_station_details("INVALID_UUID")


@pytest.mark.asyncio
async def test_get_station_details_connection_error(mock_pegelonline):
async def test_get_station_details_connection_error(mock_response):
"""Test async_get_station_details with connection error."""
with pytest.raises(ClientError):
await mock_pegelonline.async_get_station_details("CONNECT_ERROR")
await PegelOnline(ClientSession()).async_get_station_details("CONNECT_ERROR")


@pytest.mark.asyncio
async def test_get_station_measurements(mock_pegelonline):
async def test_get_station_measurements(mock_response):
"""Test async_get_station_measurements."""
measurement = await mock_pegelonline.async_get_station_measurements(
api = PegelOnline(ClientSession())
measurement = await api.async_get_station_measurements(
"915d76e1-xxxx-xxxx-xxxx-4d144cd771cc"
)
assert isinstance(measurement, StationMeasurements)
Expand All @@ -135,7 +136,7 @@ async def test_get_station_measurements(mock_pegelonline):
assert measurement.water_temperature.uom == "°C"
assert measurement.water_temperature.value == 22.1

measurement = await mock_pegelonline.async_get_station_measurements(
measurement = await api.async_get_station_measurements(
"07374faf-xxxx-xxxx-xxxx-adc0e0784c4b"
)
assert isinstance(measurement, StationMeasurements)
Expand Down

0 comments on commit 441014b

Please sign in to comment.