From 1075e85d6b65d71c23a001b1769b6a410f2f8674 Mon Sep 17 00:00:00 2001 From: KILIAN-PC Date: Tue, 5 Nov 2024 17:57:29 +0100 Subject: [PATCH] first test --- backend/src/tests/test_main.py | 83 ++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/backend/src/tests/test_main.py b/backend/src/tests/test_main.py index c5feafc..1caf7ea 100644 --- a/backend/src/tests/test_main.py +++ b/backend/src/tests/test_main.py @@ -1,6 +1,54 @@ from fastapi.testclient import TestClient -import pytest +import pytest, requests from ..main import app +from ..structs.landmark import Landmark + +def fetch_landmark(client, landmark_uuid): + """ + 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): + """ + Load all landmarks for a trip using the response from the API. + + Args: + first_uuid (str): The first UUID of the landmark. + + Returns: + LinkedLandmarks: An object containing all linked landmarks for the trip. + """ + landmarks = [] + next_uuid = first_uuid + + while next_uuid is not None: + landmark_data = fetch_landmark(next_uuid) + landmarks.append(Landmark(**landmark_data)) # Create Landmark objects + + # Prepare for the next iteration + next_uuid = landmark_data.get(client, 'next_uuid') # Assuming landmark data contains 'next_uuid' + + # Create and return a LinkedLandmarks object with the collected landmarks + return landmarks + @pytest.fixture() def client(): @@ -17,18 +65,37 @@ def test_new_trip_invalid_prefs(client): ) assert response.status_code == 422 - -def test_new_trip_single_prefs(client): +def test_1(client): response = client.post( "/trip/new", json={ - "preferences": {"sightseeing": {"type": "sightseeing", "score": 1}, "nature": {"type": "nature", "score": 1}, "shopping": {"type": "shopping", "score": 1}, "max_time_minute": 360, "detour_tolerance_minute": 0}, - "start": [48.8566, 2.3522] + "preferences": {"sightseeing": {"type": "sightseeing", "score": 5}, "nature": {"type": "nature", "score": 5}, "shopping": {"type": "shopping", "score": 5}, "max_time_minute": 15, "detour_tolerance_minute": 0}, + "start": [48.084588, 7.280405] } ) + + # check for successful planning assert response.status_code == 200 + result = response.json() -def test_new_trip_matches_prefs(client): - # todo - pass + landmarks = load_trip_landmarks(client, result['first_landmark_uuid']) + + assert isinstance(landmarks, list) + + + +# def test_new_trip_single_prefs(client): +# response = client.post( +# "/trip/new", +# json={ +# "preferences": {"sightseeing": {"type": "sightseeing", "score": 1}, "nature": {"type": "nature", "score": 1}, "shopping": {"type": "shopping", "score": 1}, "max_time_minute": 360, "detour_tolerance_minute": 0}, +# "start": [48.8566, 2.3522] +# } +# ) +# assert response.status_code == 200 + + +# def test_new_trip_matches_prefs(client): +# # todo +# pass