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

29 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE 1abcde

4from fastapi.testclient import TestClient 1abcde

5 

6 

7@pytest.fixture(name="client") 1abcde

8def get_client(): 1abcde

9 from docs_src.query_params_str_validations.tutorial010_an import app 1abcde

10 

11 client = TestClient(app) 1abcde

12 return client 1abcde

13 

14 

15def test_query_params_str_validations_no_query(client: TestClient): 1abcde

16 response = client.get("/items/") 1fghij

17 assert response.status_code == 200 1fghij

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

19 

20 

21def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1abcde

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

23 assert response.status_code == 200 1klmno

24 assert response.json() == { 1klmno

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

26 "q": "fixedquery", 

27 } 

28 

29 

30def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abcde

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

32 assert response.status_code == 200 1pqrst

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

34 

35 

36def test_query_params_str_validations_item_query_nonregexquery(client: TestClient): 1abcde

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

38 assert response.status_code == 422 1uvwxy

39 assert response.json() == IsDict( 1uvwxy

40 { 

41 "detail": [ 

42 { 

43 "type": "string_pattern_mismatch", 

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

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

46 "input": "nonregexquery", 

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

48 } 

49 ] 

50 } 

51 ) | IsDict( 

52 # TODO: remove when deprecating Pydantic v1 

53 { 

54 "detail": [ 

55 { 

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

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

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

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

60 } 

61 ] 

62 } 

63 ) 

64 

65 

66def test_openapi_schema(client: TestClient): 1abcde

67 response = client.get("/openapi.json") 1zABCD

68 assert response.status_code == 200, response.text 1zABCD

69 assert response.json() == { 1zABCD

70 "openapi": "3.1.0", 

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

72 "paths": { 

73 "/items/": { 

74 "get": { 

75 "responses": { 

76 "200": { 

77 "description": "Successful Response", 

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

79 }, 

80 "422": { 

81 "description": "Validation Error", 

82 "content": { 

83 "application/json": { 

84 "schema": { 

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

86 } 

87 } 

88 }, 

89 }, 

90 }, 

91 "summary": "Read Items", 

92 "operationId": "read_items_items__get", 

93 "parameters": [ 

94 { 

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

96 "required": False, 

97 "deprecated": True, 

98 "schema": IsDict( 

99 { 

100 "anyOf": [ 

101 { 

102 "type": "string", 

103 "minLength": 3, 

104 "maxLength": 50, 

105 "pattern": "^fixedquery$", 

106 }, 

107 {"type": "null"}, 

108 ], 

109 "title": "Query string", 

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

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

112 **( 

113 {"deprecated": True} 

114 if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) 

115 else {} 

116 ), 

117 } 

118 ) 

119 | IsDict( 

120 # TODO: remove when deprecating Pydantic v1 

121 { 

122 "title": "Query string", 

123 "maxLength": 50, 

124 "minLength": 3, 

125 "pattern": "^fixedquery$", 

126 "type": "string", 

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

128 } 

129 ), 

130 "name": "item-query", 

131 "in": "query", 

132 } 

133 ], 

134 } 

135 } 

136 }, 

137 "components": { 

138 "schemas": { 

139 "ValidationError": { 

140 "title": "ValidationError", 

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

142 "type": "object", 

143 "properties": { 

144 "loc": { 

145 "title": "Location", 

146 "type": "array", 

147 "items": { 

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

149 }, 

150 }, 

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

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

153 }, 

154 }, 

155 "HTTPValidationError": { 

156 "title": "HTTPValidationError", 

157 "type": "object", 

158 "properties": { 

159 "detail": { 

160 "title": "Detail", 

161 "type": "array", 

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

163 } 

164 }, 

165 }, 

166 } 

167 }, 

168 }