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

21 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="mod", 

10 params=[ 

11 pytest.param("tutorial002_py310"), 

12 ], 

13) 

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

15 return importlib.import_module(f"docs_src.stream_data.{request.param}") 1vwxyzABCDEFGHIJKLMNO

16 

17 

18@pytest.fixture(name="client") 1abcd

19def get_client(mod): 1abcd

20 client = TestClient(mod.app) 1vwxyzABCDEFGHIJKLMNO

21 return client 1vwxyzABCDEFGHIJKLMNO

22 

23 

24@pytest.mark.parametrize( 1abcd

25 "path", 

26 [ 

27 "/image/stream", 

28 "/image/stream-no-async", 

29 "/image/stream-no-async-yield-from", 

30 "/image/stream-no-annotation", 

31 "/image/stream-no-async-no-annotation", 

32 ], 

33) 

34def test_stream_image(mod, client: TestClient, path: str): 1abcd

35 response = client.get(path) 1efghijklmnopqrstu

36 assert response.status_code == 200 1efghijklmnopqrstu

37 assert response.headers["content-type"] == "image/png" 1efghijklmnopqrstu

38 assert response.content == mod.binary_image 1efghijklmnopqrstu

39 

40 

41def test_openapi_schema(client: TestClient): 1abcd

42 response = client.get("/openapi.json") 1PQRS

43 assert response.status_code == 200, response.text 1PQRS

44 assert response.json() == snapshot( 1PQRS

45 { 

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/image/stream": { 

50 "get": { 

51 "summary": "Stream Image", 

52 "operationId": "stream_image_image_stream_get", 

53 "responses": { 

54 "200": { 

55 "description": "Successful Response", 

56 "content": { 

57 "image/png": {"schema": {"type": "string"}} 

58 }, 

59 } 

60 }, 

61 } 

62 }, 

63 "/image/stream-no-async": { 

64 "get": { 

65 "summary": "Stream Image No Async", 

66 "operationId": "stream_image_no_async_image_stream_no_async_get", 

67 "responses": { 

68 "200": { 

69 "description": "Successful Response", 

70 "content": { 

71 "image/png": {"schema": {"type": "string"}} 

72 }, 

73 } 

74 }, 

75 } 

76 }, 

77 "/image/stream-no-async-yield-from": { 

78 "get": { 

79 "summary": "Stream Image No Async Yield From", 

80 "operationId": "stream_image_no_async_yield_from_image_stream_no_async_yield_from_get", 

81 "responses": { 

82 "200": { 

83 "description": "Successful Response", 

84 "content": { 

85 "image/png": {"schema": {"type": "string"}} 

86 }, 

87 } 

88 }, 

89 } 

90 }, 

91 "/image/stream-no-annotation": { 

92 "get": { 

93 "summary": "Stream Image No Annotation", 

94 "operationId": "stream_image_no_annotation_image_stream_no_annotation_get", 

95 "responses": { 

96 "200": { 

97 "description": "Successful Response", 

98 "content": { 

99 "image/png": {"schema": {"type": "string"}} 

100 }, 

101 } 

102 }, 

103 } 

104 }, 

105 "/image/stream-no-async-no-annotation": { 

106 "get": { 

107 "summary": "Stream Image No Async No Annotation", 

108 "operationId": "stream_image_no_async_no_annotation_image_stream_no_async_no_annotation_get", 

109 "responses": { 

110 "200": { 

111 "description": "Successful Response", 

112 "content": { 

113 "image/png": {"schema": {"type": "string"}} 

114 }, 

115 } 

116 }, 

117 } 

118 }, 

119 }, 

120 } 

121 )