Skip to content

Settings should be updated when overridden in Django tests #2466

@jamescooke

Description

@jamescooke

DRF has its own settings object at rest_framework.settings.api_settings. It appears that these are generated once and not updated when override_settings is used in Django testing.

Failing test

With this in a settings file:

REST_FRAMEWORK = {
    'PAGINATE_BY': 10,
}

This will fail:

from django.test.utils import override_settings
from rest_framework.settings import api_settings
from rest_framework.test import APITestCase


class TestSettings(TestCase):

    def test_settings_normal(self):
        self.assertEqual(settings.REST_FRAMEWORK['PAGINATE_BY'], 10)
        self.assertEqual(api_settings.PAGINATE_BY, 10)

    def test_settings_overridden(self):
        with override_settings(REST_FRAMEWORK={'PAGINATE_BY': 999}):
            self.assertEqual(settings.REST_FRAMEWORK['PAGINATE_BY'], 999)
            self.assertEqual(api_settings.PAGINATE_BY, 999)

test_settings_overridden FAILS because PAGINATE_BY in api_settings is still 10 during override, when it should have been overridden to 999.

This is unexpected behaviour - Django developers familiar with the Django settings and the settings object will expect to be able to override at testing time as per the Django docs.

Simple solutions?

There are probably some elegant solutions to wire in the build of api_settings to listen for updates made by the settings override functions in Django tests... however, what about using what's in DRF already?

There is a temporary_setting function in tests.utils: https://github.com/tomchristie/django-rest-framework/blob/master/tests/utils.py#L9 , which looks like it can do the job. If this could be included in the DRF package and documented then it could be a "good enough" solution maybe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy