|
| 1 | +import importlib |
| 2 | +from typing import Optional |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from django.test import SimpleTestCase |
| 6 | + |
| 7 | +from django_lightweight_queue import app_settings |
| 8 | +from django_lightweight_queue.job import Job |
| 9 | +from django_lightweight_queue.types import QueueName, WorkerNumber |
| 10 | +from django_lightweight_queue.utils import get_backend, load_extra_config |
| 11 | +from django_lightweight_queue.backends.base import BaseBackend |
| 12 | + |
| 13 | +TESTS_DIR = Path(__file__).parent |
| 14 | + |
| 15 | + |
| 16 | +class TestBackend(BaseBackend): |
| 17 | + def enqueue(self, job: Job, queue: QueueName) -> None: |
| 18 | + pass |
| 19 | + |
| 20 | + def dequeue(self, queue: QueueName, worker_num: WorkerNumber, timeout: int) -> Optional[Job]: |
| 21 | + pass |
| 22 | + |
| 23 | + def length(self, queue: QueueName) -> int: |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class ExtraConfigTests(SimpleTestCase): |
| 28 | + def setUp(self) -> None: |
| 29 | + get_backend.cache_clear() |
| 30 | + super().setUp() |
| 31 | + |
| 32 | + def tearDown(self) -> None: |
| 33 | + importlib.reload(app_settings) |
| 34 | + get_backend.cache_clear() |
| 35 | + super().tearDown() |
| 36 | + |
| 37 | + def test_updates_configuration(self) -> None: |
| 38 | + load_extra_config(str(TESTS_DIR / '_demo_extra_config.py')) |
| 39 | + |
| 40 | + backend = get_backend('test-queue') |
| 41 | + self.assertIsInstance(backend, TestBackend) |
| 42 | + |
| 43 | + self.assertEqual('a very bad password', app_settings.REDIS_PASSWORD) |
| 44 | + |
| 45 | + def test_warns_about_unexpected_settings(self) -> None: |
| 46 | + with self.assertWarnsRegex(Warning, r'Ignoring unexpected setting.+\bNOT_REDIS_PASSWORD\b'): |
| 47 | + load_extra_config(str(TESTS_DIR / '_demo_extra_config_unexpected.py')) |
| 48 | + |
| 49 | + self.assertEqual('expected', app_settings.REDIS_PASSWORD) |
| 50 | + |
| 51 | + def test_updates_configuration_with_falsey_values(self) -> None: |
| 52 | + load_extra_config(str(TESTS_DIR / '_demo_extra_config.py')) |
| 53 | + load_extra_config(str(TESTS_DIR / '_demo_extra_config_falsey.py')) |
| 54 | + |
| 55 | + self.assertIsNone(app_settings.REDIS_PASSWORD) |
| 56 | + self.assertFalse(app_settings.ATOMIC_JOBS) |
| 57 | + |
| 58 | + def test_rejects_missing_file(self) -> None: |
| 59 | + with self.assertRaises(FileNotFoundError): |
| 60 | + load_extra_config(str(TESTS_DIR / '_no_such_file.py')) |
0 commit comments