Skip to content

Commit 2272b67

Browse files
committed
less code
1 parent 6729adb commit 2272b67

File tree

4 files changed

+10
-55
lines changed

4 files changed

+10
-55
lines changed

tests/unit/task_processor/conftest.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,6 @@ def task_processor_mode_marked(request: pytest.FixtureRequest) -> None:
2323
request.getfixturevalue("task_processor_mode")
2424

2525

26-
class GetTaskProcessorCaplog(typing.Protocol):
27-
def __call__(
28-
self, log_level: str | int = logging.INFO
29-
) -> pytest.LogCaptureFixture: ...
30-
31-
32-
@pytest.fixture
33-
def get_task_processor_caplog(
34-
caplog: pytest.LogCaptureFixture,
35-
) -> GetTaskProcessorCaplog:
36-
# caplog doesn't allow you to capture logging outputs from loggers that don't
37-
# propagate to root. Quick hack here to get the task_processor logger to
38-
# propagate.
39-
# TODO: look into using loguru.
40-
41-
def _inner(log_level: str | int = logging.INFO) -> pytest.LogCaptureFixture:
42-
task_processor_logger = logging.getLogger("task_processor")
43-
task_processor_logger.propagate = True
44-
# Assume required level for the logger.
45-
task_processor_logger.setLevel(log_level)
46-
caplog.set_level(log_level)
47-
return caplog
48-
49-
return _inner
50-
51-
5226
@pytest.fixture(autouse=True)
5327
def task_registry() -> typing.Generator[dict[str, RegisteredTask], None, None]:
5428
from task_processor.task_registry import registered_tasks

tests/unit/task_processor/test_unit_task_processor_decorators.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import typing
34
from datetime import timedelta
45
from unittest.mock import MagicMock
@@ -18,10 +19,6 @@
1819
from task_processor.task_registry import get_task, initialise
1920
from task_processor.task_run_method import TaskRunMethod
2021

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

2623
@pytest.fixture
2724
def mock_thread_class(
@@ -36,12 +33,12 @@ def mock_thread_class(
3633

3734
@pytest.mark.django_db
3835
def test_register_task_handler_run_in_thread__transaction_commit__true__default(
39-
get_task_processor_caplog: "GetTaskProcessorCaplog",
36+
caplog: pytest.LogCaptureFixture,
4037
mock_thread_class: MagicMock,
4138
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
4239
) -> None:
4340
# Given
44-
caplog = get_task_processor_caplog()
41+
caplog.set_level(logging.DEBUG)
4542

4643
@register_task_handler()
4744
def my_function(*args: str, **kwargs: str) -> None:
@@ -69,11 +66,11 @@ def my_function(*args: str, **kwargs: str) -> None:
6966

7067

7168
def test_register_task_handler_run_in_thread__transaction_commit__false(
72-
get_task_processor_caplog: "GetTaskProcessorCaplog",
69+
caplog: pytest.LogCaptureFixture,
7370
mock_thread_class: MagicMock,
7471
) -> None:
7572
# Given
76-
caplog = get_task_processor_caplog()
73+
caplog.set_level(logging.DEBUG)
7774

7875
@register_task_handler(transaction_on_commit=False)
7976
def my_function(*args: typing.Any, **kwargs: typing.Any) -> None:

tests/unit/task_processor/test_unit_task_processor_processor.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
)
3333
from task_processor.task_registry import initialise, registered_tasks
3434

35-
if typing.TYPE_CHECKING:
36-
# This import breaks private-package-test workflow in core
37-
from tests.unit.task_processor.conftest import GetTaskProcessorCaplog
38-
39-
4035
DEFAULT_CACHE_KEY = "foo"
4136
DEFAULT_CACHE_VALUE = "bar"
4237

@@ -109,10 +104,9 @@ def test_run_task_runs_task_and_creates_task_run_object_when_success(
109104
@pytest.mark.task_processor_mode
110105
def test_run_task_kills_task_after_timeout(
111106
sleep_task: TaskHandler[[int]],
112-
get_task_processor_caplog: "GetTaskProcessorCaplog",
107+
caplog: pytest.LogCaptureFixture,
113108
) -> None:
114109
# Given
115-
caplog = get_task_processor_caplog(logging.ERROR)
116110
task = Task.create(
117111
sleep_task.task_identifier,
118112
scheduled_for=timezone.now(),
@@ -148,11 +142,9 @@ def test_run_task_kills_task_after_timeout(
148142
@pytest.mark.django_db
149143
@pytest.mark.task_processor_mode
150144
def test_run_recurring_task_kills_task_after_timeout(
151-
get_task_processor_caplog: "GetTaskProcessorCaplog",
145+
caplog: pytest.LogCaptureFixture,
152146
) -> None:
153147
# Given
154-
caplog = get_task_processor_caplog(logging.ERROR)
155-
156148
@register_recurring_task(
157149
run_every=timedelta(seconds=1), timeout=timedelta(microseconds=1)
158150
)
@@ -384,11 +376,10 @@ def _a_task() -> None:
384376
@pytest.mark.task_processor_mode
385377
def test_run_task_runs_task_and_creates_task_run_object_when_failure(
386378
raise_exception_task: TaskHandler[[str]],
387-
get_task_processor_caplog: "GetTaskProcessorCaplog",
379+
caplog: pytest.LogCaptureFixture,
388380
) -> None:
389381
# Given
390-
caplog = get_task_processor_caplog(logging.DEBUG)
391-
382+
caplog.set_level(logging.DEBUG)
392383
msg = "Error!"
393384
task = Task.create(
394385
raise_exception_task.task_identifier, args=(msg,), scheduled_for=timezone.now()

tests/unit/task_processor/test_unit_task_processor_threads.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import typing
32
from typing import Type
43

54
import pytest
@@ -8,10 +7,6 @@
87

98
from task_processor.threads import TaskRunner
109

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

1611
@pytest.mark.parametrize(
1712
"exception_class, exception_message",
@@ -20,13 +15,11 @@
2015
@pytest.mark.django_db
2116
def test_task_runner_is_resilient_to_errors(
2217
mocker: MockerFixture,
23-
get_task_processor_caplog: "GetTaskProcessorCaplog",
18+
caplog: pytest.LogCaptureFixture,
2419
exception_class: Type[Exception],
2520
exception_message: str,
2621
) -> None:
2722
# Given
28-
caplog = get_task_processor_caplog(logging.DEBUG)
29-
3023
task_runner = TaskRunner()
3124
mocker.patch(
3225
"task_processor.threads.run_tasks",

0 commit comments

Comments
 (0)