Coverage for tests/test_tutorial/test_response_model/test_tutorial005_py310.py: 100%

24 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, IsOneOf 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.response_model.tutorial005_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_read_item_name(client: TestClient): 1deabc

18 response = client.get("/items/bar/name") 1abc

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

20 assert response.json() == {"name": "Bar", "description": "The Bar fighters"} 1abc

21 

22 

23@needs_py310 1deabc

24def test_read_item_public_data(client: TestClient): 1deabc

25 response = client.get("/items/bar/public") 1abc

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

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

28 "name": "Bar", 

29 "description": "The Bar fighters", 

30 "price": 62, 

31 } 

32 

33 

34@needs_py310 1deabc

35def test_openapi_schema(client: TestClient): 1deabc

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

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

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

39 "openapi": "3.1.0", 

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

41 "paths": { 

42 "/items/{item_id}/name": { 

43 "get": { 

44 "responses": { 

45 "200": { 

46 "description": "Successful Response", 

47 "content": { 

48 "application/json": { 

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

50 } 

51 }, 

52 }, 

53 "422": { 

54 "description": "Validation Error", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 } 

60 } 

61 }, 

62 }, 

63 }, 

64 "summary": "Read Item Name", 

65 "operationId": "read_item_name_items__item_id__name_get", 

66 "parameters": [ 

67 { 

68 "required": True, 

69 "schema": {"title": "Item Id", "type": "string"}, 

70 "name": "item_id", 

71 "in": "path", 

72 } 

73 ], 

74 } 

75 }, 

76 "/items/{item_id}/public": { 

77 "get": { 

78 "responses": { 

79 "200": { 

80 "description": "Successful Response", 

81 "content": { 

82 "application/json": { 

83 "schema": {"$ref": "#/components/schemas/Item"} 

84 } 

85 }, 

86 }, 

87 "422": { 

88 "description": "Validation Error", 

89 "content": { 

90 "application/json": { 

91 "schema": { 

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

93 } 

94 } 

95 }, 

96 }, 

97 }, 

98 "summary": "Read Item Public Data", 

99 "operationId": "read_item_public_data_items__item_id__public_get", 

100 "parameters": [ 

101 { 

102 "required": True, 

103 "schema": {"title": "Item Id", "type": "string"}, 

104 "name": "item_id", 

105 "in": "path", 

106 } 

107 ], 

108 } 

109 }, 

110 }, 

111 "components": { 

112 "schemas": { 

113 "Item": { 

114 "title": "Item", 

115 "required": IsOneOf( 

116 ["name", "description", "price", "tax"], 

117 # TODO: remove when deprecating Pydantic v1 

118 ["name", "price"], 

119 ), 

120 "type": "object", 

121 "properties": { 

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

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

124 "description": IsDict( 

125 { 

126 "title": "Description", 

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

128 } 

129 ) 

130 | IsDict( 

131 # TODO: remove when deprecating Pydantic v1 

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

133 ), 

134 "tax": {"title": "Tax", "type": "number", "default": 10.5}, 

135 }, 

136 }, 

137 "ValidationError": { 

138 "title": "ValidationError", 

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

140 "type": "object", 

141 "properties": { 

142 "loc": { 

143 "title": "Location", 

144 "type": "array", 

145 "items": { 

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

147 }, 

148 }, 

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

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

151 }, 

152 }, 

153 "HTTPValidationError": { 

154 "title": "HTTPValidationError", 

155 "type": "object", 

156 "properties": { 

157 "detail": { 

158 "title": "Detail", 

159 "type": "array", 

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

161 } 

162 }, 

163 }, 

164 } 

165 }, 

166 }