This commit is contained in:
2024-04-16 04:48:10 +03:00
parent 9b4184994f
commit 903c216966
17 changed files with 363 additions and 33 deletions

View File

@@ -1,7 +1,6 @@
import pytest
from dishka.integrations.flask import setup_dishka
from flask import Flask
from httpx import AsyncClient
from flask_demo_api.ioc import create_container
from flask_demo_api.main import app_factory

View File

@@ -1,3 +1,6 @@
import json
def test_get_without_args(client):
response = client.get("/")
assert response.status_code == 400
@@ -12,3 +15,26 @@ def test_get_with_wrong_key(client):
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
def test_get_with_existing_key(client):
response = client.get("/?key=abcd")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}
data = json.dumps({"key": "abcd", "val": "def"})
response = client.post("/", data=data, content_type="application/json")
assert response.status_code == 201
response = client.get("/?key=abcd")
assert response.status_code == 200
assert response.get_json() == {"message": "Ok", "data": json.loads(data)}
client.delete("/", data=data, content_type="application/json")
response = client.get("/?key=abcd")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}

View File

@@ -14,6 +14,11 @@ def test_post_with_empty_data(client):
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
@@ -21,3 +26,72 @@ def test_post_with_data(client):
"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",
}

92
tests/test_put.py Normal file
View File

@@ -0,0 +1,92 @@
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",
}