Skip to content

Commit 8e4416b

Browse files
committed
Refactored database switching code to separate file; updated it to work with Django 1.1
- Legacy-Id: 1702
1 parent bd61d94 commit 8e4416b

2 files changed

Lines changed: 69 additions & 10 deletions

File tree

ietf/tests.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import re
5+
import sys
56
import traceback
67
import urllib2 as urllib
78
import httplib
@@ -19,6 +20,7 @@
1920
from django.core import management
2021
import ietf.urls
2122
from ietf.utils import log
23+
from ietf.utils.test_utils import RealDatabaseTest
2224

2325
# This is needed so that "manage.py test ietf.UrlTestCase" works
2426
if django.VERSION[0] > 0:
@@ -30,6 +32,7 @@
3032
ietf.models = types.ModuleType('ietf.models')
3133
# Fixture loading code uses the directory of this file, not the actual file
3234
ietf.models.__file__ = ietf.settings.__file__
35+
sys.modules['ietf.models'] = ietf.models
3336

3437

3538
startup_database = settings.DATABASE_NAME # The startup database name, before changing to test_...
@@ -255,7 +258,7 @@ def module_setup(module):
255258
settings.DATABASE_NAME = module.testdb
256259
connection.cursor()
257260

258-
class UrlTestCase(TestCase):
261+
class UrlTestCase(TestCase,RealDatabaseTest):
259262

260263
def __init__(self, *args, **kwargs):
261264
TestCase.__init__(self, *args, **kwargs)
@@ -265,17 +268,10 @@ def __init__(self, *args, **kwargs):
265268

266269
def setUp(self):
267270
self.client = Client()
268-
269-
self.testdb = settings.DATABASE_NAME
270-
connection.close()
271-
settings.DATABASE_NAME = startup_database
272-
connection.cursor()
271+
self.setUpRealDatabase()
273272

274273
def tearDown(self):
275-
# Revert to using the test database
276-
connection.close()
277-
settings.DATABASE_NAME = self.testdb
278-
connection.cursor()
274+
self.tearDownRealDatabase()
279275

280276
# ------------------------------------------------------------------------------
281277
# Test methods.

ietf/utils/test_utils.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright The IETF Trust 2007, All Rights Reserved
2+
3+
# Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4+
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
#
10+
# * Redistributions of source code must retain the above copyright
11+
# notice, this list of conditions and the following disclaimer.
12+
#
13+
# * Redistributions in binary form must reproduce the above
14+
# copyright notice, this list of conditions and the following
15+
# disclaimer in the documentation and/or other materials provided
16+
# with the distribution.
17+
#
18+
# * Neither the name of the Nokia Corporation and/or its
19+
# subsidiary(-ies) nor the names of its contributors may be used
20+
# to endorse or promote products derived from this software
21+
# without specific prior written permission.
22+
#
23+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
import django
36+
from django.db import connection
37+
import ietf
38+
39+
class RealDatabaseTest:
40+
def setUpRealDatabase(self):
41+
self._original_testdb = self._getDatabaseName()
42+
newdb = ietf.settings.DATABASE_NAME
43+
print "Switching database from "+self._original_testdb+" to "+newdb
44+
self._setDatabaseName(newdb)
45+
46+
def tearDownRealDatabase(self):
47+
curdb = self._getDatabaseName()
48+
print "Switching database from "+curdb+" to "+self._original_testdb
49+
self._setDatabaseName(self._original_testdb)
50+
51+
def _getDatabaseName(self):
52+
if django.VERSION[0] == 0:
53+
return django.conf.settings.DATABASE_NAME
54+
else:
55+
return connection.settings_dict['DATABASE_NAME']
56+
57+
def _setDatabaseName(self, name):
58+
connection.close()
59+
if django.VERSION[0] == 0:
60+
django.conf.settings.DATABASE_NAME = name
61+
else:
62+
connection.settings_dict['DATABASE_NAME'] = name
63+
connection.cursor()

0 commit comments

Comments
 (0)