forked from DataScientest-Studio/Template_MLOps_accidents
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e806721
commit 67eeca0
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
|
||
|
||
|