Skip to content

🧪 Fixes and simplifications to tests #103

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests: improve tests with parametrize and monkeypatch
  • Loading branch information
mattmess1221 committed Oct 9, 2024
commit 13bd892af2d982a4089a6e691a24b6d480e1215e
251 changes: 121 additions & 130 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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"


@pytest.fixture(autouse=True)
def assets(monkeypatch: pytest.MonkeyPatch):
monkeypatch.chdir(assets_path)


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
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
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://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
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
)
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
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() -> 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
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
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() -> 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
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:
44 changes: 18 additions & 26 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
@@ -5,41 +5,33 @@
from fastapi_cli.exceptions import FastAPICLIException
from typer.testing import CliRunner

from .utils import changing_dir

runner = CliRunner()

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


def test_no_uvicorn() -> None:
def test_no_uvicorn(monkeypatch: pytest.MonkeyPatch) -> None:
import fastapi_cli.cli
import uvicorn

fastapi_cli.cli.uvicorn = None # type: ignore[attr-defined, assignment]

with changing_dir(assets_path):
result = runner.invoke(fastapi_cli.cli.app, ["dev", "single_file_app.py"])
assert result.exit_code == 1
assert result.exception is not None
assert (
"Could not import Uvicorn, try running 'pip install uvicorn'"
in result.exception.args[0]
)

fastapi_cli.cli.uvicorn = uvicorn # type: ignore[attr-defined]
monkeypatch.setattr(fastapi_cli.cli, "uvicorn", None)
monkeypatch.chdir(assets_path)
result = runner.invoke(fastapi_cli.cli.app, ["dev", "single_file_app.py"])
assert result.exit_code == 1
assert result.exception is not None
assert (
"Could not import Uvicorn, try running 'pip install uvicorn'"
in result.exception.args[0]
)


def test_no_fastapi() -> None:
def test_no_fastapi(monkeypatch: pytest.MonkeyPatch) -> None:
import fastapi_cli.discover
from fastapi import FastAPI

fastapi_cli.discover.FastAPI = None # type: ignore[attr-defined, assignment]
with changing_dir(assets_path):
with pytest.raises(FastAPICLIException) as exc_info:
get_import_string(path=Path("single_file_app.py"))
assert "Could not import FastAPI, try running 'pip install fastapi'" in str(
exc_info.value
)
monkeypatch.setattr(fastapi_cli.discover, "FastAPI", None)
monkeypatch.chdir(assets_path)

fastapi_cli.discover.FastAPI = FastAPI # type: ignore[attr-defined]
with pytest.raises(FastAPICLIException) as exc_info:
get_import_string(path=Path("single_file_app.py"))
assert "Could not import FastAPI, try running 'pip install fastapi'" in str(
exc_info.value
)
94 changes: 29 additions & 65 deletions tests/test_utils_default_dir.py
Original file line number Diff line number Diff line change
@@ -5,84 +5,48 @@
from fastapi_cli.exceptions import FastAPICLIException
from pytest import CaptureFixture

from .utils import changing_dir

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


def test_app_dir_main(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path / "default_files" / "default_app_dir_main"):
import_string = get_import_string()
assert import_string == "app.main:app"

captured = capsys.readouterr()
assert "Using path app/main.py" in captured.out
assert "Resolved absolute path" in captured.out
assert (
"/tests/assets/default_files/default_app_dir_main/app/main.py" in captured.out
@pytest.mark.parametrize("prog_name", ["app", "api", "main"])
def test_app_dir_main(
prog_name: str,
monkeypatch: pytest.MonkeyPatch,
capsys: CaptureFixture[str],
) -> None:
monkeypatch.syspath_prepend(
assets_path / "default_files" / f"default_app_dir_{prog_name}"
)
assert "Importing from" in captured.out
assert "tests/assets/default_files/default_app_dir_main" in captured.out
assert "╭─ Python package file structure ─╮" in captured.out
assert "│ 📁 app" in captured.out
assert "│ ├── 🐍 __init__.py" in captured.out
assert "│ └── 🐍 main.py" in captured.out
assert "Importing module app.main" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from app.main import app" in captured.out
assert "Using import string app.main:app" in captured.out
monkeypatch.chdir(assets_path / "default_files" / f"default_app_dir_{prog_name}")


def test_app_dir_app(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path / "default_files" / "default_app_dir_app"):
import_string = get_import_string()
assert import_string == "app.app:app"
import_string = get_import_string()
assert import_string == f"app.{prog_name}:app"

captured = capsys.readouterr()
assert "Using path app/app.py" in captured.out
assert f"Using path app/{prog_name}.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_app_dir_app/app/app.py" in captured.out
assert "Importing from" in captured.out
assert "tests/assets/default_files/default_app_dir_app" in captured.out
assert "╭─ Python package file structure ─╮" in captured.out
assert "│ 📁 app" in captured.out
assert "│ ├── 🐍 __init__.py" in captured.out
assert "│ └── 🐍 app.py" in captured.out
assert "Importing module app.app" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from app.app import app" in captured.out
assert "Using import string app.app:app" in captured.out


def test_app_dir_api(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path / "default_files" / "default_app_dir_api"):
import_string = get_import_string()
assert import_string == "app.api:app"

captured = capsys.readouterr()
assert "Using path app/api.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_app_dir_api/app/api.py" in captured.out
assert (
f"/tests/assets/default_files/default_app_dir_{prog_name}/app/{prog_name}.py"
in captured.out
)
assert "Importing from" in captured.out
assert "tests/assets/default_files/default_app_dir_api" in captured.out
assert f"tests/assets/default_files/default_app_dir_{prog_name}" in captured.out
assert "╭─ Python package file structure ─╮" in captured.out
assert "│ 📁 app" in captured.out
assert "│ ├── 🐍 __init__.py" in captured.out
assert "│ └── 🐍 api.py" in captured.out
assert "Importing module app.api" in captured.out
assert f"│ └── 🐍 {prog_name}.py" in captured.out
assert f"Importing module app.{prog_name}" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from app.api import app" in captured.out
assert "Using import string app.api:app" in captured.out
assert f"from app.{prog_name} import app" in captured.out
assert f"Using import string app.{prog_name}:app" in captured.out


def test_app_dir_non_default() -> None:
with changing_dir(assets_path / "default_files" / "default_app_dir_non_default"):
with pytest.raises(FastAPICLIException) as e:
get_import_string()
assert (
"Could not find a default file to run, please provide an explicit path"
in e.value.args[0]
)
def test_app_dir_non_default(monkeypatch) -> None:
monkeypatch.chdir(assets_path / "default_files" / "default_app_dir_non_default")
with pytest.raises(FastAPICLIException) as e:
get_import_string()
assert (
"Could not find a default file to run, please provide an explicit path"
in e.value.args[0]
)
106 changes: 25 additions & 81 deletions tests/test_utils_default_file.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,47 @@
import importlib
import sys
from pathlib import Path

import pytest
from fastapi_cli.discover import get_import_string
from fastapi_cli.exceptions import FastAPICLIException
from pytest import CaptureFixture

from .utils import changing_dir

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


def test_single_file_main(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_main"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("main")
@pytest.mark.parametrize("prog_name", ["api", "app", "main"])
def test_single_file(
prog_name: str, monkeypatch: pytest.MonkeyPatch, capsys: CaptureFixture[str]
) -> None:
monkeypatch.chdir(assets_path / "default_files" / f"default_{prog_name}")

importlib.reload(mod)
import_string = get_import_string()
assert import_string == "main:app"
import_string = get_import_string()
assert import_string == f"{prog_name}:app"

captured = capsys.readouterr()
assert "Using path main.py" in captured.out
assert f"Using path {prog_name}.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_main/main.py" in captured.out
assert (
f"/tests/assets/default_files/default_{prog_name}/{prog_name}.py"
in captured.out
)
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_main" in captured.out
assert f"/tests/assets/default_files/default_{prog_name}" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 main.py" in captured.out
assert "Importing module main" in captured.out
assert f"│ 🐍 {prog_name}.py" in captured.out
assert f"Importing module {prog_name}" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from main import app" in captured.out
assert "Using import string main:app" in captured.out
sys.path = old_sys_path


def test_single_file_app(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_app"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("app")
assert f"from {prog_name} import app" in captured.out
assert f"Using import string {prog_name}:app" in captured.out

importlib.reload(mod)
import_string = get_import_string()
assert import_string == "app:app"

captured = capsys.readouterr()
assert "Using path app.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_app/app.py" in captured.out
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_app" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 app.py" in captured.out
assert "Importing module app" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from app import app" in captured.out
assert "Using import string app:app" in captured.out
sys.path = old_sys_path

def test_non_default_file(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path / "default_files" / "non_default")

def test_single_file_api(capsys: CaptureFixture[str]) -> None:
root_path = assets_path / "default_files" / "default_api"
old_sys_path = sys.path.copy()
with changing_dir(root_path):
sys.path.insert(0, str(root_path))
mod = importlib.import_module("api")

importlib.reload(mod)
import_string = get_import_string()
assert import_string == "api:app"

captured = capsys.readouterr()
assert "Using path api.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/default_files/default_api/api.py" in captured.out
assert "Importing from" in captured.out
assert "/tests/assets/default_files/default_api" in captured.out
assert "╭─ Python module file ─╮" in captured.out
assert "│ 🐍 api.py" in captured.out
assert "Importing module api" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from api import app" in captured.out
assert "Using import string api:app" in captured.out
sys.path = old_sys_path

with pytest.raises(FastAPICLIException) as e:
get_import_string()

def test_non_default_file(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path / "default_files" / "non_default"):
with pytest.raises(FastAPICLIException) as e:
get_import_string()
assert (
"Could not find a default file to run, please provide an explicit path"
in e.value.args[0]
)
assert (
"Could not find a default file to run, please provide an explicit path"
in e.value.args[0]
)
506 changes: 84 additions & 422 deletions tests/test_utils_package.py

Large diffs are not rendered by default.

120 changes: 34 additions & 86 deletions tests/test_utils_single_file.py
Original file line number Diff line number Diff line change
@@ -1,115 +1,63 @@
from pathlib import Path
from typing import Optional

import pytest
from fastapi_cli.discover import get_import_string
from fastapi_cli.exceptions import FastAPICLIException
from pytest import CaptureFixture

from .utils import changing_dir

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


def test_single_file_app(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_string = get_import_string(path=Path("single_file_app.py"))
assert import_string == "single_file_app:app"

captured = capsys.readouterr()
assert "Using path single_file_app.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "/tests/assets/single_file_app.py" in captured.out
assert (
"Searching for package file structure from directories with __init__.py files"
in captured.out
@pytest.fixture(autouse=True)
def chdir_assets(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(assets_path)


@pytest.mark.parametrize(
("module_name", "app_name", "import_name"),
[
("app", "app", None),
("api", "api", None),
("other", "first_other", None),
("other", "second_other", "second_other"),
],
)
def test_single_file_app(
module_name: str,
app_name: str,
import_name: Optional[str],
capsys: CaptureFixture[str],
) -> None:
import_string = get_import_string(
path=Path(f"single_file_{module_name}.py"), app_name=import_name
)
assert "Importing from" in captured.out
assert "tests/assets" in captured.out
assert "╭── Python module file ───╮" in captured.out
assert "│ 🐍 single_file_app.py" in captured.out
assert "Importing module single_file_app" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from single_file_app import app" in captured.out
assert "Using import string single_file_app:app" in captured.out


def test_single_file_api(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_string = get_import_string(path=Path("single_file_api.py"))
assert import_string == "single_file_api:api"
assert import_string == f"single_file_{module_name}:{app_name}"

captured = capsys.readouterr()
assert "Using path single_file_api.py" in captured.out
assert f"Using path single_file_{module_name}.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "tests/assets/single_file_api.py" in captured.out
assert f"/tests/assets/single_file_{module_name}.py" in captured.out
assert (
"Searching for package file structure from directories with __init__.py files"
in captured.out
)
assert "Importing from" in captured.out
assert "tests/assets" in captured.out
assert "╭── Python module file ───╮" in captured.out
assert "│ 🐍 single_file_api.py" in captured.out
assert "Importing module single_file_api" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from single_file_api import api" in captured.out
assert "Using import string single_file_api:api" in captured.out


def test_single_file_other(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_string = get_import_string(path=Path("single_file_other.py"))
assert import_string == "single_file_other:first_other"

captured = capsys.readouterr()
assert "Using path single_file_other.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "tests/assets/single_file_other.py" in captured.out
assert (
"Searching for package file structure from directories with __init__.py files"
in captured.out
"╭─── Python module file ────╮" in captured.out
or "╭── Python module file ───╮" in captured.out
)
assert "Importing from" in captured.out
assert "tests/assets" in captured.out
assert "╭─── Python module file ────╮" in captured.out
assert "│ 🐍 single_file_other.py" in captured.out
assert "Importing module single_file_other" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from single_file_other import first_other" in captured.out
assert "Using import string single_file_other:first_other" in captured.out


def test_single_file_explicit_object(capsys: CaptureFixture[str]) -> None:
with changing_dir(assets_path):
import_string = get_import_string(
path=Path("single_file_app.py"), app_name="second_other"
)
assert import_string == "single_file_app:second_other"

captured = capsys.readouterr()
assert "Using path single_file_app.py" in captured.out
assert "Resolved absolute path" in captured.out
assert "tests/assets/single_file_app.py" in captured.out
assert (
"Searching for package file structure from directories with __init__.py files"
in captured.out
)
assert "Importing from" in captured.out
assert "tests/assets" in captured.out
assert "╭── Python module file ───╮" in captured.out
assert "│ 🐍 single_file_app.py" in captured.out
assert "Importing module single_file_app" in captured.out
assert f"│ 🐍 single_file_{module_name}.py" in captured.out
assert f"Importing module single_file_{module_name}" in captured.out
assert "Found importable FastAPI app" in captured.out
assert "Importable FastAPI app" in captured.out
assert "from single_file_app import second_other" in captured.out
assert "Using import string single_file_app:second_other" in captured.out
assert f"from single_file_{module_name} import {app_name}" in captured.out
assert f"Using import string single_file_{module_name}:{app_name}" in captured.out


def test_single_non_existing_file() -> None:
with changing_dir(assets_path):
with pytest.raises(FastAPICLIException) as e:
get_import_string(path=assets_path / "non_existing.py")
with pytest.raises(FastAPICLIException) as e:
get_import_string(path=assets_path / "non_existing.py")
assert "Path does not exist" in e.value.args[0]
14 changes: 0 additions & 14 deletions tests/utils.py

This file was deleted.