1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

[multi-db] Corrected settings handling in isolation tests.

git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3429 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-23 04:01:15 +00:00
parent 5b964baa14
commit 94ccedf364
2 changed files with 48 additions and 39 deletions

View File

@ -1,7 +1,7 @@
# tests that db settings can change between requests # tests that db settings can change between requests
import copy import copy
import os import os
from django.conf import settings from django.conf import settings, UserSettingsHolder
from django.core.handlers.wsgi import WSGIHandler from django.core.handlers.wsgi import WSGIHandler
from django.db import models, model_connection_name, _default, connection from django.db import models, model_connection_name, _default, connection
from django.http import HttpResponse from django.http import HttpResponse
@ -91,19 +91,15 @@ def debug(*arg):
def setup(): def setup():
debug("setup") debug("setup")
S['ODB'] = copy.deepcopy(settings.OTHER_DATABASES) S['settings'] = settings._target
for sk in [ k for k in dir(settings) if k.startswith('DATABASE') ]: settings._target = UserSettingsHolder(copy.deepcopy(settings._target))
S[sk] = getattr(settings, sk)
settings.OTHER_DATABASES['django_test_db_a']['MODELS'] = ['ri.MX'] settings.OTHER_DATABASES['django_test_db_a']['MODELS'] = ['ri.MX']
settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = ['ri.MY'] settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = ['ri.MY']
def teardown(): def teardown():
debug("teardown") debug("teardown")
settings.OTHER_DATABASES = S['ODB'] settings._target = S['settings']
for sk in [ k for k in S.keys() if k.startswith('DATABASE') ]:
setattr(settings, sk, S[sk])
def start_response(code, headers): def start_response(code, headers):

View File

@ -20,7 +20,7 @@ import sys
import threading import threading
from thread import get_ident from thread import get_ident
from django import conf from django.conf import settings, UserSettingsHolder
from django.core.handlers.wsgi import WSGIHandler from django.core.handlers.wsgi import WSGIHandler
from django.db import models, model_connection_name, _default, connection, \ from django.db import models, model_connection_name, _default, connection, \
connections connections
@ -86,7 +86,7 @@ def test_two(path, request):
"""Between the first and second requests, settings change to assign """Between the first and second requests, settings change to assign
model MY to a different connection model MY to a different connection
""" """
from django.conf import settings # from django.conf import settings
debug("test_two: %s", settings.OTHER_DATABASES) debug("test_two: %s", settings.OTHER_DATABASES)
try: try:
@ -112,7 +112,7 @@ def test_three(path, request):
"""Between the 2nd and 3rd requests, the settings at the names in """Between the 2nd and 3rd requests, the settings at the names in
OTHER_DATABASES have changed. OTHER_DATABASES have changed.
""" """
from django.conf import settings # from django.conf import settings
debug("3 %s: %s", get_ident(), settings.OTHER_DATABASES) debug("3 %s: %s", get_ident(), settings.OTHER_DATABASES)
debug("3 %s: default: %s", get_ident(), settings.DATABASE_NAME) debug("3 %s: default: %s", get_ident(), settings.DATABASE_NAME)
debug("3 %s: conn: %s", get_ident(), connection.settings.DATABASE_NAME) debug("3 %s: conn: %s", get_ident(), connection.settings.DATABASE_NAME)
@ -146,12 +146,10 @@ def test_three(path, request):
# helpers # helpers
def thread_two(func, *arg): def thread_two(func, *arg):
def start(): def start():
from django.conf import settings # from django.conf import settings
for attr in [ a for a in dir(S['base_settings']) if a == a.upper() ]:
setattr(settings, attr,
copy.deepcopy(getattr(S['base_settings'], attr)))
settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = [] settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = []
debug("t2 ODB: %s", settings.OTHER_DATABASES)
debug("t2 waiting") debug("t2 waiting")
ev.wait(2.0) ev.wait(2.0)
func(*arg) func(*arg)
@ -163,11 +161,7 @@ def thread_two(func, *arg):
def thread_three(func, *arg): def thread_three(func, *arg):
def start(): def start():
from django.conf import settings # from django.conf import settings
for attr in [ a for a in dir(S['base_settings']) if a == a.upper() ]:
setattr(settings, attr,
copy.deepcopy(getattr(S['base_settings'], attr)))
settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = ['ti.MY'] settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = ['ti.MY']
settings.OTHER_DATABASES['django_test_db_b'], \ settings.OTHER_DATABASES['django_test_db_b'], \
settings.OTHER_DATABASES['django_test_db_a'] = \ settings.OTHER_DATABASES['django_test_db_a'] = \
@ -177,9 +171,7 @@ def thread_three(func, *arg):
settings.DATABASE_NAME = \ settings.DATABASE_NAME = \
settings.OTHER_DATABASES['django_test_db_a']['DATABASE_NAME'] settings.OTHER_DATABASES['django_test_db_a']['DATABASE_NAME']
# we just manually changed settings, so reset the default debug("t3 ODB: %s", settings.OTHER_DATABASES)
# connection (normally this isn't needed
debug("3 %s: start: default: %s", get_ident(), settings.DATABASE_NAME) debug("3 %s: start: default: %s", get_ident(), settings.DATABASE_NAME)
debug("3 %s: start: conn: %s", get_ident(), debug("3 %s: start: conn: %s", get_ident(),
connection.settings.DATABASE_NAME) connection.settings.DATABASE_NAME)
@ -192,6 +184,32 @@ def thread_three(func, *arg):
t3.start() t3.start()
return t3 return t3
# helpers
class LocalSettings:
"""Settings holder that allows thread-local overrides of defaults.
"""
def __init__(self, defaults):
self._defaults = defaults
self._local = local()
def __getattr__(self, attr):
if attr in ('_defaults', '_local'):
return self.__dict__[attr]
_local = self.__dict__['_local']
_defaults = self.__dict__['_defaults']
debug("LS get %s (%s)", attr, hasattr(_local, attr))
if not hasattr(_local, attr):
# Make sure everything we return is the local version; this
# avoids sets to deep datastructures overwriting the defaults
setattr(_local, attr, copy.deepcopy(getattr(_defaults, attr)))
return getattr(_local, attr)
def __setattr__(self, attr, val):
if attr in ('_defaults', '_local'):
self.__dict__[attr] = val
else:
debug("LS set local %s = %s", attr, val)
setattr(self.__dict__['_local'], attr, val)
class MockHandler(WSGIHandler): class MockHandler(WSGIHandler):
@ -218,25 +236,20 @@ def debug(*arg):
def setup(): def setup():
debug("setup") debug("setup")
S['settings'] = copy.deepcopy(conf.settings) S['settings'] = settings._target
S['base_settings'] = copy.deepcopy(conf.settings) settings._target = UserSettingsHolder(copy.deepcopy(settings._target))
S['base_settings'].OTHER_DATABASES['django_test_db_a']['MODELS'] = \ settings.OTHER_DATABASES['django_test_db_a']['MODELS'] = ['ti.MX']
['ti.MX'] settings.OTHER_DATABASES['django_test_db_b']['MODELS'] = ['ti.MY']
S['base_settings'].OTHER_DATABASES['django_test_db_b']['MODELS'] = \
['ti.MY']
conf.settings = local() # normal settings holders aren't thread-safe, so we need to substitute
for attr in [ a for a in dir(S['base_settings']) if a == a.upper() ]: # one that is (and so allows per-thread settings)
setattr(conf.settings, attr, holder = settings._target
copy.deepcopy(getattr(S['base_settings'], attr))) settings._target = LocalSettings(holder)
conf.settings.__module__ = S['settings'].__module__
debug("Setup: %s", conf.settings.OTHER_DATABASES)
def teardown(): def teardown():
debug("teardown") debug("teardown")
conf.settings = S['settings'] settings._target = S['settings']
def start_response(code, headers): def start_response(code, headers):