Coverage for tests / test_stringified_annotation_dependency.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from __future__ import annotations 1abcd
3from typing import TYPE_CHECKING, Annotated 1abcd
5import pytest 1abcd
6from fastapi import Depends, FastAPI 1abcd
7from fastapi.testclient import TestClient 1abcd
8from inline_snapshot import snapshot 1abcd
10if TYPE_CHECKING: # pragma: no cover 1abcd
11 from collections.abc import AsyncGenerator
14class DummyClient: 1abcd
15 async def get_people(self) -> list: 1abcd
16 return ["John Doe", "Jane Doe"] 1efgh
18 async def close(self) -> None: 1abcd
19 pass 1efgh
22async def get_client() -> AsyncGenerator[DummyClient, None]: 1abcd
23 client = DummyClient() 1efgh
24 yield client 1efgh
25 await client.close() 1efgh
28Client = Annotated[DummyClient, Depends(get_client)] 1abcd
31@pytest.fixture(name="client") 1abcd
32def client_fixture() -> TestClient: 1abcd
33 app = FastAPI() 1ijklmno
35 @app.get("/") 1ijklmno
36 async def get_people(client: Client) -> list: 1ijklmno
37 return await client.get_people() 1efgh
39 client = TestClient(app) 1ijklmno
40 return client 1ijklmno
43def test_get(client: TestClient): 1abcd
44 response = client.get("/") 1efgh
45 assert response.status_code == 200, response.text 1efgh
46 assert response.json() == ["John Doe", "Jane Doe"] 1efgh
49def test_openapi_schema(client: TestClient): 1abcd
50 response = client.get("/openapi.json") 1pqrs
51 assert response.status_code == 200, response.text 1pqrs
52 assert response.json() == snapshot( 1pqrs
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 )