-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtest_utils_default_file.py
106 lines (89 loc) · 4.09 KB
/
test_utils_default_file.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import importlib
import sys
from pathlib import Path
import pytest
from fastapi_cli.discover import get_import_string
from fastapi_cli.exceptions import FastAPICLIException
from pytest import CaptureFixture
from .utils import changing_dir
assets_path = Path(__file__).parent / "assets"
def test_single_file_main(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_main"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("main")
importlib.reload(mod)
import_string, is_factory = get_import_string()
assert import_string == "main:app"
assert is_factory is False
captured = capsys.readouterr()
assert "Using path main.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_main/main.py" in captured.out
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_main" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 main.py" in captured.out
assert "Importing module main" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from main import app" in captured.out
assert "Using import string main:app" in captured.out
sys.path = old_sys_path
def test_single_file_app(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_app"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("app")
importlib.reload(mod)
import_string, is_factory = get_import_string()
assert import_string == "app:app"
assert is_factory is False
captured = capsys.readouterr()
assert "Using path app.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_app/app.py" in captured.out
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_app" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 app.py" in captured.out
assert "Importing module app" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from app import app" in captured.out
assert "Using import string app:app" in captured.out
sys.path = old_sys_path
def test_single_file_api(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_api"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("api")
importlib.reload(mod)
import_string, is_factory = get_import_string()
assert import_string == "api:app"
assert is_factory is False
captured = capsys.readouterr()
assert "Using path api.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_api/api.py" in captured.out
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_api" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 api.py" in captured.out
assert "Importing module api" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from api import app" in captured.out
assert "Using import string api:app" in captured.out
sys.path = old_sys_path
def test_non_default_file(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path / "default_files" / "non_default"):
with pytest.raises(FastAPICLIException) as e:
get_import_string()
assert (
"Could not find a default file to run, please provide an explicit path"
in e.value.args[0]
)