diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c64acc2..ccf3a51 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/osservaprezzi/helpers.py b/osservaprezzi/helpers.py index cf02f81..e17f1be 100644 --- a/osservaprezzi/helpers.py +++ b/osservaprezzi/helpers.py @@ -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( @@ -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( diff --git a/tests/__snapshots__/test_helpers.ambr b/tests/__snapshots__/test_helpers.ambr new file mode 100644 index 0000000..5fb527d --- /dev/null +++ b/tests/__snapshots__/test_helpers.ambr @@ -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')]) +# --- diff --git a/tests/__snapshots__/test_stations.ambr b/tests/__snapshots__/test_stations.ambr index 9657987..810f35e 100644 --- a/tests/__snapshots__/test_stations.ambr +++ b/tests/__snapshots__/test_stations.ambr @@ -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([ diff --git a/tests/test_helpers.py b/tests/test_helpers.py new file mode 100644 index 0000000..5c0c90a --- /dev/null +++ b/tests/test_helpers.py @@ -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) diff --git a/tests/test_stations.py b/tests/test_stations.py index 5f8dd61..cbb4b9c 100644 --- a/tests/test_stations.py +++ b/tests/test_stations.py @@ -156,7 +156,7 @@ async def test_stations( "email": "", "website": "", "company": "Ugo Legozzi", - "services": [], + "services": [{"id": 6, "description": "Bancomat"}], }, ), ],