Skip to content

Commit 9f562a9

Browse files
authored
ci: add test for utils (#8)
* ci: add test for utils * tests: improve coverage
1 parent a30634d commit 9f562a9

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ci
2+
on: [ push, pull_request ]
3+
jobs:
4+
ci:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
python-version: [3.9, "3.10", 3.11, 3.12, 3.13, 3.14]
9+
steps:
10+
- uses: actions/cache@v4
11+
with:
12+
path: ~/.cache/pip
13+
key: ${{ runner.os }}-pip-${{ hashFiles('**/poetry.lock') }}
14+
restore-keys: |
15+
${{ runner.os }}-pip-
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-python@v5
18+
with:
19+
python-version: ${{ matrix.python-version }}
20+
allow-prereleases: true
21+
- name: Install and configure Poetry
22+
run: |
23+
pip install -U pip poetry
24+
poetry config virtualenvs.create false
25+
- name: Run CI
26+
run: make ci

pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tortoise_orm = "examples.TORTOISE_ORM"
4747
[tool.mypy]
4848
pretty = true
4949
check_untyped_defs = true
50-
ignore_missing_imports = true
50+
warn_unused_ignores = true
5151

5252
[tool.ruff]
5353
line-length = 100
@@ -60,3 +60,12 @@ extend-select = [
6060
"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
6161
"RUF100", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf
6262
]
63+
64+
[tool.coverage.report]
65+
show_missing = true
66+
exclude_also = [
67+
"pragma: no cover",
68+
"if TYPE_CHECKING:",
69+
"@overload",
70+
'if __name == "__main__":',
71+
]

tests/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from pathlib import Path
3+
from unittest.mock import patch
4+
5+
import pytest
6+
from asyncclick import BadOptionUsage, ClickException, Command, Context
7+
8+
import examples
9+
from tortoise_cli.utils import get_tortoise_config, tortoise_orm_config
10+
11+
EMPTY_TORTOISE_ORM = None
12+
13+
14+
def test_tortoise_orm_config():
15+
assert tortoise_orm_config() == "examples.TORTOISE_ORM"
16+
with patch.dict(os.environ, {"TORTOISE_ORM": "app.settings.TORTOISE_ORM"}):
17+
assert tortoise_orm_config() == "app.settings.TORTOISE_ORM"
18+
with patch.object(Path, "read_text", return_value=""):
19+
assert tortoise_orm_config() == ""
20+
21+
22+
def test_get_tortoise_config():
23+
ctx = Context(Command("shell"))
24+
assert get_tortoise_config(ctx, tortoise_orm_config()) == examples.TORTOISE_ORM
25+
with pytest.raises(
26+
ClickException,
27+
match="Error while importing configuration module: No module named 'settings'",
28+
):
29+
assert get_tortoise_config(ctx, "settings.TORTOISE_ORM")
30+
with pytest.raises(BadOptionUsage):
31+
assert get_tortoise_config(ctx, "tests.test_utils.EMPTY_TORTOISE_ORM")

0 commit comments

Comments
 (0)