diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-29 12:42:08 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-29 12:42:08 +0000 |
| commit | 0c4385bb02a839147bebe8d3ac25a35a17528a04 (patch) | |
| tree | 98100e7d0ef1c6f2c18546fd7301abf7dc29319d /django | |
| parent | d9f6470f2dd452280b5dd1108510f3f5dba0cb9c (diff) | |
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
Diffstat (limited to 'django')
| -rw-r--r-- | django/conf/global_settings.py | 7 | ||||
| -rw-r--r-- | django/test/utils.py | 24 |
2 files changed, 29 insertions, 2 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 99e547554b..2be6a4ef95 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -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 # ############ diff --git a/django/test/utils.py b/django/test/utils.py index aa2b8321be..f5122fa96d 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -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) |
