better logs
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 2m20s
Run linting on the backend code / Build (pull_request) Failing after 26s
Run testing on the backend code / Build (pull_request) Failing after 2m40s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 17s

This commit is contained in:
2025-01-09 16:58:38 +01:00
parent a0a3d76b78
commit 11bbf34375
7 changed files with 60 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
"""Collection of tests to ensure correct implementation and track progress. """
from fastapi.testclient import TestClient
import pytest
import pytest, time
from .test_utils import landmarks_to_osmid, load_trip_landmarks, log_trip_details
from ..main import app
@@ -20,7 +20,9 @@ def test_turckheim(client, request): # pylint: disable=redefined-outer-name
client:
request:
"""
start_time = time.time() # Start timer
duration_minutes = 15
response = client.post(
"/trip/new",
json={
@@ -35,6 +37,9 @@ def test_turckheim(client, request): # pylint: disable=redefined-outer-name
result = response.json()
landmarks = load_trip_landmarks(client, result['first_landmark_uuid'])
# Get computation time
comp_time = time.time() - start_time
# Add details to report
log_trip_details(request, landmarks, result['total_time'], duration_minutes)
@@ -43,6 +48,7 @@ def test_turckheim(client, request): # pylint: disable=redefined-outer-name
assert isinstance(landmarks, list) # check that the return type is a list
assert duration_minutes*0.8 < int(result['total_time']) < duration_minutes*1.2
assert len(landmarks) > 2 # check that there is something to visit
assert comp_time < 30, f"Computation time exceeded 30 seconds: {comp_time:.2f} seconds"
def test_bellecour(client, request) : # pylint: disable=redefined-outer-name
@@ -53,7 +59,9 @@ def test_bellecour(client, request) : # pylint: disable=redefined-outer-name
client:
request:
"""
start_time = time.time() # Start timer
duration_minutes = 120
response = client.post(
"/trip/new",
json={
@@ -67,7 +75,9 @@ def test_bellecour(client, request) : # pylint: disable=redefined-outer-name
)
result = response.json()
landmarks = load_trip_landmarks(client, result['first_landmark_uuid'])
osm_ids = landmarks_to_osmid(landmarks)
# Get computation time
comp_time = time.time() - start_time
# Add details to report
log_trip_details(request, landmarks, result['total_time'], duration_minutes)
@@ -79,8 +89,7 @@ def test_bellecour(client, request) : # pylint: disable=redefined-outer-name
# checks :
assert response.status_code == 200 # check for successful planning
assert duration_minutes*0.8 < int(result['total_time']) < duration_minutes*1.2
assert 136200148 in osm_ids # check for Cathédrale St. Jean in trip
# assert response.status_code == 2000 # check for successful planning
assert comp_time < 30, f"Computation time exceeded 30 seconds: {comp_time:.2f} seconds"
@@ -92,7 +101,9 @@ def test_shopping(client, request) : # pylint: disable=redefined-outer-name
client:
request:
"""
start_time = time.time() # Start timer
duration_minutes = 240
response = client.post(
"/trip/new",
json={
@@ -107,12 +118,16 @@ def test_shopping(client, request) : # pylint: disable=redefined-outer-name
result = response.json()
landmarks = load_trip_landmarks(client, result['first_landmark_uuid'])
# Get computation time
comp_time = time.time() - start_time
# Add details to report
log_trip_details(request, landmarks, result['total_time'], duration_minutes)
# checks :
assert response.status_code == 200 # check for successful planning
assert duration_minutes*0.8 < int(result['total_time']) < duration_minutes*1.2
assert comp_time < 30, f"Computation time exceeded 30 seconds: {comp_time:.2f} seconds"
# def test_new_trip_single_prefs(client):
# response = client.post(

View File

@@ -34,26 +34,25 @@ def fetch_landmark(client, landmark_uuid: str):
dict: Landmark data fetched from the API.
"""
logger = logging.getLogger(__name__)
response = client.get(f"/landmark/{landmark_uuid}")
response = client.get(f'/landmark/{landmark_uuid}')
if response.status_code != 200:
raise HTTPException(status_code=500,
detail=f"Failed to fetch landmark with UUID {landmark_uuid}: {response.status_code}")
detail=f'Failed to fetch landmark with UUID {landmark_uuid}: {response.status_code}')
try:
json_data = response.json()
logger.info(f"API Response: {json_data}")
logger.info(f'API Response: {json_data}')
except ValueError as e:
logger.error(f"Failed to parse response as JSON: {response.text}")
logger.error(f'Failed to parse response as JSON: {response.text}')
raise HTTPException(status_code=500, detail="Invalid response format from API")
# Try validating against the Landmark model here to ensure consistency
try:
landmark = Landmark(**json_data)
except ValidationError as ve:
logging.error(f"Validation error: {ve}")
logging.error(f'Validation error: {ve}')
raise HTTPException(status_code=500, detail="Invalid data format received from API")
if "detail" in json_data:
raise HTTPException(status_code=500, detail=json_data["detail"])
@@ -75,23 +74,21 @@ def fetch_landmark_cache(landmark_uuid: str):
# Try to fetch the landmark data from the cache
try:
landmark = cache_client.get(f"landmark_{landmark_uuid}")
landmark = cache_client.get(f'landmark_{landmark_uuid}')
if not landmark :
logger.warning(f"Cache miss for landmark UUID: {landmark_uuid}")
raise HTTPException(status_code=404, detail=f"Landmark with UUID {landmark_uuid} not found in cache.")
logger.warning(f'Cache miss for landmark UUID: {landmark_uuid}')
raise HTTPException(status_code=404, detail=f'Landmark with UUID {landmark_uuid} not found in cache.')
# Validate that the fetched data is a dictionary
if not isinstance(landmark, Landmark):
logger.error(f"Invalid cache data format for landmark UUID: {landmark_uuid}. Expected dict, got {type(landmark).__name__}.")
logger.error(f'Invalid cache data format for landmark UUID: {landmark_uuid}. Expected dict, got {type(landmark).__name__}.')
raise HTTPException(status_code=500, detail="Invalid cache data format.")
return landmark
except Exception as exc:
logger.error(f"Unexpected error occurred while fetching landmark UUID {landmark_uuid}: {exc}")
logger.error(f'Unexpected error occurred while fetching landmark UUID {landmark_uuid}: {exc}')
raise HTTPException(status_code=500, detail="An unexpected error occurred while fetching the landmark from the cache") from exc
def load_trip_landmarks(client, first_uuid: str, from_cache=None) -> list[Landmark]:
@@ -122,14 +119,14 @@ def load_trip_landmarks(client, first_uuid: str, from_cache=None) -> list[Landma
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]
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