Coverage for tests / test_tutorial / test_server_sent_events / test_tutorial001.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="client", 

10 params=[ 

11 pytest.param("tutorial001_py310"), 

12 ], 

13) 

14def get_client(request: pytest.FixtureRequest): 1abcd

15 mod = importlib.import_module(f"docs_src.server_sent_events.{request.param}") 1stuvwxyzABCDEFGHI

16 

17 client = TestClient(mod.app) 1stuvwxyzABCDEFGHI

18 return client 1stuvwxyzABCDEFGHI

19 

20 

21@pytest.mark.parametrize( 1abcd

22 "path", 

23 [ 

24 "/items/stream", 

25 "/items/stream-no-async", 

26 "/items/stream-no-annotation", 

27 "/items/stream-no-async-no-annotation", 

28 ], 

29) 

30def test_stream_items(client: TestClient, path: str): 1abcd

31 response = client.get(path) 1efghijklmnopqr

32 assert response.status_code == 200, response.text 1efghijklmnopqr

33 assert response.headers["content-type"] == "text/event-stream; charset=utf-8" 1efghijklmnopqr

34 data_lines = [ 1efghijklmnopqr

35 line for line in response.text.strip().split("\n") if line.startswith("data: ") 

36 ] 

37 assert len(data_lines) == 3 1efghijklmnopqr

38 

39 

40def test_openapi_schema(client: TestClient): 1abcd

41 response = client.get("/openapi.json") 1JKLM

42 assert response.status_code == 200, response.text 1JKLM

43 assert response.json() == snapshot( 1JKLM

44 { 

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/items/stream": { 

49 "get": { 

50 "summary": "Sse Items", 

51 "operationId": "sse_items_items_stream_get", 

52 "responses": { 

53 "200": { 

54 "description": "Successful Response", 

55 "content": { 

56 "text/event-stream": { 

57 "itemSchema": { 

58 "type": "object", 

59 "properties": { 

60 "data": { 

61 "type": "string", 

62 "contentMediaType": "application/json", 

63 "contentSchema": { 

64 "$ref": "#/components/schemas/Item" 

65 }, 

66 }, 

67 "event": {"type": "string"}, 

68 "id": {"type": "string"}, 

69 "retry": { 

70 "type": "integer", 

71 "minimum": 0, 

72 }, 

73 }, 

74 "required": ["data"], 

75 } 

76 } 

77 }, 

78 } 

79 }, 

80 } 

81 }, 

82 "/items/stream-no-async": { 

83 "get": { 

84 "summary": "Sse Items No Async", 

85 "operationId": "sse_items_no_async_items_stream_no_async_get", 

86 "responses": { 

87 "200": { 

88 "description": "Successful Response", 

89 "content": { 

90 "text/event-stream": { 

91 "itemSchema": { 

92 "type": "object", 

93 "properties": { 

94 "data": { 

95 "type": "string", 

96 "contentMediaType": "application/json", 

97 "contentSchema": { 

98 "$ref": "#/components/schemas/Item" 

99 }, 

100 }, 

101 "event": {"type": "string"}, 

102 "id": {"type": "string"}, 

103 "retry": { 

104 "type": "integer", 

105 "minimum": 0, 

106 }, 

107 }, 

108 "required": ["data"], 

109 } 

110 } 

111 }, 

112 } 

113 }, 

114 } 

115 }, 

116 "/items/stream-no-annotation": { 

117 "get": { 

118 "summary": "Sse Items No Annotation", 

119 "operationId": "sse_items_no_annotation_items_stream_no_annotation_get", 

120 "responses": { 

121 "200": { 

122 "description": "Successful Response", 

123 "content": { 

124 "text/event-stream": { 

125 "itemSchema": { 

126 "type": "object", 

127 "properties": { 

128 "data": {"type": "string"}, 

129 "event": {"type": "string"}, 

130 "id": {"type": "string"}, 

131 "retry": { 

132 "type": "integer", 

133 "minimum": 0, 

134 }, 

135 }, 

136 } 

137 } 

138 }, 

139 } 

140 }, 

141 } 

142 }, 

143 "/items/stream-no-async-no-annotation": { 

144 "get": { 

145 "summary": "Sse Items No Async No Annotation", 

146 "operationId": "sse_items_no_async_no_annotation_items_stream_no_async_no_annotation_get", 

147 "responses": { 

148 "200": { 

149 "description": "Successful Response", 

150 "content": { 

151 "text/event-stream": { 

152 "itemSchema": { 

153 "type": "object", 

154 "properties": { 

155 "data": {"type": "string"}, 

156 "event": {"type": "string"}, 

157 "id": {"type": "string"}, 

158 "retry": { 

159 "type": "integer", 

160 "minimum": 0, 

161 }, 

162 }, 

163 } 

164 } 

165 }, 

166 } 

167 }, 

168 } 

169 }, 

170 }, 

171 "components": { 

172 "schemas": { 

173 "Item": { 

174 "properties": { 

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

176 "description": { 

177 "anyOf": [ 

178 {"type": "string"}, 

179 {"type": "null"}, 

180 ], 

181 "title": "Description", 

182 }, 

183 }, 

184 "type": "object", 

185 "required": ["name", "description"], 

186 "title": "Item", 

187 } 

188 } 

189 }, 

190 } 

191 )