Skip to content

Commit 56ced6b

Browse files
committed
Add tests for project configs
1 parent 2f2048f commit 56ced6b

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.fastapi.cli]
2+
path = "server.py"
3+
app = "application"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import FastAPI
2+
3+
application = FastAPI()
4+
5+
6+
@application.get("/")
7+
def app_root():
8+
return {"message": "configured app"}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
4+
5+
6+
@app.get("/")
7+
def app_root():
8+
return {"message": "configured app with subcommand config"}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[tool.fastapi.cli]
2+
# global option
3+
port = 8001
4+
5+
[tool.fastapi.cli.run]
6+
reload = true
7+
workers = 4
8+
9+
[tool.fastapi.cli.dev]
10+
# overrides global option
11+
port = 8002

tests/test_cli.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from unittest.mock import patch
55

6+
import pytest
67
import uvicorn
78
from fastapi_cli.cli import app
89
from typer.testing import CliRunner
@@ -82,6 +83,47 @@ def test_dev_args() -> None:
8283
assert "│ fastapi run" in result.output
8384

8485

86+
def test_project_run() -> None:
87+
with changing_dir(assets_path / "projects/configured_app"):
88+
with patch.object(uvicorn, "run") as mock_run:
89+
result = runner.invoke(app, ["run"])
90+
assert result.exit_code == 0, result.output
91+
assert mock_run.called
92+
assert mock_run.call_args
93+
assert mock_run.call_args.kwargs == {
94+
"app": "server:application",
95+
"host": "0.0.0.0",
96+
"port": 8000,
97+
"reload": False,
98+
"workers": None,
99+
"root_path": "",
100+
"proxy_headers": True,
101+
}
102+
103+
104+
@pytest.mark.parametrize(
105+
("command", "kwargs"),
106+
[
107+
("run", {"host": "0.0.0.0", "port": 8001, "workers": 4}),
108+
("dev", {"host": "127.0.0.1", "port": 8002, "workers": None}),
109+
],
110+
)
111+
def test_project_run_subconfigure(command: str, kwargs: dict) -> None:
112+
with changing_dir(assets_path / "projects/configured_app_subtable"):
113+
with patch.object(uvicorn, "run") as mock_run:
114+
result = runner.invoke(app, [command])
115+
assert result.exit_code == 0, result.output
116+
assert mock_run.called
117+
assert mock_run.call_args
118+
assert mock_run.call_args.kwargs == {
119+
"app": "app:app",
120+
"reload": True,
121+
"root_path": "",
122+
"proxy_headers": True,
123+
**kwargs,
124+
}
125+
126+
85127
def test_run() -> None:
86128
with changing_dir(assets_path):
87129
with patch.object(uvicorn, "run") as mock_run:

0 commit comments

Comments
 (0)