| 1 |
#!/usr/bin/env python |
|---|
| 2 |
|
|---|
| 3 |
import os, sys, traceback |
|---|
| 4 |
import unittest |
|---|
| 5 |
|
|---|
| 6 |
import django.contrib as contrib |
|---|
| 7 |
|
|---|
| 8 |
try: |
|---|
| 9 |
set |
|---|
| 10 |
except NameError: |
|---|
| 11 |
from sets import Set as set # For Python 2.3 |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
CONTRIB_DIR_NAME = 'django.contrib' |
|---|
| 15 |
MODEL_TESTS_DIR_NAME = 'modeltests' |
|---|
| 16 |
REGRESSION_TESTS_DIR_NAME = 'regressiontests' |
|---|
| 17 |
|
|---|
| 18 |
TEST_TEMPLATE_DIR = 'templates' |
|---|
| 19 |
|
|---|
| 20 |
CONTRIB_DIR = os.path.dirname(contrib.__file__) |
|---|
| 21 |
MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME) |
|---|
| 22 |
REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME) |
|---|
| 23 |
|
|---|
| 24 |
ALWAYS_INSTALLED_APPS = [ |
|---|
| 25 |
'django.contrib.contenttypes', |
|---|
| 26 |
'django.contrib.auth', |
|---|
| 27 |
'django.contrib.sites', |
|---|
| 28 |
'django.contrib.flatpages', |
|---|
| 29 |
'django.contrib.redirects', |
|---|
| 30 |
'django.contrib.sessions', |
|---|
| 31 |
'django.contrib.comments', |
|---|
| 32 |
'django.contrib.admin', |
|---|
| 33 |
] |
|---|
| 34 |
|
|---|
| 35 |
def get_test_models(): |
|---|
| 36 |
models = [] |
|---|
| 37 |
for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR), (CONTRIB_DIR_NAME, CONTRIB_DIR): |
|---|
| 38 |
for f in os.listdir(dirpath): |
|---|
| 39 |
if f.startswith('__init__') or f.startswith('.') or f.startswith('sql') or f.startswith('invalid'): |
|---|
| 40 |
continue |
|---|
| 41 |
models.append((loc, f)) |
|---|
| 42 |
return models |
|---|
| 43 |
|
|---|
| 44 |
def get_invalid_models(): |
|---|
| 45 |
models = [] |
|---|
| 46 |
for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR), (CONTRIB_DIR_NAME, CONTRIB_DIR): |
|---|
| 47 |
for f in os.listdir(dirpath): |
|---|
| 48 |
if f.startswith('__init__') or f.startswith('.') or f.startswith('sql'): |
|---|
| 49 |
continue |
|---|
| 50 |
if f.startswith('invalid'): |
|---|
| 51 |
models.append((loc, f)) |
|---|
| 52 |
return models |
|---|
| 53 |
|
|---|
| 54 |
class InvalidModelTestCase(unittest.TestCase): |
|---|
| 55 |
def __init__(self, model_label): |
|---|
| 56 |
unittest.TestCase.__init__(self) |
|---|
| 57 |
self.model_label = model_label |
|---|
| 58 |
|
|---|
| 59 |
def runTest(self): |
|---|
| 60 |
from django.core.management.validation import get_validation_errors |
|---|
| 61 |
from django.db.models.loading import load_app |
|---|
| 62 |
from cStringIO import StringIO |
|---|
| 63 |
|
|---|
| 64 |
try: |
|---|
| 65 |
module = load_app(self.model_label) |
|---|
| 66 |
except Exception, e: |
|---|
| 67 |
self.fail('Unable to load invalid model module') |
|---|
| 68 |
|
|---|
| 69 |
# Make sure sys.stdout is not a tty so that we get errors without |
|---|
| 70 |
# coloring attached (makes matching the results easier). We restore |
|---|
| 71 |
# sys.stderr afterwards. |
|---|
| 72 |
orig_stdout = sys.stdout |
|---|
| 73 |
s = StringIO() |
|---|
| 74 |
sys.stdout = s |
|---|
| 75 |
count = get_validation_errors(s, module) |
|---|
| 76 |
sys.stdout = orig_stdout |
|---|
| 77 |
s.seek(0) |
|---|
| 78 |
error_log = s.read() |
|---|
| 79 |
actual = error_log.split('\n') |
|---|
| 80 |
expected = module.model_errors.split('\n') |
|---|
| 81 |
|
|---|
| 82 |
unexpected = [err for err in actual if err not in expected] |
|---|
| 83 |
missing = [err for err in expected if err not in actual] |
|---|
| 84 |
|
|---|
| 85 |
self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected)) |
|---|
| 86 |
self.assert_(not missing, "Missing Errors: " + '\n'.join(missing)) |
|---|
| 87 |
|
|---|
| 88 |
def django_tests(verbosity, interactive, test_labels): |
|---|
| 89 |
from django.conf import settings |
|---|
| 90 |
|
|---|
| 91 |
old_installed_apps = settings.INSTALLED_APPS |
|---|
| 92 |
old_test_database_name = settings.TEST_DATABASE_NAME |
|---|
| 93 |
old_root_urlconf = settings.ROOT_URLCONF |
|---|
| 94 |
old_template_dirs = settings.TEMPLATE_DIRS |
|---|
| 95 |
old_use_i18n = settings.USE_I18N |
|---|
| 96 |
old_login_url = settings.LOGIN_URL |
|---|
| 97 |
old_language_code = settings.LANGUAGE_CODE |
|---|
| 98 |
old_middleware_classes = settings.MIDDLEWARE_CLASSES |
|---|
| 99 |
|
|---|
| 100 |
# Redirect some settings for the duration of these tests. |
|---|
| 101 |
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS |
|---|
| 102 |
settings.ROOT_URLCONF = 'urls' |
|---|
| 103 |
settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),) |
|---|
| 104 |
settings.USE_I18N = True |
|---|
| 105 |
settings.LANGUAGE_CODE = 'en' |
|---|
| 106 |
settings.LOGIN_URL = '/accounts/login/' |
|---|
| 107 |
settings.MIDDLEWARE_CLASSES = ( |
|---|
| 108 |
'django.contrib.sessions.middleware.SessionMiddleware', |
|---|
| 109 |
'django.contrib.auth.middleware.AuthenticationMiddleware', |
|---|
| 110 |
'django.middleware.common.CommonMiddleware', |
|---|
| 111 |
) |
|---|
| 112 |
settings.SITE_ID = 1 |
|---|
| 113 |
|
|---|
| 114 |
# Load all the ALWAYS_INSTALLED_APPS. |
|---|
| 115 |
# (This import statement is intentionally delayed until after we |
|---|
| 116 |
# access settings because of the USE_I18N dependency.) |
|---|
| 117 |
from django.db.models.loading import get_apps, load_app |
|---|
| 118 |
get_apps() |
|---|
| 119 |
|
|---|
| 120 |
# Load all the test model apps. |
|---|
| 121 |
test_models = [] |
|---|
| 122 |
for model_dir, model_name in get_test_models(): |
|---|
| 123 |
model_label = '.'.join([model_dir, model_name]) |
|---|
| 124 |
try: |
|---|
| 125 |
# if the model was named on the command line, or |
|---|
| 126 |
# no models were named (i.e., run all), import |
|---|
| 127 |
# this model and add it to the list to test. |
|---|
| 128 |
if not test_labels or model_name in set([label.split('.')[0] for label in test_labels]): |
|---|
| 129 |
if verbosity >= 1: |
|---|
| 130 |
print "Importing model %s" % model_name |
|---|
| 131 |
mod = load_app(model_label) |
|---|
| 132 |
if mod: |
|---|
| 133 |
if model_label not in settings.INSTALLED_APPS: |
|---|
| 134 |
settings.INSTALLED_APPS.append(model_label) |
|---|
| 135 |
except Exception, e: |
|---|
| 136 |
sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:])) |
|---|
| 137 |
continue |
|---|
| 138 |
|
|---|
| 139 |
# Add tests for invalid models. |
|---|
| 140 |
extra_tests = [] |
|---|
| 141 |
for model_dir, model_name in get_invalid_models(): |
|---|
| 142 |
model_label = '.'.join([model_dir, model_name]) |
|---|
| 143 |
if not test_labels or model_name in test_labels: |
|---|
| 144 |
extra_tests.append(InvalidModelTestCase(model_label)) |
|---|
| 145 |
|
|---|
| 146 |
# Run the test suite, including the extra validation tests. |
|---|
| 147 |
from django.test.simple import run_tests |
|---|
| 148 |
failures = run_tests(test_labels, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests) |
|---|
| 149 |
if failures: |
|---|
| 150 |
sys.exit(failures) |
|---|
| 151 |
|
|---|
| 152 |
# Restore the old settings. |
|---|
| 153 |
settings.INSTALLED_APPS = old_installed_apps |
|---|
| 154 |
settings.ROOT_URLCONF = old_root_urlconf |
|---|
| 155 |
settings.TEMPLATE_DIRS = old_template_dirs |
|---|
| 156 |
settings.USE_I18N = old_use_i18n |
|---|
| 157 |
settings.LANGUAGE_CODE = old_language_code |
|---|
| 158 |
settings.LOGIN_URL = old_login_url |
|---|
| 159 |
settings.MIDDLEWARE_CLASSES = old_middleware_classes |
|---|
| 160 |
|
|---|
| 161 |
if __name__ == "__main__": |
|---|
| 162 |
from optparse import OptionParser |
|---|
| 163 |
usage = "%prog [options] [model model model ...]" |
|---|
| 164 |
parser = OptionParser(usage=usage) |
|---|
| 165 |
parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0', |
|---|
| 166 |
type='choice', choices=['0', '1', '2'], |
|---|
| 167 |
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output') |
|---|
| 168 |
parser.add_option('--noinput', action='store_false', dest='interactive', default=True, |
|---|
| 169 |
help='Tells Django to NOT prompt the user for input of any kind.') |
|---|
| 170 |
parser.add_option('--settings', |
|---|
| 171 |
help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') |
|---|
| 172 |
options, args = parser.parse_args() |
|---|
| 173 |
if options.settings: |
|---|
| 174 |
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings |
|---|
| 175 |
elif "DJANGO_SETTINGS_MODULE" not in os.environ: |
|---|
| 176 |
parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. " |
|---|
| 177 |
"Set it or use --settings.") |
|---|
| 178 |
django_tests(int(options.verbosity), options.interactive, args) |
|---|