1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
import pytest
from django.http import Http404
from ninja import NinjaAPI, Schema
from ninja.testing import TestAsyncClient, TestClient
api = NinjaAPI()
class CustomException(Exception):
pass
@api.exception_handler(CustomException)
def on_custom_error(request, exc):
return api.create_response(request, {"custom": True}, status=422)
class Payload(Schema):
test: int
@api.post("/error/{code}")
def err_thrower(request, code: str, payload: Payload = None):
if code == "base":
raise RuntimeError("test")
if code == "404":
raise Http404("test")
if code == "custom":
raise CustomException("test")
client = TestClient(api)
def test_default_handler(settings):
settings.DEBUG = True
response = client.post("/error/base")
assert response.status_code == 500
assert b"RuntimeError: test" in response.content
response = client.post("/error/404")
assert response.status_code == 404
assert response.json() == {"detail": "Not Found: test"}
response = client.post("/error/custom", body="invalid_json")
assert response.status_code == 400
assert response.json() == {
"detail": "Cannot parse request body (Expecting value: line 1 column 1 (char 0))",
}
settings.DEBUG = False
with pytest.raises(RuntimeError):
response = client.post("/error/base")
response = client.post("/error/custom", body="invalid_json")
assert response.status_code == 400
assert response.json() == {"detail": "Cannot parse request body"}
@pytest.mark.parametrize(
"route,status_code,json",
[
("/error/404", 404, {"detail": "Not Found"}),
("/error/custom", 422, {"custom": True}),
],
)
def test_exceptions(route, status_code, json):
response = client.post(route)
assert response.status_code == status_code
assert response.json() == json
@pytest.mark.asyncio
async def test_asyncio_exceptions():
api = NinjaAPI()
@api.get("/error")
async def thrower(request):
raise Http404("test")
client = TestAsyncClient(api)
response = await client.get("/error")
assert response.status_code == 404
def test_no_handlers():
api = NinjaAPI()
api._exception_handlers = {}
@api.get("/error")
def thrower(request):
raise RuntimeError("test")
client = TestClient(api)
with pytest.raises(RuntimeError):
client.get("/error")
|