|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import procrastinate |
| 4 | +import pytest |
| 5 | +from procrastinate import testing |
| 6 | + |
| 7 | +from taskbadger.procrastinate import _INSTRUMENTED_ATTR, TB_TASK_ID_KWARG, _task_cache |
| 8 | +from taskbadger.systems.procrastinate import ProcrastinateSystemIntegration |
| 9 | +from tests.utils import task_for_test |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def _clear_task_cache(): |
| 14 | + _task_cache.cache.clear() |
| 15 | + yield |
| 16 | + _task_cache.cache.clear() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def app(): |
| 21 | + in_memory = testing.InMemoryConnector() |
| 22 | + app = procrastinate.App(connector=in_memory) |
| 23 | + with app.open(): |
| 24 | + yield app |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + ("include", "exclude", "expected"), |
| 29 | + [ |
| 30 | + (None, None, True), |
| 31 | + (["myapp.tasks.export_data"], None, True), |
| 32 | + ([".*export_data"], [], True), |
| 33 | + ([".*export_da"], [], False), |
| 34 | + (["myapp.tasks.export_data"], ["myapp.tasks.export_data"], False), |
| 35 | + ([".*"], ["myapp.tasks.export_data"], False), |
| 36 | + ([".*"], [".*tasks.*"], False), |
| 37 | + ], |
| 38 | +) |
| 39 | +def test_task_name_matching(app, include, exclude, expected): |
| 40 | + integration = ProcrastinateSystemIntegration(app=app, includes=include, excludes=exclude) |
| 41 | + assert integration.track_task("myapp.tasks.export_data") is expected |
| 42 | + |
| 43 | + |
| 44 | +def test_auto_track_off_returns_false(app): |
| 45 | + integration = ProcrastinateSystemIntegration(app=app, auto_track_tasks=False) |
| 46 | + assert integration.track_task("anything") is False |
| 47 | + |
| 48 | + |
| 49 | +def test_wraps_existing_tasks(app): |
| 50 | + @app.task(name="pre_existing") |
| 51 | + def pre_existing(a): |
| 52 | + return a |
| 53 | + |
| 54 | + assert not getattr(pre_existing, _INSTRUMENTED_ATTR, False) |
| 55 | + ProcrastinateSystemIntegration(app=app, auto_track_tasks=True) |
| 56 | + assert getattr(pre_existing, _INSTRUMENTED_ATTR) is True |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.usefixtures("_bind_settings") |
| 60 | +def test_auto_track_creates_pending(app): |
| 61 | + @app.task(name="auto_target") |
| 62 | + def auto_target(a): |
| 63 | + return a |
| 64 | + |
| 65 | + ProcrastinateSystemIntegration(app=app, auto_track_tasks=True) |
| 66 | + |
| 67 | + tb = task_for_test() |
| 68 | + with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create: |
| 69 | + auto_target.defer(a=1) |
| 70 | + |
| 71 | + create.assert_called_once() |
| 72 | + # InMemoryConnector.jobs is a dict keyed by int; kwargs under "args" |
| 73 | + jobs = list(app.connector.jobs.values()) |
| 74 | + assert jobs[0]["args"][TB_TASK_ID_KWARG] == tb.id |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.usefixtures("_bind_settings") |
| 78 | +def test_auto_track_excludes_skip(app): |
| 79 | + @app.task(name="myapp.cleanup.flush") |
| 80 | + def flush(): |
| 81 | + pass |
| 82 | + |
| 83 | + ProcrastinateSystemIntegration(app=app, auto_track_tasks=True, excludes=[r"myapp\.cleanup\..*"]) |
| 84 | + |
| 85 | + with mock.patch("taskbadger.procrastinate.create_task_safe") as create: |
| 86 | + flush.defer() |
| 87 | + |
| 88 | + create.assert_not_called() |
0 commit comments