Django

Code

Changeset 6930

Show
Ignore:
Timestamp:
12/17/07 02:50:50 (10 months ago)
Author:
mtredinnick
Message:

Fixed #6134 -- Allow an on-disk SQLite database to be used for tests, if required. Patch from jdetaeye.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/utils.py

    r6583 r6930  
    1 import sys, time 
     1import sys, time, os 
    22from django.conf import settings 
    33from django.db import connection, get_creation_module 
     
    107107        print "Creating test database..." 
    108108    # If we're using SQLite, it's more convenient to test against an 
    109     # in-memory database. 
     109    # in-memory database. Using the TEST_DATABASE_NAME setting you can still choose 
     110    # to run on a physical database. 
    110111    if settings.DATABASE_ENGINE == "sqlite3": 
    111         TEST_DATABASE_NAME = ":memory:" 
     112        if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": 
     113            TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME 
     114            # Erase the old test database 
     115            if verbosity >= 1: 
     116                print "Destroying old test database..." 
     117            if os.access(TEST_DATABASE_NAME, os.F_OK): 
     118                if not autoclobber: 
     119                    confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % TEST_DATABASE_NAME) 
     120                if autoclobber or confirm == 'yes': 
     121                  try: 
     122                      if verbosity >= 1: 
     123                          print "Destroying old test database..." 
     124                      os.remove(TEST_DATABASE_NAME) 
     125                  except Exception, e: 
     126                      sys.stderr.write("Got an error deleting the old test database: %s\n" % e) 
     127                      sys.exit(2) 
     128                else: 
     129                    print "Tests cancelled." 
     130                    sys.exit(1) 
     131            if verbosity >= 1: 
     132                print "Creating test database..." 
     133        else: 
     134            TEST_DATABASE_NAME = ":memory:" 
    112135    else: 
    113136        suffix = { 
     
    172195        return 
    173196 
    174     # Unless we're using SQLite, remove the test database to clean up after 
    175     # ourselves. Connect to the previous database (not the test database) 
    176     # to do so, because it's not allowed to delete a database while being 
    177     # connected to it. 
    178197    if verbosity >= 1: 
    179198        print "Destroying test database..." 
     
    181200    TEST_DATABASE_NAME = settings.DATABASE_NAME 
    182201    settings.DATABASE_NAME = old_database_name 
    183  
    184     if settings.DATABASE_ENGINE != "sqlite3": 
     202    if settings.DATABASE_ENGINE == "sqlite3": 
     203        if TEST_DATABASE_NAME and TEST_DATABASE_NAME != ":memory:": 
     204            # Remove the SQLite database file 
     205            os.remove(TEST_DATABASE_NAME) 
     206    else: 
     207        # Remove the test database to clean up after 
     208        # ourselves. Connect to the previous database (not the test database) 
     209        # to do so, because it's not allowed to delete a database while being 
     210        # connected to it. 
    185211        cursor = connection.cursor() 
    186212        _set_autocommit(connection) 
  • django/trunk/docs/settings.txt

    r6879 r6930  
    982982Default: ``None`` 
    983983 
    984 The name of database to use when running the test suite. If a value of 
    985 ``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_. 
     984The name of database to use when running the test suite. 
     985 
     986If the default value (``None``) is used with the SQLite database engine, the 
     987tests will use a memory resident database. For all other database engines the 
     988test database will use the name ``'test_' + settings.DATABASE_NAME``. 
     989 
     990See `Testing Django Applications`_. 
    986991 
    987992.. _Testing Django Applications: ../testing/