Skip to content

Commit

Permalink
Update the extra-config tests for the new way that settings behave
Browse files Browse the repository at this point in the history
The previous approach of reloading the settings module worked ok
when the module itself was what was imported & mutated, however
with the move to a layered approach that's no longer the case.
  • Loading branch information
PeterJCLaw committed Aug 6, 2022
1 parent 33554ef commit ccd9992
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/test_extra_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib
from typing import Optional
from pathlib import Path
from unittest import mock

from django.test import SimpleTestCase

Expand All @@ -27,11 +27,19 @@ def length(self, queue: QueueName) -> int:
class ExtraConfigTests(SimpleTestCase):
def setUp(self) -> None:
get_backend.cache_clear()

self.settings: app_settings.Settings = app_settings.AppSettings([app_settings.Defaults()])
self._settings_patch = mock.patch(
'django_lightweight_queue.utils.app_settings',
new=self.settings,
)
self._settings_patch.start()

super().setUp()

def tearDown(self) -> None:
importlib.reload(app_settings)
get_backend.cache_clear()
self._settings_patch.stop()
super().tearDown()

def test_updates_configuration(self) -> None:
Expand All @@ -40,20 +48,20 @@ def test_updates_configuration(self) -> None:
backend = get_backend('test-queue')
self.assertIsInstance(backend, TestBackend)

self.assertEqual('a very bad password', app_settings.REDIS_PASSWORD)
self.assertEqual('a very bad password', self.settings.REDIS_PASSWORD)

def test_warns_about_unexpected_settings(self) -> None:
with self.assertWarnsRegex(Warning, r'Ignoring unexpected setting.+\bNOT_REDIS_PASSWORD\b'):
load_extra_config(str(TESTS_DIR / '_demo_extra_config_unexpected.py'))

self.assertEqual('expected', app_settings.REDIS_PASSWORD)
self.assertEqual('expected', self.settings.REDIS_PASSWORD)

def test_updates_configuration_with_falsey_values(self) -> None:
load_extra_config(str(TESTS_DIR / '_demo_extra_config.py'))
load_extra_config(str(TESTS_DIR / '_demo_extra_config_falsey.py'))

self.assertIsNone(app_settings.REDIS_PASSWORD)
self.assertFalse(app_settings.ATOMIC_JOBS)
self.assertIsNone(self.settings.REDIS_PASSWORD)
self.assertFalse(self.settings.ATOMIC_JOBS)

def test_rejects_missing_file(self) -> None:
with self.assertRaises(FileNotFoundError):
Expand Down

0 comments on commit ccd9992

Please sign in to comment.