Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.1 KB
Raw Blame
"""Tests for the FastAPI default-parameter pre-commit guard."""

import importlib.util
import sys
from pathlib import Path
from types import ModuleType

import pytest
import yaml

ROOT = Path(__file__).resolve().parents[2]
HOOK_PATH = ROOT / ".pre-commit-hooks" / "check-fastapi-default-parameters.py"
MODULE = "check_fastapi_default_parameters"


@pytest.fixture
def hook() -> ModuleType:
    assert HOOK_PATH.is_file(), f"hook is missing: {HOOK_PATH}"
    spec = importlib.util.spec_from_file_location(MODULE, HOOK_PATH)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


def _check(hook: ModuleType, tmp_path: Path, source: str):
    path = tmp_path / "sample.py"
    path.write_text(source, encoding="utf-8")
    return hook.check_file(path)


def test_current_fastapi_source_has_no_marker_defaults(hook):
    web = ROOT / "src" / "local_deep_research" / "web"
    paths = [web / "fastapi_app.py"]
    paths.extend(sorted((web / "routers").glob("*.py")))
    paths.extend(sorted((web / "dependencies").glob("*.py")))

    assert hook.check_paths(paths) == ()


def test_catches_typed_untyped_and_keyword_only_defaults(hook, tmp_path):
    diagnostics = _check(
        hook,
        tmp_path,
        "from fastapi import Depends, Form, Query\n"
        "def route(user: str = Depends(auth), query=Query(None), /, *, "
        "form=Form('')):\n"
        "    pass\n",
    )

    assert len(diagnostics) == 3
    assert [
        item.message.split(" uses ", 1)[1].split("(", 1)[0]
        for item in diagnostics
    ] == [
        "Depends",
        "Query",
        "Form",
    ]


def test_catches_module_imports_renames_and_simple_aliases(hook, tmp_path):
    diagnostics = _check(
        hook,
        tmp_path,
        "import fastapi as fa\n"
        "import fastapi.params as fp\n"
        "from fastapi import Depends as Inject\n"
        "from fastapi import params as params_alias\n"
        "Alias = Inject\n"
        "def route(a=fa.Query(None), b=fp.Body(None), c=Alias(auth), *, "
        "d=params_alias.Header(None)):\n"
        "    pass\n",
    )

    assert len(diagnostics) == 4
    messages = "\n".join(item.message for item in diagnostics)
    for marker in ("Query", "Body", "Depends", "Header"):
        assert f"uses {marker}(...)" in messages


def test_annotated_parameters_and_decorator_dependencies_pass(hook, tmp_path):
    diagnostics = _check(
        hook,
        tmp_path,
        "from typing import Annotated\n"
        "from fastapi import Depends, Query\n"
        "@router.get('/items', dependencies=[Depends(auth)])\n"
        "def route(user: Annotated[str, Depends(auth)], "
        "query: Annotated[str | None, Query()] = None):\n"
        "    return user, query\n",
    )

    assert diagnostics == ()


def test_same_named_non_fastapi_calls_are_not_flagged(hook, tmp_path):
    diagnostics = _check(
        hook,
        tmp_path,
        "from pathlib import Path\n"
        "def Depends(value):\n"
        "    return value\n"
        "def helper(path=Path('.'), value=Depends('local')):\n"
        "    return path, value\n",
    )

    assert diagnostics == ()


def test_fastapi_star_import_fails_closed(hook, tmp_path):
    diagnostics = _check(
        hook,
        tmp_path,
        "from fastapi import *\ndef route(value=Depends(auth)):\n    pass\n",
    )

    assert len(diagnostics) == 1
    assert (
        "star imports prevent reliable marker resolution"
        in diagnostics[0].message
    )


def test_invalid_python_fails_closed(hook, tmp_path):
    diagnostics = _check(hook, tmp_path, "def broken(:\n")

    assert len(diagnostics) == 1
    assert "invalid syntax" in diagnostics[0].message


def test_main_reports_location_and_returns_nonzero(hook, tmp_path, capsys):
    path = tmp_path / "bad.py"
    path.write_text(
        "from fastapi import Depends\ndef route(value=Depends(auth)):\n    pass\n",
        encoding="utf-8",
    )

    assert hook.main([str(path)]) == 1
    output = capsys.readouterr().out
    assert f"{path}:2:" in output
    assert "parameter 'value' uses Depends(...)" in output


def test_precommit_configuration_wires_the_guard():
    config = yaml.safe_load((ROOT / ".pre-commit-config.yaml").read_text())
    hooks = {
        hook["id"]: hook
        for repository in config["repos"]
        if repository["repo"] == "local"
        for hook in repository["hooks"]
    }

    configured = hooks["check-fastapi-default-parameters"]
    assert configured["entry"] == str(HOOK_PATH.relative_to(ROOT))
    assert configured["language"] == "script"
    assert "routers/" in configured["files"]
    assert "dependencies/" in configured["files"]

    ruff_repository = next(
        repository
        for repository in config["repos"]
        if repository["repo"] == "https://github.com/astral-sh/ruff-pre-commit"
    )
    fast002 = next(
        item
        for item in ruff_repository["hooks"]
        if item.get("alias") == "fastapi-annotated-dependencies"
    )
    assert fast002["args"] == ["--select", "FAST002"]
    assert "dependencies/" in fast002["files"]