Coverage for tests/test_regex_deprecated_body.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi import FastAPI, Form 1deabc

4from fastapi.testclient import TestClient 1deabc

5from typing_extensions import Annotated 1deabc

6 

7from .utils import needs_py310 1deabc

8 

9 

10def get_client(): 1deabc

11 app = FastAPI() 1abc

12 with pytest.warns(DeprecationWarning): 1abc

13 

14 @app.post("/items/") 1abc

15 async def read_items( 1abc

16 q: Annotated[str | None, Form(regex="^fixedquery$")] = None, 

17 ): 

18 if q: 1abc

19 return f"Hello {q}" 1abc

20 else: 

21 return "Hello World" 1abc

22 

23 client = TestClient(app) 1abc

24 return client 1abc

25 

26 

27@needs_py310 1deabc

28def test_no_query(): 1deabc

29 client = get_client() 1abc

30 response = client.post("/items/") 1abc

31 assert response.status_code == 200 1abc

32 assert response.json() == "Hello World" 1abc

33 

34 

35@needs_py310 1deabc

36def test_q_fixedquery(): 1deabc

37 client = get_client() 1abc

38 response = client.post("/items/", data={"q": "fixedquery"}) 1abc

39 assert response.status_code == 200 1abc

40 assert response.json() == "Hello fixedquery" 1abc

41 

42 

43@needs_py310 1deabc

44def test_query_nonregexquery(): 1deabc

45 client = get_client() 1abc

46 response = client.post("/items/", data={"q": "nonregexquery"}) 1abc

47 assert response.status_code == 422 1abc

48 assert response.json() == IsDict( 1abc

49 { 

50 "detail": [ 

51 { 

52 "type": "string_pattern_mismatch", 

53 "loc": ["body", "q"], 

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

55 "input": "nonregexquery", 

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

57 } 

58 ] 

59 } 

60 ) | IsDict( 

61 # TODO: remove when deprecating Pydantic v1 

62 { 

63 "detail": [ 

64 { 

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

66 "loc": ["body", "q"], 

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

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

69 } 

70 ] 

71 } 

72 ) 

73 

74 

75@needs_py310 1deabc

76def test_openapi_schema(): 1deabc

77 client = get_client() 1abc

78 response = client.get("/openapi.json") 1abc

79 assert response.status_code == 200, response.text 1abc

80 # insert_assert(response.json()) 

81 assert response.json() == { 1abc

82 "openapi": "3.1.0", 

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

84 "paths": { 

85 "/items/": { 

86 "post": { 

87 "summary": "Read Items", 

88 "operationId": "read_items_items__post", 

89 "requestBody": { 

90 "content": { 

91 "application/x-www-form-urlencoded": { 

92 "schema": IsDict( 

93 { 

94 "allOf": [ 

95 { 

96 "$ref": "#/components/schemas/Body_read_items_items__post" 

97 } 

98 ], 

99 "title": "Body", 

100 } 

101 ) 

102 | IsDict( 

103 # TODO: remove when deprecating Pydantic v1 

104 { 

105 "$ref": "#/components/schemas/Body_read_items_items__post" 

106 } 

107 ) 

108 } 

109 } 

110 }, 

111 "responses": { 

112 "200": { 

113 "description": "Successful Response", 

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

115 }, 

116 "422": { 

117 "description": "Validation Error", 

118 "content": { 

119 "application/json": { 

120 "schema": { 

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

122 } 

123 } 

124 }, 

125 }, 

126 }, 

127 } 

128 } 

129 }, 

130 "components": { 

131 "schemas": { 

132 "Body_read_items_items__post": { 

133 "properties": { 

134 "q": IsDict( 

135 { 

136 "anyOf": [ 

137 {"type": "string", "pattern": "^fixedquery$"}, 

138 {"type": "null"}, 

139 ], 

140 "title": "Q", 

141 } 

142 ) 

143 | IsDict( 

144 # TODO: remove when deprecating Pydantic v1 

145 {"type": "string", "pattern": "^fixedquery$", "title": "Q"} 

146 ) 

147 }, 

148 "type": "object", 

149 "title": "Body_read_items_items__post", 

150 }, 

151 "HTTPValidationError": { 

152 "properties": { 

153 "detail": { 

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

155 "type": "array", 

156 "title": "Detail", 

157 } 

158 }, 

159 "type": "object", 

160 "title": "HTTPValidationError", 

161 }, 

162 "ValidationError": { 

163 "properties": { 

164 "loc": { 

165 "items": { 

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

167 }, 

168 "type": "array", 

169 "title": "Location", 

170 }, 

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

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

173 }, 

174 "type": "object", 

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

176 "title": "ValidationError", 

177 }, 

178 } 

179 }, 

180 }