Debian Codegraph @

tests/test_generic_parameterless_depends.py

raw
1 from typing import Annotated, TypeVar
2
3 from fastapi import Depends, FastAPI
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 app = FastAPI()
8
9 T = TypeVar("T")
10
11 Dep = Annotated[T, Depends()]
12
13
14 class A:
15 pass
16
17
18 class B:
19 pass
20
21
22 @app.get("/a")
23 async def a(dep: Dep[A]):
24 return {"cls": dep.__class__.__name__}
25
26
27 @app.get("/b")
28 async def b(dep: Dep[B]):
29 return {"cls": dep.__class__.__name__}
30
31
32 client = TestClient(app)
33
34
35 def test_generic_parameterless_depends():
36 response = client.get("/a")
37 assert response.status_code == 200, response.text
38 assert response.json() == {"cls": "A"}
39
40 response = client.get("/b")
41 assert response.status_code == 200, response.text
42 assert response.json() == {"cls": "B"}
43
44
45 def test_openapi_schema():
46 response = client.get("/openapi.json")
47 assert response.status_code == 200, response.text
48 assert response.json() == snapshot(
49 {
50 "info": {"title": "FastAPI", "version": "0.1.0"},
51 "openapi": "3.1.0",
52 "paths": {
53 "/a": {
54 "get": {
55 "operationId": "a_a_get",
56 "responses": {
57 "200": {
58 "content": {"application/json": {"schema": {}}},
59 "description": "Successful Response",
60 }
61 },
62 "summary": "A",
63 }
64 },
65 "/b": {
66 "get": {
67 "operationId": "b_b_get",
68 "responses": {
69 "200": {
70 "content": {"application/json": {"schema": {}}},
71 "description": "Successful Response",
72 }
73 },
74 "summary": "B",
75 }
76 },
77 },
78 }
79 )