Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 0c4385bb02a839147bebe8d3ac25a35a17528a04 1 parent d9f6470
Malcolm Tredinnick authored
7  django/conf/global_settings.py
@@ -332,6 +332,13 @@
332 332
 # If None, a name of 'test_' + DATABASE_NAME will be assumed
333 333
 TEST_DATABASE_NAME = None
334 334
 
  335
+# Strings used to set the character set and collation order for the test
  336
+# database. These values are passed literally to the server, so they are
  337
+# backend-dependent. If None, no special settings are sent (system defaults are
  338
+# used).
  339
+TEST_DATABASE_CHARSET = None
  340
+TEST_DATABASE_COLLATION = None
  341
+
335 342
 ############
336 343
 # FIXTURES #
337 344
 ############
24  django/test/utils.py
@@ -73,6 +73,20 @@ def _set_autocommit(connection):
73 73
     elif hasattr(connection.connection, "set_isolation_level"):
74 74
         connection.connection.set_isolation_level(0)
75 75
 
  76
+def get_mysql_create_suffix():
  77
+    suffix = []
  78
+    if settings.TEST_DATABASE_CHARSET:
  79
+        suffix.append('CHARACTER SET %s' % settings.TEST_DATABASE_CHARSET)
  80
+    if settings.TEST_DATABASE_COLLATION:
  81
+        suffix.append('COLLATE %s' % settings.TEST_DATABASE_COLLATION)
  82
+    return ' '.join(suffix)
  83
+
  84
+def get_postgresql_create_suffix():
  85
+    assert settings.TEST_DATABASE_COLLATION is None, "PostgreSQL does not support collation setting at database creation time."
  86
+    if settings.TEST_DATABASE_CHARSET:
  87
+        return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET
  88
+    return ''
  89
+
76 90
 def create_test_db(verbosity=1, autoclobber=False):
77 91
     if verbosity >= 1:
78 92
         print "Creating test database..."
@@ -81,6 +95,12 @@ def create_test_db(verbosity=1, autoclobber=False):
81 95
     if settings.DATABASE_ENGINE == "sqlite3":
82 96
         TEST_DATABASE_NAME = ":memory:"
83 97
     else:
  98
+        suffix = {
  99
+            'postgresql': get_postgresql_create_suffix,
  100
+            'postgresql_psycopg2': get_postgresql_create_suffix,
  101
+            'mysql': get_mysql_create_suffix,
  102
+            'mysql_old': get_mysql_create_suffix,
  103
+        }.get(settings.DATABASE_ENGINE, lambda: '')()
84 104
         if settings.TEST_DATABASE_NAME:
85 105
             TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
86 106
         else:
@@ -92,7 +112,7 @@ def create_test_db(verbosity=1, autoclobber=False):
92 112
         cursor = connection.cursor()
93 113
         _set_autocommit(connection)
94 114
         try:
95  
-            cursor.execute("CREATE DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
  115
+            cursor.execute("CREATE DATABASE %s %s" % (backend.quote_name(TEST_DATABASE_NAME), suffix))
96 116
         except Exception, e:            
97 117
             sys.stderr.write("Got an error creating the test database: %s\n" % e)
98 118
             if not autoclobber:
@@ -104,7 +124,7 @@ def create_test_db(verbosity=1, autoclobber=False):
104 124
                     cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
105 125
                     if verbosity >= 1:
106 126
                         print "Creating test database..."
107  
-                    cursor.execute("CREATE DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME))
  127
+                    cursor.execute("CREATE DATABASE %s %s" % (backend.quote_name(TEST_DATABASE_NAME), suffix))
108 128
                 except Exception, e:
109 129
                     sys.stderr.write("Got an error recreating the test database: %s\n" % e)
110 130
                     sys.exit(2)
45  docs/settings.txt
@@ -826,26 +826,57 @@ misspelled) variables. See `How invalid variables are handled`_.
826 826
 
827 827
 .. _How invalid variables are handled: ../templates_python/#how-invalid-variables-are-handled
828 828
 
829  
-TEST_RUNNER
830  
------------
  829
+TEST_DATABASE_CHARSET
  830
+---------------------
831 831
 
832  
-Default: ``'django.test.simple.run_tests'``
  832
+**New in Django development version**
833 833
 
834  
-The name of the method to use for starting the test suite. See 
835  
-`Testing Django Applications`_.
  834
+Default: ``None``
836 835
 
837  
-.. _Testing Django Applications: ../testing/
  836
+The character set encoding used to create the test database. The value of this
  837
+string is passed directly through to the database, so its format is
  838
+backend-specific.
  839
+
  840
+Supported for the PostgreSQL_ (``postgresql``, ``postgresql_psycopg2``) and MySQL_ (``mysql``, ``mysql_old``) backends.
  841
+
  842
+.. _PostgreSQL: http://www.postgresql.org/docs/8.2/static/multibyte.html
  843
+.. _MySQL: http://www.mysql.org/doc/refman/5.0/en/charset-database.html
  844
+
  845
+TEST_DATABASE_COLLATION
  846
+------------------------
  847
+
  848
+**New in Django development version**
  849
+
  850
+Default: ``None``
  851
+
  852
+The collation order to use when creating the test database. This value is
  853
+passed directly to the backend, so it's format is backend-specific.
  854
+
  855
+Only supported for ``mysql`` and ``mysql_old`` backends (see `section 10.3.2`_
  856
+of the MySQL manual for details).
  857
+
  858
+.. _section 10.3.2: http://www.mysql.org/doc/refman/5.0/en/charset-database.html
838 859
 
839 860
 TEST_DATABASE_NAME
840 861
 ------------------
841 862
 
842 863
 Default: ``None``
843 864
 
844  
-The name of database to use when running the test suite. If a value of 
  865
+The name of database to use when running the test suite. If a value of
845 866
 ``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_.
846 867
 
847 868
 .. _Testing Django Applications: ../testing/
848 869
 
  870
+TEST_RUNNER
  871
+-----------
  872
+
  873
+Default: ``'django.test.simple.run_tests'``
  874
+
  875
+The name of the method to use for starting the test suite. See
  876
+`Testing Django Applications`_.
  877
+
  878
+.. _Testing Django Applications: ../testing/
  879
+
849 880
 TIME_FORMAT
850 881
 -----------
851 882
 
10  docs/testing.txt
@@ -571,6 +571,16 @@ database settings will the same as they would be for the project normally.
571 571
 If you wish to use a name other than the default for the test database,
572 572
 you can use the ``TEST_DATABASE_NAME`` setting to provide a name.
573 573
 
  574
+
  575
+**New in Django development version:** If you wish to have fine-grained
  576
+control over the character set encoding used in your database, you can control
  577
+this with the ``TEST_DATABASE_CHARSET`` setting. For MySQL users, you can also
  578
+control the particular collation used by the test database with the
  579
+``TEST_DATABASE_COLLATION`` setting. Refer to the settings_ documentation for
  580
+details of these advanced settings.
  581
+
  582
+.. _settings: ../settings.txt
  583
+
574 584
 The test database is created by the user in the ``DATABASE_USER`` setting.
575 585
 This user needs to have sufficient privileges to create a new database on the
576 586
 system.

0 notes on commit 0c4385b

Please sign in to comment.
Something went wrong with that request. Please try again.