Coverage for tests / test_tutorial / test_body / test_tutorial004.py: 100%

26 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 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

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

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_put_all(client: TestClient): 1abdc

24 response = client.put( 1efg

25 "/items/123", 

26 json={"name": "Foo", "price": 50.1, "description": "Some Foo", "tax": 0.3}, 

27 params={"q": "somequery"}, 

28 ) 

29 assert response.status_code == 200 1efg

30 assert response.json() == { 1efg

31 "item_id": 123, 

32 "name": "Foo", 

33 "price": 50.1, 

34 "description": "Some Foo", 

35 "tax": 0.3, 

36 "q": "somequery", 

37 } 

38 

39 

40def test_put_only_required(client: TestClient): 1abdc

41 response = client.put( 1hij

42 "/items/123", 

43 json={"name": "Foo", "price": 50.1}, 

44 ) 

45 assert response.status_code == 200 1hij

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

47 "item_id": 123, 

48 "name": "Foo", 

49 "price": 50.1, 

50 "description": None, 

51 "tax": None, 

52 } 

53 

54 

55def test_put_with_no_data(client: TestClient): 1abdc

56 response = client.put("/items/123", json={}) 1klm

57 assert response.status_code == 422 1klm

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

59 "detail": [ 

60 { 

61 "type": "missing", 

62 "loc": ["body", "name"], 

63 "msg": "Field required", 

64 "input": {}, 

65 }, 

66 { 

67 "type": "missing", 

68 "loc": ["body", "price"], 

69 "msg": "Field required", 

70 "input": {}, 

71 }, 

72 ] 

73 } 

74 

75 

76def test_openapi_schema(client: TestClient): 1abdc

77 response = client.get("/openapi.json") 1nop

78 assert response.status_code == 200, response.text 1nop

79 assert response.json() == snapshot( 1nop

80 { 

81 "openapi": "3.1.0", 

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

83 "paths": { 

84 "/items/{item_id}": { 

85 "put": { 

86 "parameters": [ 

87 { 

88 "in": "path", 

89 "name": "item_id", 

90 "required": True, 

91 "schema": { 

92 "title": "Item Id", 

93 "type": "integer", 

94 }, 

95 }, 

96 { 

97 "required": False, 

98 "schema": { 

99 "anyOf": [{"type": "string"}, {"type": "null"}], 

100 "title": "Q", 

101 }, 

102 "name": "q", 

103 "in": "query", 

104 }, 

105 ], 

106 "responses": { 

107 "200": { 

108 "description": "Successful Response", 

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

110 }, 

111 "422": { 

112 "description": "Validation Error", 

113 "content": { 

114 "application/json": { 

115 "schema": { 

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

117 } 

118 } 

119 }, 

120 }, 

121 }, 

122 "summary": "Update Item", 

123 "operationId": "update_item_items__item_id__put", 

124 "requestBody": { 

125 "content": { 

126 "application/json": { 

127 "schema": {"$ref": "#/components/schemas/Item"} 

128 } 

129 }, 

130 "required": True, 

131 }, 

132 } 

133 } 

134 }, 

135 "components": { 

136 "schemas": { 

137 "Item": { 

138 "title": "Item", 

139 "required": ["name", "price"], 

140 "type": "object", 

141 "properties": { 

142 "name": {"title": "Name", "type": "string"}, 

143 "price": {"title": "Price", "type": "number"}, 

144 "description": { 

145 "title": "Description", 

146 "anyOf": [{"type": "string"}, {"type": "null"}], 

147 }, 

148 "tax": { 

149 "title": "Tax", 

150 "anyOf": [{"type": "number"}, {"type": "null"}], 

151 }, 

152 }, 

153 }, 

154 "ValidationError": { 

155 "title": "ValidationError", 

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

157 "type": "object", 

158 "properties": { 

159 "loc": { 

160 "title": "Location", 

161 "type": "array", 

162 "items": { 

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

164 }, 

165 }, 

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

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

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

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

170 }, 

171 }, 

172 "HTTPValidationError": { 

173 "title": "HTTPValidationError", 

174 "type": "object", 

175 "properties": { 

176 "detail": { 

177 "title": "Detail", 

178 "type": "array", 

179 "items": { 

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

181 }, 

182 } 

183 }, 

184 }, 

185 } 

186 }, 

187 } 

188 )