Skip to content

Commit

Permalink
fix: misc fixes to support django 4.x upgrade (#7)
Browse files Browse the repository at this point in the history
* fix: misc fixes to support django 4.x upgrade

* Add pre-commit

* fix lint

* bump python and db version
  • Loading branch information
gagantrivedi authored Jul 29, 2024
1 parent 5f43c67 commit a61b821
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 83 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

services:
postgres:
image: postgres:11.12-alpine
image: postgres:15.5-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand All @@ -27,7 +27,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: ['3.10', '3.11']
python-version: ['3.11', '3.12']

steps:
- name: Cloning repo
Expand All @@ -46,7 +46,7 @@ jobs:
run: poetry install

- name: Run Linters
run: |
run: |
poetry run black --check .
poetry run isort --check-only --diff .
poetry run flake8
Expand Down
33 changes: 33 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
language_version: python3
exclude: migrations

- repo: https://github.com/pycqa/flake8
rev: 7.1.0
hooks:
- id: flake8
name: flake8

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- id: check-json
- id: check-toml

- repo: https://github.com/python-poetry/poetry
rev: 1.8.0
hooks:
- id: poetry-check
args: ['-C', 'api']

124 changes: 61 additions & 63 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ environs = "~9.2.0"
psycopg2-binary = "~2.9.5"

[tool.poetry.group.dev.dependencies]
django = ">=3.2.23"
django = "~4.2.13"
pre-commit = "~3.0.4"
flake8 = "~6.0.0"
flake8 = "~7.1.0"
pytest-mock = "~3.10.0"
pylint = "~2.16.2"
pep8 = "~1.7.1"
autopep8 = "~2.0.1"
pytest = "~7.2.1"
black = "~23.7.0"
pytest-django = "^4.6.0"
django-capture-on-commit-callbacks = "^1.11.0"
black = "~24.4.2"
pytest-django = "^4.8.0"
pytest-freezegun = "~0.4.2"
isort = "~5.13.2"

Expand Down
4 changes: 3 additions & 1 deletion task_processor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ class Meta:
@property
def should_execute(self) -> bool:
now = timezone.now()
last_task_run = self.task_runs.order_by("-started_at").first()
last_task_run = (
self.task_runs.order_by("-started_at").first() if self.id else None
)

if not last_task_run:
# If we have never run this task, then we should execute it only if
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/task_processor/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def run_by_processor(monkeypatch):


class GetTaskProcessorCaplog(typing.Protocol):
def __call__(self, log_level: str | int = logging.INFO) -> pytest.LogCaptureFixture:
...
def __call__(
self, log_level: str | int = logging.INFO
) -> pytest.LogCaptureFixture: ...


@pytest.fixture
Expand Down
17 changes: 10 additions & 7 deletions tests/unit/task_processor/test_unit_task_processor_decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import typing
from datetime import timedelta
from unittest.mock import MagicMock

import pytest
from django_capture_on_commit_callbacks import capture_on_commit_callbacks
from pytest_django import DjangoCaptureOnCommitCallbacks
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture

Expand All @@ -15,7 +16,10 @@
from task_processor.models import RecurringTask, Task, TaskPriority
from task_processor.task_registry import get_task
from task_processor.task_run_method import TaskRunMethod
from tests.unit.task_processor.conftest import GetTaskProcessorCaplog

if typing.TYPE_CHECKING:
# This import breaks private-package-test workflow in core
from tests.unit.task_processor.conftest import GetTaskProcessorCaplog


@pytest.fixture
Expand All @@ -31,8 +35,9 @@ def mock_thread_class(

@pytest.mark.django_db
def test_register_task_handler_run_in_thread__transaction_commit__true__default(
get_task_processor_caplog: GetTaskProcessorCaplog,
get_task_processor_caplog: "GetTaskProcessorCaplog",
mock_thread_class: MagicMock,
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
) -> None:
# Given
caplog = get_task_processor_caplog()
Expand All @@ -47,9 +52,7 @@ def my_function(*args: str, **kwargs: str) -> None:
kwargs = {"bar": "baz"}

# When
# TODO Switch to pytest-django's django_capture_on_commit_callbacks
# fixture when migrating to Django 4
with capture_on_commit_callbacks(execute=True):
with django_capture_on_commit_callbacks(execute=True):
my_function.run_in_thread(args=args, kwargs=kwargs)

# Then
Expand All @@ -65,7 +68,7 @@ def my_function(*args: str, **kwargs: str) -> None:


def test_register_task_handler_run_in_thread__transaction_commit__false(
get_task_processor_caplog: GetTaskProcessorCaplog,
get_task_processor_caplog: "GetTaskProcessorCaplog",
mock_thread_class: MagicMock,
) -> None:
# Given
Expand Down
Loading

0 comments on commit a61b821

Please sign in to comment.