From 14d24c5a1daf0a2b29c9a2e756e3cbbee1ff427a Mon Sep 17 00:00:00 2001 From: Jonathan Perron Date: Mon, 20 May 2024 08:16:31 +0200 Subject: [PATCH] fix(refactoring): remove refactoring when calling APIs Fix #41 --- navitia_client/client/apis/arrival_apis.py | 51 ++++--- navitia_client/client/apis/departure_apis.py | 50 ++++--- .../client/apis/places_nearby_apis.py | 139 +++++++++++++++--- .../client/apis/route_schedules_apis.py | 56 ++++--- .../client/apis/stop_schedules_apis.py | 52 ++++--- .../client/apis/terminus_schedules_apis.py | 52 ++++--- tests/client/apis/test_arrival_apis.py | 23 +-- tests/client/apis/test_departure_apis.py | 23 +-- tests/client/apis/test_places_nearby_apis.py | 127 +--------------- .../client/apis/test_route_schedules_apis.py | 23 +-- tests/client/apis/test_stop_schedules_apis.py | 23 +-- .../apis/test_terminus_schedules_apis.py | 27 ++-- tests/test_data/places_nearby.json | 110 ++++++++++++++ 13 files changed, 470 insertions(+), 286 deletions(-) create mode 100644 tests/test_data/places_nearby.json diff --git a/navitia_client/client/apis/arrival_apis.py b/navitia_client/client/apis/arrival_apis.py index dac2d9a..7600d20 100644 --- a/navitia_client/client/apis/arrival_apis.py +++ b/navitia_client/client/apis/arrival_apis.py @@ -30,14 +30,10 @@ def _get_departures( pagination = Pagination.from_payload(results.json()["pagination"]) return self._get_departure_objects_from_response(raw_results), pagination - def list_arrivals( + def list_arrivals_by_region_id_and_path( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_id: str, + resource_path: str, from_datetime: datetime = datetime.now(), duration: int = 86400, depth: int = 1, @@ -46,19 +42,36 @@ def list_arrivals( disable_geojson: bool = False, direction_type: str = "all", ) -> Tuple[Sequence[Arrival], Pagination]: - request_url: str | None = None - if region_id and resource_path: - # See https://doc.navitia.io/#route-schedules for URL description - # List of route schedules near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - elif all([region_lon, region_lat, lon, lat]): - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - if not request_url: - raise ValueError( - "Region id and resource path or region coordinates and coordinates must be provided." - ) + filters = { + "from_datetime": from_datetime, + "duration": duration, + "depth": depth, + "disable_geojson": disable_geojson, + "forbidden_uris[]": forbidden_uris, + "data_freshness": data_freshness, + "direction_type": direction_type, + } + + return self._get_departures(request_url, filters) + + def list_arrivals_by_coordinates( + self, + region_lon: float, + region_lat: float, + lon: float, + lat: float, + from_datetime: datetime = datetime.now(), + duration: int = 86400, + depth: int = 1, + forbidden_uris: Optional[Sequence[str]] = None, + data_freshness: str = "realtime", + disable_geojson: bool = False, + direction_type: str = "all", + ) -> Tuple[Sequence[Arrival], Pagination]: + # List of objects near the resource, navitia guesses the region from coordinates + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" filters = { "from_datetime": from_datetime, diff --git a/navitia_client/client/apis/departure_apis.py b/navitia_client/client/apis/departure_apis.py index fa0e665..5be6cbb 100644 --- a/navitia_client/client/apis/departure_apis.py +++ b/navitia_client/client/apis/departure_apis.py @@ -30,14 +30,10 @@ def _get_departures( pagination = Pagination.from_payload(results.json()["pagination"]) return self._get_departure_objects_from_response(raw_results), pagination - def list_departures( + def list_departures_by_region_id_and_path( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_id: str, + resource_path: str, from_datetime: datetime = datetime.now(), duration: int = 86400, depth: int = 1, @@ -46,19 +42,35 @@ def list_departures( disable_geojson: bool = False, direction_type: str = "all", ) -> Tuple[Sequence[Departure], Pagination]: - request_url: str | None = None - if region_id and resource_path: - # See https://doc.navitia.io/#route-schedules for URL description - # List of route schedules near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - elif all([region_lon, region_lat, lon, lat]): - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - if not request_url: - raise ValueError( - "Region id and resource path or region coordinates and coordinates must be provided." - ) + filters = { + "from_datetime": from_datetime, + "duration": duration, + "depth": depth, + "disable_geojson": disable_geojson, + "forbidden_uris[]": forbidden_uris, + "data_freshness": data_freshness, + "direction_type": direction_type, + } + + return self._get_departures(request_url, filters) + + def list_departures_by_coordinates( + self, + region_lon: float, + region_lat: float, + lon: float, + lat: float, + from_datetime: datetime = datetime.now(), + duration: int = 86400, + depth: int = 1, + forbidden_uris: Optional[Sequence[str]] = None, + data_freshness: str = "realtime", + disable_geojson: bool = False, + direction_type: str = "all", + ) -> Tuple[Sequence[Departure], Pagination]: + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" filters = { "from_datetime": from_datetime, diff --git a/navitia_client/client/apis/places_nearby_apis.py b/navitia_client/client/apis/places_nearby_apis.py index cf02a8b..1410dc1 100644 --- a/navitia_client/client/apis/places_nearby_apis.py +++ b/navitia_client/client/apis/places_nearby_apis.py @@ -27,14 +27,10 @@ def _get_places_nearby( pagination = Pagination.from_payload(results.json()["pagination"]) return self._get_pt_objects_from_response(raw_results), pagination - def list_objects( + def list_objects_by_region_id_and_path( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_id: str, + resource_path: str, distance: int = 500, type: Sequence[str] = ["stop_area", "stop_point", "poi"], admin_uri: Optional[Sequence[str]] = None, @@ -46,21 +42,120 @@ def list_objects( count: int = 25, add_poi_infos: Sequence[str] = ["bss_stands", "car_park"], ) -> Tuple[Sequence[Place], Pagination]: - request_url: str | None = None - if region_id: - # See https://doc.navitia.io/#places-nearby-api for URL description - if resource_path: - # List of objects near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/places_nearby" - elif lon and lat: - # List of objects near a coordinate - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{lon};{lat}/places_nearby" - elif region_lon and region_lat and lon and lat: - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/places_nearby" - elif lon and lat and not any([region_id, region_lat, region_lon]): - # List of objects near the resource without any region id (same result as above) - request_url = f"{self.base_navitia_url}/coverage/{lon};{lat}/places_nearby" + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/places_nearby" + + filters = { + "start_page": start_page, + "count": count, + "depth": depth, + "type[]": type, + "distance": distance, + "disable_geojson": disable_geojson, + "disable_disruption": disable_disruption, + "add_poi_infos[]": add_poi_infos, + } + + if admin_uri: + filters["admin_uris[]"] = admin_uri + + if filter: + filters["filter"] = filter + + return self._get_places_nearby(request_url, filters) + + def list_objects_by_region_id_and_coordinates( + self, + region_id: str, + lon: float, + lat: float, + distance: int = 500, + type: Sequence[str] = ["stop_area", "stop_point", "poi"], + admin_uri: Optional[Sequence[str]] = None, + filter: Optional[str] = None, + disable_geojson: bool = False, + disable_disruption: bool = False, + depth: int = 1, + start_page: int = 0, + count: int = 25, + add_poi_infos: Sequence[str] = ["bss_stands", "car_park"], + ) -> Tuple[Sequence[Place], Pagination]: + request_url = ( + f"{self.base_navitia_url}/coverage/{region_id}/{lon};{lat}/places_nearby" + ) + + filters = { + "start_page": start_page, + "count": count, + "depth": depth, + "type[]": type, + "distance": distance, + "disable_geojson": disable_geojson, + "disable_disruption": disable_disruption, + "add_poi_infos[]": add_poi_infos, + } + + if admin_uri: + filters["admin_uris[]"] = admin_uri + + if filter: + filters["filter"] = filter + + return self._get_places_nearby(request_url, filters) + + def list_objects_by_coordinates( + self, + region_lon: float, + region_lat: float, + lon: float, + lat: float, + distance: int = 500, + type: Sequence[str] = ["stop_area", "stop_point", "poi"], + admin_uri: Optional[Sequence[str]] = None, + filter: Optional[str] = None, + disable_geojson: bool = False, + disable_disruption: bool = False, + depth: int = 1, + start_page: int = 0, + count: int = 25, + add_poi_infos: Sequence[str] = ["bss_stands", "car_park"], + ) -> Tuple[Sequence[Place], Pagination]: + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/places_nearby" + + filters = { + "start_page": start_page, + "count": count, + "depth": depth, + "type[]": type, + "distance": distance, + "disable_geojson": disable_geojson, + "disable_disruption": disable_disruption, + "add_poi_infos[]": add_poi_infos, + } + + if admin_uri: + filters["admin_uris[]"] = admin_uri + + if filter: + filters["filter"] = filter + + return self._get_places_nearby(request_url, filters) + + def list_objects_by_object_coordinates_only( + self, + lon: float, + lat: float, + distance: int = 500, + type: Sequence[str] = ["stop_area", "stop_point", "poi"], + admin_uri: Optional[Sequence[str]] = None, + filter: Optional[str] = None, + disable_geojson: bool = False, + disable_disruption: bool = False, + depth: int = 1, + start_page: int = 0, + count: int = 25, + add_poi_infos: Sequence[str] = ["bss_stands", "car_park"], + ) -> Tuple[Sequence[Place], Pagination]: + request_url = f"{self.base_navitia_url}/coverage/{lon};{lat}/places_nearby" if not request_url: raise ValueError( diff --git a/navitia_client/client/apis/route_schedules_apis.py b/navitia_client/client/apis/route_schedules_apis.py index d78648f..841504f 100644 --- a/navitia_client/client/apis/route_schedules_apis.py +++ b/navitia_client/client/apis/route_schedules_apis.py @@ -21,19 +21,15 @@ def _generate_filter_query(filters: dict[str, Any]) -> str: filter_query = "&".join([f"{key}={value}" for key, value in filters.items()]) return "?" + filter_query if filter_query else "" - def _get_places_nearby(self, url: str, filters: dict) -> Sequence[RouteSchedule]: + def _get_routes_nearby(self, url: str, filters: dict) -> Sequence[RouteSchedule]: results = self.get_navitia_api(url + self._generate_filter_query(filters)) raw_results = results.json()["route_schedules"] return self._get_route_schedule_object_from_response(raw_results) - def list_route_schedules( + def list_route_schedules_by_region_id_and_path( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_id: str, + resource_path: str, from_datetime: datetime = datetime.now(), duration: int = 86400, depth: int = 1, @@ -43,19 +39,37 @@ def list_route_schedules( disable_geojson: bool = False, direction_type: str = "all", ) -> Sequence[RouteSchedule]: - request_url: str | None = None - if region_id and resource_path: - # See https://doc.navitia.io/#route-schedules for URL description - # List of route schedules near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/route_schedules" - elif all([region_lon, region_lat, lon, lat]): - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/route_schedules" + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/route_schedules" - if not request_url: - raise ValueError( - "Region id and resource path or region coordinates and coordinates must be provided." - ) + filters = { + "from_datetime": from_datetime, + "duration": duration, + "depth": depth, + "items_per_schedule": items_per_schedule, + "disable_geojson": disable_geojson, + "forbidden_uris[]": forbidden_uris, + "data_freshness": data_freshness, + "direction_type": direction_type, + } + + return self._get_routes_nearby(request_url, filters) + + def list_route_schedules_by_coordinates( + self, + region_lon: float, + region_lat: float, + lon: float, + lat: float, + from_datetime: datetime = datetime.now(), + duration: int = 86400, + depth: int = 1, + items_per_schedule: int = 1, + forbidden_uris: Optional[Sequence[str]] = None, + data_freshness: str = "base_schedule", + disable_geojson: bool = False, + direction_type: str = "all", + ) -> Sequence[RouteSchedule]: + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/route_schedules" filters = { "from_datetime": from_datetime, @@ -68,4 +82,4 @@ def list_route_schedules( "direction_type": direction_type, } - return self._get_places_nearby(request_url, filters) + return self._get_routes_nearby(request_url, filters) diff --git a/navitia_client/client/apis/stop_schedules_apis.py b/navitia_client/client/apis/stop_schedules_apis.py index c32f0af..aa91568 100644 --- a/navitia_client/client/apis/stop_schedules_apis.py +++ b/navitia_client/client/apis/stop_schedules_apis.py @@ -30,14 +30,12 @@ def _get_stop_schedules( pagination = Pagination.from_payload(results.json()["pagination"]) return self._get_stop_schedule_objects_from_response(raw_results), pagination - def list_stop_schedules( + def list_stop_schedules_by_coordinates( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_lon: float, + region_lat: float, + lon: float, + lat: float, from_datetime: datetime = datetime.now(), duration: int = 86400, depth: int = 1, @@ -47,19 +45,35 @@ def list_stop_schedules( disable_geojson: bool = False, direction_type: str = "all", ) -> Tuple[Sequence[StopSchedule], Pagination]: - request_url: str | None = None - if region_id and resource_path: - # See https://doc.navitia.io/#route-schedules for URL description - # List of route schedules near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/stop_schedules" - elif all([region_lon, region_lat, lon, lat]): - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/stop_schedules" + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/stop_schedules" - if not request_url: - raise ValueError( - "Region id and resource path or region coordinates and coordinates must be provided." - ) + filters = { + "from_datetime": from_datetime, + "duration": duration, + "depth": depth, + "items_per_schedule": items_per_schedule, + "disable_geojson": disable_geojson, + "forbidden_uris[]": forbidden_uris, + "data_freshness": data_freshness, + "direction_type": direction_type, + } + + return self._get_stop_schedules(request_url, filters) + + def list_stop_schedules_by_region_id_and_path( + self, + region_id: str, + resource_path: str, + from_datetime: datetime = datetime.now(), + duration: int = 86400, + depth: int = 1, + items_per_schedule: int = 1, + forbidden_uris: Optional[Sequence[str]] = None, + data_freshness: str = "realtime", + disable_geojson: bool = False, + direction_type: str = "all", + ) -> Tuple[Sequence[StopSchedule], Pagination]: + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/stop_schedules" filters = { "from_datetime": from_datetime, diff --git a/navitia_client/client/apis/terminus_schedules_apis.py b/navitia_client/client/apis/terminus_schedules_apis.py index e5e7ab5..530af90 100644 --- a/navitia_client/client/apis/terminus_schedules_apis.py +++ b/navitia_client/client/apis/terminus_schedules_apis.py @@ -34,14 +34,10 @@ def _get_stop_schedules( raw_results ), pagination - def list_terminus_schedules( + def list_terminus_schedules_by_region_id_and_path( self, - region_id: Optional[str] = None, - resource_path: Optional[str] = None, - region_lon: Optional[float] = None, - region_lat: Optional[float] = None, - lon: Optional[float] = None, - lat: Optional[float] = None, + region_id: str, + resource_path: str, from_datetime: datetime = datetime.now(), duration: int = 86400, depth: int = 1, @@ -51,19 +47,37 @@ def list_terminus_schedules( disable_geojson: bool = False, direction_type: str = "all", ) -> Tuple[Sequence[TerminusSchedule], Pagination]: - request_url: str | None = None - if region_id and resource_path: - # See https://doc.navitia.io/#route-schedules for URL description - # List of route schedules near the resource - request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - elif all([region_lon, region_lat, lon, lat]): - # List of objects near the resource, navitia guesses the region from coordinates - request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" + request_url = f"{self.base_navitia_url}/coverage/{region_id}/{resource_path}/terminus_schedules" - if not request_url: - raise ValueError( - "Region id and resource path or region coordinates and coordinates must be provided." - ) + filters = { + "from_datetime": from_datetime, + "duration": duration, + "depth": depth, + "items_per_schedule": items_per_schedule, + "disable_geojson": disable_geojson, + "forbidden_uris[]": forbidden_uris, + "data_freshness": data_freshness, + "direction_type": direction_type, + } + + return self._get_stop_schedules(request_url, filters) + + def list_terminus_schedules_by_coordinates( + self, + region_lon: float, + region_lat: float, + lon: float, + lat: float, + from_datetime: datetime = datetime.now(), + duration: int = 86400, + depth: int = 1, + items_per_schedule: int = 1, + forbidden_uris: Optional[Sequence[str]] = None, + data_freshness: str = "realtime", + disable_geojson: bool = False, + direction_type: str = "all", + ) -> Tuple[Sequence[TerminusSchedule], Pagination]: + request_url = f"{self.base_navitia_url}/coverage/{region_lon};{region_lat}/coords/{lon};{lat}/terminus_schedules" filters = { "from_datetime": from_datetime, diff --git a/tests/client/apis/test_arrival_apis.py b/tests/client/apis/test_arrival_apis.py index 68c0fa7..d84e494 100644 --- a/tests/client/apis/test_arrival_apis.py +++ b/tests/client/apis/test_arrival_apis.py @@ -17,7 +17,7 @@ def arrival_apis(): @patch.object(ArrivalApiClient, "get_navitia_api") -def test_list_objects( +def test_list_objects_by_id_and_path( mock_get_navitia_api: MagicMock, arrival_apis: ArrivalApiClient ) -> None: # Given @@ -28,7 +28,7 @@ def test_list_objects( mock_get_navitia_api.return_value = mock_response # When - arrivals, _ = arrival_apis.list_arrivals( + arrivals, _ = arrival_apis.list_arrivals_by_region_id_and_path( region_id="bar", resource_path="foo:bar:fuzz" ) @@ -38,16 +38,21 @@ def test_list_objects( @patch.object(ArrivalApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( +def test_list_objects_by_coordinates( mock_get_navitia_api: MagicMock, arrival_apis: ArrivalApiClient ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = {} + with open("tests/test_data/arrivals.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response - # When/Then - with pytest.raises(ValueError): - arrival_apis.list_arrivals( - region_id="bar", - ) + # When + arrivals, _ = arrival_apis.list_arrivals_by_coordinates( + region_lon=1.1, region_lat=2.2, lat=1.3, lon=2.3 + ) + + # Then + assert len(arrivals) == 10 + assert isinstance(arrivals[0], Arrival) diff --git a/tests/client/apis/test_departure_apis.py b/tests/client/apis/test_departure_apis.py index 543ba74..8891e17 100644 --- a/tests/client/apis/test_departure_apis.py +++ b/tests/client/apis/test_departure_apis.py @@ -17,7 +17,7 @@ def departure_apis(): @patch.object(DepartureApiClient, "get_navitia_api") -def test_list_objects( +def test_list_objects_by_region_id_and_path( mock_get_navitia_api: MagicMock, departure_apis: DepartureApiClient ) -> None: # Given @@ -28,7 +28,7 @@ def test_list_objects( mock_get_navitia_api.return_value = mock_response # When - departures, _ = departure_apis.list_departures( + departures, _ = departure_apis.list_departures_by_region_id_and_path( region_id="bar", resource_path="foo:bar:fuzz" ) @@ -38,16 +38,21 @@ def test_list_objects( @patch.object(DepartureApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( +def test_list_objects_by_coordinates( mock_get_navitia_api: MagicMock, departure_apis: DepartureApiClient ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = {} + with open("tests/test_data/departures.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response - # When/Then - with pytest.raises(ValueError): - departure_apis.list_departures( - region_id="bar", - ) + # When + departures, _ = departure_apis.list_departures_by_coordinates( + region_lon=1.1, region_lat=1.2, lon=2.1, lat=2.2 + ) + + # Then + assert len(departures) == 10 + assert isinstance(departures[0], Departure) diff --git a/tests/client/apis/test_places_nearby_apis.py b/tests/client/apis/test_places_nearby_apis.py index dad33a6..b5f6959 100644 --- a/tests/client/apis/test_places_nearby_apis.py +++ b/tests/client/apis/test_places_nearby_apis.py @@ -1,3 +1,4 @@ +import json from unittest.mock import MagicMock, patch import pytest @@ -19,89 +20,13 @@ def test_list_objects( ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = { - "pagination": { - "items_on_page": 2, - "items_per_page": 10, - "start_page": 0, - "total_result": 2, - }, - "places_nearby": [ - { - "distance": "0", - "embedded_type": "stop_area", - "id": "stop_area:SNCF:87758896", - "name": "Saint-Rémy-lès-Chevreuse " "(Saint-Rémy-lès-Chevreuse)", - "quality": 0, - "stop_area": { - "administrative_regions": [ - { - "coord": {"lat": "48.7054888", "lon": "2.071109"}, - "id": "admin:fr:78575", - "insee": "78575", - "label": "Saint-Rémy-lès-Chevreuse " "(78470)", - "level": 8, - "name": "Saint-Rémy-lès-Chevreuse", - "zip_code": "78470", - } - ], - "codes": [ - {"type": "source", "value": "87758896"}, - {"type": "uic", "value": "87758896"}, - ], - "coord": {"lat": "48.702722", "lon": "2.070924"}, - "id": "stop_area:SNCF:87758896", - "label": "Saint-Rémy-lès-Chevreuse " "(Saint-Rémy-lès-Chevreuse)", - "links": [], - "name": "Saint-Rémy-lès-Chevreuse", - "timezone": "Europe/Paris", - }, - }, - { - "distance": "0", - "embedded_type": "stop_point", - "id": "stop_point:SNCF:87758896:RapidTransit", - "name": "Saint-Rémy-lès-Chevreuse " "(Saint-Rémy-lès-Chevreuse)", - "quality": 0, - "stop_point": { - "administrative_regions": [ - { - "coord": {"lat": "48.7054888", "lon": "2.071109"}, - "id": "admin:fr:78575", - "insee": "78575", - "label": "Saint-Rémy-lès-Chevreuse " "(78470)", - "level": 8, - "name": "Saint-Rémy-lès-Chevreuse", - "zip_code": "78470", - } - ], - "coord": {"lat": "48.702722", "lon": "2.070924"}, - "equipments": [], - "id": "stop_point:SNCF:87758896:RapidTransit", - "label": "Saint-Rémy-lès-Chevreuse " "(Saint-Rémy-lès-Chevreuse)", - "links": [], - "name": "Saint-Rémy-lès-Chevreuse", - "stop_area": { - "codes": [ - {"type": "source", "value": "87758896"}, - {"type": "uic", "value": "87758896"}, - ], - "coord": {"lat": "48.702722", "lon": "2.070924"}, - "id": "stop_area:SNCF:87758896", - "label": "Saint-Rémy-lès-Chevreuse " - "(Saint-Rémy-lès-Chevreuse)", - "links": [], - "name": "Saint-Rémy-lès-Chevreuse", - "timezone": "Europe/Paris", - }, - }, - }, - ], - } + with open("tests/test_data/places_nearby.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response # When - places, _ = places_nearby_apis.list_objects( + places, _ = places_nearby_apis.list_objects_by_region_id_and_path( region_id="bar", resource_path="stop_area/foo:bar" ) @@ -110,45 +35,3 @@ def test_list_objects( assert isinstance(places[0], Place) assert places[0].embedded_type == "stop_area" assert places[1].embedded_type == "stop_point" - - -@patch.object(PlacesNearbyApiClient, "get_navitia_api") -def test_raise_on_missing_parameters( - mock_get_navitia_api: MagicMock, places_nearby_apis: PlacesNearbyApiClient -) -> None: - # Given - mock_response = MagicMock() - mock_response.json.return_value = {} - mock_get_navitia_api.return_value = mock_response - - # When/Then - with pytest.raises(ValueError): - places_nearby_apis.list_objects() - - -@patch.object(PlacesNearbyApiClient, "get_navitia_api") -def test_raise_on_missing_region_id( - mock_get_navitia_api: MagicMock, places_nearby_apis: PlacesNearbyApiClient -) -> None: - # Given - mock_response = MagicMock() - mock_response.json.return_value = {} - mock_get_navitia_api.return_value = mock_response - - # When/Then - with pytest.raises(ValueError): - places_nearby_apis.list_objects(region_id="foo:bar") - - -@patch.object(PlacesNearbyApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( - mock_get_navitia_api: MagicMock, places_nearby_apis: PlacesNearbyApiClient -) -> None: - # Given - mock_response = MagicMock() - mock_response.json.return_value = {} - mock_get_navitia_api.return_value = mock_response - - # When/Then - with pytest.raises(ValueError): - places_nearby_apis.list_objects(region_lon=2.5, region_lat=3.7) diff --git a/tests/client/apis/test_route_schedules_apis.py b/tests/client/apis/test_route_schedules_apis.py index a90c9da..732df49 100644 --- a/tests/client/apis/test_route_schedules_apis.py +++ b/tests/client/apis/test_route_schedules_apis.py @@ -17,7 +17,7 @@ def route_schedules_apis(): @patch.object(RouteSchedulesApiClient, "get_navitia_api") -def test_list_objects( +def test_list_objects_by_region_id_and_path( mock_get_navitia_api: MagicMock, route_schedules_apis: RouteSchedulesApiClient ) -> None: # Given @@ -28,7 +28,7 @@ def test_list_objects( mock_get_navitia_api.return_value = mock_response # When - route_schedules = route_schedules_apis.list_route_schedules( + route_schedules = route_schedules_apis.list_route_schedules_by_region_id_and_path( region_id="bar", resource_path="foo:bar:fuzz" ) @@ -38,16 +38,21 @@ def test_list_objects( @patch.object(RouteSchedulesApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( +def test_list_objects_by_coordinates( mock_get_navitia_api: MagicMock, route_schedules_apis: RouteSchedulesApiClient ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = {} + with open("tests/test_data/route_schedules.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response - # When/Then - with pytest.raises(ValueError): - route_schedules_apis.list_route_schedules( - region_id="bar", - ) + # When + route_schedules = route_schedules_apis.list_route_schedules_by_coordinates( + region_lon=1.1, region_lat=1.2, lon=2.1, lat=2.2 + ) + + # Then + assert len(route_schedules) == 1 + assert isinstance(route_schedules[0], RouteSchedule) diff --git a/tests/client/apis/test_stop_schedules_apis.py b/tests/client/apis/test_stop_schedules_apis.py index 43be8e7..479f4f5 100644 --- a/tests/client/apis/test_stop_schedules_apis.py +++ b/tests/client/apis/test_stop_schedules_apis.py @@ -17,7 +17,7 @@ def stop_schedules_apis(): @patch.object(StopSchedulesApiClient, "get_navitia_api") -def test_list_objects( +def test_list_objects_by_region_id_and_path( mock_get_navitia_api: MagicMock, stop_schedules_apis: StopSchedulesApiClient ) -> None: # Given @@ -28,7 +28,7 @@ def test_list_objects( mock_get_navitia_api.return_value = mock_response # When - stop_schedules, _ = stop_schedules_apis.list_stop_schedules( + stop_schedules, _ = stop_schedules_apis.list_stop_schedules_by_region_id_and_path( region_id="bar", resource_path="foo:bar:fuzz" ) @@ -38,16 +38,21 @@ def test_list_objects( @patch.object(StopSchedulesApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( +def test_list_objects_by_coordinates( mock_get_navitia_api: MagicMock, stop_schedules_apis: StopSchedulesApiClient ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = {} + with open("tests/test_data/stop_schedules.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response - # When/Then - with pytest.raises(ValueError): - stop_schedules_apis.list_stop_schedules( - region_id="bar", - ) + # When + stop_schedules, _ = stop_schedules_apis.list_stop_schedules_by_coordinates( + region_lon=1.1, region_lat=1.2, lon=2.1, lat=2.2 + ) + + # Then + assert len(stop_schedules) == 1 + assert isinstance(stop_schedules[0], StopSchedule) diff --git a/tests/client/apis/test_terminus_schedules_apis.py b/tests/client/apis/test_terminus_schedules_apis.py index 62090f7..a25d7d9 100644 --- a/tests/client/apis/test_terminus_schedules_apis.py +++ b/tests/client/apis/test_terminus_schedules_apis.py @@ -17,7 +17,7 @@ def terminus_schedules_apis(): @patch.object(TerminusSchedulesApiClient, "get_navitia_api") -def test_list_objects( +def test_list_objects_by_region_id_and_path( mock_get_navitia_api: MagicMock, terminus_schedules_apis: TerminusSchedulesApiClient ) -> None: # Given @@ -28,8 +28,10 @@ def test_list_objects( mock_get_navitia_api.return_value = mock_response # When - terminus_schedules, _ = terminus_schedules_apis.list_terminus_schedules( - region_id="bar", resource_path="foo:bar:fuzz" + terminus_schedules, _ = ( + terminus_schedules_apis.list_terminus_schedules_by_region_id_and_path( + region_id="bar", resource_path="foo:bar:fuzz" + ) ) # Then @@ -38,16 +40,23 @@ def test_list_objects( @patch.object(TerminusSchedulesApiClient, "get_navitia_api") -def test_raise_on_missing_region_coordinates( +def test_list_objects_by_coordinates( mock_get_navitia_api: MagicMock, terminus_schedules_apis: TerminusSchedulesApiClient ) -> None: # Given mock_response = MagicMock() - mock_response.json.return_value = {} + with open("tests/test_data/terminus_schedules.json", encoding="utf-8") as file: + mock_response.json.return_value = json.load(file) + mock_get_navitia_api.return_value = mock_response - # When/Then - with pytest.raises(ValueError): - terminus_schedules_apis.list_terminus_schedules( - region_id="bar", + # When + terminus_schedules, _ = ( + terminus_schedules_apis.list_terminus_schedules_by_coordinates( + region_lon=1.1, region_lat=1.2, lon=2.1, lat=2.2 ) + ) + + # Then + assert len(terminus_schedules) == 2 + assert isinstance(terminus_schedules[0], TerminusSchedule) diff --git a/tests/test_data/places_nearby.json b/tests/test_data/places_nearby.json new file mode 100644 index 0000000..5dba507 --- /dev/null +++ b/tests/test_data/places_nearby.json @@ -0,0 +1,110 @@ +{ + "context": { + "current_datetime": "20240508T184211", + "timezone": "Europe/Paris" + }, + "disruptions": [], + "pagination": { + "items_on_page": 2, + "items_per_page": 10, + "start_page": 0, + "total_result": 2 + }, + "places_nearby": [ + { + "distance": "0", + "embedded_type": "stop_area", + "id": "stop_area:SNCF:87758896", + "name": "Saint-Rémy-lès-Chevreuse", + "quality": 0, + "stop_area": { + "administrative_regions": [ + { + "coord": { + "lat": "48.7054888", + "lon": "2.071109" + }, + "id": "admin:fr:78575", + "insee": "78575", + "label": "Saint-Rémy-lès-Chevreuse", + "level": 8, + "name": "Saint-Rémy-lès-Chevreuse", + "zip_code": "78470" + } + ], + "codes": [ + { + "type": "source", + "value": "87758896" + }, + { + "type": "uic", + "value": "87758896" + } + ], + "coord": { + "lat": "48.702722", + "lon": "2.070924" + }, + "id": "stop_area:SNCF:87758896", + "label": "Saint-Rémy-lès-Chevreuse", + "links": [], + "name": "Saint-Rémy-lès-Chevreuse", + "timezone": "Europe/Paris" + } + }, + { + "distance": "0", + "embedded_type": "stop_point", + "id": "stop_point:SNCF:87758896:RapidTransit", + "name": "Saint-Rémy-lès-Chevreuse", + "quality": 0, + "stop_point": { + "administrative_regions": [ + { + "coord": { + "lat": "48.7054888", + "lon": "2.071109" + }, + "id": "admin:fr:78575", + "insee": "78575", + "label": "Saint-Rémy-lès-Chevreuse", + "level": 8, + "name": "Saint-Rémy-lès-Chevreuse", + "zip_code": "78470" + } + ], + "coord": { + "lat": "48.702722", + "lon": "2.070924" + }, + "equipments": [], + "id": "stop_point:SNCF:87758896:RapidTransit", + "label": "Saint-Rémy-lès-Chevreuse", + "links": [], + "name": "Saint-Rémy-lès-Chevreuse", + "stop_area": { + "codes": [ + { + "type": "source", + "value": "87758896" + }, + { + "type": "uic", + "value": "87758896" + } + ], + "coord": { + "lat": "48.702722", + "lon": "2.070924" + }, + "id": "stop_area:SNCF:87758896", + "label": "Saint-Rémy-lès-Chevreuse", + "links": [], + "name": "Saint-Rémy-lès-Chevreuse", + "timezone": "Europe/Paris" + } + } + } + ] +}