Skip to content

Commit

Permalink
Improve tests with syrupy and fixture (#466)
Browse files Browse the repository at this point in the history
* Add syrupy package to Poetry

* Add fixture and update the tests for syrupy

* Update packages

* Update garages and disabled parkings fixtures + tests

* Revert the downgrade of ruff
  • Loading branch information
klaasnicolaas committed Apr 20, 2024
1 parent e85cfe1 commit c02596c
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 869 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pip install liege

You can read the following datasets with this package:

- [Disabled parking spaces / Stationnement PMR][disabled_parking] (952 locations)
- [Garages / Les parkings voitures hors voirie][garages] (26 locations)
- [Disabled parking spaces / Stationnement PMR][disabled_parking] (1062 locations)
- [Garages / Les parkings voitures hors voirie][garages] (32 locations)

<details>
<summary>Click here to get more details</summary>
Expand Down Expand Up @@ -174,6 +174,12 @@ To run just the Python tests:
poetry run pytest
```

To update the [syrupy](https://github.com/tophat/syrupy) snapshot tests:

```bash
poetry run pytest --snapshot-update
```

## License

MIT License
Expand Down
2 changes: 1 addition & 1 deletion examples/disabled_parkings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
async def main() -> None:
"""Get the disabled parkings data from Liège API."""
async with ODPLiege() as client:
disabled_parkings = await client.disabled_parkings(limit=5)
disabled_parkings = await client.disabled_parkings(limit=10)

count: int = len(disabled_parkings)
for item in disabled_parkings:
Expand Down
2 changes: 1 addition & 1 deletion examples/garages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
async def main() -> None:
"""Get the garages data from Liège API."""
async with ODPLiege() as client:
garages = await client.garages(limit=12)
garages = await client.garages(limit=10)

count: int = len(garages)
for item in garages:
Expand Down
39 changes: 21 additions & 18 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pytest = "8.1.1"
pytest-asyncio = "0.23.6"
pytest-cov = "5.0.0"
ruff = "0.4.1"
syrupy = "4.6.1"
yamllint = "1.35.1"

[tool.coverage.run]
Expand Down
38 changes: 13 additions & 25 deletions src/liege/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ def from_dict(cls: type[Garage], data: dict[str, Any]) -> Garage:
url=attr.get("website"),
longitude=geo[0],
latitude=geo[1],
created_at=strptime(attr.get("created"), "%Y-%m-%d"),
updated_at=strptime(attr.get("last_modified"), "%Y-%m-%d"),
created_at=datetime.strptime(attr.get("created"), "%Y-%m-%d").replace(
tzinfo=UTC
),
updated_at=datetime.strptime(attr.get("last_modified"), "%Y-%m-%d").replace(
tzinfo=UTC
),
)


Expand All @@ -77,7 +81,7 @@ class DisabledParking:
longitude: float
latitude: float

created_at: datetime | None
created_at: datetime
updated_at: datetime

@classmethod
Expand Down Expand Up @@ -108,31 +112,15 @@ def from_dict(cls: type[DisabledParking], data: dict[str, Any]) -> DisabledParki
status=attr.get("status"),
longitude=geo[0],
latitude=geo[1],
created_at=strptime(attr.get("created"), "%Y-%m-%d"),
updated_at=strptime(attr.get("last_modified"), "%Y-%m-%d"),
created_at=datetime.strptime(attr.get("created"), "%Y-%m-%d").replace(
tzinfo=UTC
),
updated_at=datetime.strptime(attr.get("last_modified"), "%Y-%m-%d").replace(
tzinfo=UTC
),
)


def strptime(date_string: str, date_format: str, default: None = None) -> Any:
"""Strptime function with default value.
Args:
----
date_string: The date string.
date_format: The format of the date string.
default: The default value.
Returns:
-------
The datetime object.
"""
try:
return datetime.strptime(date_string, date_format).replace(tzinfo=UTC)
except (ValueError, TypeError):
return default


def set_address(street: str, number: str, postal_code: str) -> str:
"""Set the address.
Expand Down
29 changes: 29 additions & 0 deletions tests/__snapshots__/test_models.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# serializer version: 1
# name: test_all_garages
list([
Garage(name='Parking Natalis', capacity=16, charging_stations=0, address='rue Natalis 49, 4020', municipality='Liège', city='Liège', provider='Bepark', schedule='sur abonnement ou réservation 24h / 7j', url='https://www.bepark.eu/fr/parking/wallonie/liege', longitude=5.5826088906, latitude=50.6301695672, created_at=datetime.datetime(2024, 3, 5, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 5, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Saint-Denis', capacity=748, charging_stations=0, address='rue Lambert Lombard 5, 4000', municipality='Liège', city='Liège', provider='Saint-Denis', schedule='24h/24, 7j/7.', url='http://parkingstdenisliege.be', longitude=5.574644137, latitude=50.6435348112, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 4, 5, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Aquarium', capacity=70, charging_stations=0, address='rue Méan 26, 4020', municipality='Liège', city='Liège', provider='illico-park', schedule='24h/24, 7j/7.', url='http://www.illico-park.be', longitude=5.5800306678, latitude=50.6376851177, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 28, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Saint-Lambert', capacity=500, charging_stations=0, address='place Saint-Lambert 41, 4000', municipality='Liège', city='Liège', provider='mypark', schedule='7j/7 - 24h/24', url='http://www.mypark.be', longitude=5.5727508764, latitude=50.6445693701, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 4, 4, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Carré-Jonfosse', capacity=156, charging_stations=2, address='rue Jonfosse 62, 4000', municipality='Liège', city='Liège', provider='mypark', schedule='24h/24, 7j/7.', url='http://www.mypark.be', longitude=5.5630714447, latitude=50.6401206676, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 4, 4, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Sauvenière', capacity=364, charging_stations=0, address='boulevard de la Sauvenière 102, 4000', municipality='Liège', city='Liège', provider='Indigo', schedule='24h/24 - 7j/7', url='https://www.indigoneo.be/fr/parkings/26716/parking-neujean', longitude=5.5653716221, latitude=50.6428485702, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 28, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Saint-Paul', capacity=400, charging_stations=0, address='place Saint-Paul 3, 4000', municipality='Liège', city='Liège', provider='Saint-Paul', schedule='24h/24, 7j/7.', url='http://www.parkingstpaul.be', longitude=5.5713436408, latitude=50.6395769799, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 29, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Kennedy', capacity=66, charging_stations=0, address='rue André Dumont None, 4000', municipality='Liège', city='Liège', provider='UHODA', schedule='24h/24, 7j/7.', url='https://uhoda.com/', longitude=5.5754324031, latitude=50.6382306915, created_at=datetime.datetime(2020, 9, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 28, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Parking Sauvenière', capacity=18, charging_stations=0, address='boulevard de la Sauvenière 100, 4000', municipality='Liège', city='Liège', provider='Bepark', schedule='24h / 7j', url='https://www.bepark.eu/fr/parking/wallonie/liege', longitude=5.5654035149, latitude=50.6429408518, created_at=datetime.datetime(2022, 3, 17, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 3, 5, 0, 0, tzinfo=datetime.timezone.utc)),
Garage(name='Central Park', capacity=167, charging_stations=4, address='boulevard de la Sauvenière 37, 4000', municipality='Liège', city='Liège', provider='Central Park', schedule='Du lundi au mercredi de 8h00 à 1h00 / en continu du jeudi au lundi de 8h à 1h00.', url='https://parkingcentral-park.be/', longitude=5.5676012903, latitude=50.6436758876, created_at=datetime.datetime(2024, 3, 28, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2024, 4, 8, 0, 0, tzinfo=datetime.timezone.utc)),
])
# ---
# name: test_disabled_parkings
list([
DisabledParking(spot_id='cba258ed55c9c207f5bd4b9db97053be72f99eb7', number=1, address='quai Saint-Paul de Sinçay 103, 4031', municipality='Liège', city='Liège', status='Existant', longitude=5.6051201, latitude=50.602958578, created_at=datetime.datetime(2021, 11, 26, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 11, 26, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='f532805125b393e03e1feffb45c5695a9370a4b3', number=1, address='rue des Vennes 112, 4020', municipality='Liège', city='Liège', status='Existant', longitude=5.584375254, latitude=50.6273122047, created_at=datetime.datetime(2021, 3, 31, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2022, 2, 7, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='a5c82752dd23cfc9ecb1db2db496f964a59a3b82', number=1, address='avenue Freddy Terwagne 3, 4030', municipality='Liège', city='Liège', status='Existant', longitude=5.6285546432, latitude=50.6302120608, created_at=datetime.datetime(2021, 3, 27, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 3, 27, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='af10b2a70ad90340e5ade2ff9bef10e2344a94c6', number=1, address='avenue Freddy Terwagne 6, 4030', municipality='Liège', city='Liège', status='Existant', longitude=5.6296538896, latitude=50.6295297953, created_at=datetime.datetime(2021, 3, 26, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 3, 26, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='a48ff4080ce9bf776c64347026e979980b7aa708', number=1, address='avenue Rogier 18, 4000', municipality='Liège', city='Liège', status='Existant', longitude=5.5696178047, latitude=50.6318805217, created_at=datetime.datetime(2021, 3, 27, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 3, 27, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='90c7723fdd27a793bae73e9bac313d3877d646d4', number=1, address='Montagne Sainte-Walburge 43, 4000', municipality='Liège', city='Liège', status='Existant', longitude=5.5677519905, latitude=50.6487827409, created_at=datetime.datetime(2021, 3, 26, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 3, 26, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='e45cecc773093bb0196de7a0d109d2ae1609503a', number=1, address='place des Abeilles 6, 4020', municipality='Liège', city='Liège', status='Existant', longitude=5.6330619893, latitude=50.6434944053, created_at=datetime.datetime(2021, 3, 25, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 3, 25, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='0603bc39c5b386319c3b87da26f7bd2ee224ae37', number=1, address='avenue Joseph Merlot 152, 4020', municipality='Liège', city='Liège', status='Existant', longitude=5.6046285271, latitude=50.6371022363, created_at=datetime.datetime(2022, 4, 28, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2022, 8, 19, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='dcf2ee7c7db2189fd6b62ee529c686a87c9e9351', number=1, address='quai de Rome 6, 4000', municipality='Liège', city='Liège', status='Existant', longitude=5.5734664, latitude=50.6268205322, created_at=datetime.datetime(2021, 3, 27, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2022, 9, 2, 0, 0, tzinfo=datetime.timezone.utc)),
DisabledParking(spot_id='672c079710eff02d9b4b08fba7057825ed643d20', number=1, address='quai Paul Van Hoegaerden 2, 4000', municipality='Liège', city='Liège', status='Existant', longitude=5.5759121688, latitude=50.6386193622, created_at=datetime.datetime(2013, 5, 30, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2021, 9, 2, 0, 0, tzinfo=datetime.timezone.utc)),
])
# ---
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Fixtures for the Liege ODP tests."""

from collections.abc import AsyncGenerator

import pytest
from aiohttp import ClientSession

from liege import ODPLiege


@pytest.fixture(name="odp_liege_client")
async def client() -> AsyncGenerator[ODPLiege, None]:
"""Fixture to create an ODPLiege client."""
async with (
ClientSession() as session,
ODPLiege(session=session) as odp_liege_client,
):
yield odp_liege_client
Loading

0 comments on commit c02596c

Please sign in to comment.