API_Microservices/sport/test.py

60 lines
1.5 KiB
Python
Raw Normal View History

2024-03-27 16:42:52 +01:00
#!/usr/bin/env python3
import requests
import json
HOST = "http://localhost:5000"
# Get root
assert 200 == requests.get(HOST + "/").status_code
# Get list of sports as a json object
get_root = requests.get(HOST + "/sport").text
if get_root != "[]\n":
assert json.loads(get_root)
# Post on '/sport' With no data
assert 400 == requests.post(HOST + "/sport").status_code
# Post on '/sport' with uncorrect body
assert 400 == requests.post(
HOST + "/sport", json={"should": "fail"}).status_code
# Post on '/sport' with correct body
post_request = requests.post(
HOST + "/sport", json={"name": "test", "category": "aquatic"})
assert 200 == post_request.status_code
id = json.loads(post_request.text)['id']
# Get newly created object
expected_result = f"""{{
"category": "aquatic",
"id": {id},
"name": "test"
}}
"""
assert expected_result == requests.get(HOST + f"/sport/{id}").text
assert 400 == requests.patch(
HOST + f"/sport/{id}", json={"should": "fail"}).status_code
# Test to patch object
assert 200 == requests.patch(
HOST + f"/sport/{id}", json={"name": "modify"}).status_code
expected_result = f"""{{
"category": "aquatic",
"id": {id},
"name": "modify"
}}
"""
assert expected_result == requests.get(HOST + f"/sport/{id}").text
# test delete methode on the newly created sport
assert 200 == requests.delete(HOST + f"/sport/{id}").status_code
# verify that the object don't exist anymore
assert 404 == requests.get(HOST + f"/sport/{id}").status_code