|
| 1 | +from collections.abc import Callable |
| 2 | +from datetime import timedelta |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +import pytest |
| 6 | +from django.db.utils import ProgrammingError |
| 7 | +from django.utils import timezone |
| 8 | +from pytest_django.fixtures import SettingsWrapper |
| 9 | +from pytest_mock import MockerFixture |
| 10 | + |
| 11 | +from task_processor import managers |
| 12 | +from task_processor.models import Task |
| 13 | + |
| 14 | +now = timezone.now() |
| 15 | +one_hour_ago = now - timedelta(hours=1) |
| 16 | + |
| 17 | +pytestmark = pytest.mark.django_db |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def task_processor_database_tasks( |
| 22 | + mocker: MockerFixture, |
| 23 | + settings: SettingsWrapper, |
| 24 | +) -> Generator[Callable[..., list[Task]], None, None]: |
| 25 | + """ |
| 26 | + Prepare a fake tasks processor database |
| 27 | + """ |
| 28 | + |
| 29 | + def patch(*tasks: Task) -> list[Task]: |
| 30 | + def new_fetch_tasks( |
| 31 | + self: managers.TaskManager, |
| 32 | + database: str, |
| 33 | + num_tasks: int, |
| 34 | + ) -> Generator[Task, None, None]: |
| 35 | + if database == "task_processor": |
| 36 | + yield from tasks |
| 37 | + return |
| 38 | + yield from orig_fetch_tasks(self, database, num_tasks) |
| 39 | + |
| 40 | + orig_fetch_tasks = managers.TaskManager._fetch_tasks_from |
| 41 | + mocker.patch.object( |
| 42 | + managers.TaskManager, "_fetch_tasks_from", new=new_fetch_tasks |
| 43 | + ) |
| 44 | + return list(tasks) |
| 45 | + |
| 46 | + mocker.patch.object(managers, "transaction") |
| 47 | + settings.DATABASES["task_processor"] = {"not": "undefined"} |
| 48 | + yield patch |
| 49 | + del settings.DATABASES["task_processor"] |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def missing_database_function( |
| 54 | + mocker: MockerFixture, |
| 55 | +) -> Generator[Callable[..., list[Task]], None, None]: |
| 56 | + """ |
| 57 | + Pretends the `get_tasks_to_process` database function does not exist |
| 58 | + """ |
| 59 | + |
| 60 | + def patch(target_database: str, exception: Exception) -> None: |
| 61 | + class BrokenIterator: |
| 62 | + def __next__(self): |
| 63 | + raise exception |
| 64 | + |
| 65 | + def new_fetch_tasks( |
| 66 | + self: managers.TaskManager, |
| 67 | + database: str, |
| 68 | + num_tasks: int, |
| 69 | + ) -> Generator[Task, None, None]: |
| 70 | + if target_database == database: |
| 71 | + yield from BrokenIterator() |
| 72 | + yield from orig_fetch_tasks(self, database, num_tasks) |
| 73 | + |
| 74 | + orig_fetch_tasks = managers.TaskManager._fetch_tasks_from |
| 75 | + mocker.patch.object( |
| 76 | + managers.TaskManager, "_fetch_tasks_from", new=new_fetch_tasks |
| 77 | + ) |
| 78 | + |
| 79 | + return patch |
| 80 | + |
| 81 | + |
| 82 | +def test_TaskManager__default_database__consumes_tasks_from_database( |
| 83 | + mocker: MockerFixture, |
| 84 | +) -> None: |
| 85 | + # Given |
| 86 | + mocker.patch.object(managers.TaskManager, "_is_database_separate", new=False) |
| 87 | + new_tasks = [ |
| 88 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 89 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 90 | + ] |
| 91 | + Task.objects.bulk_create(new_tasks) |
| 92 | + |
| 93 | + # When |
| 94 | + tasks = Task.objects.get_tasks_to_process(1) |
| 95 | + |
| 96 | + # Then |
| 97 | + assert list(tasks) == new_tasks[:1] |
| 98 | + |
| 99 | + |
| 100 | +def test_TaskManager__default_database__task_processor_database_with_pending_tasks__consumes_tasks_from_old_database_first( |
| 101 | + mocker: MockerFixture, |
| 102 | + task_processor_database_tasks: Callable[..., list[Task]], |
| 103 | +) -> None: |
| 104 | + # Given |
| 105 | + mocker.patch.object(managers.TaskManager, "_is_database_separate", new=False) |
| 106 | + old_tasks = task_processor_database_tasks( |
| 107 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 108 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 109 | + ) |
| 110 | + new_tasks = [ |
| 111 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 112 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 113 | + ] |
| 114 | + Task.objects.bulk_create(new_tasks) |
| 115 | + |
| 116 | + # When |
| 117 | + tasks = Task.objects.get_tasks_to_process(3) |
| 118 | + |
| 119 | + # Then |
| 120 | + assert list(tasks) == old_tasks + new_tasks[:1] |
| 121 | + |
| 122 | + |
| 123 | +def test_TaskManager__default_database__task_processor_database_with_no_pending_tasks__consumes_tasks_from_new_database( |
| 124 | + mocker: MockerFixture, |
| 125 | + task_processor_database_tasks: Callable[..., list[Task]], |
| 126 | +) -> None: |
| 127 | + # Given |
| 128 | + mocker.patch.object(managers.TaskManager, "_is_database_separate", new=False) |
| 129 | + task_processor_database_tasks() # None |
| 130 | + new_tasks = [ |
| 131 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 132 | + Task(task_identifier="some.identifier", scheduled_for=one_hour_ago), |
| 133 | + ] |
| 134 | + Task.objects.bulk_create(new_tasks) |
| 135 | + |
| 136 | + # When |
| 137 | + tasks = Task.objects.get_tasks_to_process(3) |
| 138 | + |
| 139 | + # Then |
| 140 | + assert list(tasks) == new_tasks |
| 141 | + |
| 142 | + |
| 143 | +def test_TaskManager__default_database__task_processor_database_missing_function__consumes_tasks_from_default_database( |
| 144 | + mocker: MockerFixture, |
| 145 | +) -> None: |
| 146 | + # Given |
| 147 | + mocker.patch.object(managers.TaskManager, "_is_database_separate", new=False) |
| 148 | + raise |
| 149 | + |
| 150 | + |
| 151 | +def test_TaskManager__task_processor_database__default_database_with_pending_tasks__consumes_tasks_from_old_database_first( |
| 152 | + mocker: MockerFixture, |
| 153 | +) -> None: |
| 154 | + raise |
| 155 | + |
| 156 | + |
| 157 | +def test_TaskManager__task_processor_database__default_database_with_no_pending_tasks__consumes_tasks_from_new_database( |
| 158 | + mocker: MockerFixture, |
| 159 | +) -> None: |
| 160 | + raise |
| 161 | + |
| 162 | + |
| 163 | +def test_TaskManager__task_processor_database__default_database_unaware_of_tasks__consumes_tasks_from_new_database( |
| 164 | + mocker: MockerFixture, |
| 165 | +) -> None: |
| 166 | + raise |
0 commit comments