Coverage for tests/test_tutorial/test_cookie_params/test_tutorial001.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
2from types import ModuleType 1abcdef
4import pytest 1abcdef
5from dirty_equals import IsDict 1abcdef
6from fastapi.testclient import TestClient 1abcdef
8from ...utils import needs_py39, needs_py310 1abcdef
11@pytest.fixture( 1abcdef
12 name="mod",
13 params=[
14 "tutorial001",
15 pytest.param("tutorial001_py310", marks=needs_py310),
16 "tutorial001_an",
17 pytest.param("tutorial001_an_py39", marks=needs_py39),
18 pytest.param("tutorial001_an_py310", marks=needs_py310),
19 ],
20)
21def get_mod(request: pytest.FixtureRequest): 1abcdef
22 mod = importlib.import_module(f"docs_src.cookie_params.{request.param}") 1abcdef
24 return mod 1abcdef
27@pytest.mark.parametrize( 1abcdef
28 "path,cookies,expected_status,expected_response",
29 [
30 ("/items", None, 200, {"ads_id": None}),
31 ("/items", {"ads_id": "ads_track"}, 200, {"ads_id": "ads_track"}),
32 (
33 "/items",
34 {"ads_id": "ads_track", "session": "cookiesession"},
35 200,
36 {"ads_id": "ads_track"},
37 ),
38 ("/items", {"session": "cookiesession"}, 200, {"ads_id": None}),
39 ],
40)
41def test(path, cookies, expected_status, expected_response, mod: ModuleType): 1abcdef
42 client = TestClient(mod.app, cookies=cookies) 1ghijkl
43 response = client.get(path) 1ghijkl
44 assert response.status_code == expected_status 1ghijkl
45 assert response.json() == expected_response 1ghijkl
48def test_openapi_schema(mod: ModuleType): 1abcdef
49 client = TestClient(mod.app) 1mnopqr
50 response = client.get("/openapi.json") 1mnopqr
51 assert response.status_code == 200 1mnopqr
52 assert response.json() == { 1mnopqr
53 "openapi": "3.1.0",
54 "info": {"title": "FastAPI", "version": "0.1.0"},
55 "paths": {
56 "/items/": {
57 "get": {
58 "responses": {
59 "200": {
60 "description": "Successful Response",
61 "content": {"application/json": {"schema": {}}},
62 },
63 "422": {
64 "description": "Validation Error",
65 "content": {
66 "application/json": {
67 "schema": {
68 "$ref": "#/components/schemas/HTTPValidationError"
69 }
70 }
71 },
72 },
73 },
74 "summary": "Read Items",
75 "operationId": "read_items_items__get",
76 "parameters": [
77 {
78 "required": False,
79 "schema": IsDict(
80 {
81 "anyOf": [{"type": "string"}, {"type": "null"}],
82 "title": "Ads Id",
83 }
84 )
85 | IsDict(
86 # TODO: remove when deprecating Pydantic v1
87 {"title": "Ads Id", "type": "string"}
88 ),
89 "name": "ads_id",
90 "in": "cookie",
91 }
92 ],
93 }
94 }
95 },
96 "components": {
97 "schemas": {
98 "ValidationError": {
99 "title": "ValidationError",
100 "required": ["loc", "msg", "type"],
101 "type": "object",
102 "properties": {
103 "loc": {
104 "title": "Location",
105 "type": "array",
106 "items": {
107 "anyOf": [{"type": "string"}, {"type": "integer"}]
108 },
109 },
110 "msg": {"title": "Message", "type": "string"},
111 "type": {"title": "Error Type", "type": "string"},
112 },
113 },
114 "HTTPValidationError": {
115 "title": "HTTPValidationError",
116 "type": "object",
117 "properties": {
118 "detail": {
119 "title": "Detail",
120 "type": "array",
121 "items": {"$ref": "#/components/schemas/ValidationError"},
122 }
123 },
124 },
125 }
126 },
127 }