Debian Codegraph @

tests/test_orjson_response_class.py

raw
1 import warnings
2
3 import pytest
4
5 pytest.importorskip("orjson")
6
7 from fastapi import FastAPI
8 from fastapi.exceptions import FastAPIDeprecationWarning
9 from fastapi.responses import ORJSONResponse # ty: ignore[deprecated]
10 from fastapi.testclient import TestClient
11 from sqlalchemy.sql.elements import quoted_name
12
13 with warnings.catch_warnings():
14 warnings.simplefilter("ignore", FastAPIDeprecationWarning)
15 app = FastAPI(default_response_class=ORJSONResponse) # ty: ignore[deprecated]
16
17
18 @app.get("/orjson_non_str_keys")
19 def get_orjson_non_str_keys():
20 key = quoted_name(value="msg", quote=False)
21 return {key: "Hello World", 1: 1}
22
23
24 client = TestClient(app)
25
26
27 def test_orjson_non_str_keys():
28 with warnings.catch_warnings():
29 warnings.simplefilter("ignore", FastAPIDeprecationWarning)
30 with client:
31 response = client.get("/orjson_non_str_keys")
32 assert response.json() == {"msg": "Hello World", "1": 1}