Debian Codegraph @

tests/test_tutorial/test_stream_data/test_tutorial002.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="mod",
10 params=[
11 pytest.param("tutorial002_py310"),
12 ],
13 )
14 def get_mod(request: pytest.FixtureRequest):
15 return importlib.import_module(f"docs_src.stream_data.{request.param}")
16
17
18 @pytest.fixture(name="client")
19 def get_client(mod):
20 client = TestClient(mod.app)
21 return client
22
23
24 @pytest.mark.parametrize(
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 )
34 def test_stream_image(mod, client: TestClient, path: str):
35 response = client.get(path)
36 assert response.status_code == 200
37 assert response.headers["content-type"] == "image/png"
38 assert response.content == mod.binary_image
39
40
41 def test_openapi_schema(client: TestClient):
42 response = client.get("/openapi.json")
43 assert response.status_code == 200, response.text
44 assert response.json() == snapshot(
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 )