summaryrefslogtreecommitdiff
path: root/django/test/utils.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-12-17 08:50:50 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-12-17 08:50:50 +0000
commit14ed0efec9d497a2e64723b41429aa62fac78b89 (patch)
treee8ef477d64deaf8656d2d780fd118ad35d2e32a8 /django/test/utils.py
parent8ecff6b91c9cb8fac466552e36cfbdb3d5145148 (diff)
downloaddjango-14ed0efec9d497a2e64723b41429aa62fac78b89.tar.gz
Fixed #6134 -- Allow an on-disk SQLite database to be used for tests, if required. Patch from jdetaeye.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6930 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/utils.py')
-rw-r--r--django/test/utils.py44
1 files changed, 35 insertions, 9 deletions
diff --git a/django/test/utils.py b/django/test/utils.py
index 51ca37e2f0..b1c528873c 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -1,4 +1,4 @@
-import sys, time
+import sys, time, os
from django.conf import settings
from django.db import connection, get_creation_module
from django.core import mail
@@ -106,9 +106,32 @@ def create_test_db(verbosity=1, autoclobber=False):
if verbosity >= 1:
print "Creating test database..."
# If we're using SQLite, it's more convenient to test against an
- # in-memory database.
+ # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose
+ # to run on a physical database.
if settings.DATABASE_ENGINE == "sqlite3":
- TEST_DATABASE_NAME = ":memory:"
+ if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:":
+ TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
+ # Erase the old test database
+ if verbosity >= 1:
+ print "Destroying old test database..."
+ if os.access(TEST_DATABASE_NAME, os.F_OK):
+ if not autoclobber:
+ confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME)
+ if autoclobber or confirm == 'yes':
+ try:
+ if verbosity >= 1:
+ print "Destroying old test database..."
+ os.remove(TEST_DATABASE_NAME)
+ except Exception, e:
+ sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
+ sys.exit(2)
+ else:
+ print "Tests cancelled."
+ sys.exit(1)
+ if verbosity >= 1:
+ print "Creating test database..."
+ else:
+ TEST_DATABASE_NAME = ":memory:"
else:
suffix = {
'postgresql': get_postgresql_create_suffix,
@@ -171,17 +194,20 @@ def destroy_test_db(old_database_name, verbosity=1):
creation_module.destroy_test_db(settings, connection, old_database_name, verbosity)
return
- # Unless we're using SQLite, remove the test database to clean up after
- # ourselves. Connect to the previous database (not the test database)
- # to do so, because it's not allowed to delete a database while being
- # connected to it.
if verbosity >= 1:
print "Destroying test database..."
connection.close()
TEST_DATABASE_NAME = settings.DATABASE_NAME
settings.DATABASE_NAME = old_database_name
-
- if settings.DATABASE_ENGINE != "sqlite3":
+ if settings.DATABASE_ENGINE == "sqlite3":
+ if TEST_DATABASE_NAME and TEST_DATABASE_NAME != ":memory:":
+ # Remove the SQLite database file
+ os.remove(TEST_DATABASE_NAME)
+ else:
+ # Remove the test database to clean up after
+ # ourselves. Connect to the previous database (not the test database)
+ # to do so, because it's not allowed to delete a database while being
+ # connected to it.
cursor = connection.cursor()
_set_autocommit(connection)
time.sleep(1) # To avoid "database is being accessed by other users" errors.