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

22 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial005_py310", marks=needs_py310), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_read_item_name(client: TestClient): 1abdc

24 response = client.get("/items/bar/name") 1efg

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

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

27 

28 

29def test_read_item_public_data(client: TestClient): 1abdc

30 response = client.get("/items/bar/public") 1hij

31 assert response.status_code == 200, response.text 1hij

32 assert response.json() == { 1hij

33 "name": "Bar", 

34 "description": "The Bar fighters", 

35 "price": 62, 

36 } 

37 

38 

39def test_openapi_schema(client: TestClient): 1abdc

40 response = client.get("/openapi.json") 1klm

41 assert response.status_code == 200, response.text 1klm

42 assert response.json() == snapshot( 1klm

43 { 

44 "openapi": "3.1.0", 

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

46 "paths": { 

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

48 "get": { 

49 "responses": { 

50 "200": { 

51 "description": "Successful Response", 

52 "content": { 

53 "application/json": { 

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

55 } 

56 }, 

57 }, 

58 "422": { 

59 "description": "Validation Error", 

60 "content": { 

61 "application/json": { 

62 "schema": { 

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

64 } 

65 } 

66 }, 

67 }, 

68 }, 

69 "summary": "Read Item Name", 

70 "operationId": "read_item_name_items__item_id__name_get", 

71 "parameters": [ 

72 { 

73 "required": True, 

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

75 "name": "item_id", 

76 "in": "path", 

77 } 

78 ], 

79 } 

80 }, 

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

82 "get": { 

83 "responses": { 

84 "200": { 

85 "description": "Successful Response", 

86 "content": { 

87 "application/json": { 

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

89 } 

90 }, 

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 "summary": "Read Item Public Data", 

104 "operationId": "read_item_public_data_items__item_id__public_get", 

105 "parameters": [ 

106 { 

107 "required": True, 

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

109 "name": "item_id", 

110 "in": "path", 

111 } 

112 ], 

113 } 

114 }, 

115 }, 

116 "components": { 

117 "schemas": { 

118 "Item": { 

119 "title": "Item", 

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

121 "type": "object", 

122 "properties": { 

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

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

125 "description": { 

126 "title": "Description", 

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

128 }, 

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

130 }, 

131 }, 

132 "ValidationError": { 

133 "title": "ValidationError", 

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

135 "type": "object", 

136 "properties": { 

137 "ctx": {"title": "Context", "type": "object"}, 

138 "input": {"title": "Input"}, 

139 "loc": { 

140 "title": "Location", 

141 "type": "array", 

142 "items": { 

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

144 }, 

145 }, 

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

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

148 }, 

149 }, 

150 "HTTPValidationError": { 

151 "title": "HTTPValidationError", 

152 "type": "object", 

153 "properties": { 

154 "detail": { 

155 "title": "Detail", 

156 "type": "array", 

157 "items": { 

158 "$ref": "#/components/schemas/ValidationError" 

159 }, 

160 } 

161 }, 

162 }, 

163 } 

164 }, 

165 } 

166 )