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

15 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5 

6@pytest.fixture(name="client") 1abcde

7def get_client(): 1abcde

8 from docs_src.schema_extra_example.tutorial005 import app 1abcde

9 

10 client = TestClient(app) 1abcde

11 return client 1abcde

12 

13 

14def test_post_body_example(client: TestClient): 1abcde

15 response = client.put( 1abcde

16 "/items/5", 

17 json={ 

18 "name": "Foo", 

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

20 "price": 35.4, 

21 "tax": 3.2, 

22 }, 

23 ) 

24 assert response.status_code == 200 1abcde

25 

26 

27def test_openapi_schema(client: TestClient) -> None: 1abcde

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

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

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

31 "openapi": "3.1.0", 

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

33 "paths": { 

34 "/items/{item_id}": { 

35 "put": { 

36 "summary": "Update Item", 

37 "operationId": "update_item_items__item_id__put", 

38 "parameters": [ 

39 { 

40 "required": True, 

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

42 "name": "item_id", 

43 "in": "path", 

44 } 

45 ], 

46 "requestBody": { 

47 "content": { 

48 "application/json": { 

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

50 | IsDict( 

51 # TODO: remove when deprecating Pydantic v1 

52 { 

53 "allOf": [ 

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

55 ], 

56 "title": "Item", 

57 } 

58 ), 

59 "examples": { 

60 "normal": { 

61 "summary": "A normal example", 

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

63 "value": { 

64 "name": "Foo", 

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

66 "price": 35.4, 

67 "tax": 3.2, 

68 }, 

69 }, 

70 "converted": { 

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

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

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

74 }, 

75 "invalid": { 

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

77 "value": { 

78 "name": "Baz", 

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

80 }, 

81 }, 

82 }, 

83 } 

84 }, 

85 "required": True, 

86 }, 

87 "responses": { 

88 "200": { 

89 "description": "Successful Response", 

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

91 }, 

92 "422": { 

93 "description": "Validation Error", 

94 "content": { 

95 "application/json": { 

96 "schema": { 

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

98 } 

99 } 

100 }, 

101 }, 

102 }, 

103 } 

104 } 

105 }, 

106 "components": { 

107 "schemas": { 

108 "HTTPValidationError": { 

109 "title": "HTTPValidationError", 

110 "type": "object", 

111 "properties": { 

112 "detail": { 

113 "title": "Detail", 

114 "type": "array", 

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

116 } 

117 }, 

118 }, 

119 "Item": { 

120 "title": "Item", 

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

122 "type": "object", 

123 "properties": { 

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

125 "description": IsDict( 

126 { 

127 "title": "Description", 

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

129 } 

130 ) 

131 | IsDict( 

132 # TODO: remove when deprecating Pydantic v1 

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

134 ), 

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

136 "tax": IsDict( 

137 { 

138 "title": "Tax", 

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

140 } 

141 ) 

142 | IsDict( 

143 # TODO: remove when deprecating Pydantic v1 

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

145 ), 

146 }, 

147 }, 

148 "ValidationError": { 

149 "title": "ValidationError", 

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

151 "type": "object", 

152 "properties": { 

153 "loc": { 

154 "title": "Location", 

155 "type": "array", 

156 "items": { 

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

158 }, 

159 }, 

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

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

162 }, 

163 }, 

164 } 

165 }, 

166 }