Django

Code

root/django/branches/sqlalchemy/tests/runtests.py

Revision 4455, 5.5 kB (checked in by rmunn, 2 years ago)

Merged revisions 4186 to 4454 from trunk.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2
3 import os, sys, traceback
4 import unittest
5
6 MODEL_TESTS_DIR_NAME = 'modeltests'
7 REGRESSION_TESTS_DIR_NAME = 'regressiontests'
8 TEST_DATABASE_NAME = 'django_test_db'
9 TEST_TEMPLATE_DIR = 'templates'
10
11 MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME)
12 REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME)
13
14 ALWAYS_INSTALLED_APPS = [
15     'django.contrib.contenttypes',
16     'django.contrib.auth',
17     'django.contrib.sites',
18     'django.contrib.flatpages',
19     'django.contrib.redirects',
20     'django.contrib.sessions',
21     'django.contrib.comments',
22     'django.contrib.admin',
23 ]
24
25 def get_test_models():
26     models = []
27     for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR):
28         for f in os.listdir(dirpath):
29             if f.startswith('__init__') or f.startswith('.') or f.startswith('sql') or f.startswith('invalid'):
30                 continue
31             models.append((loc, f))
32     return models
33
34 def get_invalid_models():
35     models = []
36     for loc, dirpath in (MODEL_TESTS_DIR_NAME, MODEL_TEST_DIR), (REGRESSION_TESTS_DIR_NAME, REGRESSION_TEST_DIR):
37         for f in os.listdir(dirpath):
38             if f.startswith('__init__') or f.startswith('.') or f.startswith('sql'):
39                 continue
40             if f.startswith('invalid'):
41                 models.append((loc, f))
42     return models
43
44 class InvalidModelTestCase(unittest.TestCase):
45     def __init__(self, model_label):
46         unittest.TestCase.__init__(self)
47         self.model_label = model_label
48
49     def runTest(self):
50         from django.core import management
51         from django.db.models.loading import load_app
52         from cStringIO import StringIO
53
54         try:
55             module = load_app(self.model_label)
56         except Exception, e:
57             self.fail('Unable to load invalid model module')
58
59         s = StringIO()
60         count = management.get_validation_errors(s, module)
61         s.seek(0)
62         error_log = s.read()
63         actual = error_log.split('\n')
64         expected = module.model_errors.split('\n')
65
66         unexpected = [err for err in actual if err not in expected]
67         missing = [err for err in expected if err not in actual]
68
69         self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
70         self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))
71
72 def django_tests(verbosity, tests_to_run):
73     from django.conf import settings
74
75     old_installed_apps = settings.INSTALLED_APPS
76     old_test_database_name = settings.TEST_DATABASE_NAME
77     old_root_urlconf = settings.ROOT_URLCONF
78     old_template_dirs = settings.TEMPLATE_DIRS
79     old_use_i18n = settings.USE_I18N
80
81     # Redirect some settings for the duration of these tests
82     settings.TEST_DATABASE_NAME = TEST_DATABASE_NAME
83     settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
84     settings.ROOT_URLCONF = 'urls'
85     settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
86     settings.USE_I18N = True
87
88     # Load all the ALWAYS_INSTALLED_APPS.
89     # (This import statement is intentionally delayed until after we
90     # access settings because of the USE_I18N dependency.)
91     from django.db.models.loading import get_apps, load_app
92     get_apps()
93
94     # Load all the test model apps
95     test_models = []
96     for model_dir, model_name in get_test_models():
97         model_label = '.'.join([model_dir, model_name])
98         try:
99             # if the model was named on the command line, or
100             # no models were named (i.e., run all), import
101             # this model and add it to the list to test.
102             if not tests_to_run or model_name in tests_to_run:
103                 if verbosity >= 1:
104                     print "Importing model %s" % model_name
105                 mod = load_app(model_label)
106                 settings.INSTALLED_APPS.append(model_label)
107                 test_models.append(mod)
108         except Exception, e:
109             sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))
110             continue
111
112     # Add tests for invalid models
113     extra_tests = []
114     for model_dir, model_name in get_invalid_models():
115         model_label = '.'.join([model_dir, model_name])
116         if not tests_to_run or model_name in tests_to_run:
117             extra_tests.append(InvalidModelTestCase(model_label))
118
119     # Run the test suite, including the extra validation tests.
120     from django.test.simple import run_tests
121     run_tests(test_models, verbosity, extra_tests=extra_tests)
122
123     # Restore the old settings
124     settings.INSTALLED_APPS = old_installed_apps
125     settings.TESTS_DATABASE_NAME = old_test_database_name
126     settings.ROOT_URLCONF = old_root_urlconf
127     settings.TEMPLATE_DIRS = old_template_dirs
128     settings.USE_I18N = old_use_i18n
129
130 if __name__ == "__main__":
131     from optparse import OptionParser
132     usage = "%prog [options] [model model model ...]"
133     parser = OptionParser(usage=usage)
134     parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0',
135         type='choice', choices=['0', '1', '2'],
136         help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
137     parser.add_option('--settings',
138         help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
139     options, args = parser.parse_args()
140     if options.settings:
141         os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
142
143     django_tests(int(options.verbosity), args)
Note: See TracBrowser for help on using the browser.