Skip to content

Commit

Permalink
Added KEEP_TEST_DB setting that will prevent destruction and allow re…
Browse files Browse the repository at this point in the history
…use of the test database to speed up testing. Cody Soyland authored parts of this code.
  • Loading branch information
tmc committed Sep 15, 2010
1 parent afa6060 commit fb42bf0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions django_nose/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
import sys
from django.conf import settings
from django.db.backends import creation


def monkey_patch_creation():
"""
This replaces the test_db creation/deletion methods to allow a persistent
test db, by prompting the user to either keep or destroy the test database.
"""
def _create_test_db(self, verbosity, noinput):
"""alternate implementation of _create_test_db that skips test database destroying"""
suffix = self.sql_table_creation_suffix()

if self.connection.settings_dict['TEST_NAME']:
test_database_name = self.connection.settings_dict['TEST_NAME']
else:
test_database_name = creation.TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']

qn = self.connection.ops.quote_name

# Create the test database and connect to it. We need to autocommit
# if the database supports it because PostgreSQL doesn't allow
# CREATE/DROP DATABASE statements within transactions.
cursor = self.connection.cursor()
self.set_autocommit()
try:
cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
except Exception, e:
sys.stderr.write("Got an error creating the test database: %s\n" % e)
confirm = False
if not noinput:
confirm = raw_input("Type 'yes' if you would like to try deleting/recreating the test database '%s', or 'no' to continue with the existing database [no]: " % test_database_name)
if confirm == 'yes':
try:
if verbosity >= 1:
print "Destroying old test database..."
cursor.execute("DROP DATABASE %s" % qn(test_database_name))
if verbosity >= 1:
print "Creating test database..."
cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
except Exception, e:
sys.stderr.write("Got an error recreating the test database: %s\n" % e)
sys.exit(2)
elif confirm in ('', 'no') or noinput:
print "Using existing test database."
else:
print "Cancelling tests."
sys.exit(1)

return test_database_name

def destroy_test_db(self, old_database_name, verbosity=1):
"""no-op alternative to destroy_test_db"""

creation.BaseDatabaseCreation._create_test_db = _create_test_db
creation.BaseDatabaseCreation.destroy_test_db = destroy_test_db


class ResultPlugin(object):
"""
Expand Down Expand Up @@ -35,6 +92,9 @@ def begin(self):
sys_stdout = sys.stdout
sys.stdout = self.sys_stdout

if getattr(settings, 'KEEP_TEST_DB', False):
monkey_patch_creation()

self.runner.setup_test_environment()
self.old_names = self.runner.setup_databases()

Expand Down

0 comments on commit fb42bf0

Please sign in to comment.