Debian Codegraph @

tests/test_tutorial/test_query_params/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 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial002_py310", marks=needs_py310),
14 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(f"docs_src.query_params.{request.param}")
18
19 client = TestClient(mod.app)
20 return client
21
22
23 @pytest.mark.parametrize(
24 ("path", "expected_json"),
25 [
26 (
27 "/items/foo",
28 {"item_id": "foo"},
29 ),
30 (
31 "/items/bar?q=somequery",
32 {"item_id": "bar", "q": "somequery"},
33 ),
34 ],
35 )
36 def test_read_user_item(client: TestClient, path, expected_json):
37 response = client.get(path)
38 assert response.status_code == 200
39 assert response.json() == expected_json
40
41
42 def test_openapi_schema(client: TestClient):
43 response = client.get("/openapi.json")
44 assert response.status_code == 200
45 assert response.json() == snapshot(
46 {
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/items/{item_id}": {
51 "get": {
52 "summary": "Read Item",
53 "operationId": "read_item_items__item_id__get",
54 "parameters": [
55 {
56 "required": True,
57 "schema": {"title": "Item Id", "type": "string"},
58 "name": "item_id",
59 "in": "path",
60 },
61 {
62 "required": False,
63 "schema": {
64 "title": "Q",
65 "anyOf": [
66 {
67 "type": "string",
68 },
69 {
70 "type": "null",
71 },
72 ],
73 },
74 "name": "q",
75 "in": "query",
76 },
77 ],
78 "responses": {
79 "200": {
80 "description": "Successful Response",
81 "content": {"application/json": {"schema": {}}},
82 },
83 "422": {
84 "content": {
85 "application/json": {
86 "schema": {
87 "$ref": "#/components/schemas/HTTPValidationError",
88 },
89 },
90 },
91 "description": "Validation Error",
92 },
93 },
94 }
95 }
96 },
97 "components": {
98 "schemas": {
99 "ValidationError": {
100 "title": "ValidationError",
101 "required": ["loc", "msg", "type"],
102 "type": "object",
103 "properties": {
104 "loc": {
105 "title": "Location",
106 "type": "array",
107 "items": {
108 "anyOf": [{"type": "string"}, {"type": "integer"}]
109 },
110 },
111 "msg": {"title": "Message", "type": "string"},
112 "type": {"title": "Error Type", "type": "string"},
113 "input": {"title": "Input"},
114 "ctx": {"title": "Context", "type": "object"},
115 },
116 },
117 "HTTPValidationError": {
118 "title": "HTTPValidationError",
119 "type": "object",
120 "properties": {
121 "detail": {
122 "title": "Detail",
123 "type": "array",
124 "items": {
125 "$ref": "#/components/schemas/ValidationError"
126 },
127 }
128 },
129 },
130 }
131 },
132 }
133 )