Coverage for tests/test_tutorial/test_schema_extra_example/test_tutorial005.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial005", 

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

15 "tutorial005_an", 

16 pytest.param("tutorial005_an_py39", marks=needs_py39), 

17 pytest.param("tutorial005_an_py310", marks=needs_py310), 

18 ], 

19) 

20def get_client(request: pytest.FixtureRequest): 1abcdef

21 mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") 1abcdef

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27def test_post_body_example(client: TestClient): 1abcdef

28 response = client.put( 1mnopqr

29 "/items/5", 

30 json={ 

31 "name": "Foo", 

32 "description": "A very nice Item", 

33 "price": 35.4, 

34 "tax": 3.2, 

35 }, 

36 ) 

37 assert response.status_code == 200 1mnopqr

38 

39 

40def test_openapi_schema(client: TestClient) -> None: 1abcdef

41 response = client.get("/openapi.json") 1ghijkl

42 assert response.status_code == 200, response.text 1ghijkl

43 assert response.json() == { 1ghijkl

44 "openapi": "3.1.0", 

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

46 "paths": { 

47 "/items/{item_id}": { 

48 "put": { 

49 "summary": "Update Item", 

50 "operationId": "update_item_items__item_id__put", 

51 "parameters": [ 

52 { 

53 "required": True, 

54 "schema": {"title": "Item Id", "type": "integer"}, 

55 "name": "item_id", 

56 "in": "path", 

57 } 

58 ], 

59 "requestBody": { 

60 "content": { 

61 "application/json": { 

62 "schema": IsDict({"$ref": "#/components/schemas/Item"}) 

63 | IsDict( 

64 # TODO: remove when deprecating Pydantic v1 

65 { 

66 "allOf": [ 

67 {"$ref": "#/components/schemas/Item"} 

68 ], 

69 "title": "Item", 

70 } 

71 ), 

72 "examples": { 

73 "normal": { 

74 "summary": "A normal example", 

75 "description": "A **normal** item works correctly.", 

76 "value": { 

77 "name": "Foo", 

78 "description": "A very nice Item", 

79 "price": 35.4, 

80 "tax": 3.2, 

81 }, 

82 }, 

83 "converted": { 

84 "summary": "An example with converted data", 

85 "description": "FastAPI can convert price `strings` to actual `numbers` automatically", 

86 "value": {"name": "Bar", "price": "35.4"}, 

87 }, 

88 "invalid": { 

89 "summary": "Invalid data is rejected with an error", 

90 "value": { 

91 "name": "Baz", 

92 "price": "thirty five point four", 

93 }, 

94 }, 

95 }, 

96 } 

97 }, 

98 "required": True, 

99 }, 

100 "responses": { 

101 "200": { 

102 "description": "Successful Response", 

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

104 }, 

105 "422": { 

106 "description": "Validation Error", 

107 "content": { 

108 "application/json": { 

109 "schema": { 

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

111 } 

112 } 

113 }, 

114 }, 

115 }, 

116 } 

117 } 

118 }, 

119 "components": { 

120 "schemas": { 

121 "HTTPValidationError": { 

122 "title": "HTTPValidationError", 

123 "type": "object", 

124 "properties": { 

125 "detail": { 

126 "title": "Detail", 

127 "type": "array", 

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

129 } 

130 }, 

131 }, 

132 "Item": { 

133 "title": "Item", 

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

135 "type": "object", 

136 "properties": { 

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

138 "description": IsDict( 

139 { 

140 "title": "Description", 

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

142 } 

143 ) 

144 | IsDict( 

145 # TODO: remove when deprecating Pydantic v1 

146 {"title": "Description", "type": "string"} 

147 ), 

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

149 "tax": IsDict( 

150 { 

151 "title": "Tax", 

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

153 } 

154 ) 

155 | IsDict( 

156 # TODO: remove when deprecating Pydantic v1 

157 {"title": "Tax", "type": "number"} 

158 ), 

159 }, 

160 }, 

161 "ValidationError": { 

162 "title": "ValidationError", 

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

164 "type": "object", 

165 "properties": { 

166 "loc": { 

167 "title": "Location", 

168 "type": "array", 

169 "items": { 

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

171 }, 

172 }, 

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

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

175 }, 

176 }, 

177 } 

178 }, 

179 }