Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧪 Fixes and simplifications to tests #103

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions tests/assets/default_files/default_app/api.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/assets/default_files/default_app_dir_app/app/api.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/assets/default_files/default_app_dir_main/app/api.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/assets/default_files/default_app_dir_main/app/app.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/assets/default_files/default_main/api.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/assets/default_files/default_main/app.py

This file was deleted.

9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ def setup_terminal() -> None:
rich_utils.FORCE_TERMINAL = False
setup_logging(terminal_width=3000)
return


@pytest.fixture(autouse=True)
def reset_imports() -> Generator[None, None, None]:
modules = sys.modules.copy()
try:
yield
finally:
sys.modules = modules
277 changes: 134 additions & 143 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,151 @@
from pathlib import Path
from unittest.mock import patch

import pytest
import uvicorn
from fastapi_cli.cli import app
from typer.testing import CliRunner

from tests.utils import changing_dir

runner = CliRunner()

assets_path = Path(__file__).parent / "assets"


def test_dev() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(app, ["dev", "single_file_app.py"])
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "127.0.0.1",
"port": 8000,
"reload": True,
"workers": None,
"root_path": "",
"proxy_headers": True,
}
assert "Using import string single_file_app:app" in result.output
assert (
"╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
)
assert "│ Serving at: http://127.0.0.1:8000" in result.output
assert "│ API docs: http://127.0.0.1:8000/docs" in result.output
assert "│ Running in development mode, for production use:" in result.output
assert "│ fastapi run" in result.output


def test_dev_args() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app,
[
"dev",
"single_file_app.py",
"--host",
"192.168.0.2",
"--port",
"8080",
"--no-reload",
"--root-path",
"/api",
"--app",
"api",
"--no-proxy-headers",
],
)
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:api",
"host": "192.168.0.2",
"port": 8080,
"reload": False,
"workers": None,
"root_path": "/api",
"proxy_headers": False,
}
assert "Using import string single_file_app:api" in result.output
assert (
"╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
)
assert "│ Serving at: http://192.168.0.2:8080" in result.output
assert "│ API docs: http://192.168.0.2:8080/docs" in result.output
assert "│ Running in development mode, for production use:" in result.output
assert "│ fastapi run" in result.output


def test_run() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(app, ["run", "single_file_app.py"])
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "0.0.0.0",
"port": 8000,
"reload": False,
"workers": None,
"root_path": "",
"proxy_headers": True,
}
assert "Using import string single_file_app:app" in result.output
assert (
"╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
def test_dev(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(app, ["dev", "single_file_app.py"])
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "127.0.0.1",
"port": 8000,
"reload": True,
"workers": None,
"root_path": "",
"proxy_headers": True,
}
assert "Using import string single_file_app:app" in result.output
assert "╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
assert "│ Serving at: http://127.0.0.1:8000" in result.output
assert "│ API docs: http://127.0.0.1:8000/docs" in result.output
assert "│ Running in development mode, for production use:" in result.output
assert "│ fastapi run" in result.output


def test_dev_args(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app,
[
"dev",
"single_file_app.py",
"--host",
"192.168.0.2",
"--port",
"8080",
"--no-reload",
"--root-path",
"/api",
"--app",
"api",
"--no-proxy-headers",
],
)
assert "│ Serving at: http://0.0.0.0:8000" in result.output
assert "│ API docs: http://0.0.0.0:8000/docs" in result.output
assert "│ Running in production mode, for development use:" in result.output
assert "│ fastapi dev" in result.output


def test_run_args() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app,
[
"run",
"single_file_app.py",
"--host",
"192.168.0.2",
"--port",
"8080",
"--no-reload",
"--workers",
"2",
"--root-path",
"/api",
"--app",
"api",
"--no-proxy-headers",
],
)
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:api",
"host": "192.168.0.2",
"port": 8080,
"reload": False,
"workers": 2,
"root_path": "/api",
"proxy_headers": False,
}
assert "Using import string single_file_app:api" in result.output
assert (
"╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:api",
"host": "192.168.0.2",
"port": 8080,
"reload": False,
"workers": None,
"root_path": "/api",
"proxy_headers": False,
}
assert "Using import string single_file_app:api" in result.output
assert "╭────────── FastAPI CLI - Development mode ───────────╮" in result.output
assert "│ Serving at: http://192.168.0.2:8080" in result.output
assert "│ API docs: http://192.168.0.2:8080/docs" in result.output
assert "│ Running in development mode, for production use:" in result.output
assert "│ fastapi run" in result.output


def test_run(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(app, ["run", "single_file_app.py"])
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:app",
"host": "0.0.0.0",
"port": 8000,
"reload": False,
"workers": None,
"root_path": "",
"proxy_headers": True,
}
assert "Using import string single_file_app:app" in result.output
assert "╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
assert "│ Serving at: http://0.0.0.0:8000" in result.output
assert "│ API docs: http://0.0.0.0:8000/docs" in result.output
assert "│ Running in production mode, for development use:" in result.output
assert "│ fastapi dev" in result.output


def test_run_args(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app,
[
"run",
"single_file_app.py",
"--host",
"192.168.0.2",
"--port",
"8080",
"--no-reload",
"--workers",
"2",
"--root-path",
"/api",
"--app",
"api",
"--no-proxy-headers",
],
)
assert "│ Serving at: http://192.168.0.2:8080" in result.output
assert "│ API docs: http://192.168.0.2:8080/docs" in result.output
assert "│ Running in production mode, for development use:" in result.output
assert "│ fastapi dev" in result.output


def test_run_error() -> None:
with changing_dir(assets_path):
result = runner.invoke(app, ["run", "non_existing_file.py"])
assert result.exit_code == 1, result.output
assert "Path does not exist non_existing_file.py" in result.output
assert result.exit_code == 0, result.output
assert mock_run.called
assert mock_run.call_args
assert mock_run.call_args.kwargs == {
"app": "single_file_app:api",
"host": "192.168.0.2",
"port": 8080,
"reload": False,
"workers": 2,
"root_path": "/api",
"proxy_headers": False,
}
assert "Using import string single_file_app:api" in result.output
assert "╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
assert "│ Serving at: http://192.168.0.2:8080" in result.output
assert "│ API docs: http://192.168.0.2:8080/docs" in result.output
assert "│ Running in production mode, for development use:" in result.output
assert "│ fastapi dev" in result.output


def test_run_error(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)
result = runner.invoke(app, ["run", "non_existing_file.py"])
assert result.exit_code == 1, result.output
assert "Path does not exist non_existing_file.py" in result.output


def test_dev_help() -> None:
Expand Down
Loading
Loading