From 67eeca04cd32f7852948c73aba0fe0c9030b2797 Mon Sep 17 00:00:00 2001 From: Soumaya-JE <140071440+Soumaya-JE@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:40:50 +0200 Subject: [PATCH] test --- src/testing/test_endpoint_prediction.py | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/testing/test_endpoint_prediction.py diff --git a/src/testing/test_endpoint_prediction.py b/src/testing/test_endpoint_prediction.py new file mode 100644 index 00000000..8c11bfb2 --- /dev/null +++ b/src/testing/test_endpoint_prediction.py @@ -0,0 +1,81 @@ +import json +import time +import requests + + +# Sample credentials for users +VALID_CREDENTIALS_USER1 = ("user1", "datascientest") +VALID_CREDENTIALS_ADMIN = ("admin", "adminsecret") +INVALID_CREDENTIALS = ("user1", "wrongpassword") + +# Définir les URLs de API GATEWAY +API_GATEWAY_PREDICTION = "http://localhost:8000/prediction" + +# Define payload +test_data = { + "num_acc": 4000000002, #this will be remplaced with a unique identifiant + "place": 1, + "catu": 2, + "trajet": 10.0, + "an_nais": 2024, + "catv": 3, + "choc": 1.0, + "manv": 2.0, + "mois": 6, + "jour": 15, + "lum": 1, + "agg": 1, + "int": 1, + "col": 2.0, + "com": 75000, + "dep": 75, + "hr": 14, + "mn": 30, + "catr": 1, + "circ": 1.0, + "nbv": 2, + "prof": 0.0, + "plan": 0.0, + "lartpc": 0, + "larrout": 0, + "situ": 1.0 +} + +# Function to generate unique num_acc to avoid conflict with existing num_acc +def generate_unique_num_acc(): + timestamp = int(time.time() * 1000) + return timestamp + +def test_prediction_endpoint_valid(): + # Generate a unique num_acc + test_data["num_acc"] = generate_unique_num_acc() + print(f"Generated num_acc: {test_data['num_acc']}") + + response = requests.post( + url=API_GATEWAY_PREDICTION, + auth=VALID_CREDENTIALS_ADMIN, + json=test_data + ) + + assert response.status_code == 200 + response_json = response.json() + assert "gravite_predite" in response_json + assert response_json["gravite_predite"] == 1 + + +def test_prediction_endpoint_invalid(): + # Generate a unique num_acc + test_data["num_acc"] = generate_unique_num_acc() + + response = requests.post( + url=API_GATEWAY_PREDICTION, + auth=INVALID_CREDENTIALS, + json=test_data + ) + + assert response.status_code == 401 + + + + + \ No newline at end of file