diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3b58330 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,26 @@ +name: ci +on: [ push, pull_request ] +jobs: + ci: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.9, "3.10", 3.11, 3.12, 3.13, 3.14] + steps: + - uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }} + restore-keys: | + ${{ runner.os }}-pip- + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + allow-prereleases: true + - name: Install and configure Poetry + run: | + pip install -U pip poetry + poetry config virtualenvs.create false + - name: Run CI + run: make ci diff --git a/pyproject.toml b/pyproject.toml index 794190d..524784a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,7 +47,7 @@ tortoise_orm = "examples.TORTOISE_ORM" [tool.mypy] pretty = true check_untyped_defs = true -ignore_missing_imports = true +warn_unused_ignores = true [tool.ruff] line-length = 100 @@ -60,3 +60,12 @@ extend-select = [ "UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up "RUF100", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf ] + +[tool.coverage.report] +show_missing = true +exclude_also = [ + "pragma: no cover", + "if TYPE_CHECKING:", + "@overload", + 'if __name == "__main__":', +] diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..a22a225 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,31 @@ +import os +from pathlib import Path +from unittest.mock import patch + +import pytest +from asyncclick import BadOptionUsage, ClickException, Command, Context + +import examples +from tortoise_cli.utils import get_tortoise_config, tortoise_orm_config + +EMPTY_TORTOISE_ORM = None + + +def test_tortoise_orm_config(): + assert tortoise_orm_config() == "examples.TORTOISE_ORM" + with patch.dict(os.environ, {"TORTOISE_ORM": "app.settings.TORTOISE_ORM"}): + assert tortoise_orm_config() == "app.settings.TORTOISE_ORM" + with patch.object(Path, "read_text", return_value=""): + assert tortoise_orm_config() == "" + + +def test_get_tortoise_config(): + ctx = Context(Command("shell")) + assert get_tortoise_config(ctx, tortoise_orm_config()) == examples.TORTOISE_ORM + with pytest.raises( + ClickException, + match="Error while importing configuration module: No module named 'settings'", + ): + assert get_tortoise_config(ctx, "settings.TORTOISE_ORM") + with pytest.raises(BadOptionUsage): + assert get_tortoise_config(ctx, "tests.test_utils.EMPTY_TORTOISE_ORM")