|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import uvicorn |
| 5 | +from typer.testing import CliRunner |
| 6 | + |
| 7 | +from fastapi_cli.cli import app |
| 8 | +from tests.utils import changing_dir |
| 9 | + |
| 10 | +runner = CliRunner() |
| 11 | + |
| 12 | +assets_path = Path(__file__).parent / "assets" |
| 13 | + |
| 14 | + |
| 15 | +def test_dev_with_pyproject_app_config_uses() -> None: |
| 16 | + with changing_dir(assets_path / "pyproject_config"), patch.object( |
| 17 | + uvicorn, "run" |
| 18 | + ) as mock_run: |
| 19 | + result = runner.invoke(app, ["dev"]) |
| 20 | + assert result.exit_code == 0, result.output |
| 21 | + |
| 22 | + assert mock_run.call_args.kwargs["app"] == "my_module:app" |
| 23 | + assert mock_run.call_args.kwargs["host"] == "127.0.0.1" |
| 24 | + assert mock_run.call_args.kwargs["port"] == 8000 |
| 25 | + assert mock_run.call_args.kwargs["reload"] is True |
| 26 | + |
| 27 | + assert "Using import string: my_module:app" in result.output |
| 28 | + |
| 29 | + |
| 30 | +def test_run_with_pyproject_app_config() -> None: |
| 31 | + with changing_dir(assets_path / "pyproject_config"), patch.object( |
| 32 | + uvicorn, "run" |
| 33 | + ) as mock_run: |
| 34 | + result = runner.invoke(app, ["run"]) |
| 35 | + assert result.exit_code == 0, result.output |
| 36 | + |
| 37 | + assert mock_run.call_args.kwargs["app"] == "my_module:app" |
| 38 | + assert mock_run.call_args.kwargs["host"] == "0.0.0.0" |
| 39 | + assert mock_run.call_args.kwargs["port"] == 8000 |
| 40 | + assert mock_run.call_args.kwargs["reload"] is False |
| 41 | + |
| 42 | + assert "Using import string: my_module:app" in result.output |
| 43 | + |
| 44 | + |
| 45 | +def test_cli_arg_overrides_pyproject_config() -> None: |
| 46 | + with changing_dir(assets_path / "pyproject_config"), patch.object( |
| 47 | + uvicorn, "run" |
| 48 | + ) as mock_run: |
| 49 | + result = runner.invoke(app, ["dev", "another_module.py"]) |
| 50 | + |
| 51 | + assert result.exit_code == 0, result.output |
| 52 | + |
| 53 | + assert mock_run.call_args.kwargs["app"] == "another_module:app" |
| 54 | + |
| 55 | + |
| 56 | +def test_pyproject_app_config_invalid_format() -> None: |
| 57 | + test_dir = assets_path / "pyproject_invalid_config" |
| 58 | + test_dir.mkdir(exist_ok=True) |
| 59 | + |
| 60 | + pyproject_file = test_dir / "pyproject.toml" |
| 61 | + pyproject_file.write_text(""" |
| 62 | +[tool.fastapi] |
| 63 | +entrypoint = "invalid_format_without_colon" |
| 64 | +""") |
| 65 | + |
| 66 | + try: |
| 67 | + with changing_dir(test_dir): |
| 68 | + result = runner.invoke(app, ["dev"]) |
| 69 | + assert result.exit_code == 1 |
| 70 | + assert ( |
| 71 | + "Import string must be in the format module.submodule:app_name" |
| 72 | + in result.output |
| 73 | + ) |
| 74 | + finally: |
| 75 | + pyproject_file.unlink() |
| 76 | + test_dir.rmdir() |
| 77 | + |
| 78 | + |
| 79 | +def test_pyproject_validation_error() -> None: |
| 80 | + test_dir = assets_path / "pyproject_validation_error" |
| 81 | + test_dir.mkdir(exist_ok=True) |
| 82 | + |
| 83 | + pyproject_file = test_dir / "pyproject.toml" |
| 84 | + pyproject_file.write_text(""" |
| 85 | +[tool.fastapi] |
| 86 | +entrypoint = 123 |
| 87 | +""") |
| 88 | + |
| 89 | + try: |
| 90 | + with changing_dir(test_dir): |
| 91 | + result = runner.invoke(app, ["dev"]) |
| 92 | + assert result.exit_code == 1 |
| 93 | + assert "Invalid configuration in pyproject.toml:" in result.output |
| 94 | + assert "entrypoint" in result.output.lower() |
| 95 | + finally: |
| 96 | + pyproject_file.unlink() |
| 97 | + test_dir.rmdir() |
0 commit comments