mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -7,7 +7,7 @@ import os, re, sys, time, traceback
|
||||
# and Django aims to work with Python 2.3+.
|
||||
import doctest
|
||||
|
||||
APP_NAME = 'testapp'
|
||||
MODEL_TESTS_DIR_NAME = 'modeltests'
|
||||
OTHER_TESTS_DIR = "othertests"
|
||||
TEST_DATABASE_NAME = 'django_test_db'
|
||||
|
||||
@@ -18,10 +18,10 @@ def log_error(model_name, title, description):
|
||||
'description': description,
|
||||
})
|
||||
|
||||
MODEL_DIR = os.path.join(os.path.dirname(__file__), APP_NAME, 'models')
|
||||
MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME)
|
||||
|
||||
def get_test_models():
|
||||
return [f[:-3] for f in os.listdir(MODEL_DIR) if f.endswith('.py') and not f.startswith('__init__')]
|
||||
return [f for f in os.listdir(MODEL_TEST_DIR) if not f.startswith('__init__') and not f.startswith('.')]
|
||||
|
||||
class DjangoDoctestRunner(doctest.DocTestRunner):
|
||||
def __init__(self, verbosity_level, *args, **kwargs):
|
||||
@@ -39,9 +39,13 @@ class DjangoDoctestRunner(doctest.DocTestRunner):
|
||||
"Code: %r\nLine: %s\nExpected: %r\nGot: %r" % (example.source.strip(), example.lineno, example.want, got))
|
||||
|
||||
def report_unexpected_exception(self, out, test, example, exc_info):
|
||||
from django.db import transaction
|
||||
tb = ''.join(traceback.format_exception(*exc_info)[1:])
|
||||
log_error(test.name, "API test raised an exception",
|
||||
"Code: %r\nLine: %s\nException: %s" % (example.source.strip(), example.lineno, tb))
|
||||
# Rollback, in case of database errors. Otherwise they'd have
|
||||
# side effects on other tests.
|
||||
transaction.rollback_unless_managed()
|
||||
|
||||
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
|
||||
|
||||
@@ -68,14 +72,27 @@ class TestRunner:
|
||||
|
||||
def run_tests(self):
|
||||
from django.conf import settings
|
||||
from django.core.db import db
|
||||
from django.core import management, meta
|
||||
|
||||
# Manually set INSTALLED_APPS to point to the test app.
|
||||
settings.INSTALLED_APPS = (APP_NAME,)
|
||||
# Manually set INSTALLED_APPS to point to the test models.
|
||||
settings.INSTALLED_APPS = [MODEL_TESTS_DIR_NAME + '.' + a for a in get_test_models()]
|
||||
|
||||
# Manually set DEBUG = False.
|
||||
settings.DEBUG = False
|
||||
|
||||
from django.db import connection
|
||||
from django.core import management
|
||||
import django.db.models
|
||||
|
||||
# Determine which models we're going to test.
|
||||
test_models = get_test_models()
|
||||
if 'othertests' in self.which_tests:
|
||||
self.which_tests.remove('othertests')
|
||||
run_othertests = True
|
||||
if not self.which_tests:
|
||||
test_models = []
|
||||
else:
|
||||
run_othertests = not self.which_tests
|
||||
|
||||
if self.which_tests:
|
||||
# Only run the specified tests.
|
||||
bad_models = [m for m in self.which_tests if m not in test_models]
|
||||
@@ -96,9 +113,9 @@ class TestRunner:
|
||||
# Create the test database and connect to it. We need autocommit()
|
||||
# because PostgreSQL doesn't allow CREATE DATABASE statements
|
||||
# within transactions.
|
||||
cursor = db.cursor()
|
||||
cursor = connection.cursor()
|
||||
try:
|
||||
db.connection.autocommit(1)
|
||||
connection.connection.autocommit(1)
|
||||
except AttributeError:
|
||||
pass
|
||||
self.output(1, "Creating test database")
|
||||
@@ -113,43 +130,62 @@ class TestRunner:
|
||||
else:
|
||||
print "Tests cancelled."
|
||||
return
|
||||
db.close()
|
||||
connection.close()
|
||||
old_database_name = settings.DATABASE_NAME
|
||||
settings.DATABASE_NAME = TEST_DATABASE_NAME
|
||||
|
||||
# Initialize the test database.
|
||||
cursor = db.cursor()
|
||||
self.output(1, "Initializing test database")
|
||||
management.init()
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Run the tests for each test model.
|
||||
self.output(1, "Running app tests")
|
||||
for model_name in test_models:
|
||||
self.output(1, "%s model: Importing" % model_name)
|
||||
try:
|
||||
mod = meta.get_app(model_name)
|
||||
# TODO: Abstract this into a meta.get_app() replacement?
|
||||
mod = __import__(MODEL_TESTS_DIR_NAME + '.' + model_name + '.models', '', '', [''])
|
||||
except Exception, e:
|
||||
log_error(model_name, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:]))
|
||||
continue
|
||||
self.output(1, "%s model: Installing" % model_name)
|
||||
management.install(mod)
|
||||
|
||||
# Run the API tests.
|
||||
p = doctest.DocTestParser()
|
||||
test_namespace = dict([(m._meta.module_name, getattr(mod, m._meta.module_name)) for m in mod._MODELS])
|
||||
dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
|
||||
# Manually set verbose=False, because "-v" command-line parameter
|
||||
# has side effects on doctest TestRunner class.
|
||||
runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
|
||||
self.output(1, "%s model: Running tests" % model_name)
|
||||
try:
|
||||
if not getattr(mod, 'error_log', None):
|
||||
# Model is not marked as an invalid model
|
||||
self.output(1, "%s model: Installing" % model_name)
|
||||
management.install(mod)
|
||||
|
||||
# Run the API tests.
|
||||
p = doctest.DocTestParser()
|
||||
test_namespace = dict([(m._meta.object_name, m) \
|
||||
for m in django.db.models.get_models(mod)])
|
||||
dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
|
||||
# Manually set verbose=False, because "-v" command-line parameter
|
||||
# has side effects on doctest TestRunner class.
|
||||
runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
|
||||
self.output(1, "%s model: Running tests" % model_name)
|
||||
runner.run(dtest, clear_globs=True, out=sys.stdout.write)
|
||||
finally:
|
||||
# Rollback, in case of database errors. Otherwise they'd have
|
||||
# side effects on other tests.
|
||||
db.rollback()
|
||||
else:
|
||||
# Check that model known to be invalid is invalid for the right reasons.
|
||||
self.output(1, "%s model: Validating" % model_name)
|
||||
|
||||
if not self.which_tests:
|
||||
from cStringIO import StringIO
|
||||
s = StringIO()
|
||||
count = management.get_validation_errors(s, mod)
|
||||
s.seek(0)
|
||||
error_log = s.read()
|
||||
actual = error_log.split('\n')
|
||||
expected = mod.error_log.split('\n')
|
||||
|
||||
unexpected = [err for err in actual if err not in expected]
|
||||
missing = [err for err in expected if err not in actual]
|
||||
|
||||
if unexpected or missing:
|
||||
unexpected_log = '\n'.join(unexpected)
|
||||
missing_log = '\n'.join(missing)
|
||||
log_error(model_name,
|
||||
"Validator found %d validation errors, %d expected" % (count, len(expected) - 1),
|
||||
"Missing errors:\n%s\n\nUnexpected errors:\n%s" % (missing_log, unexpected_log))
|
||||
|
||||
if run_othertests:
|
||||
# Run the non-model tests in the other tests dir
|
||||
self.output(1, "Running other tests")
|
||||
other_tests_dir = os.path.join(os.path.dirname(__file__), OTHER_TESTS_DIR)
|
||||
@@ -180,12 +216,12 @@ class TestRunner:
|
||||
# to do so, because it's not allowed to delete a database while being
|
||||
# connected to it.
|
||||
if settings.DATABASE_ENGINE != "sqlite3":
|
||||
db.close()
|
||||
connection.close()
|
||||
settings.DATABASE_NAME = old_database_name
|
||||
cursor = db.cursor()
|
||||
cursor = connection.cursor()
|
||||
self.output(1, "Deleting test database")
|
||||
try:
|
||||
db.connection.autocommit(1)
|
||||
connection.connection.autocommit(1)
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user