Django

Code

Changeset 5380

Show
Ignore:
Timestamp:
05/29/07 07:42:08 (1 year ago)
Author:
mtredinnick
Message:

Added new TEST_DATABASE_CHARSET and TEST_DATABASE_COLLATION settings to ensure
that databases are created with the expected encoding during testing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/conf/global_settings.py

    r5379 r5380  
    333333TEST_DATABASE_NAME = None 
    334334 
     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). 
     339TEST_DATABASE_CHARSET = None 
     340TEST_DATABASE_COLLATION = None 
     341 
    335342############ 
    336343# FIXTURES # 
  • django/trunk/django/test/utils.py

    r5173 r5380  
    7474        connection.connection.set_isolation_level(0) 
    7575 
     76def 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 
     84def 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 
    7690def create_test_db(verbosity=1, autoclobber=False): 
    7791    if verbosity >= 1: 
     
    8296        TEST_DATABASE_NAME = ":memory:" 
    8397    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: '')() 
    84104        if settings.TEST_DATABASE_NAME: 
    85105            TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME 
     
    93113        _set_autocommit(connection) 
    94114        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)) 
    96116        except Exception, e:             
    97117            sys.stderr.write("Got an error creating the test database: %s\n" % e) 
     
    105125                    if verbosity >= 1: 
    106126                        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)) 
    108128                except Exception, e: 
    109129                    sys.stderr.write("Got an error recreating the test database: %s\n" % e) 
  • django/trunk/docs/settings.txt

    r5379 r5380  
    827827.. _How invalid variables are handled: ../templates_python/#how-invalid-variables-are-handled 
    828828 
     829TEST_DATABASE_CHARSET 
     830--------------------- 
     831 
     832**New in Django development version** 
     833 
     834Default: ``None`` 
     835 
     836The character set encoding used to create the test database. The value of this 
     837string is passed directly through to the database, so its format is 
     838backend-specific. 
     839 
     840Supported 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 
     845TEST_DATABASE_COLLATION 
     846------------------------ 
     847 
     848**New in Django development version** 
     849 
     850Default: ``None`` 
     851 
     852The collation order to use when creating the test database. This value is 
     853passed directly to the backend, so it's format is backend-specific. 
     854 
     855Only supported for ``mysql`` and ``mysql_old`` backends (see `section 10.3.2`_ 
     856of the MySQL manual for details). 
     857 
     858.. _section 10.3.2: http://www.mysql.org/doc/refman/5.0/en/charset-database.html 
     859 
     860TEST_DATABASE_NAME 
     861------------------ 
     862 
     863Default: ``None`` 
     864 
     865The name of database to use when running the test suite. If a value of 
     866``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_. 
     867 
     868.. _Testing Django Applications: ../testing/ 
     869 
    829870TEST_RUNNER 
    830871----------- 
     
    832873Default: ``'django.test.simple.run_tests'`` 
    833874 
    834 The name of the method to use for starting the test suite. See  
     875The name of the method to use for starting the test suite. See 
    835876`Testing Django Applications`_. 
    836  
    837 .. _Testing Django Applications: ../testing/ 
    838  
    839 TEST_DATABASE_NAME 
    840 ------------------ 
    841  
    842 Default: ``None`` 
    843  
    844 The name of database to use when running the test suite. If a value of  
    845 ``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_. 
    846877 
    847878.. _Testing Django Applications: ../testing/ 
  • django/trunk/docs/testing.txt

    r5367 r5380  
    571571If you wish to use a name other than the default for the test database, 
    572572you can use the ``TEST_DATABASE_NAME`` setting to provide a name. 
     573 
     574 
     575**New in Django development version:** If you wish to have fine-grained 
     576control over the character set encoding used in your database, you can control 
     577this with the ``TEST_DATABASE_CHARSET`` setting. For MySQL users, you can also 
     578control the particular collation used by the test database with the 
     579``TEST_DATABASE_COLLATION`` setting. Refer to the settings_ documentation for 
     580details of these advanced settings. 
     581 
     582.. _settings: ../settings.txt 
    573583 
    574584The test database is created by the user in the ``DATABASE_USER`` setting.