Skip to content

Commit 6406ea0

Browse files
committed
Merge branch 'extra-config-use-importlib'
2 parents dd79c5f + 284e337 commit 6406ea0

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

django_lightweight_queue/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import imp
21
import time
32
import datetime
43
import warnings
54
import importlib
5+
import importlib.util
66
from typing import (
77
Any,
88
Set,
@@ -33,7 +33,10 @@
3333

3434

3535
def load_extra_config(file_path: str) -> None:
36-
extra_settings = imp.load_source('extra_settings', file_path)
36+
# Based on https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
37+
spec = importlib.util.spec_from_file_location('extra_settings', file_path)
38+
extra_settings = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
39+
spec.loader.exec_module(extra_settings) # type: ignore[union-attr]
3740

3841
def get_setting_names(module: object) -> Set[str]:
3942
return set(name for name in dir(module) if name.isupper())

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)