Ticket #6134: sqlitetest.patch

File sqlitetest.patch, 3.2 KB (added by jdetaeye, 17 years ago)

patch

  • django/test/utils.py

     
    1 import sys, time
     1import sys, time, os
    22from django.conf import settings
    33from django.db import connection, get_creation_module
    44from django.core import mail
     
    106106    if verbosity >= 1:
    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 = {
    114137            'postgresql': get_postgresql_create_suffix,
     
    171194        creation_module.destroy_test_db(settings, connection, old_database_name, verbosity)
    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..."
    180199    connection.close()
    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)
    187213        time.sleep(1) # To avoid "database is being accessed by other users" errors.
Back to Top