Skip to content

Commit

Permalink
Rework tests (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 authored Feb 11, 2024
1 parent 618c511 commit 6f8ce4a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
"python.linting.pylintPath": "/usr/local/bin/pylint",
"python.formatting.provider": "black",
"python.testing.pytestArgs": [
"--no-cov"
"--no-cov",
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-r requirements.txt
aioresponses==0.7.4
aioresponses==0.7.6
black==23.7.0
flake8-docstrings==1.7.0
flake8==6.0.0
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ include_trailing_comma = True
force_grid_wrap = 0
use_parentheses = True
line_length = 88

[tool:pytest]
asyncio_mode = auto
30 changes: 24 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@
from .const import MOCK_DATA


@pytest.fixture()
def mock_pegelonline():
"""Fixture that the PegelOnline is used with mocked response."""
with aioresponses() as mock_resp:
@pytest.fixture
def mock_aioresponse():
"""Mock a web request and provide a response."""
with aioresponses() as m:
yield m

@pytest.fixture
async def mock_pegelonline():
"""Return PegelOnline session."""
session = aiohttp.ClientSession()
api = PegelOnline(session)
yield api
await session.close()

@pytest.fixture
def mock_pegelonline_with_data(mock_aioresponse, mock_pegelonline):
"""Comfort fixture to initialize deCONZ session."""

async def data_to_pegelonline() -> PegelOnline:
"""Initialize PegelOnline session."""
for path, data in MOCK_DATA.items():
mock_resp.get(
mock_aioresponse.get(
f"{BASE_URL}/{path}",
status=data["status"],
body=json.dumps(data["body"]),
exception=data.get("exception"),
)
yield PegelOnline(aiohttp.ClientSession())
return mock_pegelonline

return data_to_pegelonline
45 changes: 22 additions & 23 deletions tests/test_aiopegelonline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from aiopegelonline.models import Station, StationMeasurements


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

station = stations["70272185-xxxx-xxxx-xxxx-43bea330dcae"]
Expand Down Expand Up @@ -43,10 +42,10 @@ 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_pegelonline_with_data):
"""Test async_get_nearby_stations."""
stations = await mock_pegelonline.async_get_nearby_stations(13, 51, 25)
api = await mock_pegelonline_with_data()
stations = await api.async_get_nearby_stations(13, 51, 25)
assert len(stations) == 1

station = stations["70272185-xxxx-xxxx-xxxx-43bea330dcae"]
Expand All @@ -64,17 +63,17 @@ 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_pegelonline_with_data):
"""Test async_get_nearby_stations."""
stations = await mock_pegelonline.async_get_nearby_stations(10, 45, 25)
api = await mock_pegelonline_with_data()
stations = await api.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_pegelonline_with_data):
"""Test async_get_station_details."""
station = await mock_pegelonline.async_get_station_details(
api = await mock_pegelonline_with_data()
station = await api.async_get_station_details(
"70272185-xxxx-xxxx-xxxx-43bea330dcae"
)
assert isinstance(station, Station)
Expand All @@ -91,24 +90,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_pegelonline_with_data):
"""Test async_get_station_details with invalid uuid."""
api = await mock_pegelonline_with_data()
with pytest.raises(PegelonlineDataError):
await mock_pegelonline.async_get_station_details("INVALID_UUID")
await api.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_pegelonline_with_data):
"""Test async_get_station_details with connection error."""
api = await mock_pegelonline_with_data()
with pytest.raises(ClientError):
await mock_pegelonline.async_get_station_details("CONNECT_ERROR")
await api.async_get_station_details("CONNECT_ERROR")


@pytest.mark.asyncio
async def test_get_station_measurements(mock_pegelonline):
async def test_get_station_measurements(mock_pegelonline_with_data):
"""Test async_get_station_measurements."""
measurement = await mock_pegelonline.async_get_station_measurements(
api = await mock_pegelonline_with_data()
measurement = await api.async_get_station_measurements(
"915d76e1-xxxx-xxxx-xxxx-4d144cd771cc"
)
assert isinstance(measurement, StationMeasurements)
Expand All @@ -135,7 +134,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 6f8ce4a

Please sign in to comment.