Skip to content

Commit 7abba98

Browse files
committed
[TEST] add startproject cli operation testcase
1 parent f721f65 commit 7abba98

File tree

6 files changed

+102
-52
lines changed

6 files changed

+102
-52
lines changed

src/fastapi_fastkit/utils/logging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from fastapi_fastkit.core.settings import FastkitConfig
1414

1515

16-
def setup_logging(settings: FastkitConfig, terminal_width: Union[int, None] = None) -> None:
16+
def setup_logging(
17+
settings: FastkitConfig, terminal_width: Union[int, None] = None
18+
) -> None:
1719
logger = logging.getLogger("fastapi-fastkit")
1820
console = Console(width=terminal_width) if terminal_width else None
1921
formatter = logging.Formatter("%(message)s")

tests/conftest.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@
44
# @author bnbong bbbong9@gmail.com
55
# --------------------------------------------------------------------------
66
import os
7+
import shutil
78
import pytest
89

910
from fastapi_fastkit.core.settings import FastkitConfig
1011

1112

1213
@pytest.fixture(autouse=True, scope="session")
13-
def console() -> None:
14-
pass
14+
def temp_dir():
15+
"""
16+
Fixture that creates a temporary directory for test cases and yields its path.
17+
After tests are done, the directory is removed.
18+
"""
19+
current_workspace = os.getcwd()
20+
temp_dir = os.path.join(current_workspace, "temp_test_workspace")
21+
os.makedirs(temp_dir, exist_ok=True)
22+
yield temp_dir
23+
shutil.rmtree(temp_dir)
1524

1625

1726
@pytest.fixture(autouse=True, scope="session")
1827
def set_terminal_width() -> None:
28+
"""
29+
Fixture to set the terminal width for tests.
30+
"""
1931
os.environ["COLUMNS"] = str(FastkitConfig.TEST_DEFAULT_TERMINAL_WIDTH)

tests/test_backends/test_configure.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,35 @@
44
# @author bnbong bbbong9@gmail.com
55
# --------------------------------------------------------------------------
66
import os
7-
import shutil
87

98
from pathlib import Path
109

1110
from click.testing import CliRunner
1211

12+
from fastapi_fastkit.cli import fastkit_cli
13+
1314

1415
class TestBackend:
1516
def setup_method(self) -> None:
1617
self.runner = CliRunner()
1718
self.current_workspace = os.getcwd()
18-
self.temp_dir = os.path.join(self.current_workspace, "temp_test_workspace")
19-
os.makedirs(self.temp_dir, exist_ok=True)
2019

2120
def teardown_method(self) -> None:
2221
os.chdir(self.current_workspace)
23-
shutil.rmtree(self.temp_dir)
2422

25-
def test_project_root_and_user_workspace(self) -> None:
23+
def test_project_root_and_user_workspace(self, temp_dir) -> None:
2624
"""
2725
Test that USER_WORKSPACE can change dynamically and is independent
2826
of FASTKIT_PROJECT_ROOT, which should always point to the source directory.
2927
"""
3028
# given
31-
os.chdir(self.temp_dir)
32-
from fastapi_fastkit.cli import fastkit_cli
29+
os.chdir(temp_dir)
3330

3431
# when
3532
result = self.runner.invoke(fastkit_cli, ["--debug", "echo"]) # type: ignore
3633

3734
# then
38-
expected_current_user_workspace = self.temp_dir
35+
expected_current_user_workspace = temp_dir
3936
expected_project_root = str(Path(__file__).parent.parent.parent)
4037

4138
assert "running at debugging mode!!" in result.output

tests/test_cli_operations/test_base_cli.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/test_cli_operations/test_cli.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# --------------------------------------------------------------------------
2+
# Testcases of base CLI operations.
3+
#
4+
# @author bnbong bbbong9@gmail.com
5+
# --------------------------------------------------------------------------
6+
import os
7+
8+
from click.testing import CliRunner
9+
from pathlib import Path
10+
11+
from fastapi_fastkit.cli import fastkit_cli
12+
13+
14+
class TestCLI:
15+
def setup_method(self) -> None:
16+
self.runner = CliRunner()
17+
self.current_workspace = os.getcwd()
18+
19+
def teardown_method(self, console) -> None:
20+
os.chdir(self.current_workspace)
21+
22+
def test_echo(self) -> None:
23+
# given
24+
25+
# when
26+
result = self.runner.invoke(fastkit_cli, ["echo"]) # type: ignore
27+
28+
# then
29+
assert (
30+
"╭─────────────────────────── About FastAPI-fastkit ────────────────────────────╮"
31+
in result.output
32+
)
33+
assert (
34+
"│ ⚡️ FastAPI fastkit - fastest FastAPI initializer. ⚡️"
35+
in result.output
36+
)
37+
assert (
38+
"│ Deploy FastAPI app foundation instantly at your local!"
39+
in result.output
40+
)
41+
assert "│ - Project Maintainer : bnbong(JunHyeok Lee)" in result.output
42+
assert (
43+
"│ - Github : https://github.com/bnbong/FastAPI-fastkit"
44+
in result.output
45+
)
46+
47+
def test_startproject(self, temp_dir) -> None:
48+
# given
49+
os.chdir(temp_dir)
50+
51+
# when
52+
result = self.runner.invoke(
53+
fastkit_cli, # type: ignore
54+
["startproject", "fastapi-default"],
55+
input='\n'.join(["test-project", "bnbong", "bbbong9@gmail.com", "test project"]),
56+
)
57+
58+
# then
59+
project_path = Path(temp_dir) / "fastapi-default" # TODO : change this after adding folder naming feature.
60+
assert project_path.exists() and project_path.is_dir()
61+
assert f"FastAPI project 'test-project' from 'fastapi-default' has been created and saved to {temp_dir}!" in result.output
62+
63+
expected_files = ["main.py", "setup.py"]
64+
for file in expected_files:
65+
file_path = project_path / file
66+
assert file_path.exists() and file_path.is_file()
67+
68+
main_py_path = project_path / "main.py"
69+
setup_py_path = project_path / "setup.py"
70+
71+
with open(main_py_path, "r") as main_py:
72+
main_py_content = main_py.read()
73+
assert "test-project" in main_py_content
74+
assert "test project" in main_py_content
75+
76+
with open(setup_py_path, "r") as setup_py:
77+
setup_py_content = setup_py.read()
78+
assert "test-project" in setup_py_content
79+
assert "bnbong" in setup_py_content
80+
assert "bbbong9@gmail.com" in setup_py_content

tests/test_cli_operations/test_deploy_template.py

Whitespace-only changes.

0 commit comments

Comments
 (0)