mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Added support for sqlite backend to test framework
git-svn-id: http://code.djangoproject.com/svn/django/trunk@341 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9e64035796
commit
f6e75ab3cf
@ -51,24 +51,29 @@ class TestRunner:
|
|||||||
# Manually set INSTALLED_APPS to point to the test app.
|
# Manually set INSTALLED_APPS to point to the test app.
|
||||||
settings.INSTALLED_APPS = (APP_NAME,)
|
settings.INSTALLED_APPS = (APP_NAME,)
|
||||||
|
|
||||||
# Create the test database and connect to it. We need autocommit() because
|
# If we're using SQLite, it's more convient to test against an in-memory database
|
||||||
# PostgreSQL doesn't allow CREATE DATABASE statements within transactions.
|
if settings.DATABASE_ENGINE == "sqlite3":
|
||||||
cursor = db.cursor()
|
global TEST_DATABASE_NAME
|
||||||
try:
|
TEST_DATABASE_NAME = ":memory:"
|
||||||
db.connection.autocommit()
|
else:
|
||||||
except AttributeError:
|
# Create the test database and connect to it. We need autocommit() because
|
||||||
pass
|
# PostgreSQL doesn't allow CREATE DATABASE statements within transactions.
|
||||||
self.output(1, "Creating test database")
|
cursor = db.cursor()
|
||||||
try:
|
try:
|
||||||
cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME)
|
db.connection.autocommit()
|
||||||
except:
|
except AttributeError:
|
||||||
confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)
|
pass
|
||||||
if confirm == 'yes':
|
self.output(1, "Creating test database")
|
||||||
cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)
|
try:
|
||||||
cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME)
|
cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME)
|
||||||
else:
|
except:
|
||||||
print "Tests cancelled."
|
confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)
|
||||||
return
|
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()
|
db.close()
|
||||||
old_database_name = settings.DATABASE_NAME
|
old_database_name = settings.DATABASE_NAME
|
||||||
settings.DATABASE_NAME = TEST_DATABASE_NAME
|
settings.DATABASE_NAME = TEST_DATABASE_NAME
|
||||||
@ -101,20 +106,22 @@ class TestRunner:
|
|||||||
self.output(1, "%s model: Running tests" % model_name)
|
self.output(1, "%s model: Running tests" % model_name)
|
||||||
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
||||||
|
|
||||||
# Remove the test database, to clean up after ourselves. Connect to the
|
# Unless we're using SQLite, remove the test database, to clean up after
|
||||||
# previous database (not the test database) to do so, because it's not
|
# ourselves. Connect to the previous database (not the test database)
|
||||||
# allowed to delete a database while being connected to it.
|
# to do so, because it's not allowed to delete a database while being
|
||||||
db.close()
|
# connected to it.
|
||||||
settings.DATABASE_NAME = old_database_name
|
if settings.DATABASE_ENGINE != "sqlite3":
|
||||||
cursor = db.cursor()
|
db.close()
|
||||||
self.output(1, "Deleting test database")
|
settings.DATABASE_NAME = old_database_name
|
||||||
try:
|
cursor = db.cursor()
|
||||||
db.connection.autocommit()
|
self.output(1, "Deleting test database")
|
||||||
except AttributeError:
|
try:
|
||||||
pass
|
db.connection.autocommit()
|
||||||
else:
|
except AttributeError:
|
||||||
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
pass
|
||||||
cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)
|
else:
|
||||||
|
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
||||||
|
cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)
|
||||||
|
|
||||||
# Display output.
|
# Display output.
|
||||||
if error_list:
|
if error_list:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user