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

21 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("tutorial001_py310", marks=needs_py310), 

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_get(client: TestClient): 1abdc

24 response = client.get("/items/baz") 1efg

25 assert response.status_code == 200, response.text 1efg

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

27 "name": "Baz", 

28 "description": None, 

29 "price": 50.2, 

30 "tax": 10.5, 

31 "tags": [], 

32 } 

33 

34 

35def test_put(client: TestClient): 1abdc

36 response = client.put( 1klm

37 "/items/bar", json={"name": "Barz", "price": 3, "description": None} 

38 ) 

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

40 "name": "Barz", 

41 "description": None, 

42 "price": 3, 

43 "tax": 10.5, 

44 "tags": [], 

45 } 

46 

47 

48def test_openapi_schema(client: TestClient): 1abdc

49 response = client.get("/openapi.json") 1hij

50 assert response.status_code == 200, response.text 1hij

51 assert response.json() == snapshot( 1hij

52 { 

53 "openapi": "3.1.0", 

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

55 "paths": { 

56 "/items/{item_id}": { 

57 "get": { 

58 "responses": { 

59 "200": { 

60 "description": "Successful Response", 

61 "content": { 

62 "application/json": { 

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

64 } 

65 }, 

66 }, 

67 "422": { 

68 "description": "Validation Error", 

69 "content": { 

70 "application/json": { 

71 "schema": { 

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

73 } 

74 } 

75 }, 

76 }, 

77 }, 

78 "summary": "Read Item", 

79 "operationId": "read_item_items__item_id__get", 

80 "parameters": [ 

81 { 

82 "required": True, 

83 "schema": {"title": "Item Id", "type": "string"}, 

84 "name": "item_id", 

85 "in": "path", 

86 } 

87 ], 

88 }, 

89 "put": { 

90 "responses": { 

91 "200": { 

92 "description": "Successful Response", 

93 "content": { 

94 "application/json": { 

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

96 } 

97 }, 

98 }, 

99 "422": { 

100 "description": "Validation Error", 

101 "content": { 

102 "application/json": { 

103 "schema": { 

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

105 } 

106 } 

107 }, 

108 }, 

109 }, 

110 "summary": "Update Item", 

111 "operationId": "update_item_items__item_id__put", 

112 "parameters": [ 

113 { 

114 "required": True, 

115 "schema": {"title": "Item Id", "type": "string"}, 

116 "name": "item_id", 

117 "in": "path", 

118 } 

119 ], 

120 "requestBody": { 

121 "content": { 

122 "application/json": { 

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

124 } 

125 }, 

126 "required": True, 

127 }, 

128 }, 

129 } 

130 }, 

131 "components": { 

132 "schemas": { 

133 "Item": { 

134 "type": "object", 

135 "title": "Item", 

136 "properties": { 

137 "name": { 

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

139 "title": "Name", 

140 }, 

141 "description": { 

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

143 "title": "Description", 

144 }, 

145 "price": { 

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

147 "title": "Price", 

148 }, 

149 "tax": {"title": "Tax", "type": "number", "default": 10.5}, 

150 "tags": { 

151 "title": "Tags", 

152 "type": "array", 

153 "items": {"type": "string"}, 

154 "default": [], 

155 }, 

156 }, 

157 }, 

158 "ValidationError": { 

159 "title": "ValidationError", 

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

161 "type": "object", 

162 "properties": { 

163 "loc": { 

164 "title": "Location", 

165 "type": "array", 

166 "items": { 

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

168 }, 

169 }, 

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

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

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

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

174 }, 

175 }, 

176 "HTTPValidationError": { 

177 "title": "HTTPValidationError", 

178 "type": "object", 

179 "properties": { 

180 "detail": { 

181 "title": "Detail", 

182 "type": "array", 

183 "items": { 

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

185 }, 

186 } 

187 }, 

188 }, 

189 } 

190 }, 

191 } 

192 )