flask-demo-api/tests/test_post.py

98 lines
3.0 KiB
Python

import json
def test_post_with_wrong_content_type(client):
data = json.dumps({})
response = client.post("/", data=data)
assert response.status_code == 415
def test_post_with_empty_data(client):
data = json.dumps({})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 400
def test_post_with_data(client):
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 201
assert response.get_json() == {
"message": "Ok",
"data": {"key": "abc", "val": "def"},
}
client.delete("/", data=data, content_type="application/json")
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
def test_post_without_key(client):
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
data = json.dumps({"val": "def"})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {"error": "400 Bad Request: 'key' required"}
def test_post_without_value(client):
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
data = json.dumps({"key": "abc"})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {"error": "400 Bad Request: 'val' required"}
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
def test_post_with_data_double_adding(client):
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 201
assert response.get_json() == {
"message": "Ok",
"data": {"key": "abc", "val": "def"},
}
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {
"error": "400 Bad Request: Key alredy exist",
}
client.delete("/", data=data, content_type="application/json")
response = client.get("/?key=abc")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}