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

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.schema_extra_example.tutorial004_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16# Test required and embedded body parameters with no bodies sent 

17@needs_py310 1deabc

18def test_post_body_example(client: TestClient): 1deabc

19 response = client.put( 1abc

20 "/items/5", 

21 json={ 

22 "name": "Foo", 

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

24 "price": 35.4, 

25 "tax": 3.2, 

26 }, 

27 ) 

28 assert response.status_code == 200 1abc

29 

30 

31@needs_py310 1deabc

32def test_openapi_schema(client: TestClient): 1deabc

33 response = client.get("/openapi.json") 1abc

34 assert response.status_code == 200, response.text 1abc

35 assert response.json() == { 1abc

36 "openapi": "3.1.0", 

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

38 "paths": { 

39 "/items/{item_id}": { 

40 "put": { 

41 "summary": "Update Item", 

42 "operationId": "update_item_items__item_id__put", 

43 "parameters": [ 

44 { 

45 "required": True, 

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

47 "name": "item_id", 

48 "in": "path", 

49 } 

50 ], 

51 "requestBody": { 

52 "content": { 

53 "application/json": { 

54 "schema": IsDict( 

55 { 

56 "$ref": "#/components/schemas/Item", 

57 "examples": [ 

58 { 

59 "name": "Foo", 

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

61 "price": 35.4, 

62 "tax": 3.2, 

63 }, 

64 {"name": "Bar", "price": "35.4"}, 

65 { 

66 "name": "Baz", 

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

68 }, 

69 ], 

70 } 

71 ) 

72 | IsDict( 

73 # TODO: remove when deprecating Pydantic v1 

74 { 

75 "allOf": [ 

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

77 ], 

78 "title": "Item", 

79 "examples": [ 

80 { 

81 "name": "Foo", 

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

83 "price": 35.4, 

84 "tax": 3.2, 

85 }, 

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

87 { 

88 "name": "Baz", 

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

90 }, 

91 ], 

92 } 

93 ) 

94 } 

95 }, 

96 "required": True, 

97 }, 

98 "responses": { 

99 "200": { 

100 "description": "Successful Response", 

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

102 }, 

103 "422": { 

104 "description": "Validation Error", 

105 "content": { 

106 "application/json": { 

107 "schema": { 

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

109 } 

110 } 

111 }, 

112 }, 

113 }, 

114 } 

115 } 

116 }, 

117 "components": { 

118 "schemas": { 

119 "HTTPValidationError": { 

120 "title": "HTTPValidationError", 

121 "type": "object", 

122 "properties": { 

123 "detail": { 

124 "title": "Detail", 

125 "type": "array", 

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

127 } 

128 }, 

129 }, 

130 "Item": { 

131 "title": "Item", 

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

133 "type": "object", 

134 "properties": { 

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

136 "description": IsDict( 

137 { 

138 "title": "Description", 

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

140 } 

141 ) 

142 | IsDict( 

143 # TODO: remove when deprecating Pydantic v1 

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

145 ), 

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

147 "tax": IsDict( 

148 { 

149 "title": "Tax", 

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

151 } 

152 ) 

153 | IsDict( 

154 # TODO: remove when deprecating Pydantic v1 

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

156 ), 

157 }, 

158 }, 

159 "ValidationError": { 

160 "title": "ValidationError", 

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

162 "type": "object", 

163 "properties": { 

164 "loc": { 

165 "title": "Location", 

166 "type": "array", 

167 "items": { 

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

169 }, 

170 }, 

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

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

173 }, 

174 }, 

175 } 

176 }, 

177 }