Coverage for tests / test_allow_inf_nan_in_enforcing.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +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" 1efghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

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}") 1qrstuvwxKLMNOP234567

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

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}") 1nop89!#$HIJ%'(Z01)*+

52 assert response.status_code == code, response.text 1nop89!#$HIJ%'(Z01)*+

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}") 1hijklm,-BCDEFGTUVWXY

68 assert response.status_code == code, response.text 1hijklm,-BCDEFGTUVWXY

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) 1efg./:;=yzA?@[QRS]^_

84 assert response.status_code == code, response.text 1efg./:;=yzA?@[QRS]^_