Skip to content

Commit 7a609fa

Browse files
committed
test: cli tests
1 parent 5f39707 commit 7a609fa

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

tests/test_cli.py

+53-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from unittest.mock import patch
55

66
import uvicorn
7-
from fastapi_cli.cli import app
7+
from fastapi_cli.cli import DEFAULT_DOCS_URL, app
88
from typer.testing import CliRunner
99

1010
from tests.utils import changing_dir
@@ -221,3 +221,55 @@ def test_script() -> None:
221221
encoding="utf-8",
222222
)
223223
assert "Usage" in result.stdout
224+
225+
226+
def test_app_with_docs_url_set_should_show_correctly_url_in_stdout() -> None:
227+
with changing_dir(assets_path):
228+
with patch.object(uvicorn, "run") as mock_run:
229+
result = runner.invoke(app, ["dev", "with_docs_url_set.py"])
230+
assert result.exit_code == 0, result.output
231+
assert mock_run.called
232+
assert mock_run.call_args
233+
assert mock_run.call_args.kwargs == {
234+
"app": "with_docs_url_set:app",
235+
"host": "127.0.0.1",
236+
"port": 8000,
237+
"reload": True,
238+
"workers": None,
239+
"root_path": "",
240+
"proxy_headers": True,
241+
}
242+
assert "Using import string with_docs_url_set:app" in result.output
243+
assert (
244+
"╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
245+
)
246+
assert "│ Serving at: http://127.0.0.1:8000" in result.output
247+
assert "│ API docs: http://127.0.0.1:8000/any-other-path" in result.output
248+
assert "│ Running in development mode, for production use:" in result.output
249+
assert "│ fastapi run" in result.output
250+
251+
252+
def test_app_without_docs_url_set_should_show_default_url_in_stdout() -> None:
253+
with changing_dir(assets_path):
254+
with patch.object(uvicorn, "run") as mock_run:
255+
result = runner.invoke(app, ["dev", "without_docs_url_set.py"])
256+
assert result.exit_code == 0, result.output
257+
assert mock_run.called
258+
assert mock_run.call_args
259+
assert mock_run.call_args.kwargs == {
260+
"app": "without_docs_url_set:app",
261+
"host": "127.0.0.1",
262+
"port": 8000,
263+
"reload": True,
264+
"workers": None,
265+
"root_path": "",
266+
"proxy_headers": True,
267+
}
268+
assert "Using import string without_docs_url_set:app" in result.output
269+
assert (
270+
"╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
271+
)
272+
assert "│ Serving at: http://127.0.0.1:8000" in result.output
273+
assert f"│ API docs: http://127.0.0.1:8000{DEFAULT_DOCS_URL}" in result.output
274+
assert "│ Running in development mode, for production use:" in result.output
275+
assert "│ fastapi run" in result.output

0 commit comments

Comments
 (0)