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

18 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

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

9def get_client(): 1eabcd

10 from docs_src.schema_extra_example.tutorial005_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_post_body_example(client: TestClient): 1eabcd

18 response = client.put( 1abcd

19 "/items/5", 

20 json={ 

21 "name": "Foo", 

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

23 "price": 35.4, 

24 "tax": 3.2, 

25 }, 

26 ) 

27 assert response.status_code == 200 1abcd

28 

29 

30@needs_py39 1eabcd

31def test_openapi_schema(client: TestClient) -> None: 1eabcd

32 response = client.get("/openapi.json") 1abcd

33 assert response.status_code == 200, response.text 1abcd

34 assert response.json() == { 1abcd

35 "openapi": "3.1.0", 

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

37 "paths": { 

38 "/items/{item_id}": { 

39 "put": { 

40 "summary": "Update Item", 

41 "operationId": "update_item_items__item_id__put", 

42 "parameters": [ 

43 { 

44 "required": True, 

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

46 "name": "item_id", 

47 "in": "path", 

48 } 

49 ], 

50 "requestBody": { 

51 "content": { 

52 "application/json": { 

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

54 | IsDict( 

55 # TODO: remove when deprecating Pydantic v1 

56 { 

57 "allOf": [ 

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

59 ], 

60 "title": "Item", 

61 } 

62 ), 

63 "examples": { 

64 "normal": { 

65 "summary": "A normal example", 

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

67 "value": { 

68 "name": "Foo", 

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

70 "price": 35.4, 

71 "tax": 3.2, 

72 }, 

73 }, 

74 "converted": { 

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

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

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

78 }, 

79 "invalid": { 

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

81 "value": { 

82 "name": "Baz", 

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

84 }, 

85 }, 

86 }, 

87 } 

88 }, 

89 "required": True, 

90 }, 

91 "responses": { 

92 "200": { 

93 "description": "Successful Response", 

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

95 }, 

96 "422": { 

97 "description": "Validation Error", 

98 "content": { 

99 "application/json": { 

100 "schema": { 

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

102 } 

103 } 

104 }, 

105 }, 

106 }, 

107 } 

108 } 

109 }, 

110 "components": { 

111 "schemas": { 

112 "HTTPValidationError": { 

113 "title": "HTTPValidationError", 

114 "type": "object", 

115 "properties": { 

116 "detail": { 

117 "title": "Detail", 

118 "type": "array", 

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

120 } 

121 }, 

122 }, 

123 "Item": { 

124 "title": "Item", 

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

126 "type": "object", 

127 "properties": { 

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

129 "description": IsDict( 

130 { 

131 "title": "Description", 

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

133 } 

134 ) 

135 | IsDict( 

136 # TODO: remove when deprecating Pydantic v1 

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

138 ), 

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

140 "tax": IsDict( 

141 { 

142 "title": "Tax", 

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

144 } 

145 ) 

146 | IsDict( 

147 # TODO: remove when deprecating Pydantic v1 

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

149 ), 

150 }, 

151 }, 

152 "ValidationError": { 

153 "title": "ValidationError", 

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

155 "type": "object", 

156 "properties": { 

157 "loc": { 

158 "title": "Location", 

159 "type": "array", 

160 "items": { 

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

162 }, 

163 }, 

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

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

166 }, 

167 }, 

168 } 

169 }, 

170 }