Coverage for tests/test_enforce_once_required_parameter.py: 100%
27 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Optional 1abcde
3from fastapi import Depends, FastAPI, Query, status 1abcde
4from fastapi.testclient import TestClient 1abcde
6app = FastAPI() 1abcde
9def _get_client_key(client_id: str = Query(...)) -> str: 1abcde
10 return f"{client_id}_key" 1abcde
13def _get_client_tag(client_id: Optional[str] = Query(None)) -> Optional[str]: 1abcde
14 if client_id is None: 1abcde
15 return None 1abcde
16 return f"{client_id}_tag" 1abcde
19@app.get("/foo") 1abcde
20def foo_handler( 1abcde
21 client_key: str = Depends(_get_client_key),
22 client_tag: Optional[str] = Depends(_get_client_tag),
23):
24 return {"client_id": client_key, "client_tag": client_tag} 1abcde
27client = TestClient(app) 1abcde
29expected_schema = { 1abcde
30 "components": {
31 "schemas": {
32 "HTTPValidationError": {
33 "properties": {
34 "detail": {
35 "items": {"$ref": "#/components/schemas/ValidationError"},
36 "title": "Detail",
37 "type": "array",
38 }
39 },
40 "title": "HTTPValidationError",
41 "type": "object",
42 },
43 "ValidationError": {
44 "properties": {
45 "loc": {
46 "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
47 "title": "Location",
48 "type": "array",
49 },
50 "msg": {"title": "Message", "type": "string"},
51 "type": {"title": "Error " "Type", "type": "string"},
52 },
53 "required": ["loc", "msg", "type"],
54 "title": "ValidationError",
55 "type": "object",
56 },
57 }
58 },
59 "info": {"title": "FastAPI", "version": "0.1.0"},
60 "openapi": "3.1.0",
61 "paths": {
62 "/foo": {
63 "get": {
64 "operationId": "foo_handler_foo_get",
65 "parameters": [
66 {
67 "in": "query",
68 "name": "client_id",
69 "required": True,
70 "schema": {"title": "Client Id", "type": "string"},
71 },
72 ],
73 "responses": {
74 "200": {
75 "content": {"application/json": {"schema": {}}},
76 "description": "Successful " "Response",
77 },
78 "422": {
79 "content": {
80 "application/json": {
81 "schema": {
82 "$ref": "#/components/schemas/HTTPValidationError"
83 }
84 }
85 },
86 "description": "Validation " "Error",
87 },
88 },
89 "summary": "Foo Handler",
90 }
91 }
92 },
93}
96def test_schema(): 1abcde
97 response = client.get("/openapi.json") 1abcde
98 assert response.status_code == status.HTTP_200_OK 1abcde
99 actual_schema = response.json() 1abcde
100 assert actual_schema == expected_schema 1abcde
103def test_get_invalid(): 1abcde
104 response = client.get("/foo") 1abcde
105 assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY 1abcde
108def test_get_valid(): 1abcde
109 response = client.get("/foo", params={"client_id": "bar"}) 1abcde
110 assert response.status_code == 200 1abcde
111 assert response.json() == {"client_id": "bar_key", "client_tag": "bar_tag"} 1abcde