Skip to content

Commit

Permalink
Use test snapshots (#342)
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 Apr 18, 2024
1 parent d79ba75 commit 7237116
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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"))
13 changes: 13 additions & 0 deletions tests/snapshots/test_init.ambr
Original file line number Diff line number Diff line change
@@ -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))
# ---
55 changes: 7 additions & 48 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import aiohttp
import pytest
from aioresponses import aioresponses
from syrupy import SnapshotAssertion

from gios import ApiError, Gios, InvalidSensorsDataError, NoStationError

Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7237116

Please sign in to comment.