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
This commit is contained in:
parent
8ecff6b91c
commit
14ed0efec9
|
@ -1,4 +1,4 @@
|
||||||
import sys, time
|
import sys, time, os
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import connection, get_creation_module
|
from django.db import connection, get_creation_module
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
|
@ -106,9 +106,32 @@ def create_test_db(verbosity=1, autoclobber=False):
|
||||||
if verbosity >= 1:
|
if verbosity >= 1:
|
||||||
print "Creating test database..."
|
print "Creating test database..."
|
||||||
# If we're using SQLite, it's more convenient to test against an
|
# 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":
|
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:
|
else:
|
||||||
suffix = {
|
suffix = {
|
||||||
'postgresql': get_postgresql_create_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)
|
creation_module.destroy_test_db(settings, connection, old_database_name, verbosity)
|
||||||
return
|
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:
|
if verbosity >= 1:
|
||||||
print "Destroying test database..."
|
print "Destroying test database..."
|
||||||
connection.close()
|
connection.close()
|
||||||
TEST_DATABASE_NAME = settings.DATABASE_NAME
|
TEST_DATABASE_NAME = settings.DATABASE_NAME
|
||||||
settings.DATABASE_NAME = old_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()
|
cursor = connection.cursor()
|
||||||
_set_autocommit(connection)
|
_set_autocommit(connection)
|
||||||
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
||||||
|
|
|
@ -981,8 +981,13 @@ TEST_DATABASE_NAME
|
||||||
|
|
||||||
Default: ``None``
|
Default: ``None``
|
||||||
|
|
||||||
The name of database to use when running the test suite. If a value of
|
The name of database to use when running the test suite.
|
||||||
``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_.
|
|
||||||
|
If the default value (``None``) is used with the SQLite database engine, the
|
||||||
|
tests will use a memory resident database. For all other database engines the
|
||||||
|
test database will use the name ``'test_' + settings.DATABASE_NAME``.
|
||||||
|
|
||||||
|
See `Testing Django Applications`_.
|
||||||
|
|
||||||
.. _Testing Django Applications: ../testing/
|
.. _Testing Django Applications: ../testing/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue