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

18 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 dirty_equals import IsList 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8from ...utils import needs_py310 1abdc

9 

10 

11@pytest.fixture( 1abdc

12 name="client", 

13 params=[ 

14 pytest.param("tutorial001_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest) -> TestClient: 1abdc

18 mod = importlib.import_module( 1abc

19 f"docs_src.path_operation_configuration.{request.param}" 

20 ) 

21 return TestClient(mod.app) 1abc

22 

23 

24def test_post_items(client: TestClient): 1abdc

25 response = client.post( 1efg

26 "/items/", 

27 json={ 

28 "name": "Foo", 

29 "description": "Item description", 

30 "price": 42.0, 

31 "tax": 3.2, 

32 "tags": ["bar", "baz"], 

33 }, 

34 ) 

35 assert response.status_code == 201, response.text 1efg

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

37 "name": "Foo", 

38 "description": "Item description", 

39 "price": 42.0, 

40 "tax": 3.2, 

41 "tags": IsList("bar", "baz", check_order=False), 

42 } 

43 

44 

45def test_openapi_schema(client: TestClient): 1abdc

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

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

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

49 { 

50 "openapi": "3.1.0", 

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

52 "paths": { 

53 "/items/": { 

54 "post": { 

55 "summary": "Create Item", 

56 "operationId": "create_item_items__post", 

57 "requestBody": { 

58 "content": { 

59 "application/json": { 

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

61 } 

62 }, 

63 "required": True, 

64 }, 

65 "responses": { 

66 "201": { 

67 "description": "Successful Response", 

68 "content": { 

69 "application/json": { 

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

71 } 

72 }, 

73 }, 

74 "422": { 

75 "description": "Validation Error", 

76 "content": { 

77 "application/json": { 

78 "schema": { 

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

80 } 

81 } 

82 }, 

83 }, 

84 }, 

85 }, 

86 }, 

87 }, 

88 "components": { 

89 "schemas": { 

90 "HTTPValidationError": { 

91 "properties": { 

92 "detail": { 

93 "items": { 

94 "$ref": "#/components/schemas/ValidationError", 

95 }, 

96 "title": "Detail", 

97 "type": "array", 

98 }, 

99 }, 

100 "title": "HTTPValidationError", 

101 "type": "object", 

102 }, 

103 "Item": { 

104 "properties": { 

105 "description": { 

106 "anyOf": [ 

107 { 

108 "type": "string", 

109 }, 

110 { 

111 "type": "null", 

112 }, 

113 ], 

114 "title": "Description", 

115 }, 

116 "name": { 

117 "title": "Name", 

118 "type": "string", 

119 }, 

120 "price": { 

121 "title": "Price", 

122 "type": "number", 

123 }, 

124 "tags": { 

125 "default": [], 

126 "items": { 

127 "type": "string", 

128 }, 

129 "title": "Tags", 

130 "type": "array", 

131 "uniqueItems": True, 

132 }, 

133 "tax": { 

134 "anyOf": [ 

135 { 

136 "type": "number", 

137 }, 

138 { 

139 "type": "null", 

140 }, 

141 ], 

142 "title": "Tax", 

143 }, 

144 }, 

145 "required": [ 

146 "name", 

147 "price", 

148 ], 

149 "title": "Item", 

150 "type": "object", 

151 }, 

152 "ValidationError": { 

153 "properties": { 

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

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

156 "loc": { 

157 "items": { 

158 "anyOf": [ 

159 { 

160 "type": "string", 

161 }, 

162 { 

163 "type": "integer", 

164 }, 

165 ], 

166 }, 

167 "title": "Location", 

168 "type": "array", 

169 }, 

170 "msg": { 

171 "title": "Message", 

172 "type": "string", 

173 }, 

174 "type": { 

175 "title": "Error Type", 

176 "type": "string", 

177 }, 

178 }, 

179 "required": [ 

180 "loc", 

181 "msg", 

182 "type", 

183 ], 

184 "title": "ValidationError", 

185 "type": "object", 

186 }, 

187 }, 

188 }, 

189 } 

190 )