Coverage for tests / test_allow_inf_nan_in_enforcing.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated 1abcd

2 

3import pytest 1abcd

4from fastapi import Body, FastAPI, Query 1abcd

5from fastapi.testclient import TestClient 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10@app.post("/") 1abcd

11async def get( 1abcd

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

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

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

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

16) -> str: 

17 return "OK" 1efghijklmnop

18 

19 

20client = TestClient(app) 1abcd

21 

22 

23@pytest.mark.parametrize( 1abcd

24 "value,code", 

25 [ 

26 ("-1", 200), 

27 ("inf", 200), 

28 ("-inf", 200), 

29 ("nan", 200), 

30 ("0", 200), 

31 ("342", 200), 

32 ], 

33) 

34def test_allow_inf_nan_param_true(value: str, code: int): 1abcd

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

36 assert response.status_code == code, response.text 1hlp

37 

38 

39@pytest.mark.parametrize( 1abcd

40 "value,code", 

41 [ 

42 ("-1", 200), 

43 ("inf", 422), 

44 ("-inf", 422), 

45 ("nan", 422), 

46 ("0", 200), 

47 ("342", 200), 

48 ], 

49) 

50def test_allow_inf_nan_param_false(value: str, code: int): 1abcd

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

52 assert response.status_code == code, response.text 1gko

53 

54 

55@pytest.mark.parametrize( 1abcd

56 "value,code", 

57 [ 

58 ("-1", 200), 

59 ("inf", 200), 

60 ("-inf", 200), 

61 ("nan", 200), 

62 ("0", 200), 

63 ("342", 200), 

64 ], 

65) 

66def test_allow_inf_nan_param_default(value: str, code: int): 1abcd

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

68 assert response.status_code == code, response.text 1fjn

69 

70 

71@pytest.mark.parametrize( 1abcd

72 "value,code", 

73 [ 

74 ("-1", 200), 

75 ("inf", 422), 

76 ("-inf", 422), 

77 ("nan", 422), 

78 ("0", 200), 

79 ("342", 200), 

80 ], 

81) 

82def test_allow_inf_nan_body(value: str, code: int): 1abcd

83 response = client.post("/", json=value) 1eim

84 assert response.status_code == code, response.text 1eim