Debian Codegraph @

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