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

16 statements  

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

1from dirty_equals import IsDict, IsOneOf 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.response_model.tutorial005 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_read_item_name(): 1abcde

10 response = client.get("/items/bar/name") 1abcde

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

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

13 

14 

15def test_read_item_public_data(): 1abcde

16 response = client.get("/items/bar/public") 1abcde

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

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

19 "name": "Bar", 

20 "description": "The Bar fighters", 

21 "price": 62, 

22 } 

23 

24 

25def test_openapi_schema(): 1abcde

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

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

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

29 "openapi": "3.1.0", 

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

31 "paths": { 

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

33 "get": { 

34 "responses": { 

35 "200": { 

36 "description": "Successful Response", 

37 "content": { 

38 "application/json": { 

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

40 } 

41 }, 

42 }, 

43 "422": { 

44 "description": "Validation Error", 

45 "content": { 

46 "application/json": { 

47 "schema": { 

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

49 } 

50 } 

51 }, 

52 }, 

53 }, 

54 "summary": "Read Item Name", 

55 "operationId": "read_item_name_items__item_id__name_get", 

56 "parameters": [ 

57 { 

58 "required": True, 

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

60 "name": "item_id", 

61 "in": "path", 

62 } 

63 ], 

64 } 

65 }, 

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

67 "get": { 

68 "responses": { 

69 "200": { 

70 "description": "Successful Response", 

71 "content": { 

72 "application/json": { 

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

74 } 

75 }, 

76 }, 

77 "422": { 

78 "description": "Validation Error", 

79 "content": { 

80 "application/json": { 

81 "schema": { 

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

83 } 

84 } 

85 }, 

86 }, 

87 }, 

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

89 "operationId": "read_item_public_data_items__item_id__public_get", 

90 "parameters": [ 

91 { 

92 "required": True, 

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

94 "name": "item_id", 

95 "in": "path", 

96 } 

97 ], 

98 } 

99 }, 

100 }, 

101 "components": { 

102 "schemas": { 

103 "Item": { 

104 "title": "Item", 

105 "required": IsOneOf( 

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

107 # TODO: remove when deprecating Pydantic v1 

108 ["name", "price"], 

109 ), 

110 "type": "object", 

111 "properties": { 

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

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

114 "description": IsDict( 

115 { 

116 "title": "Description", 

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

118 } 

119 ) 

120 | IsDict( 

121 # TODO: remove when deprecating Pydantic v1 

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

123 ), 

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

125 }, 

126 }, 

127 "ValidationError": { 

128 "title": "ValidationError", 

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

130 "type": "object", 

131 "properties": { 

132 "loc": { 

133 "title": "Location", 

134 "type": "array", 

135 "items": { 

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

137 }, 

138 }, 

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

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

141 }, 

142 }, 

143 "HTTPValidationError": { 

144 "title": "HTTPValidationError", 

145 "type": "object", 

146 "properties": { 

147 "detail": { 

148 "title": "Detail", 

149 "type": "array", 

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

151 } 

152 }, 

153 }, 

154 } 

155 }, 

156 }