Skip to content

Commit

Permalink
Merge pull request #48 from ARYAN-NIKNEZHAD/chore/pre-commit-config
Browse files Browse the repository at this point in the history
🔧  Update pre-commit config
  • Loading branch information
sepehr-akbarzadeh authored Dec 5, 2024
2 parents 0a68cbc + 0c090b4 commit 769f685
Show file tree
Hide file tree
Showing 19 changed files with 1,459 additions and 236 deletions.
63 changes: 40 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,46 @@ on: [push, pull_request]

jobs:
test:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-latest

env:
DJANGO_SETTINGS_MODULE: tests.testapp.settings.settings
PYTHONPATH: ${{ github.workspace }}

strategy:
matrix:
python-version:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install coverage codecov pytest
pip install -r packages/requirements-dev.txt
- name: Run tests
run: |
coverage run -m pytest
- name: Generate coverage report
run: coverage xml
- name: Run Tox tests
run: tox
- name: Run pre-commmit hooks
run: tox -e pre-commit
- name: Upload coverage to Codecov
run: codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install coverage codecov pytest poetry
pip install -r packages/requirements-dev.txt
- name: Run tests with coverage
run: pytest --cov=iranian_cities --cov-report=xml

- name: Run Tox tests
run: tox

- name: Run pre-commit hooks
run: pre-commit run --all-files --config=.pre-commit-config-ci.yaml

- name: Upload coverage to Codecov
run: codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
101 changes: 101 additions & 0 deletions .pre-commit-config-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-toml
- id: check-yaml
files: \.yaml$
- id: trailing-whitespace
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: end-of-file-fixer
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: check-added-large-files
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: check-case-conflict
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: check-merge-conflict
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: check-docstring-first
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.1
hooks:
- id: pyproject-fmt

- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 1.3.1
hooks:
- id: tox-ini-fmt

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.5.5
hooks:
- id: ruff
args: ["--config=pyproject.toml"]
exclude: (migrations/|tests/|docs/|static/|media/|apps.py).*

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: [ "--config=pyproject.toml" ]
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/commitizen-tools/commitizen
rev: v3.28.0
hooks:
- id: commitizen

- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
hooks:
- id: bandit
args: [ "-c", "pyproject.toml", "-r", "." ]
additional_dependencies: [ "bandit[toml]" ]
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/PyCQA/docformatter
rev: v1.7.5
hooks:
- id: docformatter
args: [ "--in-place", "--recursive", "--blank" ]
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/adamchainz/blacken-docs
rev: 1.18.0
hooks:
- id: blacken-docs
additional_dependencies:
- black==24.4.2
files: '\.rst$'

- repo: https://github.com/rstcheck/rstcheck
rev: "v6.2.4"
hooks:
- id: rstcheck
args: [ "--report-level=warning" ]
files: ^(docs/(.*/)*.*\.rst)
additional_dependencies: [ Sphinx==6.2.1 ]

- repo: local
hooks:
- id: pytest
name: Pytest
entry: poetry run pytest -v
language: system
types: [ python ]
stages: [ commit ]
pass_filenames: false
always_run: true
27 changes: 26 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-toml
- id: check-yaml
files: \.yaml$
- id: trailing-whitespace
exclude: (migrations/|tests/|docs/|static/|media/).*
- id: end-of-file-fixer
Expand All @@ -15,6 +18,21 @@ repos:
- id: check-docstring-first
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 2.2.1
hooks:
- id: pyproject-fmt

- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 1.3.1
hooks:
- id: tox-ini-fmt

- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.5.5
hooks:
Expand All @@ -39,7 +57,6 @@ repos:
rev: v3.28.0
hooks:
- id: commitizen
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
Expand All @@ -56,6 +73,14 @@ repos:
args: ["--in-place", "--recursive", "--blank"]
exclude: (migrations/|tests/|docs/|static/|media/).*

- repo: https://github.com/adamchainz/blacken-docs
rev: 1.18.0
hooks:
- id: blacken-docs
additional_dependencies:
- black==24.4.2
files: '\.rst$'

- repo: https://github.com/rstcheck/rstcheck
rev: "v6.2.4"
hooks:
Expand Down
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Thank you for your interest in contributing to our package! This document outlin
11. **Export Dependencies**: Export dependencies for development and production.
```bash
poetry export -f requirements.txt --output packages/requirements.txt --without-hashes
poetry export -f requirements.txt --dev --output packages/requirements-dev.txt --without-hashes
poetry export -f requirements.txt --with dev --output packages/requirements-dev.txt --without-hashes
```

## Commitizen Message Rule
Expand Down
43 changes: 30 additions & 13 deletions docs/pytest_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ Example File Structure
pytestmark = [
pytest.mark.django_db,
pytest.mark.admin,
pytest.mark.skipif(sys.version_info < (3, 8), reason="Python 3.8 or higher required")
pytest.mark.skipif(
sys.version_info < (3, 8), reason="Python 3.8 or higher required"
),
]
User = get_user_model()
class TestMyModelAdmin:
"""Class for testing MyModelAdmin functionality."""
def test_some_functionality(self) -> None:
# Test logic goes here
pass
Expand All @@ -76,7 +79,9 @@ Example of pytestmark
pytestmark = [
pytest.mark.django_db,
pytest.mark.admin,
pytest.mark.skipif(sys.version_info < (3, 8), reason="Python 3.8 or higher required")
pytest.mark.skipif(
sys.version_info < (3, 8), reason="Python 3.8 or higher required"
),
]
Docstrings
Expand Down Expand Up @@ -149,14 +154,17 @@ Admin Model Test Example
pytestmark = [
pytest.mark.django_db,
pytest.mark.admin,
pytest.mark.skipif(sys.version_info < (3, 8), reason="Python 3.8 or higher required")
pytest.mark.skipif(
sys.version_info < (3, 8), reason="Python 3.8 or higher required"
),
]
User = get_user_model()
class TestMyModelAdmin:
"""Class for testing MyModelAdmin functionality."""
def test_formfield_for_foreignkey_sets_queryset(self) -> None:
"""
Test that the formfield for the ForeignKey correctly sets the queryset.
Expand All @@ -165,12 +173,18 @@ Admin Model Test Example
"""
admin_site = AdminSite()
request = HttpRequest()
request.user = User.objects.create_superuser('admin', '[email protected]', 'password')
request.user = User.objects.create_superuser(
"admin", "[email protected]", "password"
)
model_admin = MyModelAdmin(model=MyModel, admin_site=admin_site)
db_field = MyModel._meta.get_field('related_model')
with patch.object(MyModelAdmin, 'get_field_queryset', return_value=MyModel.objects.filter(name="Test")) as mock_get_field_queryset:
db_field = MyModel._meta.get_field("related_model")
with patch.object(
MyModelAdmin,
"get_field_queryset",
return_value=MyModel.objects.filter(name="Test"),
) as mock_get_field_queryset:
formfield = model_admin.formfield_for_foreignkey(db_field, request)
mock_get_field_queryset.assert_called_once_with(None, db_field, request)
assert isinstance(formfield.widget, widgets.ForeignKeyRawIdWidget)
Expand All @@ -189,12 +203,15 @@ Settings Check Test Example
pytestmark = [
pytest.mark.settings_checks,
pytest.mark.skipif(sys.version_info < (3, 8), reason="Python 3.8 or higher required")
pytest.mark.skipif(
sys.version_info < (3, 8), reason="Python 3.8 or higher required"
),
]
class TestMyAppConfig:
"""Test MyApp configuration and settings."""
def test_check_my_app_config_correct_settings(self, settings) -> None:
"""
Test the MyApp configuration checker with correct settings.
Expand All @@ -204,7 +221,7 @@ Settings Check Test Example
settings.MY_SETTING_ENABLED = True
errors = check_my_app_config({})
assert len(errors) == 0
def test_check_my_app_config_invalid_type(self, settings) -> None:
"""
Test the MyApp configuration checker with invalid type settings.
Expand Down
5 changes: 2 additions & 3 deletions docs/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ Quick Start
.. code-block:: python
INSTALLED_APPS = [
...
'iranian_cities',
...
## Other apps
"iranian_cities",
]
3. **Run migrations to apply model changes**:
Expand Down
2 changes: 2 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ You can use the provided fields and admin mixin in your Django models:
from django.db import models
from iranian_cities.fields import ProvinceField
class TestModel(models.Model):
province = ProvinceField()
Expand All @@ -29,6 +30,7 @@ You can use the provided fields and admin mixin in your Django models:
from iranian_cities.admin import IranianCitiesAdmin
from test_app.models import TestModel
@admin.register(TestModel)
class TestModelAdmin(IranianCitiesAdmin):
pass
Loading

0 comments on commit 769f685

Please sign in to comment.