This commit is contained in:
2024-04-15 22:03:02 +00:00
parent 3e57c393c2
commit e4c6d10781
19 changed files with 507 additions and 46 deletions

28
tests/conftest.py Normal file
View File

@@ -0,0 +1,28 @@
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
@pytest.fixture(scope="session", autouse=True)
def app():
app: Flask = app_factory()
app.config.update(
{
"TESTING": True,
}
)
container = create_container()
setup_dishka(container=container, app=app, auto_inject=True)
yield app
container.close()
@pytest.fixture()
def client(app):
return app.test_client()

14
tests/test_get.py Normal file
View File

@@ -0,0 +1,14 @@
def test_get_without_args(client):
response = client.get("/")
assert response.status_code == 400
assert response.get_json() == {
"error": "400 Bad Request: Invalid GET parameters",
}
def test_get_with_wrong_key(client):
response = client.get("/?key=blabla")
assert response.status_code == 404
assert response.get_json() == {
"error": "404 Not Found: Key not found",
}

23
tests/test_post.py Normal file
View File

@@ -0,0 +1,23 @@
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):
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"},
}