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

11 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.schema_extra_example.tutorial004_an import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

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

10def test_post_body_example(): 1abcde

11 response = client.put( 1abcde

12 "/items/5", 

13 json={ 

14 "name": "Foo", 

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

16 "price": 35.4, 

17 "tax": 3.2, 

18 }, 

19 ) 

20 assert response.status_code == 200 1abcde

21 

22 

23def test_openapi_schema(): 1abcde

24 response = client.get("/openapi.json") 1abcde

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

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

27 "openapi": "3.1.0", 

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

29 "paths": { 

30 "/items/{item_id}": { 

31 "put": { 

32 "summary": "Update Item", 

33 "operationId": "update_item_items__item_id__put", 

34 "parameters": [ 

35 { 

36 "required": True, 

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

38 "name": "item_id", 

39 "in": "path", 

40 } 

41 ], 

42 "requestBody": { 

43 "content": { 

44 "application/json": { 

45 "schema": IsDict( 

46 { 

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

48 "examples": [ 

49 { 

50 "name": "Foo", 

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

52 "price": 35.4, 

53 "tax": 3.2, 

54 }, 

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

56 { 

57 "name": "Baz", 

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

59 }, 

60 ], 

61 } 

62 ) 

63 | IsDict( 

64 # TODO: remove when deprecating Pydantic v1 

65 { 

66 "allOf": [ 

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

68 ], 

69 "title": "Item", 

70 "examples": [ 

71 { 

72 "name": "Foo", 

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

74 "price": 35.4, 

75 "tax": 3.2, 

76 }, 

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

78 { 

79 "name": "Baz", 

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

81 }, 

82 ], 

83 } 

84 ) 

85 } 

86 }, 

87 "required": True, 

88 }, 

89 "responses": { 

90 "200": { 

91 "description": "Successful Response", 

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

93 }, 

94 "422": { 

95 "description": "Validation Error", 

96 "content": { 

97 "application/json": { 

98 "schema": { 

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

100 } 

101 } 

102 }, 

103 }, 

104 }, 

105 } 

106 } 

107 }, 

108 "components": { 

109 "schemas": { 

110 "HTTPValidationError": { 

111 "title": "HTTPValidationError", 

112 "type": "object", 

113 "properties": { 

114 "detail": { 

115 "title": "Detail", 

116 "type": "array", 

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

118 } 

119 }, 

120 }, 

121 "Item": { 

122 "title": "Item", 

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

124 "type": "object", 

125 "properties": { 

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

127 "description": IsDict( 

128 { 

129 "title": "Description", 

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

131 } 

132 ) 

133 | IsDict( 

134 # TODO: remove when deprecating Pydantic v1 

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

136 ), 

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

138 "tax": IsDict( 

139 { 

140 "title": "Tax", 

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

142 } 

143 ) 

144 | IsDict( 

145 # TODO: remove when deprecating Pydantic v1 

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

147 ), 

148 }, 

149 }, 

150 "ValidationError": { 

151 "title": "ValidationError", 

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

153 "type": "object", 

154 "properties": { 

155 "loc": { 

156 "title": "Location", 

157 "type": "array", 

158 "items": { 

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

160 }, 

161 }, 

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

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

164 }, 

165 }, 

166 } 

167 }, 

168 }