From e6f19ec3223ba7c398aea515c5e0f8b93e6f4359 Mon Sep 17 00:00:00 2001 From: wrwrwr Date: Sat, 29 Nov 2014 14:42:06 +0100 Subject: [PATCH] Fixed #23933 -- Made override_settings(DATABASE_ROUTERS) affect the master router. --- django/test/signals.py | 9 ++++++++- docs/releases/1.8.txt | 3 +++ tests/test_utils/tests.py | 10 +++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/django/test/signals.py b/django/test/signals.py index 066abdfc80..6e7b88bfb0 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -4,7 +4,8 @@ import threading import warnings from django.conf import settings -from django.db import connections +from django.db import connections, router +from django.db.utils import ConnectionRouter from django.dispatch import receiver, Signal from django.utils import timezone from django.utils.functional import empty @@ -77,6 +78,12 @@ def update_connections_time_zone(**kwargs): conn.cursor().execute(tz_sql, [tz]) +@receiver(setting_changed) +def clear_routers_cache(**kwargs): + if kwargs['setting'] == 'DATABASE_ROUTERS': + router.routers = ConnectionRouter().routers + + @receiver(setting_changed) def reset_default_template_engine(**kwargs): if kwargs['setting'] in { diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 28b0770aa9..f96067fa81 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -500,6 +500,9 @@ Tests for Oracle: :setting:`DATAFILE`, :setting:`DATAFILE_TMP`, :setting:`DATAFILE_MAXSIZE` and :setting:`DATAFILE_TMP_MAXSIZE`. +* The :func:`~django.test.override_settings` decorator can now affect the + master router in :setting:`DATABASE_ROUTERS`. + Validators ^^^^^^^^^^ diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index ddef92c193..3889ebf9ee 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -6,7 +6,7 @@ import unittest from django.conf.urls import url from django.core.files.storage import default_storage from django.core.urlresolvers import NoReverseMatch, reverse -from django.db import connection +from django.db import connection, router from django.forms import EmailField, IntegerField from django.http import HttpResponse from django.template.loader import render_to_string @@ -842,3 +842,11 @@ class OverrideSettingsTests(TestCase): self.assertIsNone(default_storage.directory_permissions_mode) with self.settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o777): self.assertEqual(default_storage.directory_permissions_mode, 0o777) + + def test_override_database_routers(self): + """ + Overriding DATABASE_ROUTERS should update the master router. + """ + test_routers = (object(),) + with self.settings(DATABASE_ROUTERS=test_routers): + self.assertSequenceEqual(router.routers, test_routers)