#20290 closed Bug (fixed)
override_settings cannot be nested
Reported by: | Oliver Beattie | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 1.5 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Due to to the way override_settings
stores the underlying settings object, they can't properly be nested. They grab the value seen at the time they were instantiated, which is not necessarily the same time as when they are used (especially when using class [or function] decorators).
I have made a test-case and patch for this (GitHub pull request)
Change History (9)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 12 years ago
Thank you for the report and patch, obeattie.
Can you confirm (preferably via an additional test) whether your patch also fixes this issue for instances where the @override_settings
decorator is used to decorate a method within a class which is also decorated? i.e.
@override_settings(foo=1, bar=2) class Test(TestCase): def test_one(self): # assert that setup was correctly performed using foo=1, bar=2 @override_settings(bar=3) def test_two(self): # assert that setup was correctly performed using foo=1, bar=3
I ran into this whilst writing tests for #19670, in which STATICFILES_STORAGE was overridden at the class level, and the method-level decorator would produce correct values in settings.STATICFILES_STORAGE but not upstream enough to actually affect the staticfiles_storage object -- see draft snippet from django.test.staticfiles_tests.tests:
@override_settings(**dict(TEST_SETTINGS, STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage', DEBUG=False, )) class TestCollectionCachedStorage(BaseCollectionTestCase, BaseStaticFilesTestCase, TestCase): # ... @override_settings( STATICFILES_STORAGE=( 'staticfiles_tests.storage.MultiCachedStaticFilesStorage' ) ) def test_multi_extension_patterns(self): """ #19670 """ self.assertEqual( settings.STATICFILES_STORAGE, 'staticfiles_tests.storage.MultiCachedStaticFilesStorage' ) self.assertEqual( storage.staticfiles_storage._wrapped.__class__.__name__, 'MultiCachedStaticFilesStorage' )
AssertionError: 'CachedStaticFilesStorage' != u'MultiCachedStaticFilesStorage'
Perhaps I'm asking too much of the decorator, but this feels like it should work to me.
comment:4 by , 12 years ago
It appears there are already tests that check this. I have beefed them up slightly from what they were to check that things are applying in the correct precedence order for sure, but they weren't insufficient before.
comment:5 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:6 by , 11 years ago
@DrMeers: your issue is related to module objects (caches) not being flushed when your setting is overriden. You probably need to add one more setting_changed signal (see for example https://github.com/django/django/blob/master/django/contrib/auth/hashers.py#L25).
Pull request: https://github.com/django/django/pull/1021