From 3c3295bd8cc74fd8330a1519eaf5eb8296074997 Mon Sep 17 00:00:00 2001 From: Merlin Unterfinger Date: Mon, 19 Aug 2024 11:19:35 +0200 Subject: [PATCH] TEST: NAV-145 - Add integration test cases for client exceptions --- tests/integration/test_integration_routing.py | 79 ++++++++++++++++++- .../integration/test_integration_schedule.py | 25 +++++- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_integration_routing.py b/tests/integration/test_integration_routing.py index 1d32777..9463b91 100644 --- a/tests/integration/test_integration_routing.py +++ b/tests/integration/test_integration_routing.py @@ -2,8 +2,10 @@ import pytest -from public_transit_client.client import PublicTransitClient -from public_transit_client.model import Connection, StopConnection, TimeType +from public_transit_client.client import (PublicTransitClient, + PublicTransitClientException) +from public_transit_client.model import (Connection, Coordinate, + StopConnection, TimeType) HOST = "http://localhost:8080" @@ -32,6 +34,46 @@ def test_get_connections(client): assert connections[0].to_stop.id == to_stop +@pytest.mark.integration +def test_get_connections_invalid_stop(client): + from_stop = "INVALID_STOP" + to_stop = "BULLFROG" + departure_time = datetime(2008, 6, 1) + + with pytest.raises(PublicTransitClientException) as exc_info: + client.get_connections( + from_stop=from_stop, + to_stop=to_stop, + time=departure_time, + time_type=TimeType.DEPARTURE, + ) + + assert exc_info.value.api_error.status == 404 + assert "'INVALID_STOP' was not found" in exc_info.value.api_error.message + + +@pytest.mark.integration +def test_get_connections_negative_walking_duration(client): + from_stop = "NANAA" + to_stop = "BULLFROG" + departure_time = datetime(2008, 6, 1) + + with pytest.raises(PublicTransitClientException) as exc_info: + client.get_connections( + from_stop=from_stop, + to_stop=to_stop, + time=departure_time, + time_type=TimeType.DEPARTURE, + max_walking_duration=-10, + ) + + assert exc_info.value.api_error.status == 400 + assert ( + "Max walking duration must be greater than or equal to 0" + in exc_info.value.api_error.message + ) + + @pytest.mark.integration def test_get_isolines(client): from_stop = "NANAA" @@ -50,3 +92,36 @@ def test_get_isolines(client): assert all( isinstance(stop_connection, StopConnection) for stop_connection in isolines ) + + +@pytest.mark.integration +def test_get_isolines_invalid_stop(client): + from_stop = "INVALID_STOP" + departure_time = datetime(2008, 6, 1) + + with pytest.raises(PublicTransitClientException) as exc_info: + client.get_isolines( + from_stop=from_stop, + time=departure_time, + time_type=TimeType.DEPARTURE, + max_walking_duration=10, + max_transfer_number=1, + return_connections=True, + ) + + assert exc_info.value.api_error.status == 404 + assert "'INVALID_STOP' was not found" in exc_info.value.api_error.message + + +@pytest.mark.integration +def test_nearest_stops_invalid_coordinate(client): + invalid_coordinate = Coordinate(latitude=999.0, longitude=999.0) + + with pytest.raises(PublicTransitClientException) as exc_info: + client.nearest_stops(coordinate=invalid_coordinate, limit=5, max_distance=500) + + assert exc_info.value.api_error.status == 400 + assert ( + "Latitude must be between -90 and 90 degrees" + in exc_info.value.api_error.message + ) diff --git a/tests/integration/test_integration_schedule.py b/tests/integration/test_integration_schedule.py index eb3be0c..8b49ee0 100644 --- a/tests/integration/test_integration_schedule.py +++ b/tests/integration/test_integration_schedule.py @@ -1,6 +1,7 @@ import pytest -from public_transit_client.client import PublicTransitClient +from public_transit_client.client import (PublicTransitClient, + PublicTransitClientException) from public_transit_client.model import Coordinate, SearchType, Stop HOST = "http://localhost:8080" @@ -31,6 +32,17 @@ def test_nearest_stops(client): assert all(isinstance(stop.stop, Stop) for stop in stops) +@pytest.mark.integration +def test_nearest_stops_invalid_limit(client): + coordinate = Coordinate(latitude=36, longitude=-116) + + with pytest.raises(PublicTransitClientException) as exc_info: + client.nearest_stops(coordinate=coordinate, limit=-1, max_distance=100000) + + assert exc_info.value.api_error.status == 400 + assert "Limit must be greater than 0" in exc_info.value.api_error.message + + @pytest.mark.integration def test_get_stop(client): stop_id = "NANAA" @@ -39,3 +51,14 @@ def test_get_stop(client): assert stop is not None assert isinstance(stop, Stop) assert stop.id == stop_id + + +@pytest.mark.integration +def test_get_stop_invalid_stop_id(client): + invalid_stop_id = "INVALID_STOP_ID" + + with pytest.raises(PublicTransitClientException) as exc_info: + client.get_stop(stop_id=invalid_stop_id) + + assert exc_info.value.api_error.status == 404 + assert "'INVALID_STOP_ID' was not found" in exc_info.value.api_error.message