diff options
| author | Jason Pellerin <jpellerin@gmail.com> | 2006-09-04 02:20:26 +0000 |
|---|---|---|
| committer | Jason Pellerin <jpellerin@gmail.com> | 2006-09-04 02:20:26 +0000 |
| commit | b17f250907351923f39f8a50b87a35b26d2ca307 (patch) | |
| tree | bd0202dea501c6678a0b56b8e108194aab78468d /django/test | |
| parent | 5a58772a1ee470e2890d3c716ce4918555100a55 (diff) | |
[multi-db] Merge trunk to [3661]
git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3712 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test')
| -rw-r--r-- | django/test/simple.py | 3 | ||||
| -rw-r--r-- | django/test/utils.py | 63 |
2 files changed, 45 insertions, 21 deletions
diff --git a/django/test/simple.py b/django/test/simple.py index e72f693459..2469f80b3a 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -61,7 +61,8 @@ def run_tests(module_list, verbosity=1, extra_tests=[]): for test in extra_tests: suite.addTest(test) - old_name = create_test_db(verbosity) + old_name = settings.DATABASE_NAME + create_test_db(verbosity) management.syncdb(verbosity, interactive=False) unittest.TextTestRunner(verbosity=verbosity).run(suite) destroy_test_db(old_name, verbosity) diff --git a/django/test/utils.py b/django/test/utils.py index 28db46dafd..bde323fa4e 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -1,6 +1,6 @@ import sys, time from django.conf import settings -from django.db import connection, transaction +from django.db import backend, connect, connection, connection_info, connections # The prefix to put on the default database name when creating # the test database. @@ -12,67 +12,90 @@ def _set_autocommit(connection): connection.connection.autocommit(True) elif hasattr(connection.connection, "set_isolation_level"): connection.connection.set_isolation_level(0) - + def create_test_db(verbosity=1, autoclobber=False): if verbosity >= 1: print "Creating test database..." + # If we're using SQLite, it's more convenient to test against an # in-memory database. if settings.DATABASE_ENGINE == "sqlite3": TEST_DATABASE_NAME = ":memory:" + if verbosity >= 2: + print "Using in-memory sqlite database for testing" else: - TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME - + if settings.TEST_DATABASE_NAME: + TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME + else: + TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME + + qn = backend.quote_name # Create the test database and connect to it. We need to autocommit # if the database supports it because PostgreSQL doesn't allow # CREATE/DROP DATABASE statements within transactions. cursor = connection.cursor() _set_autocommit(connection) try: - cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) + cursor.execute("CREATE DATABASE %s" % qn(db_name)) except Exception, e: sys.stderr.write("Got an error creating the test database: %s\n" % e) if not autoclobber: - confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME) + confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % db_name) if autoclobber or confirm == 'yes': try: if verbosity >= 1: print "Destroying old test database..." - cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) + cursor.execute("DROP DATABASE %s" % qn(db_name)) if verbosity >= 1: print "Creating test database..." - cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) + cursor.execute("CREATE DATABASE %s" % qn(db_name)) except Exception, e: sys.stderr.write("Got an error recreating the test database: %s\n" % e) sys.exit(2) else: print "Tests cancelled." sys.exit(1) - - connection.close() - old_database_name = settings.DATABASE_NAME - settings.DATABASE_NAME = TEST_DATABASE_NAME + # Close the old connection + connection.close() - # Get a cursor (even though we don't need one yet). This has - # the side effect of initializing the test database. - cursor = connection.cursor() - - return old_database_name + # Get a cursor (even though we don't need one yet). This has + # the side effect of initializing the test database. + cursor = connection.cursor() -def destroy_test_db(old_database_name, verbosity=1): + # Fill OTHER_DATABASES with the TEST_DATABASES settings, + # and connect each named connection to the test database, using + # a separate connection instance for each (so, eg, transactions don't + # collide) + test_databases = {} + for db_name in settings.TEST_DATABASES: + if settings.DATABASE_ENGINE == 'sqlite3': + full_name = TEST_DATABASE_NAME + else: + full_name = TEST_DATABASE_NAME + db_name + db_st = {'DATABASE_NAME': full_name} + if db_name in settings.TEST_DATABASE_MODELS: + db_st['MODELS'] = settings.TEST_DATABASE_MODELS.get(db_name, []) + test_databases[db_name] = db_st + connections[db_name] = connect(connection_info.settings) + connections[db_name].connection.cursor() # Initialize it + settings.OTHER_DATABASES = test_databases + +def destroy_test_db(old_database_name, old_databases, verbosity=1): # 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 verbosity >= 1: print "Destroying test database..." - if settings.DATABASE_ENGINE != "sqlite3": + if settings.DATABASE_ENGINE != "sqlite3": connection.close() TEST_DATABASE_NAME = settings.DATABASE_NAME settings.DATABASE_NAME = old_database_name + settings.OTHER_DATABASES = old_databases cursor = connection.cursor() _set_autocommit(connection) time.sleep(1) # To avoid "database is being accessed by other users" errors. - cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) + cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME)) connection.close() + |
