flask-demo-api/tests/test_put.py

93 lines
2.9 KiB
Python

import json
def test_put_with_wrong_content_type(client):
data = json.dumps({})
response = client.put("/", data=data)
assert response.status_code == 415
def test_put_with_empty_data(client):
data = json.dumps({})
response = client.put("/", data=data, content_type="application/json")
assert response.status_code == 400
def test_put_with_wrong_data(client):
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
put_data = json.dumps({"key": "abcasd", "val": "def"})
response = client.put("/", data=put_data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {"error": "400 Bad Request: No item for update"}
client.delete("/", data=data, content_type="application/json")
response = client.get("/?key=abcasd")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
def test_put_with_correct_data(client):
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
put_data = json.dumps({"key": "abc", "val": "defan"})
response = client.put("/", data=put_data, content_type="application/json")
assert response.status_code == 200
assert response.get_json() == {
"message": "Updated",
"data": {"key": "abc", "val": "defan"},
}
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_put_without_key(client):
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
put_data = json.dumps({"val": "defdd"})
response = client.put("/", data=put_data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {"error": "400 Bad Request: 'key' required"}
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_put_without_value(client):
data = json.dumps({"key": "abc", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
put_data = json.dumps({"key": "abcd"})
response = client.put("/", data=put_data, content_type="application/json")
assert response.status_code == 400
assert response.get_json() == {"error": "400 Bad Request: 'val' required"}
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",
}