<s> import unittest as real_unittest <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db . models import get_app , get_apps <EOL> from django . test import _doctest as doctest <EOL> from django . test . utils import setup_test_environment , teardown_test_environment <EOL> from django . test . testcases import OutputChecker , DocTestRunner , TestCase <EOL> from django . utils import unittest <EOL> try : <EOL> all <EOL> except NameError : <EOL> from django . utils . itercompat import all <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> TEST_MODULE = '<STR_LIT>' <EOL> doctestOutputChecker = OutputChecker ( ) <EOL> class DjangoTestRunner ( unittest . TextTestRunner ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> import warnings <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> PendingDeprecationWarning <EOL> ) <EOL> super ( DjangoTestRunner , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_tests ( app_module ) : <EOL> try : <EOL> app_path = app_module . __name__ . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] <EOL> test_module = __import__ ( '<STR_LIT:.>' . join ( app_path + [ TEST_MODULE ] ) , { } , { } , TEST_MODULE ) <EOL> except ImportError , e : <EOL> import os . path <EOL> from imp import find_module <EOL> try : <EOL> mod = find_module ( TEST_MODULE , [ os . path . dirname ( app_module . __file__ ) ] ) <EOL> except ImportError : <EOL> test_module = None <EOL> else : <EOL> if mod [ <NUM_LIT:0> ] : <EOL> mod [ <NUM_LIT:0> ] . close ( ) <EOL> raise <EOL> return test_module <EOL> def build_suite ( app_module ) : <EOL> "<STR_LIT>" <EOL> suite = unittest . TestSuite ( ) <EOL> if hasattr ( app_module , '<STR_LIT>' ) : <EOL> suite . addTest ( app_module . suite ( ) ) <EOL> else : <EOL> suite . addTest ( unittest . defaultTestLoader . loadTestsFromModule ( app_module ) ) <EOL> try : <EOL> suite . addTest ( doctest . DocTestSuite ( app_module , <EOL> checker = doctestOutputChecker , <EOL> runner = DocTestRunner ) ) <EOL> except ValueError : <EOL> pass <EOL> test_module = get_tests ( app_module ) <EOL> if test_module : <EOL> if hasattr ( test_module , '<STR_LIT>' ) : <EOL> suite . addTest ( test_module . suite ( ) ) <EOL> else : <EOL> suite . addTest ( unittest . defaultTestLoader . loadTestsFromModule ( test_module ) ) <EOL> try : <EOL> suite . addTest ( doctest . DocTestSuite ( test_module , <EOL> checker = doctestOutputChecker , <EOL> runner = DocTestRunner ) ) <EOL> except ValueError : <EOL> pass <EOL> return suite <EOL> def build_test ( label ) : <EOL> """<STR_LIT>""" <EOL> parts = label . split ( '<STR_LIT:.>' ) <EOL> if len ( parts ) < <NUM_LIT:2> or len ( parts ) > <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" % label ) <EOL> app_module = get_app ( parts [ <NUM_LIT:0> ] ) <EOL> test_module = get_tests ( app_module ) <EOL> TestClass = getattr ( app_module , parts [ <NUM_LIT:1> ] , None ) <EOL> if TestClass is None : <EOL> if test_module : <EOL> TestClass = getattr ( test_module , parts [ <NUM_LIT:1> ] , None ) <EOL> try : <EOL> if issubclass ( TestClass , ( unittest . TestCase , real_unittest . TestCase ) ) : <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> try : <EOL> return unittest . TestLoader ( ) . loadTestsFromTestCase ( TestClass ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" % label ) <EOL> else : <EOL> return TestClass ( parts [ <NUM_LIT:2> ] ) <EOL> except TypeError : <EOL> pass <EOL> tests = [ ] <EOL> for module in app_module , test_module : <EOL> try : <EOL> doctests = doctest . DocTestSuite ( module , <EOL> checker = doctestOutputChecker , <EOL> runner = DocTestRunner ) <EOL> for test in doctests : <EOL> if test . _dt_test . name in ( <EOL> '<STR_LIT>' % ( module . __name__ , '<STR_LIT:.>' . join ( parts [ <NUM_LIT:1> : ] ) ) , <EOL> '<STR_LIT>' % ( module . __name__ , '<STR_LIT:.>' . join ( parts [ <NUM_LIT:1> : ] ) ) ) : <EOL> tests . append ( test ) <EOL> except ValueError : <EOL> pass <EOL> if not tests : <EOL> raise ValueError ( "<STR_LIT>" % label ) <EOL> return unittest . TestSuite ( tests ) <EOL> def partition_suite ( suite , classes , bins ) : <EOL> """<STR_LIT>""" <EOL> for test in suite : <EOL> if isinstance ( test , unittest . TestSuite ) : <EOL> partition_suite ( test , classes , bins ) <EOL> else : <EOL> for i in range ( len ( classes ) ) : <EOL> if isinstance ( test , classes [ i ] ) : <EOL> bins [ i ] . addTest ( test ) <EOL> break <EOL> else : <EOL> bins [ - <NUM_LIT:1> ] . addTest ( test ) <EOL> def reorder_suite ( suite , classes ) : <EOL> """<STR_LIT>""" <EOL> class_count = len ( classes ) <EOL> bins = [ unittest . TestSuite ( ) for i in range ( class_count + <NUM_LIT:1> ) ] <EOL> partition_suite ( suite , classes , bins ) <EOL> for i in range ( class_count ) : <EOL> bins [ <NUM_LIT:0> ] . addTests ( bins [ i + <NUM_LIT:1> ] ) <EOL> return bins [ <NUM_LIT:0> ] <EOL> def dependency_ordered ( test_databases , dependencies ) : <EOL> """<STR_LIT>""" <EOL> ordered_test_databases = [ ] <EOL> resolved_databases = set ( ) <EOL> while test_databases : <EOL> changed = False <EOL> deferred = [ ] <EOL> while test_databases : <EOL> signature , ( db_name , aliases ) = test_databases . pop ( ) <EOL> dependencies_satisfied = True <EOL> for alias in aliases : <EOL> if alias in dependencies : <EOL> if all ( a in resolved_databases for a in dependencies [ alias ] ) : <EOL> dependencies . pop ( alias ) <EOL> resolved_databases . add ( alias ) <EOL> else : <EOL> dependencies_satisfied = False <EOL> else : <EOL> resolved_databases . add ( alias ) <EOL> if dependencies_satisfied : <EOL> ordered_test_databases . append ( ( signature , ( db_name , aliases ) ) ) <EOL> changed = True <EOL> else : <EOL> deferred . append ( ( signature , ( db_name , aliases ) ) ) <EOL> if not changed : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> test_databases = deferred <EOL> return ordered_test_databases <EOL> class DjangoTestSuiteRunner ( object ) : <EOL> def __init__ ( self , verbosity = <NUM_LIT:1> , interactive = True , failfast = True , ** kwargs ) : <EOL> self . verbosity = verbosity <EOL> self . interactive = interactive <EOL> self . failfast = failfast <EOL> def setup_test_environment ( self , ** kwargs ) : <EOL> setup_test_environment ( ) <EOL> settings . DEBUG = False <EOL> unittest . installHandler ( ) <EOL> def build_suite ( self , test_labels , extra_tests = None , ** kwargs ) : <EOL> suite = unittest . TestSuite ( ) <EOL> if test_labels : <EOL> for label in test_labels : <EOL> if '<STR_LIT:.>' in label : <EOL> suite . addTest ( build_test ( label ) ) <EOL> else : <EOL> app = get_app ( label ) <EOL> suite . addTest ( build_suite ( app ) ) <EOL> else : <EOL> for app in get_apps ( ) : <EOL> suite . addTest ( build_suite ( app ) ) <EOL> if extra_tests : <EOL> for test in extra_tests : <EOL> suite . addTest ( test ) <EOL> return reorder_suite ( suite , ( TestCase , ) ) <EOL> def setup_databases ( self , ** kwargs ) : <EOL> from django . db import connections , DEFAULT_DB_ALIAS <EOL> mirrored_aliases = { } <EOL> test_databases = { } <EOL> dependencies = { } <EOL> for alias in connections : <EOL> connection = connections [ alias ] <EOL> if connection . settings_dict [ '<STR_LIT>' ] : <EOL> mirrored_aliases [ alias ] = connection . settings_dict [ '<STR_LIT>' ] <EOL> else : <EOL> item = test_databases . setdefault ( <EOL> connection . creation . test_db_signature ( ) , <EOL> ( connection . settings_dict [ '<STR_LIT>' ] , [ ] ) <EOL> ) <EOL> item [ <NUM_LIT:1> ] . append ( alias ) <EOL> if '<STR_LIT>' in connection . settings_dict : <EOL> dependencies [ alias ] = connection . settings_dict [ '<STR_LIT>' ] <EOL> else : <EOL> if alias != DEFAULT_DB_ALIAS : <EOL> dependencies [ alias ] = connection . settings_dict . get ( '<STR_LIT>' , [ DEFAULT_DB_ALIAS ] ) <EOL> old_names = [ ] <EOL> mirrors = [ ] <EOL> for signature , ( db_name , aliases ) in dependency_ordered ( test_databases . items ( ) , dependencies ) : <EOL> connection = connections [ aliases [ <NUM_LIT:0> ] ] <EOL> old_names . append ( ( connection , db_name , True ) ) <EOL> test_db_name = connection . creation . create_test_db ( self . verbosity , autoclobber = not self . interactive ) <EOL> for alias in aliases [ <NUM_LIT:1> : ] : <EOL> connection = connections [ alias ] <EOL> if db_name : <EOL> old_names . append ( ( connection , db_name , False ) ) <EOL> connection . settings_dict [ '<STR_LIT>' ] = test_db_name <EOL> else : <EOL> old_names . append ( ( connection , db_name , True ) ) <EOL> connection . creation . create_test_db ( self . verbosity , autoclobber = not self . interactive ) <EOL> for alias , mirror_alias in mirrored_aliases . items ( ) : <EOL> mirrors . append ( ( alias , connections [ alias ] . settings_dict [ '<STR_LIT>' ] ) ) <EOL> connections [ alias ] . settings_dict [ '<STR_LIT>' ] = connections [ mirror_alias ] . settings_dict [ '<STR_LIT>' ] <EOL> return old_names , mirrors <EOL> def run_suite ( self , suite , ** kwargs ) : <EOL> return unittest . TextTestRunner ( verbosity = self . verbosity , failfast = self . failfast ) . run ( suite ) <EOL> def teardown_databases ( self , old_config , ** kwargs ) : <EOL> from django . db import connections <EOL> old_names , mirrors = old_config <EOL> for alias , old_name in mirrors : <EOL> connections [ alias ] . settings_dict [ '<STR_LIT>' ] = old_name <EOL> for connection , old_name , destroy in old_names : <EOL> if destroy : <EOL> connection . creation . destroy_test_db ( old_name , self . verbosity ) <EOL> else : <EOL> connection . settings_dict [ '<STR_LIT>' ] = old_name <EOL> def teardown_test_environment ( self , ** kwargs ) : <EOL> unittest . removeHandler ( ) <EOL> teardown_test_environment ( ) <EOL> def suite_result ( self , suite , result , ** kwargs ) : <EOL> return len ( result . failures ) + len ( result . errors ) <EOL> def run_tests ( self , test_labels , extra_tests = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . setup_test_environment ( ) <EOL> suite = self . build_suite ( test_labels , extra_tests ) <EOL> old_config = self . setup_databases ( ) <EOL> result = self . run_suite ( suite ) <EOL> self . teardown_databases ( old_config ) <EOL> self . teardown_test_environment ( ) <EOL> return self . suite_result ( suite , result ) <EOL> def run_tests ( test_labels , verbosity = <NUM_LIT:1> , interactive = True , failfast = False , extra_tests = None ) : <EOL> import warnings <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> DeprecationWarning <EOL> ) <EOL> test_runner = DjangoTestSuiteRunner ( verbosity = verbosity , interactive = interactive , failfast = failfast ) <EOL> return test_runner . run_tests ( test_labels , extra_tests = extra_tests ) </s>
