Coverage for tests/test_allow_inf_nan_in_enforcing.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-13 13:38 +0000

1import pytest 1abcde

2from fastapi import Body, FastAPI, Query 1abcde

3from fastapi.testclient import TestClient 1abcde

4from typing_extensions import Annotated 1abcde

5 

6app = FastAPI() 1abcde

7 

8 

9@app.post("/") 1abcde

10async def get( 1abcde

11 x: Annotated[float, Query(allow_inf_nan=True)] = 0, 

12 y: Annotated[float, Query(allow_inf_nan=False)] = 0, 

13 z: Annotated[float, Query()] = 0, 

14 b: Annotated[float, Body(allow_inf_nan=False)] = 0, 

15) -> str: 

16 return "OK" 1fghijklmnopqrstuvwxy

17 

18 

19client = TestClient(app) 1abcde

20 

21 

22@pytest.mark.parametrize( 1abcde

23 "value,code", 

24 [ 

25 ("-1", 200), 

26 ("inf", 200), 

27 ("-inf", 200), 

28 ("nan", 200), 

29 ("0", 200), 

30 ("342", 200), 

31 ], 

32) 

33def test_allow_inf_nan_param_true(value: str, code: int): 1abcde

34 response = client.post(f"/?x={value}") 1imquy

35 assert response.status_code == code, response.text 1imquy

36 

37 

38@pytest.mark.parametrize( 1abcde

39 "value,code", 

40 [ 

41 ("-1", 200), 

42 ("inf", 422), 

43 ("-inf", 422), 

44 ("nan", 422), 

45 ("0", 200), 

46 ("342", 200), 

47 ], 

48) 

49def test_allow_inf_nan_param_false(value: str, code: int): 1abcde

50 response = client.post(f"/?y={value}") 1hlptx

51 assert response.status_code == code, response.text 1hlptx

52 

53 

54@pytest.mark.parametrize( 1abcde

55 "value,code", 

56 [ 

57 ("-1", 200), 

58 ("inf", 200), 

59 ("-inf", 200), 

60 ("nan", 200), 

61 ("0", 200), 

62 ("342", 200), 

63 ], 

64) 

65def test_allow_inf_nan_param_default(value: str, code: int): 1abcde

66 response = client.post(f"/?z={value}") 1gkosw

67 assert response.status_code == code, response.text 1gkosw

68 

69 

70@pytest.mark.parametrize( 1abcde

71 "value,code", 

72 [ 

73 ("-1", 200), 

74 ("inf", 422), 

75 ("-inf", 422), 

76 ("nan", 422), 

77 ("0", 200), 

78 ("342", 200), 

79 ], 

80) 

81def test_allow_inf_nan_body(value: str, code: int): 1abcde

82 response = client.post("/", json=value) 1fjnrv

83 assert response.status_code == code, response.text 1fjnrv