Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial002.py: 100%

30 statements  

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

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7from ...utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[ 

13 pytest.param("tutorial002_py310", marks=needs_py310), 

14 pytest.param("tutorial002_an_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abcd

18 mod = importlib.import_module( 1efghijklmnopqrstuvwxyzABCDEFGHIJ

19 f"docs_src.query_params_str_validations.{request.param}" 

20 ) 

21 

22 client = TestClient(mod.app) 1efghijklmnopqrstuvwxyzABCDEFGHIJ

23 return client 1efghijklmnopqrstuvwxyzABCDEFGHIJ

24 

25 

26def test_query_params_str_validations_no_query(client: TestClient): 1abcd

27 response = client.get("/items/") 1KLMNOPQ

28 assert response.status_code == 200 1KLMNOPQ

29 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1KLMNOPQ

30 

31 

32def test_query_params_str_validations_q_empty_str(client: TestClient): 1abcd

33 response = client.get("/items/", params={"q": ""}) 1RSTUVWXY

34 assert response.status_code == 200 1RSTUVWXY

35 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1RSTUVWXY

36 

37 

38def test_query_params_str_validations_q_query(client: TestClient): 1abcd

39 response = client.get("/items/", params={"q": "query"}) 1Z0123456

40 assert response.status_code == 200 1Z0123456

41 assert response.json() == { 1Z0123456

42 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], 

43 "q": "query", 

44 } 

45 

46 

47def test_query_params_str_validations_q_too_long(client: TestClient): 1abcd

48 response = client.get("/items/", params={"q": "q" * 51}) 1789!#$%'

49 assert response.status_code == 422 1789!#$%'

50 assert response.json() == { 1789!#$%'

51 "detail": [ 

52 { 

53 "type": "string_too_long", 

54 "loc": ["query", "q"], 

55 "msg": "String should have at most 50 characters", 

56 "input": "q" * 51, 

57 "ctx": {"max_length": 50}, 

58 } 

59 ] 

60 } 

61 

62 

63def test_openapi_schema(client: TestClient): 1abcd

64 response = client.get("/openapi.json") 1()*+,-.

65 assert response.status_code == 200, response.text 1()*+,-.

66 assert response.json() == snapshot( 1()*+,-.

67 { 

68 "openapi": "3.1.0", 

69 "info": {"title": "FastAPI", "version": "0.1.0"}, 

70 "paths": { 

71 "/items/": { 

72 "get": { 

73 "responses": { 

74 "200": { 

75 "description": "Successful Response", 

76 "content": {"application/json": {"schema": {}}}, 

77 }, 

78 "422": { 

79 "description": "Validation Error", 

80 "content": { 

81 "application/json": { 

82 "schema": { 

83 "$ref": "#/components/schemas/HTTPValidationError" 

84 } 

85 } 

86 }, 

87 }, 

88 }, 

89 "summary": "Read Items", 

90 "operationId": "read_items_items__get", 

91 "parameters": [ 

92 { 

93 "required": False, 

94 "schema": { 

95 "anyOf": [ 

96 { 

97 "type": "string", 

98 "maxLength": 50, 

99 }, 

100 {"type": "null"}, 

101 ], 

102 "title": "Q", 

103 }, 

104 "name": "q", 

105 "in": "query", 

106 } 

107 ], 

108 } 

109 } 

110 }, 

111 "components": { 

112 "schemas": { 

113 "ValidationError": { 

114 "title": "ValidationError", 

115 "required": ["loc", "msg", "type"], 

116 "type": "object", 

117 "properties": { 

118 "loc": { 

119 "title": "Location", 

120 "type": "array", 

121 "items": { 

122 "anyOf": [{"type": "string"}, {"type": "integer"}] 

123 }, 

124 }, 

125 "msg": {"title": "Message", "type": "string"}, 

126 "type": {"title": "Error Type", "type": "string"}, 

127 "input": {"title": "Input"}, 

128 "ctx": {"title": "Context", "type": "object"}, 

129 }, 

130 }, 

131 "HTTPValidationError": { 

132 "title": "HTTPValidationError", 

133 "type": "object", 

134 "properties": { 

135 "detail": { 

136 "title": "Detail", 

137 "type": "array", 

138 "items": { 

139 "$ref": "#/components/schemas/ValidationError" 

140 }, 

141 } 

142 }, 

143 }, 

144 } 

145 }, 

146 } 

147 )