From f6e75ab3cfbb4e806d00aa332ec9b149f6699556 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Fri, 29 Jul 2005 19:22:50 +0000 Subject: [PATCH] Added support for sqlite backend to test framework git-svn-id: http://code.djangoproject.com/svn/django/trunk@341 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/runtests.py | 69 ++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/tests/runtests.py b/tests/runtests.py index 26ab9e4fa0..2f6c7ed5d7 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -51,24 +51,29 @@ class TestRunner: # Manually set INSTALLED_APPS to point to the test app. settings.INSTALLED_APPS = (APP_NAME,) - # Create the test database and connect to it. We need autocommit() because - # PostgreSQL doesn't allow CREATE DATABASE statements within transactions. - cursor = db.cursor() - try: - db.connection.autocommit() - except AttributeError: - pass - self.output(1, "Creating test database") - try: - cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) - except: - confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME) - if confirm == 'yes': - cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) + # If we're using SQLite, it's more convient to test against an in-memory database + if settings.DATABASE_ENGINE == "sqlite3": + global TEST_DATABASE_NAME + TEST_DATABASE_NAME = ":memory:" + else: + # Create the test database and connect to it. We need autocommit() because + # PostgreSQL doesn't allow CREATE DATABASE statements within transactions. + cursor = db.cursor() + try: + db.connection.autocommit() + except AttributeError: + pass + self.output(1, "Creating test database") + try: cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) - else: - print "Tests cancelled." - return + except: + confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME) + if confirm == 'yes': + cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) + cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) + else: + print "Tests cancelled." + return db.close() old_database_name = settings.DATABASE_NAME settings.DATABASE_NAME = TEST_DATABASE_NAME @@ -101,20 +106,22 @@ class TestRunner: self.output(1, "%s model: Running tests" % model_name) runner.run(dtest, clear_globs=True, out=sys.stdout.write) - # Remove the test database, to clean up after ourselves. Connect to the - # previous database (not the test database) to do so, because it's not - # allowed to delete a database while being connected to it. - db.close() - settings.DATABASE_NAME = old_database_name - cursor = db.cursor() - self.output(1, "Deleting test database") - try: - db.connection.autocommit() - except AttributeError: - pass - else: - time.sleep(1) # To avoid "database is being accessed by other users" errors. - cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) + # Unless we're using SQLite, remove the test database, to clean up after + # ourselves. Connect to the previous database (not the test database) + # to do so, because it's not allowed to delete a database while being + # connected to it. + if settings.DATABASE_ENGINE != "sqlite3": + db.close() + settings.DATABASE_NAME = old_database_name + cursor = db.cursor() + self.output(1, "Deleting test database") + try: + db.connection.autocommit() + except AttributeError: + pass + else: + time.sleep(1) # To avoid "database is being accessed by other users" errors. + cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) # Display output. if error_list: