Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6134 -- Allow an on-disk SQLite database to be used for tests,…

… if required. Patch from jdetaeye.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6930 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 14ed0efec9d497a2e64723b41429aa62fac78b89 1 parent 8ecff6b
Malcolm Tredinnick authored December 17, 2007
44  django/test/utils.py
... ...
@@ -1,4 +1,4 @@
1  
-import sys, time
  1
+import sys, time, os
2 2
 from django.conf import settings
3 3
 from django.db import connection, get_creation_module
4 4
 from django.core import mail
@@ -106,9 +106,32 @@ def create_test_db(verbosity=1, autoclobber=False):
106 106
     if verbosity >= 1:
107 107
         print "Creating test database..."
108 108
     # 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.
110 111
     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:"
112 135
     else:
113 136
         suffix = {
114 137
             'postgresql': get_postgresql_create_suffix,
@@ -171,17 +194,20 @@ def destroy_test_db(old_database_name, verbosity=1):
171 194
         creation_module.destroy_test_db(settings, connection, old_database_name, verbosity)
172 195
         return
173 196
 
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.
178 197
     if verbosity >= 1:
179 198
         print "Destroying test database..."
180 199
     connection.close()
181 200
     TEST_DATABASE_NAME = settings.DATABASE_NAME
182 201
     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.
185 211
         cursor = connection.cursor()
186 212
         _set_autocommit(connection)
187 213
         time.sleep(1) # To avoid "database is being accessed by other users" errors.
9  docs/settings.txt
@@ -981,8 +981,13 @@ TEST_DATABASE_NAME
981 981
 
982 982
 Default: ``None``
983 983
 
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`_.
  984
+The name of database to use when running the test suite.
  985
+
  986
+If the default value (``None``) is used with the SQLite database engine, the
  987
+tests will use a memory resident database. For all other database engines the
  988
+test database will use the name ``'test_' + settings.DATABASE_NAME``.
  989
+
  990
+See `Testing Django Applications`_.
986 991
 
987 992
 .. _Testing Django Applications: ../testing/
988 993
 

0 notes on commit 14ed0ef

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