API_Microservices/medal/test.py
2024-03-27 16:42:43 +01:00

69 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
import requests
import json
HOST = "http://localhost:5000"
# Get root
assert 200 == requests.get(HOST + "/").status_code
# Get list of medals as a json object
get_root = requests.get(HOST + "/medal").text
# If the list is currently empty, it cannot interpreted a JSON
if get_root != "[]\n":
assert json.loads(get_root)
# Post on '/medal' With no data
assert 400 == requests.post(HOST + "/medal").status_code
# Post on '/medal' with uncorrect body
assert 400 == requests.post(
HOST + "/medal", json={"should": "fail"}).status_code
# Post on '/medal' with uncorrect value of rank
assert 400 == requests.post(
HOST + "/medal", json={"rank": "fake", "athleteID": 15}).status_code
# Post on '/medal' with correct body
post_request = requests.post(
HOST + "/medal", json={"rank": "gold", "athleteID": 15})
assert 200 == post_request.status_code
id = json.loads(post_request.text)['id']
# Get newly created object
expected_result = f"""{{
"athleteID": 15,
"id": {id},
"rank": "gold"
}}
"""
assert expected_result == requests.get(HOST + f"/medal/{id}").text
assert 400 == requests.patch(
HOST + f"/medal/{id}", json={"should": "fail"}).status_code
# Test to patch with incorrect value for rank
assert 400 == requests.patch(
HOST + f"/medal/{id}", json={"rank": "fkae"}).status_code
# Test to patch object with correct data
assert 200 == requests.patch(
HOST + f"/medal/{id}", json={"rank": "silver"}).status_code
expected_result = f"""{{
"athleteID": 15,
"id": {id},
"rank": "silver"
}}
"""
assert expected_result == requests.get(HOST + f"/medal/{id}").text
# test delete methode on the newly created medal
assert 200 == requests.delete(HOST + f"/medal/{id}").status_code
# verify that the object don't exist anymore
assert 404 == requests.get(HOST + f"/medal/{id}").status_code