Coverage for tests/test_generic_parameterless_depends.py: 100%
29 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 TypeVar 1abcde
3from fastapi import Depends, FastAPI 1abcde
4from fastapi.testclient import TestClient 1abcde
5from typing_extensions import Annotated 1abcde
7app = FastAPI() 1abcde
9T = TypeVar("T") 1abcde
11Dep = Annotated[T, Depends()] 1abcde
14class A: 1abcde
15 pass 1abcde
18class B: 1abcde
19 pass 1abcde
22@app.get("/a") 1abcde
23async def a(dep: Dep[A]): 1abcde
24 return {"cls": dep.__class__.__name__} 1abcde
27@app.get("/b") 1abcde
28async def b(dep: Dep[B]): 1abcde
29 return {"cls": dep.__class__.__name__} 1abcde
32client = TestClient(app) 1abcde
35def test_generic_parameterless_depends(): 1abcde
36 response = client.get("/a") 1abcde
37 assert response.status_code == 200, response.text 1abcde
38 assert response.json() == {"cls": "A"} 1abcde
40 response = client.get("/b") 1abcde
41 assert response.status_code == 200, response.text 1abcde
42 assert response.json() == {"cls": "B"} 1abcde
45def test_openapi_schema(): 1abcde
46 response = client.get("/openapi.json") 1abcde
47 assert response.status_code == 200, response.text 1abcde
48 assert response.json() == { 1abcde
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "openapi": "3.1.0",
51 "paths": {
52 "/a": {
53 "get": {
54 "operationId": "a_a_get",
55 "responses": {
56 "200": {
57 "content": {"application/json": {"schema": {}}},
58 "description": "Successful " "Response",
59 }
60 },
61 "summary": "A",
62 }
63 },
64 "/b": {
65 "get": {
66 "operationId": "b_b_get",
67 "responses": {
68 "200": {
69 "content": {"application/json": {"schema": {}}},
70 "description": "Successful " "Response",
71 }
72 },
73 "summary": "B",
74 }
75 },
76 },
77 }