Commits

Lukas Linhart committed f96186c

Add --persist-test-database (refs #32)

  • Participants
  • Parent commits dc8073a

Comments (0)

Files changed (3)

djangosanetesting/noseplugins.py

 #from djagnosanetesting.cache import flush_django_cache
 from djangosanetesting.selenium.driver import selenium
 from djangosanetesting.utils import (
-    get_live_server_path, 
+    get_live_server_path, test_database_exists,
     DEFAULT_LIVE_SERVER_ADDRESS, DEFAULT_LIVE_SERVER_PORT
 )
 
     """
     activation_parameter = '--with-django'
     name = 'django'
+    env_opt = 'DST_PERSIST_TEST_DATABASE'
 
     def options(self, parser, env=os.environ):
         Plugin.options(self, parser, env)
+        
+        parser.add_option(
+            "", "--persist-test-database", action="store_true",
+            default=env.get(self.env_opt), dest="persist_test_database",
+            help="Do not flush database unless neccessary [%s]" % self.env_opt)
 
     def configure(self, options, config):
         Plugin.configure(self, options, config)
+        self.persist_test_database = options.persist_test_database
     
     def begin(self):
         """
         """
         from django.test.utils import setup_test_environment
         setup_test_environment()
-        
+
         #FIXME: This should be perhaps moved to startTest and be lazy
         # for tests that do not need test database at all
         from django.db import connection
         from django.conf import settings
         self.old_name = settings.DATABASE_NAME
-        
-        connection.creation.create_test_db(verbosity=False, autoclobber=True)
 
-        if getattr(settings, "FLUSH_TEST_DATABASE_AFTER_INITIAL_SYNCDB", False):
-            getattr(settings, "TEST_DATABASE_FLUSH_COMMAND", flush_database)(self)
+        if not self.persist_test_database or test_database_exists():
+            connection.creation.create_test_db(verbosity=False, autoclobber=True)
+
+            if getattr(settings, "FLUSH_TEST_DATABASE_AFTER_INITIAL_SYNCDB", False):
+                getattr(settings, "TEST_DATABASE_FLUSH_COMMAND", flush_database)(self)
 
         flush_cache()
 
         from django.test.utils import teardown_test_environment
         teardown_test_environment()
 
-        from django.db import connection
-        connection.creation.destroy_test_db(self.old_name, verbosity=False)
+        if not self.persist_test_database:
+            from django.db import connection
+            connection.creation.destroy_test_db(self.old_name, verbosity=False)
     
     
     def startTest(self, test):

djangosanetesting/utils.py

+import os
 from django.conf import settings
 
 DEFAULT_LIVE_SERVER_PROTOCOL = "http"
 DEFAULT_LIVE_SERVER_ADDRESS = '0.0.0.0'
 DEFAULT_URL_ROOT_SERVER_ADDRESS = 'localhost'
 
-
-
 def is_test_database():
     """
     Return whether we're using test database. Can be used to determine if we're
 
     return settings.DATABASE_NAME == test_database_name
 
+def test_database_exists():
+    from django.db import connection, DatabaseError
+
+    try:
+        if settings.DATABASE_ENGINE == 'sqlite3':
+            if not os.path.exists(settings.DATABASE_NAME):
+                raise DatabaseError()
+        connection.cursor()
+        return True
+    except DatabaseError, err:
+        return False
+
 def get_live_server_path():
 
     return getattr(settings, "URL_ROOT", "%s://%s:%s/" % (
 
 django.db.transaction is also available under self.transaction. Use at own discretion; you should only access it when using :class:`DestructiveDatabaseTestCase` (to make data available for server thread), messing with it when using :attr:`database_single_transaction` can cause test interaction.
 
+Since 0.6, You can use ``--persist-test-database``. This is similar to quicktest command from django-test-utils: database is not flushed at the beginning if it exists and is not dropped at the end of the test run. Useful if You are debugging single test in flush-heavy applications.
+
+.. Warning::
+
+  By definition, strange things will happen if You'll change tests You're executing. Do not overuse this feature.
+
+.. Warning::
+
+  Tested by hand, not covered by automatic tests. Please report any bugs/testcases You'll encounter.
+
+
 .. _django-live-server-plugin:
 
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^