Coverage for tests / test_stringified_annotation_dependency.py: 100%

32 statements  

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

1from __future__ import annotations 1abdc

2 

3from typing import TYPE_CHECKING, Annotated 1abdc

4 

5import pytest 1abdc

6from fastapi import Depends, FastAPI 1abdc

7from fastapi.testclient import TestClient 1abdc

8from inline_snapshot import snapshot 1abdc

9 

10if TYPE_CHECKING: # pragma: no cover 1abdc

11 from collections.abc import AsyncGenerator 

12 

13 

14class DummyClient: 1abdc

15 async def get_people(self) -> list: 1abdc

16 return ["John Doe", "Jane Doe"] 1efg

17 

18 async def close(self) -> None: 1abdc

19 pass 1efg

20 

21 

22async def get_client() -> AsyncGenerator[DummyClient, None]: 1abdc

23 client = DummyClient() 1efg

24 yield client 1efg

25 await client.close() 1efg

26 

27 

28Client = Annotated[DummyClient, Depends(get_client)] 1abdc

29 

30 

31@pytest.fixture(name="client") 1abdc

32def client_fixture() -> TestClient: 1abdc

33 app = FastAPI() 1abc

34 

35 @app.get("/") 1abc

36 async def get_people(client: Client) -> list: 1abc

37 return await client.get_people() 1efg

38 

39 client = TestClient(app) 1abc

40 return client 1abc

41 

42 

43def test_get(client: TestClient): 1abdc

44 response = client.get("/") 1efg

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

46 assert response.json() == ["John Doe", "Jane Doe"] 1efg

47 

48 

49def test_openapi_schema(client: TestClient): 1abdc

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

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

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

53 { 

54 "openapi": "3.1.0", 

55 "info": {"title": "FastAPI", "version": "0.1.0"}, 

56 "paths": { 

57 "/": { 

58 "get": { 

59 "summary": "Get People", 

60 "operationId": "get_people__get", 

61 "responses": { 

62 "200": { 

63 "description": "Successful Response", 

64 "content": { 

65 "application/json": { 

66 "schema": { 

67 "items": {}, 

68 "type": "array", 

69 "title": "Response Get People Get", 

70 } 

71 } 

72 }, 

73 } 

74 }, 

75 } 

76 } 

77 }, 

78 } 

79 )