From 7237116eb3b0d55de2005cbc44d0e069a8746f44 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Thu, 18 Apr 2024 21:57:11 +0200 Subject: [PATCH] Use test snapshots (#342) Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com> --- pyproject.toml | 3 +- requirements-test.txt | 1 + tests/conftest.py | 24 +++++++++++++++ tests/snapshots/test_init.ambr | 13 ++++++++ tests/test_init.py | 55 +++++----------------------------- 5 files changed, 47 insertions(+), 49 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/snapshots/test_init.ambr diff --git a/pyproject.toml b/pyproject.toml index 4fd2b55..813be7c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,8 @@ lint.ignore = [ [tool.ruff.lint.per-file-ignores] "tests/*" = [ - "ASYNC101", # Async functions should not call `open`, `time.sleep`, or `subprocess` methods + "ANN102", # Missing type annotation for `cls` in classmethod + "ASYNC101", # Async functions should not call `open`, `time.sleep`, or `subprocess` methods "PLR2004", # Magic value used in comparison "PTH123", # `open()` should be replaced by `Path.open()` "S101", # Use of `assert` detected diff --git a/requirements-test.txt b/requirements-test.txt index f0bec92..e64ee18 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -7,3 +7,4 @@ pytest-error-for-skips==2.0.2 pytest-timeout==2.3.1 pytest==8.1.1 ruff==0.3.7 +syrupy==4.6.1 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..acba03b --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,24 @@ +"""Set up some common test helper things.""" + +from pathlib import Path + +import pytest +from syrupy.assertion import SnapshotAssertion +from syrupy.extensions.amber import AmberSnapshotExtension +from syrupy.location import PyTestLocation + + +@pytest.fixture() +def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion: + """Return snapshot assertion fixture.""" + return snapshot.use_extension(SnapshotExtension) + + +class SnapshotExtension(AmberSnapshotExtension): + """Extension for Syrupy.""" + + @classmethod + def dirname(cls, *, test_location: PyTestLocation) -> str: + """Return the directory for the snapshot files.""" + test_dir = Path(test_location.filepath).parent + return str(test_dir.joinpath("snapshots")) diff --git a/tests/snapshots/test_init.ambr b/tests/snapshots/test_init.ambr new file mode 100644 index 0000000..878cc68 --- /dev/null +++ b/tests/snapshots/test_init.ambr @@ -0,0 +1,13 @@ +# serializer version: 1 +# name: test_no_indexes_data + GiosSensors(aqi=None, c6h6=Sensor(name='benzene', id=658, index=None, value=2.57148), co=Sensor(name='carbon monoxide', id=660, index=None, value=786.702), no2=Sensor(name='nitrogen dioxide', id=665, index=None, value=59.9545), o3=Sensor(name='ozone', id=667, index=None, value=8.63111), pm10=Sensor(name='particulate matter 10', id=14395, index=None, value=123.879), pm25=Sensor(name='particulate matter 2.5', id=670, index=None, value=59.9428), so2=Sensor(name='sulfur dioxide', id=672, index=None, value=11.6502)) +# --- +# name: test_valid_data_first_value + GiosSensors(aqi=Sensor(name='AQI', id=None, index=None, value='good'), c6h6=Sensor(name='benzene', id=658, index='very_good', value=2.57148), co=Sensor(name='carbon monoxide', id=660, index='very_bad', value=786.702), no2=Sensor(name='nitrogen dioxide', id=665, index='very_good', value=59.9545), o3=Sensor(name='ozone', id=667, index='bad', value=8.63111), pm10=Sensor(name='particulate matter 10', id=14395, index='moderate', value=123.879), pm25=Sensor(name='particulate matter 2.5', id=670, index='sufficient', value=59.9428), so2=Sensor(name='sulfur dioxide', id=672, index='very_good', value=11.6502)) +# --- +# name: test_valid_data_first_value.1 + GiosSensors(aqi=Sensor(name='AQI', id=None, index=None, value='good'), c6h6=Sensor(name='benzene', id=658, index='very_good', value=2.57148), co=Sensor(name='carbon monoxide', id=660, index='very_bad', value=786.702), no2=Sensor(name='nitrogen dioxide', id=665, index='very_good', value=59.9545), o3=Sensor(name='ozone', id=667, index='bad', value=8.63111), pm10=Sensor(name='particulate matter 10', id=14395, index='moderate', value=123.879), pm25=Sensor(name='particulate matter 2.5', id=670, index='sufficient', value=59.9428), so2=Sensor(name='sulfur dioxide', id=672, index='very_good', value=11.6502)) +# --- +# name: test_valid_data_second_value + GiosSensors(aqi=Sensor(name='AQI', id=None, index=None, value='good'), c6h6=Sensor(name='benzene', id=658, index='very_good', value=3.24432), co=Sensor(name='carbon monoxide', id=660, index='very_bad', value=1041.74), no2=Sensor(name='nitrogen dioxide', id=665, index='very_good', value=52.6198), o3=Sensor(name='ozone', id=667, index='bad', value=4.93778), pm10=Sensor(name='particulate matter 10', id=14395, index='moderate', value=115.559), pm25=Sensor(name='particulate matter 2.5', id=670, index='sufficient', value=72.0243), so2=Sensor(name='sulfur dioxide', id=672, index='very_good', value=11.501)) +# --- diff --git a/tests/test_init.py b/tests/test_init.py index 2e1cc0c..42f1368 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -5,6 +5,7 @@ import aiohttp import pytest from aioresponses import aioresponses +from syrupy import SnapshotAssertion from gios import ApiError, Gios, InvalidSensorsDataError, NoStationError @@ -17,7 +18,7 @@ @pytest.mark.asyncio() -async def test_valid_data_first_value() -> None: +async def test_valid_data_first_value(snapshot: SnapshotAssertion) -> None: """Test with valid data and valid first sensor's value.""" with open("tests/fixtures/stations.json", encoding="utf-8") as file: stations = json.load(file) @@ -93,21 +94,7 @@ async def test_valid_data_first_value() -> None: assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE - assert data.so2.value == 11.6502 - assert data.so2.index == "very_good" - assert data.c6h6.value == 2.57148 - assert data.c6h6.index == "very_good" - assert data.co.value == 786.702 - assert data.co.index == "very_bad" - assert data.no2.value == 59.9545 - assert data.no2.index == "very_good" - assert data.o3.value == 8.63111 - assert data.o3.index == "bad" - assert data.pm25.value == 59.9428 - assert data.pm25.index == "sufficient" - assert data.pm10.value == 123.879 - assert data.pm10.index == "moderate" - assert data.aqi.value == "good" + assert data == snapshot @pytest.mark.asyncio() @@ -131,7 +118,7 @@ async def test_api_error() -> None: @pytest.mark.asyncio() -async def test_valid_data_second_value() -> None: +async def test_valid_data_second_value(snapshot: SnapshotAssertion) -> None: """Test with valid data and valid second sensor's value.""" with open("tests/fixtures/stations.json", encoding="utf-8") as file: stations = json.load(file) @@ -215,25 +202,11 @@ async def test_valid_data_second_value() -> None: assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE - assert data.so2.value == 11.501 - assert data.so2.index == "very_good" - assert data.c6h6.value == 3.24432 - assert data.c6h6.index == "very_good" - assert data.co.value == 1041.74 - assert data.co.index == "very_bad" - assert data.no2.value == 52.6198 - assert data.no2.index == "very_good" - assert data.o3.value == 4.93778 - assert data.o3.index == "bad" - assert data.pm25.value == 72.0243 - assert data.pm25.index == "sufficient" - assert data.pm10.value == 115.559 - assert data.pm10.index == "moderate" - assert data.aqi.value == "good" + assert data == snapshot @pytest.mark.asyncio() -async def test_no_indexes_data() -> None: +async def test_no_indexes_data(snapshot: SnapshotAssertion) -> None: """Test with valid data.""" with open("tests/fixtures/stations.json", encoding="utf-8") as file: stations = json.load(file) @@ -307,21 +280,7 @@ async def test_no_indexes_data() -> None: assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE - assert data.so2.value == 11.6502 - assert data.so2.index is None - assert data.c6h6.value == 2.57148 - assert data.c6h6.index is None - assert data.co.value == 786.702 - assert data.co.index is None - assert data.no2.value == 59.9545 - assert data.no2.index is None - assert data.o3.value == 8.63111 - assert data.o3.index is None - assert data.pm25.value == 59.9428 - assert data.pm25.index is None - assert data.pm10.value == 123.879 - assert data.pm10.index is None - assert data.aqi is None + assert data == snapshot @pytest.mark.asyncio()