Coverage for tests / test_generic_parameterless_depends.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated, TypeVar 1abcd

2 

3from fastapi import Depends, FastAPI 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7app = FastAPI() 1abcd

8 

9T = TypeVar("T") 1abcd

10 

11Dep = Annotated[T, Depends()] 1abcd

12 

13 

14class A: 1abcd

15 pass 1abcd

16 

17 

18class B: 1abcd

19 pass 1abcd

20 

21 

22@app.get("/a") 1abcd

23async def a(dep: Dep[A]): 1abcd

24 return {"cls": dep.__class__.__name__} 1efg

25 

26 

27@app.get("/b") 1abcd

28async def b(dep: Dep[B]): 1abcd

29 return {"cls": dep.__class__.__name__} 1efg

30 

31 

32client = TestClient(app) 1abcd

33 

34 

35def test_generic_parameterless_depends(): 1abcd

36 response = client.get("/a") 1efg

37 assert response.status_code == 200, response.text 1efg

38 assert response.json() == {"cls": "A"} 1efg

39 

40 response = client.get("/b") 1efg

41 assert response.status_code == 200, response.text 1efg

42 assert response.json() == {"cls": "B"} 1efg

43 

44 

45def test_openapi_schema(): 1abcd

46 response = client.get("/openapi.json") 1hij

47 assert response.status_code == 200, response.text 1hij

48 assert response.json() == snapshot( 1hij

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 )