Author: mtredinnick
Date: 2007-05-29 07:42:08 -0500 (Tue, 29 May 2007)
New Revision: 5380

Modified:
   django/trunk/django/conf/global_settings.py
   django/trunk/django/test/utils.py
   django/trunk/docs/settings.txt
   django/trunk/docs/testing.txt
Log:
Added new TEST_DATABASE_CHARSET and TEST_DATABASE_COLLATION settings to ensure
that databases are created with the expected encoding during testing.


Modified: django/trunk/django/conf/global_settings.py
===================================================================
--- django/trunk/django/conf/global_settings.py 2007-05-29 11:09:24 UTC (rev 
5379)
+++ django/trunk/django/conf/global_settings.py 2007-05-29 12:42:08 UTC (rev 
5380)
@@ -332,6 +332,13 @@
 # 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 #
 ############

Modified: django/trunk/django/test/utils.py
===================================================================
--- django/trunk/django/test/utils.py   2007-05-29 11:09:24 UTC (rev 5379)
+++ django/trunk/django/test/utils.py   2007-05-29 12:42:08 UTC (rev 5380)
@@ -73,6 +73,20 @@
     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 @@
     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 @@
         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 @@
                     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)

Modified: django/trunk/docs/settings.txt
===================================================================
--- django/trunk/docs/settings.txt      2007-05-29 11:09:24 UTC (rev 5379)
+++ django/trunk/docs/settings.txt      2007-05-29 12:42:08 UTC (rev 5380)
@@ -826,26 +826,57 @@
 
 .. _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
 -----------
 

Modified: django/trunk/docs/testing.txt
===================================================================
--- django/trunk/docs/testing.txt       2007-05-29 11:09:24 UTC (rev 5379)
+++ django/trunk/docs/testing.txt       2007-05-29 12:42:08 UTC (rev 5380)
@@ -571,6 +571,16 @@
 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.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to