thirdparty/google_appengine/lib/django/tests/runtests.py
changeset 109 620f9b141567
equal deleted inserted replaced
108:261778de26ff 109:620f9b141567
       
     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     old_middleware_classes = settings.MIDDLEWARE_CLASSES
       
    81 
       
    82     # Redirect some settings for the duration of these tests.
       
    83     settings.TEST_DATABASE_NAME = TEST_DATABASE_NAME
       
    84     settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
       
    85     settings.ROOT_URLCONF = 'urls'
       
    86     settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
       
    87     settings.USE_I18N = True
       
    88     settings.MIDDLEWARE_CLASSES = (
       
    89         'django.contrib.sessions.middleware.SessionMiddleware',
       
    90         'django.contrib.auth.middleware.AuthenticationMiddleware',
       
    91         'django.middleware.common.CommonMiddleware',
       
    92     )
       
    93 
       
    94     # Load all the ALWAYS_INSTALLED_APPS.
       
    95     # (This import statement is intentionally delayed until after we
       
    96     # access settings because of the USE_I18N dependency.)
       
    97     from django.db.models.loading import get_apps, load_app
       
    98     get_apps()
       
    99 
       
   100     # Load all the test model apps.
       
   101     test_models = []
       
   102     for model_dir, model_name in get_test_models():
       
   103         model_label = '.'.join([model_dir, model_name])
       
   104         try:
       
   105             # if the model was named on the command line, or
       
   106             # no models were named (i.e., run all), import
       
   107             # this model and add it to the list to test.
       
   108             if not tests_to_run or model_name in tests_to_run:
       
   109                 if verbosity >= 1:
       
   110                     print "Importing model %s" % model_name
       
   111                 mod = load_app(model_label)
       
   112                 settings.INSTALLED_APPS.append(model_label)
       
   113                 test_models.append(mod)
       
   114         except Exception, e:
       
   115             sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))
       
   116             continue
       
   117 
       
   118     # Add tests for invalid models.
       
   119     extra_tests = []
       
   120     for model_dir, model_name in get_invalid_models():
       
   121         model_label = '.'.join([model_dir, model_name])
       
   122         if not tests_to_run or model_name in tests_to_run:
       
   123             extra_tests.append(InvalidModelTestCase(model_label))
       
   124 
       
   125     # Run the test suite, including the extra validation tests.
       
   126     from django.test.simple import run_tests
       
   127     failures = run_tests(test_models, verbosity, extra_tests=extra_tests)
       
   128     if failures:
       
   129         sys.exit(failures)
       
   130 
       
   131     # Restore the old settings.
       
   132     settings.INSTALLED_APPS = old_installed_apps
       
   133     settings.TESTS_DATABASE_NAME = old_test_database_name
       
   134     settings.ROOT_URLCONF = old_root_urlconf
       
   135     settings.TEMPLATE_DIRS = old_template_dirs
       
   136     settings.USE_I18N = old_use_i18n
       
   137     settings.MIDDLEWARE_CLASSES = old_middleware_classes
       
   138 
       
   139 if __name__ == "__main__":
       
   140     from optparse import OptionParser
       
   141     usage = "%prog [options] [model model model ...]"
       
   142     parser = OptionParser(usage=usage)
       
   143     parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0',
       
   144         type='choice', choices=['0', '1', '2'],
       
   145         help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
       
   146     parser.add_option('--settings',
       
   147         help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
       
   148     options, args = parser.parse_args()
       
   149     if options.settings:
       
   150         os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
       
   151     elif "DJANGO_SETTINGS_MODULE" not in os.environ:
       
   152         parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
       
   153                       "Set it or use --settings.")
       
   154     django_tests(int(options.verbosity), args)