<s> import re <EOL> import unittest <EOL> from urlparse import urlsplit , urlunsplit <EOL> from xml . dom . minidom import parseString , Node <EOL> from django . conf import settings <EOL> from django . core import mail <EOL> from django . core . management import call_command <EOL> from django . core . urlresolvers import clear_url_caches <EOL> from django . db import transaction , connection <EOL> from django . http import QueryDict <EOL> from django . test import _doctest as doctest <EOL> from django . test . client import Client <EOL> from django . utils import simplejson <EOL> from django . utils . encoding import smart_str <EOL> normalize_long_ints = lambda s : re . sub ( r'<STR_LIT>' , '<STR_LIT>' , s ) <EOL> normalize_decimals = lambda s : re . sub ( r"<STR_LIT>" , lambda m : "<STR_LIT>" % m . groups ( ) [ <NUM_LIT:0> ] , s ) <EOL> def to_list ( value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> value = [ ] <EOL> elif not isinstance ( value , list ) : <EOL> value = [ value ] <EOL> return value <EOL> real_commit = transaction . commit <EOL> real_rollback = transaction . rollback <EOL> real_enter_transaction_management = transaction . enter_transaction_management <EOL> real_leave_transaction_management = transaction . leave_transaction_management <EOL> real_savepoint_commit = transaction . savepoint_commit <EOL> real_savepoint_rollback = transaction . savepoint_rollback <EOL> real_managed = transaction . managed <EOL> def nop ( * args , ** kwargs ) : <EOL> return <EOL> def disable_transaction_methods ( ) : <EOL> transaction . commit = nop <EOL> transaction . rollback = nop <EOL> transaction . savepoint_commit = nop <EOL> transaction . savepoint_rollback = nop <EOL> transaction . enter_transaction_management = nop <EOL> transaction . leave_transaction_management = nop <EOL> transaction . managed = nop <EOL> def restore_transaction_methods ( ) : <EOL> transaction . commit = real_commit <EOL> transaction . rollback = real_rollback <EOL> transaction . savepoint_commit = real_savepoint_commit <EOL> transaction . savepoint_rollback = real_savepoint_rollback <EOL> transaction . enter_transaction_management = real_enter_transaction_management <EOL> transaction . leave_transaction_management = real_leave_transaction_management <EOL> transaction . managed = real_managed <EOL> class OutputChecker ( doctest . OutputChecker ) : <EOL> def check_output ( self , want , got , optionflags ) : <EOL> "<STR_LIT>" <EOL> checks = ( self . check_output_default , <EOL> self . check_output_numeric , <EOL> self . check_output_xml , <EOL> self . check_output_json ) <EOL> for check in checks : <EOL> if check ( want , got , optionflags ) : <EOL> return True <EOL> return False <EOL> def check_output_default ( self , want , got , optionflags ) : <EOL> "<STR_LIT>" <EOL> return doctest . OutputChecker . check_output ( self , want , got , optionflags ) <EOL> def check_output_numeric ( self , want , got , optionflags ) : <EOL> """<STR_LIT>""" <EOL> return doctest . OutputChecker . check_output ( self , <EOL> normalize_decimals ( normalize_long_ints ( want ) ) , <EOL> normalize_decimals ( normalize_long_ints ( got ) ) , <EOL> optionflags ) <EOL> def check_output_xml ( self , want , got , optionsflags ) : <EOL> """<STR_LIT>""" <EOL> _norm_whitespace_re = re . compile ( r'<STR_LIT>' ) <EOL> def norm_whitespace ( v ) : <EOL> return _norm_whitespace_re . sub ( '<STR_LIT:U+0020>' , v ) <EOL> def child_text ( element ) : <EOL> return '<STR_LIT>' . join ( [ c . data for c in element . childNodes <EOL> if c . nodeType == Node . TEXT_NODE ] ) <EOL> def children ( element ) : <EOL> return [ c for c in element . childNodes <EOL> if c . nodeType == Node . ELEMENT_NODE ] <EOL> def norm_child_text ( element ) : <EOL> return norm_whitespace ( child_text ( element ) ) <EOL> def attrs_dict ( element ) : <EOL> return dict ( element . attributes . items ( ) ) <EOL> def check_element ( want_element , got_element ) : <EOL> if want_element . tagName != got_element . tagName : <EOL> return False <EOL> if norm_child_text ( want_element ) != norm_child_text ( got_element ) : <EOL> return False <EOL> if attrs_dict ( want_element ) != attrs_dict ( got_element ) : <EOL> return False <EOL> want_children = children ( want_element ) <EOL> got_children = children ( got_element ) <EOL> if len ( want_children ) != len ( got_children ) : <EOL> return False <EOL> for want , got in zip ( want_children , got_children ) : <EOL> if not check_element ( want , got ) : <EOL> return False <EOL> return True <EOL> want , got = self . _strip_quotes ( want , got ) <EOL> want = want . replace ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> got = got . replace ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> if not want . startswith ( '<STR_LIT>' ) : <EOL> wrapper = '<STR_LIT>' <EOL> want = wrapper % want <EOL> got = wrapper % got <EOL> try : <EOL> want_root = parseString ( want ) . firstChild <EOL> got_root = parseString ( got ) . firstChild <EOL> except : <EOL> return False <EOL> return check_element ( want_root , got_root ) <EOL> def check_output_json ( self , want , got , optionsflags ) : <EOL> "<STR_LIT>" <EOL> want , got = self . _strip_quotes ( want , got ) <EOL> try : <EOL> want_json = simplejson . loads ( want ) <EOL> got_json = simplejson . loads ( got ) <EOL> except : <EOL> return False <EOL> return want_json == got_json <EOL> def _strip_quotes ( self , want , got ) : <EOL> """<STR_LIT>""" <EOL> def is_quoted_string ( s ) : <EOL> s = s . strip ( ) <EOL> return ( len ( s ) >= <NUM_LIT:2> <EOL> and s [ <NUM_LIT:0> ] == s [ - <NUM_LIT:1> ] <EOL> and s [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) <EOL> def is_quoted_unicode ( s ) : <EOL> s = s . strip ( ) <EOL> return ( len ( s ) >= <NUM_LIT:3> <EOL> and s [ <NUM_LIT:0> ] == '<STR_LIT:u>' <EOL> and s [ <NUM_LIT:1> ] == s [ - <NUM_LIT:1> ] <EOL> and s [ <NUM_LIT:1> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) <EOL> if is_quoted_string ( want ) and is_quoted_string ( got ) : <EOL> want = want . strip ( ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> got = got . strip ( ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> elif is_quoted_unicode ( want ) and is_quoted_unicode ( got ) : <EOL> want = want . strip ( ) [ <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> got = got . strip ( ) [ <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> return want , got <EOL> class DocTestRunner ( doctest . DocTestRunner ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> doctest . DocTestRunner . __init__ ( self , * args , ** kwargs ) <EOL> self . optionflags = doctest . ELLIPSIS <EOL> def report_unexpected_exception ( self , out , test , example , exc_info ) : <EOL> doctest . DocTestRunner . report_unexpected_exception ( self , out , test , <EOL> example , exc_info ) <EOL> transaction . rollback_unless_managed ( ) <EOL> class TransactionTestCase ( unittest . TestCase ) : <EOL> def _pre_setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fixture_setup ( ) <EOL> self . _urlconf_setup ( ) <EOL> mail . outbox = [ ] <EOL> def _fixture_setup ( self ) : <EOL> call_command ( '<STR_LIT>' , verbosity = <NUM_LIT:0> , interactive = False ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> call_command ( '<STR_LIT>' , * self . fixtures , ** { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def _urlconf_setup ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . _old_root_urlconf = settings . ROOT_URLCONF <EOL> settings . ROOT_URLCONF = self . urls <EOL> clear_url_caches ( ) <EOL> def __call__ ( self , result = None ) : <EOL> """<STR_LIT>""" <EOL> self . client = Client ( ) <EOL> try : <EOL> self . _pre_setup ( ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except Exception : <EOL> import sys <EOL> result . addError ( self , sys . exc_info ( ) ) <EOL> return <EOL> super ( TransactionTestCase , self ) . __call__ ( result ) <EOL> try : <EOL> self . _post_teardown ( ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except Exception : <EOL> import sys <EOL> result . addError ( self , sys . exc_info ( ) ) <EOL> return <EOL> def _post_teardown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _fixture_teardown ( ) <EOL> self . _urlconf_teardown ( ) <EOL> def _fixture_teardown ( self ) : <EOL> pass <EOL> def _urlconf_teardown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> settings . ROOT_URLCONF = self . _old_root_urlconf <EOL> clear_url_caches ( ) <EOL> def assertRedirects ( self , response , expected_url , status_code = <NUM_LIT> , <EOL> target_status_code = <NUM_LIT:200> , host = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( response , '<STR_LIT>' ) : <EOL> self . failUnless ( len ( response . redirect_chain ) > <NUM_LIT:0> , <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( response . status_code , status_code ) ) ) <EOL> self . assertEqual ( response . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , status_code , <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( response . redirect_chain [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , status_code ) ) ) <EOL> url , status_code = response . redirect_chain [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( response . status_code , target_status_code , <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( response . status_code , target_status_code ) ) ) <EOL> else : <EOL> self . assertEqual ( response . status_code , status_code , <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( response . status_code , status_code ) ) ) <EOL> url = response [ '<STR_LIT>' ] <EOL> scheme , netloc , path , query , fragment = urlsplit ( url ) <EOL> redirect_response = response . client . get ( path , QueryDict ( query ) ) <EOL> self . assertEqual ( redirect_response . status_code , target_status_code , <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> ( path , redirect_response . status_code , target_status_code ) ) <EOL> e_scheme , e_netloc , e_path , e_query , e_fragment = urlsplit ( expected_url ) <EOL> if not ( e_scheme or e_netloc ) : <EOL> expected_url = urlunsplit ( ( '<STR_LIT:http>' , host or '<STR_LIT>' , e_path , <EOL> e_query , e_fragment ) ) <EOL> self . assertEqual ( url , expected_url , <EOL> "<STR_LIT>" % ( url , expected_url ) ) <EOL> def assertContains ( self , response , text , count = None , status_code = <NUM_LIT:200> ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( response . status_code , status_code , <EOL> "<STR_LIT>" % <EOL> ( response . status_code , status_code ) ) <EOL> text = smart_str ( text , response . _charset ) <EOL> real_count = response . content . count ( text ) <EOL> if count is not None : <EOL> self . assertEqual ( real_count , count , <EOL> "<STR_LIT>" % <EOL> ( real_count , text , count ) ) <EOL> else : <EOL> self . failUnless ( real_count != <NUM_LIT:0> , <EOL> "<STR_LIT>" % text ) <EOL> def assertNotContains ( self , response , text , status_code = <NUM_LIT:200> ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( response . status_code , status_code , <EOL> "<STR_LIT>" % <EOL> ( response . status_code , status_code ) ) <EOL> text = smart_str ( text , response . _charset ) <EOL> self . assertEqual ( response . content . count ( text ) , <EOL> <NUM_LIT:0> , "<STR_LIT>" % text ) <EOL> def assertFormError ( self , response , form , field , errors ) : <EOL> """<STR_LIT>""" <EOL> contexts = to_list ( response . context ) <EOL> if not contexts : <EOL> self . fail ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> errors = to_list ( errors ) <EOL> found_form = False <EOL> for i , context in enumerate ( contexts ) : <EOL> if form not in context : <EOL> continue <EOL> found_form = True <EOL> for err in errors : <EOL> if field : <EOL> if field in context [ form ] . errors : <EOL> field_errors = context [ form ] . errors [ field ] <EOL> self . failUnless ( err in field_errors , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( field , form , i , err , <EOL> repr ( field_errors ) ) ) <EOL> elif field in context [ form ] . fields : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( field , form , i ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( form , i , field ) ) <EOL> else : <EOL> non_field_errors = context [ form ] . non_field_errors ( ) <EOL> self . failUnless ( err in non_field_errors , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( form , i , err , non_field_errors ) ) <EOL> if not found_form : <EOL> self . fail ( "<STR_LIT>" % <EOL> form ) <EOL> def assertTemplateUsed ( self , response , template_name ) : <EOL> """<STR_LIT>""" <EOL> template_names = [ t . name for t in to_list ( response . template ) ] <EOL> if not template_names : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . failUnless ( template_name in template_names , <EOL> ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) % ( template_name , <EOL> u'<STR_LIT:U+002CU+0020>' . join ( template_names ) ) ) <EOL> def assertTemplateNotUsed ( self , response , template_name ) : <EOL> """<STR_LIT>""" <EOL> template_names = [ t . name for t in to_list ( response . template ) ] <EOL> self . failIf ( template_name in template_names , <EOL> ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) % template_name ) <EOL> class TestCase ( TransactionTestCase ) : <EOL> """<STR_LIT>""" <EOL> def _fixture_setup ( self ) : <EOL> if not settings . DATABASE_SUPPORTS_TRANSACTIONS : <EOL> return super ( TestCase , self ) . _fixture_setup ( ) <EOL> transaction . enter_transaction_management ( ) <EOL> transaction . managed ( True ) <EOL> disable_transaction_methods ( ) <EOL> from django . contrib . sites . models import Site <EOL> Site . objects . clear_cache ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> call_command ( '<STR_LIT>' , * self . fixtures , ** { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : False <EOL> } ) <EOL> def _fixture_teardown ( self ) : <EOL> if not settings . DATABASE_SUPPORTS_TRANSACTIONS : <EOL> return super ( TestCase , self ) . _fixture_teardown ( ) <EOL> restore_transaction_methods ( ) <EOL> transaction . rollback ( ) <EOL> transaction . leave_transaction_management ( ) <EOL> connection . close ( ) </s>
