<s> import sys , time <EOL> from django . conf import settings <EOL> from django . db import connection , transaction , backend <EOL> from django . core import management <EOL> from django . dispatch import dispatcher <EOL> from django . test import signals <EOL> from django . template import Template <EOL> TEST_DATABASE_PREFIX = '<STR_LIT>' <EOL> def instrumented_test_render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> dispatcher . send ( signal = signals . template_rendered , sender = self , template = self , context = context ) <EOL> return self . nodelist . render ( context ) <EOL> def setup_test_environment ( ) : <EOL> """<STR_LIT>""" <EOL> Template . original_render = Template . render <EOL> Template . render = instrumented_test_render <EOL> def teardown_test_environment ( ) : <EOL> """<STR_LIT>""" <EOL> Template . render = Template . original_render <EOL> del Template . original_render <EOL> def _set_autocommit ( connection ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( connection . connection , "<STR_LIT>" ) : <EOL> connection . connection . autocommit ( True ) <EOL> elif hasattr ( connection . connection , "<STR_LIT>" ) : <EOL> connection . connection . set_isolation_level ( <NUM_LIT:0> ) <EOL> def create_test_db ( verbosity = <NUM_LIT:1> , autoclobber = False ) : <EOL> if verbosity >= <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> if settings . DATABASE_ENGINE == "<STR_LIT>" : <EOL> TEST_DATABASE_NAME = "<STR_LIT>" <EOL> else : <EOL> if settings . TEST_DATABASE_NAME : <EOL> TEST_DATABASE_NAME = settings . TEST_DATABASE_NAME <EOL> else : <EOL> TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings . DATABASE_NAME <EOL> cursor = connection . cursor ( ) <EOL> _set_autocommit ( connection ) <EOL> try : <EOL> cursor . execute ( "<STR_LIT>" % backend . quote_name ( TEST_DATABASE_NAME ) ) <EOL> except Exception , e : <EOL> sys . stderr . write ( "<STR_LIT>" % e ) <EOL> if not autoclobber : <EOL> confirm = raw_input ( "<STR_LIT>" % TEST_DATABASE_NAME ) <EOL> if autoclobber or confirm == '<STR_LIT:yes>' : <EOL> try : <EOL> if verbosity >= <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> cursor . execute ( "<STR_LIT>" % backend . quote_name ( TEST_DATABASE_NAME ) ) <EOL> if verbosity >= <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> cursor . execute ( "<STR_LIT>" % backend . quote_name ( TEST_DATABASE_NAME ) ) <EOL> except Exception , e : <EOL> sys . stderr . write ( "<STR_LIT>" % e ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> connection . close ( ) <EOL> settings . DATABASE_NAME = TEST_DATABASE_NAME <EOL> management . syncdb ( verbosity , interactive = False ) <EOL> cursor = connection . cursor ( ) <EOL> def destroy_test_db ( old_database_name , verbosity = <NUM_LIT:1> ) : <EOL> if verbosity >= <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> connection . close ( ) <EOL> TEST_DATABASE_NAME = settings . DATABASE_NAME <EOL> settings . DATABASE_NAME = old_database_name <EOL> if settings . DATABASE_ENGINE != "<STR_LIT>" : <EOL> cursor = connection . cursor ( ) <EOL> _set_autocommit ( connection ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> cursor . execute ( "<STR_LIT>" % backend . quote_name ( TEST_DATABASE_NAME ) ) <EOL> connection . close ( ) </s>
