Coverage for tests / test_tutorial / test_dependencies / test_tutorial008.py: 100%
26 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
1import importlib 1bcde
2import sys 1bcde
3from types import ModuleType 1bcde
4from typing import Annotated, Any 1bcde
5from unittest.mock import Mock, patch 1bcde
7import pytest 1bcde
8from fastapi import Depends, FastAPI 1bcde
9from fastapi.testclient import TestClient 1bcde
12@pytest.fixture( 1bcde
13 name="module",
14 params=[
15 "tutorial008_py310",
16 pytest.param(
17 "tutorial008_an_py310",
18 marks=pytest.mark.xfail(
19 sys.version_info < (3, 14),
20 reason="Fails with `NameError: name 'DepA' is not defined`",
21 ),
22 ),
23 ],
24)
25def get_module(request: pytest.FixtureRequest): 1bcde
26 mod_name = f"docs_src.dependencies.{request.param}" 1ojklpmqn
27 mod = importlib.import_module(mod_name) 1ojklpmqn
28 return mod 1jklmn
31def test_get_db(module: ModuleType): 1bcde
32 app = FastAPI() 1fghia
34 @app.get("/") 1fghia
35 def read_root(c: Annotated[Any, Depends(module.dependency_c)]): 1fghia
36 return {"c": str(c)} 1fghia
38 client = TestClient(app) 1fghia
40 a_mock = Mock() 1fghia
41 b_mock = Mock() 1fghia
42 c_mock = Mock() 1fghia
44 with ( 1a
45 patch(
46 f"{module.__name__}.generate_dep_a",
47 return_value=a_mock,
48 create=True,
49 ),
50 patch(
51 f"{module.__name__}.generate_dep_b",
52 return_value=b_mock,
53 create=True,
54 ),
55 patch(
56 f"{module.__name__}.generate_dep_c",
57 return_value=c_mock,
58 create=True,
59 ),
60 ):
61 response = client.get("/") 1fghia
63 assert response.status_code == 200 1fghia
64 assert response.json() == {"c": str(c_mock)} 1fghia