Opened 2 years ago

Closed 2 years ago

Last modified 12 months ago

#20032 closed Cleanup/optimization (fixed)

Allow override_settings to delete settings

Reported by: coolRR Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

override_settings is used to change settings' values. Sometimes you want to delete a setting (not just set it to an empty value), and then have it restored later. override_settings should be able to do this, probably by using some kind of constant like DELETE_FROM_SETTINGS as the value given to it.

Change History (7)

comment:1 Changed 2 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

You can do that currently by deleting a setting when settings are overriden, something like:

@override_settings()
def test_something(self):
    del settings.SOME_SETTING
    ...

Sufficient or not? A documentation issue?

comment:2 Changed 2 years ago by jacob

  • Component changed from Testing framework to Documentation
  • Triage Stage changed from Unreviewed to Accepted

Agreed - documentation is the way to go.

comment:3 Changed 2 years ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 50eb70b08fae48445a52680b2d07a9535b25e3c5:

Fixed #20032 -- Documented how to simulate the absence of a setting

Thanks Ram Rachum for the report.

comment:4 Changed 12 months ago by ztorstri@…

I've read the documentation, and tried the approach above, but it doesn't work for me.

Unit test:

from django.test.utils import override_settings
from django.conf import settings

class SimpleTests(TestCase):

@override_settings()
def test_simple_settings_removal(self):

del settings.STATIC_URL

Error thrown:

======================================================================
ERROR: test_simple_settings_removal (manager_core.tests.SimpleTests)


Traceback (most recent call last):

File "/projects/Shepherd/ShepherdEnvironment/local/lib/python2.7/site-packages/django/test/utils.py", line 249, in inner

return test_func(*args, kwargs)

File "/projects/Shepherd/zach_local_cluster/trunk/manager_core/tests.py", line 76, in test_simple_settings_removal

del settings.STATIC_URL

File "/projects/Shepherd/ShepherdEnvironment/local/lib/python2.7/site-packages/django/utils/functional.py", line 249, in delattr

delattr(self._wrapped, name)

File "/projects/Shepherd/ShepherdEnvironment/local/lib/python2.7/site-packages/django/conf/init.py", line 193, in delattr

return super(UserSettingsHolder, self).delattr(name)

AttributeError: STATIC_URL


I always get attribute errors when I try to delete settings and use override_settings at the same time.

comment:5 Changed 12 months ago by claudep

You are right, this works only when the setting is actually mentionned inside the override_settings call. I'll fix this soon.

comment:6 Changed 12 months ago by Claude Paroz <claude@…>

In 3417ba03094b644f01597d6b5a43ccbc71a55378:

Also allowed a non-overridden setting to be deleted

Refs #20032, #18824. Thanks ztorstri at gmail.com for the report.

comment:7 Changed 12 months ago by Claude Paroz <claude@…>

In a6eda3a5fc524333b476deed5057ee9803d91108:

[1.7.x] Also allowed a non-overridden setting to be deleted

Refs #20032, #18824. Thanks ztorstri at gmail.com for the report.
Backport of 3417ba0309 from master.

Note: See TracTickets for help on using tickets.
Back to Top