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

35 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE 1eabcd

4from fastapi.testclient import TestClient 1eabcd

5 

6from ...utils import needs_py39 1eabcd

7 

8 

9@pytest.fixture(name="client") 1eabcd

10def get_client(): 1eabcd

11 from docs_src.query_params_str_validations.tutorial010_an_py39 import app 1abcd

12 

13 client = TestClient(app) 1abcd

14 return client 1abcd

15 

16 

17@needs_py39 1eabcd

18def test_query_params_str_validations_no_query(client: TestClient): 1eabcd

19 response = client.get("/items/") 1fghi

20 assert response.status_code == 200 1fghi

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

22 

23 

24@needs_py39 1eabcd

25def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1eabcd

26 response = client.get("/items/", params={"item-query": "fixedquery"}) 1jklm

27 assert response.status_code == 200 1jklm

28 assert response.json() == { 1jklm

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

30 "q": "fixedquery", 

31 } 

32 

33 

34@needs_py39 1eabcd

35def test_query_params_str_validations_q_fixedquery(client: TestClient): 1eabcd

36 response = client.get("/items/", params={"q": "fixedquery"}) 1nopq

37 assert response.status_code == 200 1nopq

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

39 

40 

41@needs_py39 1eabcd

42def test_query_params_str_validations_item_query_nonregexquery(client: TestClient): 1eabcd

43 response = client.get("/items/", params={"item-query": "nonregexquery"}) 1rstu

44 assert response.status_code == 422 1rstu

45 assert response.json() == IsDict( 1rstu

46 { 

47 "detail": [ 

48 { 

49 "type": "string_pattern_mismatch", 

50 "loc": ["query", "item-query"], 

51 "msg": "String should match pattern '^fixedquery$'", 

52 "input": "nonregexquery", 

53 "ctx": {"pattern": "^fixedquery$"}, 

54 } 

55 ] 

56 } 

57 ) | IsDict( 

58 # TODO: remove when deprecating Pydantic v1 

59 { 

60 "detail": [ 

61 { 

62 "ctx": {"pattern": "^fixedquery$"}, 

63 "loc": ["query", "item-query"], 

64 "msg": 'string does not match regex "^fixedquery$"', 

65 "type": "value_error.str.regex", 

66 } 

67 ] 

68 } 

69 ) 

70 

71 

72@needs_py39 1eabcd

73def test_openapi_schema(client: TestClient): 1eabcd

74 response = client.get("/openapi.json") 1vwxy

75 assert response.status_code == 200, response.text 1vwxy

76 assert response.json() == { 1vwxy

77 "openapi": "3.1.0", 

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

79 "paths": { 

80 "/items/": { 

81 "get": { 

82 "responses": { 

83 "200": { 

84 "description": "Successful Response", 

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

86 }, 

87 "422": { 

88 "description": "Validation Error", 

89 "content": { 

90 "application/json": { 

91 "schema": { 

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

93 } 

94 } 

95 }, 

96 }, 

97 }, 

98 "summary": "Read Items", 

99 "operationId": "read_items_items__get", 

100 "parameters": [ 

101 { 

102 "description": "Query string for the items to search in the database that have a good match", 

103 "required": False, 

104 "deprecated": True, 

105 "schema": IsDict( 

106 { 

107 "anyOf": [ 

108 { 

109 "type": "string", 

110 "minLength": 3, 

111 "maxLength": 50, 

112 "pattern": "^fixedquery$", 

113 }, 

114 {"type": "null"}, 

115 ], 

116 "title": "Query string", 

117 "description": "Query string for the items to search in the database that have a good match", 

118 # See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34. 

119 **( 

120 {"deprecated": True} 

121 if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) 

122 else {} 

123 ), 

124 } 

125 ) 

126 | IsDict( 

127 # TODO: remove when deprecating Pydantic v1 

128 { 

129 "title": "Query string", 

130 "maxLength": 50, 

131 "minLength": 3, 

132 "pattern": "^fixedquery$", 

133 "type": "string", 

134 "description": "Query string for the items to search in the database that have a good match", 

135 } 

136 ), 

137 "name": "item-query", 

138 "in": "query", 

139 } 

140 ], 

141 } 

142 } 

143 }, 

144 "components": { 

145 "schemas": { 

146 "ValidationError": { 

147 "title": "ValidationError", 

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

149 "type": "object", 

150 "properties": { 

151 "loc": { 

152 "title": "Location", 

153 "type": "array", 

154 "items": { 

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

156 }, 

157 }, 

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

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

160 }, 

161 }, 

162 "HTTPValidationError": { 

163 "title": "HTTPValidationError", 

164 "type": "object", 

165 "properties": { 

166 "detail": { 

167 "title": "Detail", 

168 "type": "array", 

169 "items": {"$ref": "#/components/schemas/ValidationError"}, 

170 } 

171 }, 

172 }, 

173 } 

174 }, 

175 }