Coverage for tests / test_tutorial / test_request_form_models / test_tutorial001.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 "tutorial001_py310", 

12 "tutorial001_an_py310", 

13 ], 

14) 

15def get_client(request: pytest.FixtureRequest): 1abdc

16 mod = importlib.import_module(f"docs_src.request_form_models.{request.param}") 1abc

17 

18 client = TestClient(mod.app) 1abc

19 return client 1abc

20 

21 

22def test_post_body_form(client: TestClient): 1abdc

23 response = client.post("/login/", data={"username": "Foo", "password": "secret"}) 1efg

24 assert response.status_code == 200 1efg

25 assert response.json() == {"username": "Foo", "password": "secret"} 1efg

26 

27 

28def test_post_body_form_no_password(client: TestClient): 1abdc

29 response = client.post("/login/", data={"username": "Foo"}) 1hij

30 assert response.status_code == 422 1hij

31 assert response.json() == { 1hij

32 "detail": [ 

33 { 

34 "type": "missing", 

35 "loc": ["body", "password"], 

36 "msg": "Field required", 

37 "input": {"username": "Foo"}, 

38 } 

39 ] 

40 } 

41 

42 

43def test_post_body_form_no_username(client: TestClient): 1abdc

44 response = client.post("/login/", data={"password": "secret"}) 1klm

45 assert response.status_code == 422 1klm

46 assert response.json() == { 1klm

47 "detail": [ 

48 { 

49 "type": "missing", 

50 "loc": ["body", "username"], 

51 "msg": "Field required", 

52 "input": {"password": "secret"}, 

53 } 

54 ] 

55 } 

56 

57 

58def test_post_body_form_no_data(client: TestClient): 1abdc

59 response = client.post("/login/") 1nop

60 assert response.status_code == 422 1nop

61 assert response.json() == { 1nop

62 "detail": [ 

63 { 

64 "type": "missing", 

65 "loc": ["body", "username"], 

66 "msg": "Field required", 

67 "input": {}, 

68 }, 

69 { 

70 "type": "missing", 

71 "loc": ["body", "password"], 

72 "msg": "Field required", 

73 "input": {}, 

74 }, 

75 ] 

76 } 

77 

78 

79def test_post_body_json(client: TestClient): 1abdc

80 response = client.post("/login/", json={"username": "Foo", "password": "secret"}) 1qrs

81 assert response.status_code == 422, response.text 1qrs

82 assert response.json() == { 1qrs

83 "detail": [ 

84 { 

85 "type": "missing", 

86 "loc": ["body", "username"], 

87 "msg": "Field required", 

88 "input": {}, 

89 }, 

90 { 

91 "type": "missing", 

92 "loc": ["body", "password"], 

93 "msg": "Field required", 

94 "input": {}, 

95 }, 

96 ] 

97 } 

98 

99 

100def test_openapi_schema(client: TestClient): 1abdc

101 response = client.get("/openapi.json") 1tuv

102 assert response.status_code == 200, response.text 1tuv

103 assert response.json() == snapshot( 1tuv

104 { 

105 "openapi": "3.1.0", 

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

107 "paths": { 

108 "/login/": { 

109 "post": { 

110 "responses": { 

111 "200": { 

112 "description": "Successful Response", 

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

114 }, 

115 "422": { 

116 "description": "Validation Error", 

117 "content": { 

118 "application/json": { 

119 "schema": { 

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

121 } 

122 } 

123 }, 

124 }, 

125 }, 

126 "summary": "Login", 

127 "operationId": "login_login__post", 

128 "requestBody": { 

129 "content": { 

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

131 "schema": {"$ref": "#/components/schemas/FormData"} 

132 } 

133 }, 

134 "required": True, 

135 }, 

136 } 

137 } 

138 }, 

139 "components": { 

140 "schemas": { 

141 "FormData": { 

142 "properties": { 

143 "username": {"type": "string", "title": "Username"}, 

144 "password": {"type": "string", "title": "Password"}, 

145 }, 

146 "type": "object", 

147 "required": ["username", "password"], 

148 "title": "FormData", 

149 }, 

150 "ValidationError": { 

151 "title": "ValidationError", 

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

153 "type": "object", 

154 "properties": { 

155 "loc": { 

156 "title": "Location", 

157 "type": "array", 

158 "items": { 

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

160 }, 

161 }, 

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

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

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

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

166 }, 

167 }, 

168 "HTTPValidationError": { 

169 "title": "HTTPValidationError", 

170 "type": "object", 

171 "properties": { 

172 "detail": { 

173 "title": "Detail", 

174 "type": "array", 

175 "items": { 

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

177 }, 

178 } 

179 }, 

180 }, 

181 } 

182 }, 

183 } 

184 )