Added new TEST_DATABASE_CHARSET and TEST_DATABASE_COLLATION settings to ensure
that databases are created with the expected encoding during testing. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5380 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d9f6470f2d
commit
0c4385bb02
|
@ -332,6 +332,13 @@ TEST_RUNNER = 'django.test.simple.run_tests'
|
|||
# If None, a name of 'test_' + DATABASE_NAME will be assumed
|
||||
TEST_DATABASE_NAME = None
|
||||
|
||||
# Strings used to set the character set and collation order for the test
|
||||
# database. These values are passed literally to the server, so they are
|
||||
# backend-dependent. If None, no special settings are sent (system defaults are
|
||||
# used).
|
||||
TEST_DATABASE_CHARSET = None
|
||||
TEST_DATABASE_COLLATION = None
|
||||
|
||||
############
|
||||
# FIXTURES #
|
||||
############
|
||||
|
|
|
@ -73,6 +73,20 @@ def _set_autocommit(connection):
|
|||
elif hasattr(connection.connection, "set_isolation_level"):
|
||||
connection.connection.set_isolation_level(0)
|
||||
|
||||
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 ''
|
||||
|
||||
def create_test_db(verbosity=1, autoclobber=False):
|
||||
if verbosity >= 1:
|
||||
print "Creating test database..."
|
||||
|
@ -81,6 +95,12 @@ def create_test_db(verbosity=1, autoclobber=False):
|
|||
if settings.DATABASE_ENGINE == "sqlite3":
|
||||
TEST_DATABASE_NAME = ":memory:"
|
||||
else:
|
||||
suffix = {
|
||||
'postgresql': get_postgresql_create_suffix,
|
||||
'postgresql_psycopg2': get_postgresql_create_suffix,
|
||||
'mysql': get_mysql_create_suffix,
|
||||
'mysql_old': get_mysql_create_suffix,
|
||||
}.get(settings.DATABASE_ENGINE, lambda: '')()
|
||||
if settings.TEST_DATABASE_NAME:
|
||||
TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
|
||||
else:
|
||||
|
@ -92,7 +112,7 @@ def create_test_db(verbosity=1, autoclobber=False):
|
|||
cursor = connection.cursor()
|
||||
_set_autocommit(connection)
|
||||
try:
|
||||
cursor.execute("CREATE DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
|
||||
cursor.execute("CREATE DATABASE %s %s" % (backend.quote_name(TEST_DATABASE_NAME), suffix))
|
||||
except Exception, e:
|
||||
sys.stderr.write("Got an error creating the test database: %s\n" % e)
|
||||
if not autoclobber:
|
||||
|
@ -104,7 +124,7 @@ def create_test_db(verbosity=1, autoclobber=False):
|
|||
cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
|
||||
if verbosity >= 1:
|
||||
print "Creating test database..."
|
||||
cursor.execute("CREATE DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
|
||||
cursor.execute("CREATE DATABASE %s %s" % (backend.quote_name(TEST_DATABASE_NAME), suffix))
|
||||
except Exception, e:
|
||||
sys.stderr.write("Got an error recreating the test database: %s\n" % e)
|
||||
sys.exit(2)
|
||||
|
|
|
@ -826,26 +826,57 @@ misspelled) variables. See `How invalid variables are handled`_.
|
|||
|
||||
.. _How invalid variables are handled: ../templates_python/#how-invalid-variables-are-handled
|
||||
|
||||
TEST_RUNNER
|
||||
-----------
|
||||
TEST_DATABASE_CHARSET
|
||||
---------------------
|
||||
|
||||
Default: ``'django.test.simple.run_tests'``
|
||||
**New in Django development version**
|
||||
|
||||
The name of the method to use for starting the test suite. See
|
||||
`Testing Django Applications`_.
|
||||
Default: ``None``
|
||||
|
||||
.. _Testing Django Applications: ../testing/
|
||||
The character set encoding used to create the test database. The value of this
|
||||
string is passed directly through to the database, so its format is
|
||||
backend-specific.
|
||||
|
||||
Supported for the PostgreSQL_ (``postgresql``, ``postgresql_psycopg2``) and MySQL_ (``mysql``, ``mysql_old``) backends.
|
||||
|
||||
.. _PostgreSQL: http://www.postgresql.org/docs/8.2/static/multibyte.html
|
||||
.. _MySQL: http://www.mysql.org/doc/refman/5.0/en/charset-database.html
|
||||
|
||||
TEST_DATABASE_COLLATION
|
||||
------------------------
|
||||
|
||||
**New in Django development version**
|
||||
|
||||
Default: ``None``
|
||||
|
||||
The collation order to use when creating the test database. This value is
|
||||
passed directly to the backend, so it's format is backend-specific.
|
||||
|
||||
Only supported for ``mysql`` and ``mysql_old`` backends (see `section 10.3.2`_
|
||||
of the MySQL manual for details).
|
||||
|
||||
.. _section 10.3.2: http://www.mysql.org/doc/refman/5.0/en/charset-database.html
|
||||
|
||||
TEST_DATABASE_NAME
|
||||
------------------
|
||||
|
||||
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. If a value of
|
||||
``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_.
|
||||
|
||||
.. _Testing Django Applications: ../testing/
|
||||
|
||||
TEST_RUNNER
|
||||
-----------
|
||||
|
||||
Default: ``'django.test.simple.run_tests'``
|
||||
|
||||
The name of the method to use for starting the test suite. See
|
||||
`Testing Django Applications`_.
|
||||
|
||||
.. _Testing Django Applications: ../testing/
|
||||
|
||||
TIME_FORMAT
|
||||
-----------
|
||||
|
||||
|
|
|
@ -571,6 +571,16 @@ database settings will the same as they would be for the project normally.
|
|||
If you wish to use a name other than the default for the test database,
|
||||
you can use the ``TEST_DATABASE_NAME`` setting to provide a name.
|
||||
|
||||
|
||||
**New in Django development version:** If you wish to have fine-grained
|
||||
control over the character set encoding used in your database, you can control
|
||||
this with the ``TEST_DATABASE_CHARSET`` setting. For MySQL users, you can also
|
||||
control the particular collation used by the test database with the
|
||||
``TEST_DATABASE_COLLATION`` setting. Refer to the settings_ documentation for
|
||||
details of these advanced settings.
|
||||
|
||||
.. _settings: ../settings.txt
|
||||
|
||||
The test database is created by the user in the ``DATABASE_USER`` setting.
|
||||
This user needs to have sufficient privileges to create a new database on the
|
||||
system.
|
||||
|
|
Loading…
Reference in New Issue