Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial012.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1import importlib 1abcdefg
3import pytest 1abcdefg
4from fastapi.testclient import TestClient 1abcdefg
6from ...utils import needs_py39 1abcdefg
9@pytest.fixture( 1abcdefg
10 name="client",
11 params=[
12 "tutorial012",
13 pytest.param("tutorial012_py39", marks=needs_py39),
14 "tutorial012_an",
15 pytest.param("tutorial012_an_py39", marks=needs_py39),
16 ],
17)
18def get_client(request: pytest.FixtureRequest): 1abcdefg
19 mod = importlib.import_module( 1abcdefg
20 f"docs_src.query_params_str_validations.{request.param}"
21 )
23 client = TestClient(mod.app) 1abcdefg
24 return client 1abcdefg
27def test_default_query_values(client: TestClient): 1abcdefg
28 url = "/items/" 1hijklmn
29 response = client.get(url) 1hijklmn
30 assert response.status_code == 200, response.text 1hijklmn
31 assert response.json() == {"q": ["foo", "bar"]} 1hijklmn
34def test_multi_query_values(client: TestClient): 1abcdefg
35 url = "/items/?q=baz&q=foobar" 1opqrstu
36 response = client.get(url) 1opqrstu
37 assert response.status_code == 200, response.text 1opqrstu
38 assert response.json() == {"q": ["baz", "foobar"]} 1opqrstu
41def test_openapi_schema(client: TestClient): 1abcdefg
42 response = client.get("/openapi.json") 1vwxyzAB
43 assert response.status_code == 200, response.text 1vwxyzAB
44 assert response.json() == { 1vwxyzAB
45 "openapi": "3.1.0",
46 "info": {"title": "FastAPI", "version": "0.1.0"},
47 "paths": {
48 "/items/": {
49 "get": {
50 "responses": {
51 "200": {
52 "description": "Successful Response",
53 "content": {"application/json": {"schema": {}}},
54 },
55 "422": {
56 "description": "Validation Error",
57 "content": {
58 "application/json": {
59 "schema": {
60 "$ref": "#/components/schemas/HTTPValidationError"
61 }
62 }
63 },
64 },
65 },
66 "summary": "Read Items",
67 "operationId": "read_items_items__get",
68 "parameters": [
69 {
70 "required": False,
71 "schema": {
72 "title": "Q",
73 "type": "array",
74 "items": {"type": "string"},
75 "default": ["foo", "bar"],
76 },
77 "name": "q",
78 "in": "query",
79 }
80 ],
81 }
82 }
83 },
84 "components": {
85 "schemas": {
86 "ValidationError": {
87 "title": "ValidationError",
88 "required": ["loc", "msg", "type"],
89 "type": "object",
90 "properties": {
91 "loc": {
92 "title": "Location",
93 "type": "array",
94 "items": {
95 "anyOf": [{"type": "string"}, {"type": "integer"}]
96 },
97 },
98 "msg": {"title": "Message", "type": "string"},
99 "type": {"title": "Error Type", "type": "string"},
100 },
101 },
102 "HTTPValidationError": {
103 "title": "HTTPValidationError",
104 "type": "object",
105 "properties": {
106 "detail": {
107 "title": "Detail",
108 "type": "array",
109 "items": {"$ref": "#/components/schemas/ValidationError"},
110 }
111 },
112 },
113 }
114 },
115 }