more testing and better pylint score
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 1m48s
Run linting on the backend code / Build (pull_request) Failing after 29s
Run testing on the backend code / Build (pull_request) Failing after 1m13s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 47s
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 1m48s
Run linting on the backend code / Build (pull_request) Failing after 29s
Run testing on the backend code / Build (pull_request) Failing after 1m13s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 47s
This commit is contained in:
62
backend/src/tests/test_invalid_input.py
Normal file
62
backend/src/tests/test_invalid_input.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""Collection of tests to ensure correct handling of invalid input."""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
import pytest
|
||||
|
||||
from ..main import app
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def invalid_client():
|
||||
"""Client used to call the app."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"start,preferences,status_code",
|
||||
[
|
||||
# Invalid case: no preferences at all.
|
||||
([48.8566, 2.3522], {}, 422),
|
||||
|
||||
# Invalid cases: incomplete preferences.
|
||||
([48.084588, 7.280405], {"sightseeing": {"type": "nature", "score": 5}, # no shopping
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
}, 422),
|
||||
([48.084588, 7.280405], {"sightseeing": {"type": "nature", "score": 5}, # no nature
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 422),
|
||||
([48.084588, 7.280405], {"nature": {"type": "nature", "score": 5}, # no sightseeing
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 422),
|
||||
|
||||
# Invalid cases: unexisting coords
|
||||
([91, 181], {"sightseeing": {"type": "nature", "score": 5},
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 423),
|
||||
([-91, 181], {"sightseeing": {"type": "nature", "score": 5},
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 423),
|
||||
([91, -181], {"sightseeing": {"type": "nature", "score": 5},
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 423),
|
||||
([-91, -181], {"sightseeing": {"type": "nature", "score": 5},
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
}, 423),
|
||||
]
|
||||
)
|
||||
def test_input(invalid_client, start, preferences, status_code): # pylint: disable=redefined-outer-name
|
||||
"""
|
||||
Test new trip creation with different sets of preferences and locations.
|
||||
"""
|
||||
response = invalid_client.post(
|
||||
"/trip/new",
|
||||
json={
|
||||
"preferences": preferences,
|
||||
"start": start
|
||||
}
|
||||
)
|
||||
assert response.status_code == status_code
|
@@ -1,38 +1,20 @@
|
||||
"""Collection of tests to ensure correct implementation and track progress. """
|
||||
|
||||
from typing import List
|
||||
from fastapi.testclient import TestClient
|
||||
import pytest
|
||||
|
||||
from .test_utils import landmarks_to_osmid, load_trip_landmarks, log_trip_details
|
||||
from ..main import app
|
||||
from ..structs.landmark import Landmark
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
"""Client used to call the app."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_new_trip_invalid_prefs(client):
|
||||
def test_turckheim(client, request): # pylint: disable=redefined-outer-name
|
||||
"""
|
||||
Test n°1 : base test for checking if the API returns correct error code when no preferences are specified.
|
||||
|
||||
Args:
|
||||
client:
|
||||
"""
|
||||
response = client.post(
|
||||
"/trip/new",
|
||||
json={
|
||||
"preferences": {},
|
||||
"start": [48.8566, 2.3522]
|
||||
}
|
||||
)
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_turckheim(client, request):
|
||||
"""
|
||||
Test n°2 : Custom test in Turckheim to ensure small villages are also supported.
|
||||
Test n°1 : Custom test in Turckheim to ensure small villages are also supported.
|
||||
|
||||
Args:
|
||||
client:
|
||||
@@ -42,7 +24,11 @@ def test_turckheim(client, request):
|
||||
response = client.post(
|
||||
"/trip/new",
|
||||
json={
|
||||
"preferences": {"sightseeing": {"type": "sightseeing", "score": 5}, "nature": {"type": "nature", "score": 5}, "shopping": {"type": "shopping", "score": 5}, "max_time_minute": duration_minutes, "detour_tolerance_minute": 0},
|
||||
"preferences": {"sightseeing": {"type": "sightseeing", "score": 5},
|
||||
"nature": {"type": "nature", "score": 5},
|
||||
"shopping": {"type": "shopping", "score": 5},
|
||||
"max_time_minute": duration_minutes,
|
||||
"detour_tolerance_minute": 0},
|
||||
"start": [48.084588, 7.280405]
|
||||
}
|
||||
)
|
||||
@@ -59,16 +45,15 @@ def test_turckheim(client, request):
|
||||
assert len(landmarks) > 2 # check that there is something to visit
|
||||
|
||||
|
||||
# Test no. 3
|
||||
def test_bellecour(client, request) :
|
||||
def test_bellecour(client, request) : # pylint: disable=redefined-outer-name
|
||||
"""
|
||||
Test n°3 : Custom test in Lyon centre to ensure proper decision making in crowded area.
|
||||
Test n°2 : Custom test in Lyon centre to ensure proper decision making in crowded area.
|
||||
|
||||
Args:
|
||||
client:
|
||||
request:
|
||||
"""
|
||||
duration_minutes = 60
|
||||
duration_minutes = 30
|
||||
response = client.post(
|
||||
"/trip/new",
|
||||
json={
|
||||
@@ -90,89 +75,6 @@ def test_bellecour(client, request) :
|
||||
|
||||
|
||||
|
||||
def landmarks_to_osmid(landmarks: List[Landmark]) -> List[int] :
|
||||
"""
|
||||
Convert the list of landmarks into a list containing their osm ids for quick landmark checking.
|
||||
|
||||
Args :
|
||||
landmarks (list): the list of landmarks
|
||||
|
||||
Returns :
|
||||
ids (list) : the list of corresponding OSM ids
|
||||
"""
|
||||
ids = []
|
||||
for landmark in landmarks :
|
||||
ids.append(landmark.osm_id)
|
||||
|
||||
return ids
|
||||
|
||||
def fetch_landmark(client, landmark_uuid: str):
|
||||
"""
|
||||
Fetch landmark data from the API based on the landmark UUID.
|
||||
|
||||
Args:
|
||||
landmark_uuid (str): The UUID of the landmark.
|
||||
|
||||
Returns:
|
||||
dict: Landmark data fetched from the API.
|
||||
"""
|
||||
response = client.get(f"/landmark/{landmark_uuid}")
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Failed to fetch landmark with UUID {landmark_uuid}: {response.status_code}")
|
||||
|
||||
json_data = response.json()
|
||||
|
||||
if "detail" in json_data:
|
||||
raise Exception(json_data["detail"])
|
||||
|
||||
return json_data
|
||||
|
||||
|
||||
def load_trip_landmarks(client, first_uuid: str) -> List[Landmark]:
|
||||
"""
|
||||
Load all landmarks for a trip using the response from the API.
|
||||
|
||||
Args:
|
||||
first_uuid (str) : The first UUID of the landmark.
|
||||
|
||||
Returns:
|
||||
landmarks (list) : An list containing all landmarks for the trip.
|
||||
"""
|
||||
landmarks = []
|
||||
next_uuid = first_uuid
|
||||
|
||||
while next_uuid is not None:
|
||||
landmark_data = fetch_landmark(client, next_uuid)
|
||||
# # Convert UUIDs to strings explicitly
|
||||
# landmark_data = {
|
||||
# key: str(value) if isinstance(value, UUID) else value
|
||||
# for key, value in landmark_data.items()
|
||||
# }
|
||||
landmarks.append(Landmark(**landmark_data)) # Create Landmark objects
|
||||
next_uuid = landmark_data.get('next_uuid') # Prepare for the next iteration
|
||||
|
||||
return landmarks
|
||||
|
||||
|
||||
def log_trip_details(request, landmarks: List[Landmark], duration: int, target_duration: int) :
|
||||
"""
|
||||
Allows to show the detailed trip in the html test report.
|
||||
|
||||
Args:
|
||||
request:
|
||||
landmarks (list): the ordered list of visited landmarks
|
||||
duration (int): the total duration of this trip
|
||||
target_duration(int): the target duration of this trip
|
||||
"""
|
||||
trip_string = [f"{landmark.name} ({landmark.attractiveness} | {landmark.duration}) - {landmark.time_to_reach_next}" for landmark in landmarks]
|
||||
|
||||
# Pass additional info to pytest for reporting
|
||||
request.node.trip_details = trip_string
|
||||
request.node.trip_duration = str(duration) # result['total_time']
|
||||
request.node.target_duration = str(target_duration)
|
||||
|
||||
|
||||
# def test_new_trip_single_prefs(client):
|
||||
# response = client.post(
|
||||
# "/trip/new",
|
||||
@@ -185,5 +87,4 @@ def log_trip_details(request, landmarks: List[Landmark], duration: int, target_d
|
||||
|
||||
|
||||
# def test_new_trip_matches_prefs(client):
|
||||
# # todo
|
||||
# pass
|
||||
|
89
backend/src/tests/test_utils.py
Normal file
89
backend/src/tests/test_utils.py
Normal file
@@ -0,0 +1,89 @@
|
||||
"""Helper methods for testing."""
|
||||
from typing import List
|
||||
from fastapi import HTTPException
|
||||
|
||||
from ..structs.landmark import Landmark
|
||||
|
||||
|
||||
def landmarks_to_osmid(landmarks: List[Landmark]) -> List[int] :
|
||||
"""
|
||||
Convert the list of landmarks into a list containing their osm ids for quick landmark checking.
|
||||
|
||||
Args :
|
||||
landmarks (list): the list of landmarks
|
||||
|
||||
Returns :
|
||||
ids (list) : the list of corresponding OSM ids
|
||||
"""
|
||||
ids = []
|
||||
for landmark in landmarks :
|
||||
ids.append(landmark.osm_id)
|
||||
|
||||
return ids
|
||||
|
||||
def fetch_landmark(client, landmark_uuid: str):
|
||||
"""
|
||||
Fetch landmark data from the API based on the landmark UUID.
|
||||
|
||||
Args:
|
||||
landmark_uuid (str): The UUID of the landmark.
|
||||
|
||||
Returns:
|
||||
dict: Landmark data fetched from the API.
|
||||
"""
|
||||
response = client.get(f"/landmark/{landmark_uuid}")
|
||||
|
||||
if response.status_code != 200:
|
||||
raise HTTPException(status_code=999, detail=f"Failed to fetch landmark with UUID {landmark_uuid}: {response.status_code}")
|
||||
|
||||
json_data = response.json()
|
||||
|
||||
if "detail" in json_data:
|
||||
raise HTTPException(status_code=999, detail=json_data["detail"])
|
||||
|
||||
|
||||
return json_data
|
||||
|
||||
|
||||
def load_trip_landmarks(client, first_uuid: str) -> List[Landmark]:
|
||||
"""
|
||||
Load all landmarks for a trip using the response from the API.
|
||||
|
||||
Args:
|
||||
first_uuid (str) : The first UUID of the landmark.
|
||||
|
||||
Returns:
|
||||
landmarks (list) : An list containing all landmarks for the trip.
|
||||
"""
|
||||
landmarks = []
|
||||
next_uuid = first_uuid
|
||||
|
||||
while next_uuid is not None:
|
||||
landmark_data = fetch_landmark(client, next_uuid)
|
||||
# # Convert UUIDs to strings explicitly
|
||||
# landmark_data = {
|
||||
# key: str(value) if isinstance(value, UUID) else value
|
||||
# for key, value in landmark_data.items()
|
||||
# }
|
||||
landmarks.append(Landmark(**landmark_data)) # Create Landmark objects
|
||||
next_uuid = landmark_data.get('next_uuid') # Prepare for the next iteration
|
||||
|
||||
return landmarks
|
||||
|
||||
|
||||
def log_trip_details(request, landmarks: List[Landmark], duration: int, target_duration: int) :
|
||||
"""
|
||||
Allows to show the detailed trip in the html test report.
|
||||
|
||||
Args:
|
||||
request:
|
||||
landmarks (list): the ordered list of visited landmarks
|
||||
duration (int): the total duration of this trip
|
||||
target_duration(int): the target duration of this trip
|
||||
"""
|
||||
trip_string = [f"{landmark.name} ({landmark.attractiveness} | {landmark.duration}) - {landmark.time_to_reach_next}" for landmark in landmarks]
|
||||
|
||||
# Pass additional info to pytest for reporting
|
||||
request.node.trip_details = trip_string
|
||||
request.node.trip_duration = str(duration) # result['total_time']
|
||||
request.node.target_duration = str(target_duration)
|
Reference in New Issue
Block a user