Coverage for tests / test_deprecated_responses.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import warnings 1adbc

2 

3import pytest 1adbc

4from fastapi import FastAPI 1adbc

5from fastapi.exceptions import FastAPIDeprecationWarning 1adbc

6from fastapi.responses import ORJSONResponse, UJSONResponse 1adbc

7from fastapi.testclient import TestClient 1adbc

8from pydantic import BaseModel 1adbc

9 

10 

11class Item(BaseModel): 1adbc

12 name: str 1abc

13 price: float 1abc

14 

15 

16# ORJSON 

17 

18 

19def _make_orjson_app() -> FastAPI: 1adbc

20 with warnings.catch_warnings(): 1efgh

21 warnings.simplefilter("ignore", FastAPIDeprecationWarning) 1efgh

22 app = FastAPI(default_response_class=ORJSONResponse) 1efgh

23 

24 @app.get("/items") 1efgh

25 def get_items() -> Item: 1efgh

26 return Item(name="widget", price=9.99) 1efgh

27 

28 return app 1efgh

29 

30 

31def test_orjson_response_returns_correct_data(): 1adbc

32 app = _make_orjson_app() 1efgh

33 client = TestClient(app) 1efgh

34 with warnings.catch_warnings(): 1efgh

35 warnings.simplefilter("ignore", FastAPIDeprecationWarning) 1efgh

36 response = client.get("/items") 1efgh

37 assert response.status_code == 200 1efgh

38 assert response.json() == {"name": "widget", "price": 9.99} 1efgh

39 

40 

41def test_orjson_response_emits_deprecation_warning(): 1adbc

42 with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"): 1mnop

43 ORJSONResponse(content={"hello": "world"}) 1mnop

44 

45 

46# UJSON 

47 

48 

49def _make_ujson_app() -> FastAPI: 1adbc

50 with warnings.catch_warnings(): 1ijkl

51 warnings.simplefilter("ignore", FastAPIDeprecationWarning) 1ijkl

52 app = FastAPI(default_response_class=UJSONResponse) 1ijkl

53 

54 @app.get("/items") 1ijkl

55 def get_items() -> Item: 1ijkl

56 return Item(name="widget", price=9.99) 1ijkl

57 

58 return app 1ijkl

59 

60 

61def test_ujson_response_returns_correct_data(): 1adbc

62 app = _make_ujson_app() 1ijkl

63 client = TestClient(app) 1ijkl

64 with warnings.catch_warnings(): 1ijkl

65 warnings.simplefilter("ignore", FastAPIDeprecationWarning) 1ijkl

66 response = client.get("/items") 1ijkl

67 assert response.status_code == 200 1ijkl

68 assert response.json() == {"name": "widget", "price": 9.99} 1ijkl

69 

70 

71def test_ujson_response_emits_deprecation_warning(): 1adbc

72 with pytest.warns(FastAPIDeprecationWarning, match="UJSONResponse is deprecated"): 1qrst

73 UJSONResponse(content={"hello": "world"}) 1qrst