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

19 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.stream_data.{request.param}") 1efghijklmnopqrstuvwxyzABCDEFG

16 

17 client = TestClient(mod.app) 1efghijklmnopqrstuvwxyzABCDEFG

18 return client 1efghijklmnopqrstuvwxyzABCDEFG

19 

20 

21expected_text = ( 1abcd

22 "" 

23 "Rick: (stumbles in drunkenly, and turns on the lights)" 

24 " Morty! You gotta come on. You got--... you gotta come with me." 

25 "Morty: (rubs his eyes) What, Rick? What's going on?" 

26 "Rick: I got a surprise for you, Morty." 

27 "Morty: It's the middle of the night. What are you talking about?" 

28 "Rick: (spills alcohol on Morty's bed) Come on, I got a surprise for you." 

29 " (drags Morty by the ankle) Come on, hurry up." 

30 " (pulls Morty out of his bed and into the hall)" 

31 "Morty: Ow! Ow! You're tugging me too hard!" 

32 "Rick: We gotta go, gotta get outta here, come on." 

33 " Got a surprise for you Morty." 

34) 

35 

36 

37@pytest.mark.parametrize( 1abcd

38 "path", 

39 [ 

40 "/story/stream", 

41 "/story/stream-no-async", 

42 "/story/stream-no-annotation", 

43 "/story/stream-no-async-no-annotation", 

44 "/story/stream-bytes", 

45 "/story/stream-no-async-bytes", 

46 "/story/stream-no-annotation-bytes", 

47 "/story/stream-no-async-no-annotation-bytes", 

48 ], 

49) 

50def test_stream_story(client: TestClient, path: str): 1abcd

51 response = client.get(path) 1HIJKLMNOPQRSTUVWXYZ0123456

52 assert response.status_code == 200, response.text 1HIJKLMNOPQRSTUVWXYZ0123456

53 assert response.text == expected_text 1HIJKLMNOPQRSTUVWXYZ0123456

54 

55 

56def test_openapi_schema(client: TestClient): 1abcd

57 response = client.get("/openapi.json") 1789!

58 assert response.status_code == 200, response.text 1789!

59 assert response.json() == snapshot( 1789!

60 { 

61 "openapi": "3.1.0", 

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

63 "paths": { 

64 "/story/stream": { 

65 "get": { 

66 "summary": "Stream Story", 

67 "operationId": "stream_story_story_stream_get", 

68 "responses": { 

69 "200": { 

70 "description": "Successful Response", 

71 } 

72 }, 

73 } 

74 }, 

75 "/story/stream-no-async": { 

76 "get": { 

77 "summary": "Stream Story No Async", 

78 "operationId": "stream_story_no_async_story_stream_no_async_get", 

79 "responses": { 

80 "200": { 

81 "description": "Successful Response", 

82 } 

83 }, 

84 } 

85 }, 

86 "/story/stream-no-annotation": { 

87 "get": { 

88 "summary": "Stream Story No Annotation", 

89 "operationId": "stream_story_no_annotation_story_stream_no_annotation_get", 

90 "responses": { 

91 "200": { 

92 "description": "Successful Response", 

93 } 

94 }, 

95 } 

96 }, 

97 "/story/stream-no-async-no-annotation": { 

98 "get": { 

99 "summary": "Stream Story No Async No Annotation", 

100 "operationId": "stream_story_no_async_no_annotation_story_stream_no_async_no_annotation_get", 

101 "responses": { 

102 "200": { 

103 "description": "Successful Response", 

104 } 

105 }, 

106 } 

107 }, 

108 "/story/stream-bytes": { 

109 "get": { 

110 "summary": "Stream Story Bytes", 

111 "operationId": "stream_story_bytes_story_stream_bytes_get", 

112 "responses": { 

113 "200": { 

114 "description": "Successful Response", 

115 } 

116 }, 

117 } 

118 }, 

119 "/story/stream-no-async-bytes": { 

120 "get": { 

121 "summary": "Stream Story No Async Bytes", 

122 "operationId": "stream_story_no_async_bytes_story_stream_no_async_bytes_get", 

123 "responses": { 

124 "200": { 

125 "description": "Successful Response", 

126 } 

127 }, 

128 } 

129 }, 

130 "/story/stream-no-annotation-bytes": { 

131 "get": { 

132 "summary": "Stream Story No Annotation Bytes", 

133 "operationId": "stream_story_no_annotation_bytes_story_stream_no_annotation_bytes_get", 

134 "responses": { 

135 "200": { 

136 "description": "Successful Response", 

137 } 

138 }, 

139 } 

140 }, 

141 "/story/stream-no-async-no-annotation-bytes": { 

142 "get": { 

143 "summary": "Stream Story No Async No Annotation Bytes", 

144 "operationId": "stream_story_no_async_no_annotation_bytes_story_stream_no_async_no_annotation_bytes_get", 

145 "responses": { 

146 "200": { 

147 "description": "Successful Response", 

148 } 

149 }, 

150 } 

151 }, 

152 }, 

153 } 

154 )