Coverage for tests/test_utils_single_file.py: 100%
36 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-11 09:18 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-11 09:18 +0000
1from pathlib import Path 1abcdefghijklmn
3import pytest 1abcdefghijklmn
4from pytest import CaptureFixture 1abcdefghijklmn
6from fastapi_cli.discover import get_import_data 1abcdefghijklmn
7from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijklmn
9from .utils import changing_dir 1abcdefghijklmn
11assets_path = Path(__file__).parent / "assets" 1abcdefghijklmn
14def test_single_file_app(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn
15 with changing_dir(assets_path): 1opqrstuvwxyzAB
16 import_data = get_import_data(path=Path("single_file_app.py")) 1opqrstuvwxyzAB
18 assert import_data.import_string == "single_file_app:app" 1opqrstuvwxyzAB
20 assert import_data.module_data.extra_sys_path == assets_path 1opqrstuvwxyzAB
21 assert import_data.module_data.module_import_str == "single_file_app" 1opqrstuvwxyzAB
24def test_single_file_api(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn
25 with changing_dir(assets_path): 1CDEFGHIJKLMNOP
26 import_data = get_import_data(path=Path("single_file_api.py")) 1CDEFGHIJKLMNOP
28 assert import_data.import_string == "single_file_api:api" 1CDEFGHIJKLMNOP
30 assert import_data.module_data.extra_sys_path == assets_path 1CDEFGHIJKLMNOP
31 assert import_data.module_data.module_import_str == "single_file_api" 1CDEFGHIJKLMNOP
34def test_single_file_other(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn
35 with changing_dir(assets_path): 1QRSTUVWXYZ0123
36 import_data = get_import_data(path=Path("single_file_other.py")) 1QRSTUVWXYZ0123
38 assert import_data.import_string == "single_file_other:first_other" 1QRSTUVWXYZ0123
40 assert import_data.module_data.extra_sys_path == assets_path 1QRSTUVWXYZ0123
41 assert import_data.module_data.module_import_str == "single_file_other" 1QRSTUVWXYZ0123
44def test_single_file_explicit_object(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn
45 with changing_dir(assets_path): 1456789!#$%'()*
46 import_data = get_import_data( 1456789!#$%'()*
47 path=Path("single_file_app.py"), app_name="second_other"
48 )
50 assert import_data.import_string == "single_file_app:second_other" 1456789!#$%'()*
52 assert import_data.module_data.extra_sys_path == assets_path 1456789!#$%'()*
53 assert import_data.module_data.module_import_str == "single_file_app" 1456789!#$%'()*
56def test_single_non_existing_file() -> None: 1abcdefghijklmn
57 with changing_dir(assets_path): 1+,-./:;=?@[]^_
58 with pytest.raises(FastAPICLIException) as e: 1+,-./:;=?@[]^_
59 get_import_data(path=assets_path / "non_existing.py") 1+,-./:;=?@[]^_
60 assert "Path does not exist" in e.value.args[0] 1+,-./:;=?@[]^_