Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial014.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("tutorial014_py310", marks=needs_py310),
14 pytest.param("tutorial014_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_hidden_query(client: TestClient):
27 response = client.get("/items?hidden_query=somevalue")
28 assert response.status_code == 200, response.text
29 assert response.json() == {"hidden_query": "somevalue"}
30
31
32 def test_no_hidden_query(client: TestClient):
33 response = client.get("/items")
34 assert response.status_code == 200, response.text
35 assert response.json() == {"hidden_query": "Not found"}
36
37
38 def test_openapi_schema(client: TestClient):
39 response = client.get("/openapi.json")
40 assert response.status_code == 200, response.text
41 assert response.json() == snapshot(
42 {
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/items/": {
47 "get": {
48 "summary": "Read Items",
49 "operationId": "read_items_items__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 }
67 }
68 },
69 "components": {
70 "schemas": {
71 "HTTPValidationError": {
72 "title": "HTTPValidationError",
73 "type": "object",
74 "properties": {
75 "detail": {
76 "title": "Detail",
77 "type": "array",
78 "items": {
79 "$ref": "#/components/schemas/ValidationError"
80 },
81 }
82 },
83 },
84 "ValidationError": {
85 "title": "ValidationError",
86 "required": ["loc", "msg", "type"],
87 "type": "object",
88 "properties": {
89 "loc": {
90 "title": "Location",
91 "type": "array",
92 "items": {
93 "anyOf": [{"type": "string"}, {"type": "integer"}]
94 },
95 },
96 "msg": {"title": "Message", "type": "string"},
97 "type": {"title": "Error Type", "type": "string"},
98 "input": {"title": "Input"},
99 "ctx": {"title": "Context", "type": "object"},
100 },
101 },
102 }
103 },
104 }
105 )