2005-07-29 17:15:40 +02:00
#!/usr/bin/env python
2006-08-27 15:59:47 +02:00
import os , sys , traceback
import unittest
2005-07-29 17:15:40 +02:00
2006-05-02 03:31:56 +02:00
MODEL_TESTS_DIR_NAME = ' modeltests '
2006-06-20 07:29:19 +02:00
REGRESSION_TESTS_DIR_NAME = ' regressiontests '
2006-09-01 15:33:26 +02:00
TEST_DATABASE_NAME = ' django_test_db '
2006-09-02 11:34:40 +02:00
TEST_TEMPLATE_DIR = ' templates '
2005-07-29 17:15:40 +02:00
2006-05-02 03:31:56 +02:00
MODEL_TEST_DIR = os . path . join ( os . path . dirname ( __file__ ) , MODEL_TESTS_DIR_NAME )
2006-06-20 07:29:19 +02:00
REGRESSION_TEST_DIR = os . path . join ( os . path . dirname ( __file__ ) , REGRESSION_TESTS_DIR_NAME )
2005-07-30 00:35:54 +02:00
2006-05-26 23:28:12 +02:00
ALWAYS_INSTALLED_APPS = [
' django.contrib.contenttypes ' ,
2006-06-20 16:27:44 +02:00
' django.contrib.auth ' ,
2006-06-16 20:58:45 +02:00
' django.contrib.sites ' ,
2006-05-26 23:28:12 +02:00
' django.contrib.flatpages ' ,
' django.contrib.redirects ' ,
' django.contrib.sessions ' ,
2006-06-16 20:58:45 +02:00
' django.contrib.comments ' ,
' django.contrib.admin ' ,
2006-05-26 23:28:12 +02:00
]
2005-07-30 00:35:54 +02:00
def get_test_models ( ) :
2006-06-20 09:12:45 +02:00
models = [ ]
2006-06-20 16:27:44 +02:00
for loc , dirpath in ( MODEL_TESTS_DIR_NAME , MODEL_TEST_DIR ) , ( REGRESSION_TESTS_DIR_NAME , REGRESSION_TEST_DIR ) :
for f in os . listdir ( dirpath ) :
2006-08-27 15:59:47 +02:00
if f . startswith ( ' __init__ ' ) or f . startswith ( ' . ' ) or f . startswith ( ' sql ' ) or f . startswith ( ' invalid ' ) :
2006-06-20 09:12:45 +02:00
continue
models . append ( ( loc , f ) )
return models
2005-07-30 00:35:54 +02:00
2006-08-27 15:59:47 +02:00
def get_invalid_models ( ) :
models = [ ]
for loc , dirpath in ( MODEL_TESTS_DIR_NAME , MODEL_TEST_DIR ) , ( REGRESSION_TESTS_DIR_NAME , REGRESSION_TEST_DIR ) :
for f in os . listdir ( dirpath ) :
if f . startswith ( ' __init__ ' ) or f . startswith ( ' . ' ) or f . startswith ( ' sql ' ) :
continue
if f . startswith ( ' invalid ' ) :
models . append ( ( loc , f ) )
return models
2006-12-15 07:06:52 +01:00
2006-08-27 15:59:47 +02:00
class InvalidModelTestCase ( unittest . TestCase ) :
def __init__ ( self , model_label ) :
unittest . TestCase . __init__ ( self )
self . model_label = model_label
2006-12-15 07:06:52 +01:00
2006-08-27 15:59:47 +02:00
def runTest ( self ) :
2006-05-02 03:31:56 +02:00
from django . core import management
2006-06-23 06:37:00 +02:00
from django . db . models . loading import load_app
2006-08-27 15:59:47 +02:00
from cStringIO import StringIO
try :
module = load_app ( self . model_label )
except Exception , e :
self . fail ( ' Unable to load invalid model module ' )
2006-12-15 07:06:52 +01:00
2006-08-27 15:59:47 +02:00
s = StringIO ( )
count = management . get_validation_errors ( s , module )
s . seek ( 0 )
error_log = s . read ( )
actual = error_log . split ( ' \n ' )
expected = module . model_errors . split ( ' \n ' )
unexpected = [ err for err in actual if err not in expected ]
missing = [ err for err in expected if err not in actual ]
self . assert_ ( not unexpected , " Unexpected Errors: " + ' \n ' . join ( unexpected ) )
self . assert_ ( not missing , " Missing Errors: " + ' \n ' . join ( missing ) )
def django_tests ( verbosity , tests_to_run ) :
from django . conf import settings
2006-09-02 11:34:40 +02:00
2006-08-27 15:59:47 +02:00
old_installed_apps = settings . INSTALLED_APPS
2006-09-01 15:33:26 +02:00
old_test_database_name = settings . TEST_DATABASE_NAME
2006-09-02 11:34:40 +02:00
old_root_urlconf = settings . ROOT_URLCONF
old_template_dirs = settings . TEMPLATE_DIRS
2006-12-15 07:06:52 +01:00
old_use_i18n = settings . USE_I18N
2007-02-10 05:01:19 +01:00
old_middleware_classes = settings . MIDDLEWARE_CLASSES
2006-12-15 07:06:52 +01:00
2007-02-10 05:01:19 +01:00
# Redirect some settings for the duration of these tests.
2006-09-01 15:33:26 +02:00
settings . TEST_DATABASE_NAME = TEST_DATABASE_NAME
2006-08-27 15:59:47 +02:00
settings . INSTALLED_APPS = ALWAYS_INSTALLED_APPS
2006-09-02 11:34:40 +02:00
settings . ROOT_URLCONF = ' urls '
settings . TEMPLATE_DIRS = ( os . path . join ( os . path . dirname ( __file__ ) , TEST_TEMPLATE_DIR ) , )
2006-12-15 07:06:52 +01:00
settings . USE_I18N = True
2007-02-10 05:01:19 +01:00
settings . MIDDLEWARE_CLASSES = (
' django.contrib.sessions.middleware.SessionMiddleware ' ,
' django.contrib.auth.middleware.AuthenticationMiddleware ' ,
' django.middleware.common.CommonMiddleware ' ,
)
2006-12-15 07:06:52 +01:00
# Load all the ALWAYS_INSTALLED_APPS.
# (This import statement is intentionally delayed until after we
# access settings because of the USE_I18N dependency.)
from django . db . models . loading import get_apps , load_app
2006-08-27 15:59:47 +02:00
get_apps ( )
2006-12-15 07:06:52 +01:00
2007-02-10 05:01:19 +01:00
# Load all the test model apps.
2006-09-02 11:34:40 +02:00
test_models = [ ]
2006-12-15 07:06:52 +01:00
for model_dir , model_name in get_test_models ( ) :
2006-08-27 15:59:47 +02:00
model_label = ' . ' . join ( [ model_dir , model_name ] )
try :
# if the model was named on the command line, or
2006-12-15 07:06:52 +01:00
# no models were named (i.e., run all), import
2006-08-27 15:59:47 +02:00
# this model and add it to the list to test.
if not tests_to_run or model_name in tests_to_run :
if verbosity > = 1 :
print " Importing model %s " % model_name
mod = load_app ( model_label )
2006-12-15 07:06:52 +01:00
settings . INSTALLED_APPS . append ( model_label )
2006-08-27 15:59:47 +02:00
test_models . append ( mod )
except Exception , e :
sys . stderr . write ( " Error while importing %s : " % model_name + ' ' . join ( traceback . format_exception ( * sys . exc_info ( ) ) [ 1 : ] ) )
2006-12-15 07:06:52 +01:00
continue
2006-08-27 15:59:47 +02:00
2007-02-10 05:01:19 +01:00
# Add tests for invalid models.
2006-08-27 15:59:47 +02:00
extra_tests = [ ]
for model_dir , model_name in get_invalid_models ( ) :
model_label = ' . ' . join ( [ model_dir , model_name ] )
if not tests_to_run or model_name in tests_to_run :
extra_tests . append ( InvalidModelTestCase ( model_label ) )
2006-12-15 07:06:52 +01:00
2006-08-27 15:59:47 +02:00
# Run the test suite, including the extra validation tests.
from django . test . simple import run_tests
2007-02-26 13:52:01 +01:00
failures = run_tests ( test_models , verbosity , extra_tests = extra_tests )
if failures :
sys . exit ( failures )
2006-12-15 07:06:52 +01:00
2007-02-10 05:01:19 +01:00
# Restore the old settings.
2006-08-27 15:59:47 +02:00
settings . INSTALLED_APPS = old_installed_apps
2006-09-01 15:33:26 +02:00
settings . TESTS_DATABASE_NAME = old_test_database_name
2006-09-02 11:34:40 +02:00
settings . ROOT_URLCONF = old_root_urlconf
settings . TEMPLATE_DIRS = old_template_dirs
2006-12-15 07:06:52 +01:00
settings . USE_I18N = old_use_i18n
2007-02-10 05:01:19 +01:00
settings . MIDDLEWARE_CLASSES = old_middleware_classes
2006-12-15 07:06:52 +01:00
2005-07-29 17:15:40 +02:00
if __name__ == " __main__ " :
from optparse import OptionParser
2005-09-19 03:18:04 +02:00
usage = " % prog [options] [model model model ...] "
2006-02-18 21:08:20 +01:00
parser = OptionParser ( usage = usage )
2006-08-27 15:59:47 +02:00
parser . add_option ( ' -v ' , ' --verbosity ' , action = ' store ' , dest = ' verbosity ' , default = ' 0 ' ,
type = ' choice ' , choices = [ ' 0 ' , ' 1 ' , ' 2 ' ] ,
2006-12-15 07:06:52 +01:00
help = ' Verbosity level; 0=minimal output, 1=normal output, 2=all output ' )
2005-08-10 17:36:16 +02:00
parser . add_option ( ' --settings ' ,
2006-01-01 19:37:33 +01:00
help = ' Python path to settings module, e.g. " myproject.settings " . If this isn \' t provided, the DJANGO_SETTINGS_MODULE environment variable will be used. ' )
2005-07-29 17:15:40 +02:00
options , args = parser . parse_args ( )
2005-08-10 17:36:16 +02:00
if options . settings :
os . environ [ ' DJANGO_SETTINGS_MODULE ' ] = options . settings
2007-02-26 23:34:56 +01:00
elif " DJANGO_SETTINGS_MODULE " not in os . environ :
parser . error ( " DJANGO_SETTINGS_MODULE is not set in the environment. "
" Set it or use --settings. " )
2006-08-27 15:59:47 +02:00
django_tests ( int ( options . verbosity ) , args )