2007-12-17 16:50:50 +08:00
|
|
|
import sys, time, os
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.conf import settings
|
2007-08-20 11:16:55 +08:00
|
|
|
from django.db import connection, get_creation_module
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.core import mail
|
|
|
|
from django.core.management import call_command
|
2006-09-02 17:26:24 +08:00
|
|
|
from django.dispatch import dispatcher
|
|
|
|
from django.test import signals
|
|
|
|
from django.template import Template
|
2007-10-22 01:26:32 +08:00
|
|
|
from django.utils.translation import deactivate
|
2006-08-27 20:24:59 +08:00
|
|
|
|
|
|
|
# The prefix to put on the default database name when creating
|
|
|
|
# the test database.
|
|
|
|
TEST_DATABASE_PREFIX = 'test_'
|
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def instrumented_test_render(self, context):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
|
|
|
An instrumented Template render method, providing a signal
|
2007-06-22 15:15:04 +08:00
|
|
|
that can be intercepted by the test system Client
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
|
|
|
dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context)
|
|
|
|
return self.nodelist.render(context)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
class TestSMTPConnection(object):
|
|
|
|
"""A substitute SMTP connection for use during test sessions.
|
|
|
|
The test connection stores email messages in a dummy outbox,
|
|
|
|
rather than sending them out on the wire.
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
"""
|
|
|
|
def __init__(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
def open(self):
|
|
|
|
"Mock the SMTPConnection open() interface"
|
|
|
|
pass
|
|
|
|
def close(self):
|
|
|
|
"Mock the SMTPConnection close() interface"
|
|
|
|
pass
|
|
|
|
def send_messages(self, messages):
|
|
|
|
"Redirect messages to the dummy outbox"
|
|
|
|
mail.outbox.extend(messages)
|
2007-08-16 18:54:28 +08:00
|
|
|
return len(messages)
|
2007-05-08 19:19:34 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def setup_test_environment():
|
|
|
|
"""Perform any global pre-test setup. This involves:
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
- Installing the instrumented test renderer
|
2007-05-08 19:19:34 +08:00
|
|
|
- Diverting the email sending functions to a test buffer
|
2007-10-22 01:26:32 +08:00
|
|
|
- Setting the active locale to match the LANGUAGE_CODE setting.
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
|
|
|
Template.original_render = Template.render
|
|
|
|
Template.render = instrumented_test_render
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
mail.original_SMTPConnection = mail.SMTPConnection
|
|
|
|
mail.SMTPConnection = TestSMTPConnection
|
|
|
|
|
|
|
|
mail.outbox = []
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-10-22 01:26:32 +08:00
|
|
|
deactivate()
|
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def teardown_test_environment():
|
|
|
|
"""Perform any global post-test teardown. This involves:
|
|
|
|
|
|
|
|
- Restoring the original test renderer
|
2007-05-08 19:19:34 +08:00
|
|
|
- Restoring the email sending functions
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
"""
|
|
|
|
Template.render = Template.original_render
|
2007-06-22 15:15:04 +08:00
|
|
|
del Template.original_render
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
mail.SMTPConnection = mail.original_SMTPConnection
|
|
|
|
del mail.original_SMTPConnection
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
del mail.outbox
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def _set_autocommit(connection):
|
|
|
|
"Make sure a connection is in autocommit mode."
|
|
|
|
if hasattr(connection.connection, "autocommit"):
|
2008-07-17 21:24:05 +08:00
|
|
|
if callable(connection.connection.autocommit):
|
|
|
|
connection.connection.autocommit(True)
|
|
|
|
else:
|
|
|
|
connection.connection.autocommit = True
|
2006-08-27 20:24:59 +08:00
|
|
|
elif hasattr(connection.connection, "set_isolation_level"):
|
|
|
|
connection.connection.set_isolation_level(0)
|
|
|
|
|
2007-05-29 20:42:08 +08:00
|
|
|
def get_mysql_create_suffix():
|
|
|
|
suffix = []
|
|
|
|
if settings.TEST_DATABASE_CHARSET:
|
|
|
|
suffix.append('CHARACTER SET %s' % settings.TEST_DATABASE_CHARSET)
|
|
|
|
if settings.TEST_DATABASE_COLLATION:
|
|
|
|
suffix.append('COLLATE %s' % settings.TEST_DATABASE_COLLATION)
|
|
|
|
return ' '.join(suffix)
|
|
|
|
|
|
|
|
def get_postgresql_create_suffix():
|
|
|
|
assert settings.TEST_DATABASE_COLLATION is None, "PostgreSQL does not support collation setting at database creation time."
|
|
|
|
if settings.TEST_DATABASE_CHARSET:
|
|
|
|
return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
|
|
|
|
return ''
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def create_test_db(verbosity=1, autoclobber=False):
|
2007-08-17 06:46:00 +08:00
|
|
|
"""
|
|
|
|
Creates a test database, prompting the user for confirmation if the
|
|
|
|
database already exists. Returns the name of the test database created.
|
|
|
|
"""
|
2007-06-23 22:16:00 +08:00
|
|
|
# If the database backend wants to create the test DB itself, let it
|
|
|
|
creation_module = get_creation_module()
|
|
|
|
if hasattr(creation_module, "create_test_db"):
|
2007-08-20 11:16:55 +08:00
|
|
|
creation_module.create_test_db(settings, connection, verbosity, autoclobber)
|
2007-06-23 22:16:00 +08:00
|
|
|
return
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
if verbosity >= 1:
|
|
|
|
print "Creating test database..."
|
|
|
|
# If we're using SQLite, it's more convenient to test against an
|
2007-12-17 16:50:50 +08:00
|
|
|
# in-memory database. Using the TEST_DATABASE_NAME setting you can still choose
|
|
|
|
# to run on a physical database.
|
2006-08-27 20:24:59 +08:00
|
|
|
if settings.DATABASE_ENGINE == "sqlite3":
|
2007-12-17 16:50:50 +08:00
|
|
|
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:"
|
2006-08-27 20:24:59 +08:00
|
|
|
else:
|
2007-05-29 20:42:08 +08:00
|
|
|
suffix = {
|
|
|
|
'postgresql': get_postgresql_create_suffix,
|
|
|
|
'postgresql_psycopg2': get_postgresql_create_suffix,
|
|
|
|
'mysql': get_mysql_create_suffix,
|
|
|
|
}.get(settings.DATABASE_ENGINE, lambda: '')()
|
2006-09-01 21:33:26 +08:00
|
|
|
if settings.TEST_DATABASE_NAME:
|
|
|
|
TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
|
|
|
|
else:
|
|
|
|
TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
# Create the test database and connect to it. We need to autocommit
|
2007-08-16 14:06:55 +08:00
|
|
|
# if the database supports it because PostgreSQL doesn't allow
|
2006-08-27 20:24:59 +08:00
|
|
|
# CREATE/DROP DATABASE statements within transactions.
|
|
|
|
cursor = connection.cursor()
|
|
|
|
_set_autocommit(connection)
|
|
|
|
try:
|
2007-08-20 09:03:33 +08:00
|
|
|
cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix))
|
2007-08-16 14:06:55 +08:00
|
|
|
except Exception, e:
|
2006-08-27 20:24:59 +08:00
|
|
|
sys.stderr.write("Got an error creating the test database: %s\n" % e)
|
|
|
|
if not autoclobber:
|
2007-07-20 22:42:57 +08:00
|
|
|
confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME)
|
2006-08-27 20:24:59 +08:00
|
|
|
if autoclobber or confirm == 'yes':
|
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
2007-08-16 14:06:55 +08:00
|
|
|
print "Destroying old test database..."
|
2007-08-20 09:03:33 +08:00
|
|
|
cursor.execute("DROP DATABASE %s" % qn(TEST_DATABASE_NAME))
|
2006-08-27 20:24:59 +08:00
|
|
|
if verbosity >= 1:
|
|
|
|
print "Creating test database..."
|
2007-08-20 09:03:33 +08:00
|
|
|
cursor.execute("CREATE DATABASE %s %s" % (qn(TEST_DATABASE_NAME), suffix))
|
2006-08-27 20:24:59 +08:00
|
|
|
except Exception, e:
|
|
|
|
sys.stderr.write("Got an error recreating the test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
|
|
|
print "Tests cancelled."
|
|
|
|
sys.exit(1)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
connection.close()
|
|
|
|
settings.DATABASE_NAME = TEST_DATABASE_NAME
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
call_command('syncdb', verbosity=verbosity, interactive=False)
|
2007-02-18 16:07:18 +08:00
|
|
|
|
2007-07-27 06:59:34 +08:00
|
|
|
if settings.CACHE_BACKEND.startswith('db://'):
|
|
|
|
cache_name = settings.CACHE_BACKEND[len('db://'):]
|
2007-08-16 14:06:55 +08:00
|
|
|
call_command('createcachetable', cache_name)
|
2007-07-27 06:59:34 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
# Get a cursor (even though we don't need one yet). This has
|
|
|
|
# the side effect of initializing the test database.
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
2007-08-17 06:46:00 +08:00
|
|
|
return TEST_DATABASE_NAME
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def destroy_test_db(old_database_name, verbosity=1):
|
2007-06-23 22:16:00 +08:00
|
|
|
# If the database wants to drop the test DB itself, let it
|
|
|
|
creation_module = get_creation_module()
|
|
|
|
if hasattr(creation_module, "destroy_test_db"):
|
2007-08-20 11:16:55 +08:00
|
|
|
creation_module.destroy_test_db(settings, connection, old_database_name, verbosity)
|
2007-06-23 22:16:00 +08:00
|
|
|
return
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
if verbosity >= 1:
|
|
|
|
print "Destroying test database..."
|
2006-09-05 21:32:08 +08:00
|
|
|
connection.close()
|
|
|
|
TEST_DATABASE_NAME = settings.DATABASE_NAME
|
|
|
|
settings.DATABASE_NAME = old_database_name
|
2007-12-17 16:50:50 +08:00
|
|
|
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.
|
2006-08-27 20:24:59 +08:00
|
|
|
cursor = connection.cursor()
|
|
|
|
_set_autocommit(connection)
|
|
|
|
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
2007-08-20 09:03:33 +08:00
|
|
|
cursor.execute("DROP DATABASE %s" % connection.ops.quote_name(TEST_DATABASE_NAME))
|
2006-08-27 20:24:59 +08:00
|
|
|
connection.close()
|