Skip to content

Commit

Permalink
Add helpers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lnx85 committed Jul 3, 2024
1 parent 77653c5 commit dd6a859
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
- id: check-merge-conflict
- id: detect-private-key
- id: no-commit-to-branch
args: [--branch, main, --branch, dev]
args: [--branch, main]
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
Expand Down
34 changes: 17 additions & 17 deletions osservaprezzi/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
from .models import Brand, Fuel, GPSCoordinates, Marker, Service, Station


def gps_coordinates_from_json(json: dict[str, Any]) -> GPSCoordinates:
"""Create a GPS Coordinates from JSON."""
return GPSCoordinates(
latitude=json.get("lat", 0),
longitude=json.get("lng", 0),
)


def marker_from_json(json: dict[str, Any]) -> Marker:
"""Create a marker from JSON."""
return Marker(
type=json.get("tipoFile", ""),
extension=json.get("estensione", ""),
content=json.get("content", ""),
)


def brand_from_json(json: dict[str, Any]) -> Brand:
"""Create a brand from JSON."""
return Brand(
Expand All @@ -51,6 +34,23 @@ def fuel_from_json(json: dict[str, Any]) -> Fuel:
)


def gps_coordinates_from_json(json: dict[str, Any]) -> GPSCoordinates:
"""Create a GPS Coordinates from JSON."""
return GPSCoordinates(
latitude=json.get("lat", 0),
longitude=json.get("lng", 0),
)


def marker_from_json(json: dict[str, Any]) -> Marker:
"""Create a marker from JSON."""
return Marker(
type=json.get("tipoFile", ""),
extension=json.get("estensione", ""),
content=json.get("content", ""),
)


def service_from_json(json: dict[str, Any]) -> Service:
"""Create a service from JSON."""
return Service(
Expand Down
40 changes: 40 additions & 0 deletions tests/__snapshots__/test_helpers.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# serializer version: 1
# name: test_brand_from_json[json0][brand_from_json]
Brand(id=1, name='Brand #1', markers=[])
# ---
# name: test_brand_from_json[json1][brand_from_json]
Brand(id=2, name='Brand #2', markers=[Marker(type='logo', extension='png', content='iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==')])
# ---
# name: test_fuel_from_json[json0][fuel_from_json]
Fuel(id='1-x', price=0, name='Benzina', fuel_id=0, is_self=False, insert_date=None, validity_date=None)
# ---
# name: test_fuel_from_json[json1][fuel_from_json]
Fuel(id=75557219, price=2.079, name='Benzina', fuel_id=1, is_self=False, insert_date=None, validity_date=None)
# ---
# name: test_fuel_from_json[json2][fuel_from_json]
Fuel(id=75557219, price=2.079, name='Benzina', fuel_id=1, is_self=False, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc))
# ---
# name: test_gps_coordinates_from_json[json0][gps_coordinates_from_json]
GPSCoordinates(latitude=0, longitude=0)
# ---
# name: test_gps_coordinates_from_json[json1][gps_coordinates_from_json]
GPSCoordinates(latitude=45.541553, longitude=10.211802)
# ---
# name: test_marker_from_json[json0][marker_from_json]
Marker(type='logo', extension='png', content='iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==')
# ---
# name: test_service_from_json[json0][service_from_json]
Service(id=1, description='Dummy Service')
# ---
# name: test_service_from_json[json1][service_from_json]
Service(id=6, description='Bancomat')
# ---
# name: test_station_from_json[json0][station_from_json]
Station(id=1, name='Service Area 1', description='', fuels=[Fuel(id=10001, price=2.079, name='Benzina', fuel_id=1, is_self=False, insert_date=None, validity_date=None), Fuel(id=10002, price=1.869, name='Benzina', fuel_id=1, is_self=True, insert_date=None, validity_date=None), Fuel(id=10003, price=1.989, name='Gasolio', fuel_id=2, is_self=False, insert_date=None, validity_date=None), Fuel(id=10004, price=1.779, name='Gasolio', fuel_id=2, is_self=True, insert_date=None, validity_date=None)], location=GPSCoordinates(latitude=1.5, longitude=5.9), insert_date=datetime.datetime(1985, 5, 1, 18, 20, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), address='This is the service area address', brand='Octan', email=None, website=None, phone=None, company=None, services=[])
# ---
# name: test_station_from_json[json1][station_from_json]
Station(id=2, name='Service Area 2', description='', fuels=[Fuel(id=20001, price=1.839, name='Benzina', fuel_id=1, is_self=True, insert_date=None, validity_date=None), Fuel(id=20002, price=1.709, name='Gasolio', fuel_id=2, is_self=True, insert_date=None, validity_date=None)], location=GPSCoordinates(latitude=1.6, longitude=4.7), insert_date=datetime.datetime(1985, 9, 5, 11, 56, 40, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), address=None, brand='Octan', email=None, website=None, phone=None, company=None, services=[])
# ---
# name: test_station_from_json[json2][station_from_json]
Station(id=1, name='Octan Service Area', description='Octan Service Area', fuels=[Fuel(id=20001, price=1.839, name='Benzina', fuel_id=1, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc)), Fuel(id=20002, price=1.709, name='Gasolio', fuel_id=2, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc))], location=GPSCoordinates(latitude=0, longitude=0), insert_date=None, address='Service Area Address', brand='Octan', email='', website='', phone='', company='Ugo Legozzi', services=[Service(id=6, description='Bancomat')])
# ---
2 changes: 1 addition & 1 deletion tests/__snapshots__/test_stations.ambr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# serializer version: 1
# name: test_single_station[1-response0][station_1]
Station(id=1, name='Octan Service Area', description='Octan Service Area', fuels=[Fuel(id=20001, price=1.839, name='Benzina', fuel_id=1, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc)), Fuel(id=20002, price=1.709, name='Gasolio', fuel_id=2, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc))], location=GPSCoordinates(latitude=0, longitude=0), insert_date=None, address='Service Area Address', brand='Octan', email='', website='', phone='', company='Ugo Legozzi', services=[])
Station(id=1, name='Octan Service Area', description='Octan Service Area', fuels=[Fuel(id=20001, price=1.839, name='Benzina', fuel_id=1, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc)), Fuel(id=20002, price=1.709, name='Gasolio', fuel_id=2, is_self=True, insert_date=datetime.datetime(2024, 7, 2, 12, 0, 38, tzinfo=datetime.timezone.utc), validity_date=datetime.datetime(2024, 7, 2, 11, 59, tzinfo=datetime.timezone.utc))], location=GPSCoordinates(latitude=0, longitude=0), insert_date=None, address='Service Area Address', brand='Octan', email='', website='', phone='', company='Ugo Legozzi', services=[Service(id=6, description='Bancomat')])
# ---
# name: test_stations[test0-response0][stations]
list([
Expand Down
219 changes: 219 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any

import pytest

from osservaprezzi.helpers import (
brand_from_json,
fuel_from_json,
gps_coordinates_from_json,
marker_from_json,
service_from_json,
station_from_json,
)

if TYPE_CHECKING:
from syrupy.assertion import SnapshotAssertion


@pytest.mark.parametrize(
"json",
[
{"bandieraId": 1, "bandiera": "Brand #1"},
{
"bandieraId": 2,
"bandiera": "Brand #2",
"logoMarkerList": [
{
"tipoFile": "logo",
"estensione": "png",
"content": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
}
],
},
],
)
async def test_brand_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="brand_from_json") == brand_from_json(json)


@pytest.mark.parametrize(
"json",
[
{"id": "1-x", "description": "Benzina"},
{
"id": 75557219,
"price": 2.079,
"name": "Benzina",
"fuelId": 1,
"isSelf": False,
},
{
"id": 75557219,
"price": 2.079,
"name": "Benzina",
"fuelId": 1,
"isSelf": False,
"insertDate": "2024-07-02T12:00:38Z",
"validityDate": "2024-07-02T11:59:00Z",
},
],
)
async def test_fuel_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="fuel_from_json") == fuel_from_json(json)


@pytest.mark.parametrize(
"json",
[{"lat": 0, "lng": 0}, {"lat": 45.541553, "lng": 10.211802}],
)
async def test_gps_coordinates_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="gps_coordinates_from_json") == gps_coordinates_from_json(json)


@pytest.mark.parametrize(
"json",
[
{
"tipoFile": "logo",
"estensione": "png",
"content": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
}
],
)
async def test_marker_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="marker_from_json") == marker_from_json(json)


@pytest.mark.parametrize(
"json",
[{"id": 1, "description": "Dummy Service"}, {"id": 6, "description": "Bancomat"}],
)
async def test_service_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="service_from_json") == service_from_json(json)


@pytest.mark.parametrize(
"json",
[
{
"id": 1,
"name": "Service Area 1",
"fuels": [
{
"id": 10001,
"price": 2.079,
"name": "Benzina",
"fuelId": 1,
"isSelf": False,
},
{
"id": 10002,
"price": 1.869,
"name": "Benzina",
"fuelId": 1,
"isSelf": True,
},
{
"id": 10003,
"price": 1.989,
"name": "Gasolio",
"fuelId": 2,
"isSelf": False,
},
{
"id": 10004,
"price": 1.779,
"name": "Gasolio",
"fuelId": 2,
"isSelf": True,
},
],
"location": {
"lat": 1.5,
"lng": 5.9,
},
"insertDate": "1985-05-01T18:20:00+01:00",
"address": "This is the service area address",
"brand": "Octan",
},
{
"id": 2,
"name": "Service Area 2",
"fuels": [
{
"id": 20001,
"price": 1.839,
"name": "Benzina",
"fuelId": 1,
"isSelf": True,
},
{
"id": 20002,
"price": 1.709,
"name": "Gasolio",
"fuelId": 2,
"isSelf": True,
},
],
"location": {"lat": 1.6, "lng": 4.7},
"insertDate": "1985-09-05T11:56:40+01:00",
"brand": "Octan",
},
{
"id": 1,
"name": "Octan Service Area",
"nomeImpianto": "Octan Service Area",
"address": "Service Area Address",
"brand": "Octan",
"fuels": [
{
"id": 20001,
"price": 1.839,
"name": "Benzina",
"fuelId": 1,
"isSelf": True,
"serviceAreaId": 1,
"insertDate": "2024-07-02T12:00:38Z",
"validityDate": "2024-07-02T11:59:00Z",
},
{
"id": 20002,
"price": 1.709,
"name": "Gasolio",
"fuelId": 2,
"isSelf": True,
"serviceAreaId": 1,
"insertDate": "2024-07-02T12:00:38Z",
"validityDate": "2024-07-02T11:59:00Z",
},
],
"phoneNumber": "",
"email": "",
"website": "",
"company": "Ugo Legozzi",
"services": [{"id": 6, "description": "Bancomat"}],
},
],
)
async def test_station_from_json(
snapshot: SnapshotAssertion,
json: dict[str, Any],
) -> None:
assert snapshot(name="station_from_json") == station_from_json(json)
2 changes: 1 addition & 1 deletion tests/test_stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async def test_stations(
"email": "",
"website": "",
"company": "Ugo Legozzi",
"services": [],
"services": [{"id": 6, "description": "Bancomat"}],
},
),
],
Expand Down

0 comments on commit dd6a859

Please sign in to comment.