Coverage for tests / test_tutorial / test_dependencies / test_tutorial005.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial005_py310", marks=needs_py310),
14 pytest.param("tutorial005_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24@pytest.mark.parametrize( 1abdc
25 "path,cookie,expected_status,expected_response",
26 [
27 (
28 "/items",
29 "from_cookie",
30 200,
31 {"q_or_cookie": "from_cookie"},
32 ),
33 (
34 "/items?q=foo",
35 "from_cookie",
36 200,
37 {"q_or_cookie": "foo"},
38 ),
39 (
40 "/items",
41 None,
42 200,
43 {"q_or_cookie": None},
44 ),
45 ],
46)
47def test_get(path, cookie, expected_status, expected_response, client: TestClient): 1abdc
48 if cookie is not None: 1efg
49 client.cookies.set("last_query", cookie) 1efg
50 else:
51 client.cookies.clear() 1efg
52 response = client.get(path) 1efg
53 assert response.status_code == expected_status 1efg
54 assert response.json() == expected_response 1efg
57def test_openapi_schema(client: TestClient): 1abdc
58 response = client.get("/openapi.json") 1hij
59 assert response.status_code == 200, response.text 1hij
60 assert response.json() == snapshot( 1hij
61 {
62 "openapi": "3.1.0",
63 "info": {"title": "FastAPI", "version": "0.1.0"},
64 "paths": {
65 "/items/": {
66 "get": {
67 "responses": {
68 "200": {
69 "description": "Successful Response",
70 "content": {"application/json": {"schema": {}}},
71 },
72 "422": {
73 "description": "Validation Error",
74 "content": {
75 "application/json": {
76 "schema": {
77 "$ref": "#/components/schemas/HTTPValidationError"
78 }
79 }
80 },
81 },
82 },
83 "summary": "Read Query",
84 "operationId": "read_query_items__get",
85 "parameters": [
86 {
87 "required": False,
88 "schema": {
89 "anyOf": [{"type": "string"}, {"type": "null"}],
90 "title": "Q",
91 },
92 "name": "q",
93 "in": "query",
94 },
95 {
96 "required": False,
97 "schema": {
98 "anyOf": [{"type": "string"}, {"type": "null"}],
99 "title": "Last Query",
100 },
101 "name": "last_query",
102 "in": "cookie",
103 },
104 ],
105 }
106 }
107 },
108 "components": {
109 "schemas": {
110 "ValidationError": {
111 "title": "ValidationError",
112 "required": ["loc", "msg", "type"],
113 "type": "object",
114 "properties": {
115 "loc": {
116 "title": "Location",
117 "type": "array",
118 "items": {
119 "anyOf": [{"type": "string"}, {"type": "integer"}]
120 },
121 },
122 "msg": {"title": "Message", "type": "string"},
123 "type": {"title": "Error Type", "type": "string"},
124 "input": {"title": "Input"},
125 "ctx": {"title": "Context", "type": "object"},
126 },
127 },
128 "HTTPValidationError": {
129 "title": "HTTPValidationError",
130 "type": "object",
131 "properties": {
132 "detail": {
133 "title": "Detail",
134 "type": "array",
135 "items": {
136 "$ref": "#/components/schemas/ValidationError"
137 },
138 }
139 },
140 },
141 }
142 },
143 }
144 )