Coverage for tests / test_enforce_once_required_parameter.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from fastapi import Depends, FastAPI, Query 1abcd
2from fastapi.testclient import TestClient 1abcd
3from inline_snapshot import snapshot 1abcd
5app = FastAPI() 1abcd
8def _get_client_key(client_id: str = Query(...)) -> str: 1abcd
9 return f"{client_id}_key" 1efgh
12def _get_client_tag(client_id: str | None = Query(None)) -> str | None: 1abcd
13 if client_id is None: 1iejfkglh
14 return None 1ijkl
15 return f"{client_id}_tag" 1efgh
18@app.get("/foo") 1abcd
19def foo_handler( 1abcd
20 client_key: str = Depends(_get_client_key),
21 client_tag: str | None = Depends(_get_client_tag),
22):
23 return {"client_id": client_key, "client_tag": client_tag} 1efgh
26client = TestClient(app) 1abcd
29def test_get_invalid(): 1abcd
30 response = client.get("/foo") 1ijkl
31 assert response.status_code == 422 1ijkl
34def test_get_valid(): 1abcd
35 response = client.get("/foo", params={"client_id": "bar"}) 1efgh
36 assert response.status_code == 200 1efgh
37 assert response.json() == {"client_id": "bar_key", "client_tag": "bar_tag"} 1efgh
40def test_openapi_schema(): 1abcd
41 response = client.get("/openapi.json") 1mnop
42 assert response.status_code == 200, response.text 1mnop
43 assert response.json() == snapshot( 1mnop
44 {
45 "components": {
46 "schemas": {
47 "HTTPValidationError": {
48 "properties": {
49 "detail": {
50 "items": {
51 "$ref": "#/components/schemas/ValidationError"
52 },
53 "title": "Detail",
54 "type": "array",
55 }
56 },
57 "title": "HTTPValidationError",
58 "type": "object",
59 },
60 "ValidationError": {
61 "properties": {
62 "ctx": {"title": "Context", "type": "object"},
63 "input": {"title": "Input"},
64 "loc": {
65 "items": {
66 "anyOf": [{"type": "string"}, {"type": "integer"}]
67 },
68 "title": "Location",
69 "type": "array",
70 },
71 "msg": {"title": "Message", "type": "string"},
72 "type": {"title": "Error Type", "type": "string"},
73 },
74 "required": ["loc", "msg", "type"],
75 "title": "ValidationError",
76 "type": "object",
77 },
78 }
79 },
80 "info": {"title": "FastAPI", "version": "0.1.0"},
81 "openapi": "3.1.0",
82 "paths": {
83 "/foo": {
84 "get": {
85 "operationId": "foo_handler_foo_get",
86 "parameters": [
87 {
88 "in": "query",
89 "name": "client_id",
90 "required": True,
91 "schema": {"title": "Client Id", "type": "string"},
92 },
93 ],
94 "responses": {
95 "200": {
96 "content": {"application/json": {"schema": {}}},
97 "description": "Successful Response",
98 },
99 "422": {
100 "content": {
101 "application/json": {
102 "schema": {
103 "$ref": "#/components/schemas/HTTPValidationError"
104 }
105 }
106 },
107 "description": "Validation Error",
108 },
109 },
110 "summary": "Foo Handler",
111 }
112 }
113 },
114 }
115 )