-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest_utils_single_file.py
More file actions
71 lines (45 loc) 路 2.53 KB
/
Copy pathtest_utils_single_file.py
File metadata and controls
71 lines (45 loc) 路 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from pathlib import Path
import pytest
from pytest import CaptureFixture
from fastapi_cli.discover import get_import_data
from fastapi_cli.exceptions import FastAPICLIException
from .utils import changing_dir
assets_path = Path(__file__).parent / "assets"
def test_single_file_app(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_data = get_import_data(path=Path("single_file_app.py"))
assert import_data.import_string == "single_file_app:app"
assert import_data.module_data.extra_sys_path == assets_path
assert import_data.module_data.module_import_str == "single_file_app"
def test_single_file_api(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_data = get_import_data(path=Path("single_file_api.py"))
assert import_data.import_string == "single_file_api:api"
assert import_data.module_data.extra_sys_path == assets_path
assert import_data.module_data.module_import_str == "single_file_api"
def test_single_file_other(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_data = get_import_data(path=Path("single_file_other.py"))
assert import_data.import_string == "single_file_other:first_other"
assert import_data.module_data.extra_sys_path == assets_path
assert import_data.module_data.module_import_str == "single_file_other"
def test_single_file_explicit_object(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_data = get_import_data(
path=Path("single_file_app.py"), app_name="second_other"
)
assert import_data.import_string == "single_file_app:second_other"
assert import_data.module_data.extra_sys_path == assets_path
assert import_data.module_data.module_import_str == "single_file_app"
def test_single_file_non_fastapi_app_and_api(capsys: CaptureFixture[str]) -> None:
"""Fallback walks past non-FastAPI `app`/`api` names to find the real app."""
with changing_dir(assets_path):
import_data = get_import_data(path=Path("single_file_non_app.py"))
assert import_data.import_string == "single_file_non_app:my_app"
assert import_data.module_data.extra_sys_path == assets_path
assert import_data.module_data.module_import_str == "single_file_non_app"
def test_single_non_existing_file() -> None:
with changing_dir(assets_path):
with pytest.raises(FastAPICLIException) as e:
get_import_data(path=assets_path / "non_existing.py")
assert "Path does not exist" in e.value.args[0]