Skip to content

Commit 506e20e

Browse files
committed
Add tests for loading of extra config
Prior to a move from 'imp' to 'importlib'.
1 parent f6106f6 commit 506e20e

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

tests/_demo_extra_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django_lightweight_queue.types import QueueName
2+
3+
LIGHTWEIGHT_QUEUE_BACKEND_OVERRIDES = {
4+
QueueName('test-queue'): 'tests.test_extra_config.TestBackend',
5+
}
6+
7+
LIGHTWEIGHT_QUEUE_REDIS_PASSWORD = 'a very bad password'

tests/_demo_extra_config_falsey.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Override to falsey values
2+
LIGHTWEIGHT_QUEUE_REDIS_PASSWORD = None
3+
4+
LIGHTWEIGHT_QUEUE_ATOMIC_JOBS = False
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
LIGHTWEIGHT_QUEUE_REDIS_PASSWORD = 'expected'
3+
4+
NOT_REDIS_PASSWORD = 'unexpected'

tests/test_extra_config.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)