From 2eedfb30882abd2ba21dfef8b9fc23fd9dc7c2e2 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Sun, 15 Jun 2025 11:31:56 +0800 Subject: [PATCH 1/2] ci: add test for utils --- .github/workflows/ci.yml | 26 ++++++++++++++++++++++++++ tests/test_utils.py | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 tests/test_utils.py 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/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..2c57786 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,26 @@ +import os +from pathlib import Path +from unittest.mock import patch + +import pytest +from asyncclick.exceptions import ClickException + +import examples +from tortoise_cli.utils import get_tortoise_config, tortoise_orm_config + + +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(): + assert get_tortoise_config(None, tortoise_orm_config()) == examples.TORTOISE_ORM # type:ignore[arg-type] + with pytest.raises( + ClickException, + match="Error while importing configuration module: No module named 'settings'", + ): + assert get_tortoise_config(None, "settings.TORTOISE_ORM") # type:ignore[arg-type] From f4400414e351baddf81583c39417d0af4a58754a Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Sun, 15 Jun 2025 11:55:56 +0800 Subject: [PATCH 2/2] tests: improve coverage --- pyproject.toml | 11 ++++++++++- tests/test_utils.py | 11 ++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) 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 index 2c57786..a22a225 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,11 +3,13 @@ from unittest.mock import patch import pytest -from asyncclick.exceptions import ClickException +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" @@ -18,9 +20,12 @@ def test_tortoise_orm_config(): def test_get_tortoise_config(): - assert get_tortoise_config(None, tortoise_orm_config()) == examples.TORTOISE_ORM # type:ignore[arg-type] + 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(None, "settings.TORTOISE_ORM") # type:ignore[arg-type] + assert get_tortoise_config(ctx, "settings.TORTOISE_ORM") + with pytest.raises(BadOptionUsage): + assert get_tortoise_config(ctx, "tests.test_utils.EMPTY_TORTOISE_ORM")