-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errors.py
More file actions
95 lines (71 loc) · 2.8 KB
/
test_errors.py
File metadata and controls
95 lines (71 loc) · 2.8 KB
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
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from python_api.utils.errors import (
AppError,
AuthenticationError,
NotFoundError,
ValidationError,
app_error_handler,
global_exception_handler,
)
@pytest.fixture
def app() -> FastAPI:
app = FastAPI()
app.add_exception_handler(AppError, app_error_handler) # type: ignore[arg-type]
app.add_exception_handler(Exception, global_exception_handler)
@app.get("/app_error")
def raise_app_error() -> None:
raise AppError("Custom app error", code="CUSTOM_ERROR", status_code=418)
@app.get("/app_error_detail")
def raise_app_error_detail() -> None:
raise AppError(
"Error with detail",
code="DETAILED_ERROR",
status_code=400,
detail={"foo": "bar"},
)
@app.get("/not_found")
def raise_not_found() -> None:
raise NotFoundError("Resource not found")
@app.get("/validation")
def raise_validation() -> None:
raise ValidationError("Invalid input")
@app.get("/auth_error")
def raise_auth_error() -> None:
raise AuthenticationError("Not authorized")
@app.get("/unexpected")
def raise_unexpected() -> None:
raise ValueError("Something went wrong")
return app
@pytest.fixture
def client(app: FastAPI) -> TestClient:
return TestClient(app, raise_server_exceptions=False)
def test_app_error(client: TestClient) -> None:
response = client.get("/app_error")
assert response.status_code == 418
assert response.json() == {"error": "CUSTOM_ERROR", "detail": "Custom app error"}
def test_app_error_with_detail(client: TestClient) -> None:
response = client.get("/app_error_detail")
assert response.status_code == 400
assert response.json() == {
"error": "DETAILED_ERROR",
"detail": "Error with detail",
"context": {"foo": "bar"},
}
def test_not_found_error(client: TestClient) -> None:
response = client.get("/not_found")
assert response.status_code == 404
assert response.json() == {"error": "NOT_FOUND", "detail": "Resource not found"}
def test_validation_error(client: TestClient) -> None:
response = client.get("/validation")
assert response.status_code == 400
assert response.json() == {"error": "VALIDATION_ERROR", "detail": "Invalid input"}
def test_authentication_error(client: TestClient) -> None:
response = client.get("/auth_error")
assert response.status_code == 401
assert response.json() == {"error": "AUTHENTICATION_ERROR", "detail": "Not authorized"}
def test_unexpected_error(client: TestClient) -> None:
response = client.get("/unexpected")
assert response.status_code == 500
assert response.json() == {"error": "INTERNAL_ERROR", "detail": "An unexpected error occurred."}