
TQc           @  s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z	 y d  d l
 m Z m Z Wn' e k
 r d  d l m Z m Z n Xd  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z m Z d  d
 l m Z d  d l m Z d  d l  m! Z! d  d l" m# Z# d  d l$ m% Z% m& Z& m' Z' d  d l( m) Z) d  d l* m+ Z+ d  d l, m- Z- m. Z. m/ Z/ m0 Z0 m1 Z1 d  d l2 m3 Z3 d  d l4 m5 Z5 d  d l6 m7 Z8 d  d l9 m: Z: d  d l; m< Z< m= Z= d  d l> m? Z? d  d l@ mA ZA mB ZB mC ZC mD ZD mE ZE d  d l@ mF ZF d  d lG mH ZI d  d lJ mK ZK d  d lG mL ZL d  d lM mN ZN d  d lO mP ZP d  d lQ mR ZR d  d! d" d# d$ d% d& f ZS d'   ZT d(   ZU d)   ZV e- jW ZX e- jY ZZ e- j[ Z\ e- j] Z^ e- j_ Z` e- ja Zb d*   Zc d+   Zd d,   Ze d-   Zf d. e8 jg f d/     YZg d0 e8 jh f d1     YZh d2 ei f d3     YZj d4 ei f d5     YZk d6 ek f d7     YZl d8 eI jm f d9     YZn d: en f d;     YZo d<   Zp d= eo f d>     YZm d?   Zq d@   Zr dA   Zs dB e% f dC     YZt e ju dD dD dE f k roe jv Zw n= e ju dF dG dE f k re jx Zw n dH e jx f dI     YZw dJ e& f dK     YZy dL e f dM     YZz dN e j{ f dO     YZ| dP eo f dQ     YZ} d S(R   i(   t   unicode_literalsN(   t   copy(   t   wraps(   t   urlsplitt
   urlunsplit(   t   settings(   t   StaticFilesHandler(   t   mail(   t   ValidationErrort   ImproperlyConfigured(   t   WSGIHandler(   t   call_command(   t   no_style(   t   request_started(   t   WSGIRequestHandlert
   WSGIServert   WSGIServerException(   t   clear_url_caches(   t   EMPTY_VALUES(   t   transactiont
   connectiont   connectionst   DEFAULT_DB_ALIASt   reset_queries(   t	   CharField(   t	   QueryDict(   t   _doctest(   t   Client(   t   HTMLParseErrort
   parse_html(   t   template_rendered(   t   get_warnings_statet   restore_warnings_statet   override_settingst   compare_xmlt   strip_quotes(   t   ContextList(   t   unittest(   t
   force_text(   t   six(   t	   safe_repr(   t   skipIf(   t   serveu   DocTestRunneru   OutputCheckeru   TestCaseu   TransactionTestCaseu   SimpleTestCaseu   skipIfDBFeatureu   skipUnlessDBFeaturec         C  s   t  j d d |   S(   Nu   (?<![\w])(\d+)L(?![\w])u   \1(   t   ret   sub(   t   s(    (    s.   ../Django//lib/python/django/test/testcases.pyt   <lambda>4   s    c         C  s   t  j d d   |   S(   Nu   Decimal\('(\d+(\.\d*)?)'\)c         S  s   d |  j    d S(   Nu   Decimal("%s")i    (   t   groups(   t   m(    (    s.   ../Django//lib/python/django/test/testcases.pyR.   6   s    (   R+   R,   (   R-   (    (    s.   ../Django//lib/python/django/test/testcases.pyR.   5   s   	c         C  s4   |  d k r g  }  n t |  t  s0 |  g }  n  |  S(   ue   
    Puts value into a list if it's not already one.
    Returns an empty list if value is None.
    N(   t   Nonet
   isinstancet   list(   t   value(    (    s.   ../Django//lib/python/django/test/testcases.pyt   to_list9   s
    	c          O  s   d  S(   N(    (   t   argst   kwargs(    (    s.   ../Django//lib/python/django/test/testcases.pyt   nopK   s    c           C  s:   t  t _ t  t _ t  t _ t  t _ t  t _ t  t _ d  S(   N(   R8   R   t   committ   rollbackt   enter_transaction_managementt   leave_transaction_managementt   managedt   abort(    (    (    s.   ../Django//lib/python/django/test/testcases.pyt   disable_transaction_methodsN   s    					c           C  s:   t  t _ t t _ t t _ t t _ t	 t _
 t t _ d  S(   N(   t   real_commitR   R9   t   real_rollbackR:   t!   real_enter_transaction_managementR;   t!   real_leave_transaction_managementR<   t   real_managedR=   t
   real_abortR>   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyt   restore_transaction_methodsV   s    					c         C  sV   y t  |  } Wn? t k
 rQ } d | | j f } |  j |  j | |   n X| S(   Nu   %s
%s(   R   R   t   msgt   failt   _formatMessage(   t   selft   htmlt   user_msgRG   t   domt   et   standardMsg(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assert_and_parse_html_   s    t   OutputCheckerc           B  s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C  sI   |  j  |  j |  j |  j f } x$ | D] } | | | |  r% t Sq% Wt S(   un   
        The entry method for doctest output checking. Defers to a sequence of
        child checkers
        (   t   check_output_defaultt   check_output_numerict   check_output_xmlt   check_output_jsont   Truet   False(   RJ   t   wantt   gott   optionflagst   checkst   check(    (    s.   ../Django//lib/python/django/test/testcases.pyt   check_outputi   s    c         C  s   t  j j |  | | |  S(   un   
        The default comparator provided by doctest - not perfect, but good for
        most purposes
        (   t   doctestRQ   R]   (   RJ   RX   RY   RZ   (    (    s.   ../Django//lib/python/django/test/testcases.pyRR   w   s    c         C  s1   t  j j |  t t |   t t |   |  S(   uX  Doctest does an exact string comparison of output, which means that
        some numerically equivalent values aren't equal. This check normalizes
         * long integers (22L) so that they equal normal integers. (22)
         * Decimals so that they are comparable, regardless of the change
           made to __repr__ in Python 2.6.
        (   R^   RQ   R]   t   normalize_decimalst   normalize_long_ints(   RJ   RX   RY   RZ   (    (    s.   ../Django//lib/python/django/test/testcases.pyRS   ~   s    c         C  s*   y t  | |  SWn t k
 r% t SXd  S(   N(   R"   t	   ExceptionRW   (   RJ   RX   RY   t   optionsflags(    (    s.   ../Django//lib/python/django/test/testcases.pyRT      s    c         C  sV   t  | |  \ } } y" t j |  } t j |  } Wn t k
 rK t SX| | k S(   uQ   
        Tries to compare want and got as if they were JSON-encoded data
        (   R#   t   jsont   loadsRa   RW   (   RJ   RX   RY   Rb   t	   want_jsont   got_json(    (    s.   ../Django//lib/python/django/test/testcases.pyRU      s    (   t   __name__t
   __module__R]   RR   RS   RT   RU   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyRQ   h   s
   				t   DocTestRunnerc           B  s   e  Z d    Z d   Z RS(   c         O  s&   t  j j |  | |  t  j |  _ d  S(   N(   R^   Ri   t   __init__t   ELLIPSISRZ   (   RJ   R6   R7   (    (    s.   ../Django//lib/python/django/test/testcases.pyRj      s    c         C  sA   t  j j |  | | | |  x t D] } t j d |  q# Wd  S(   Nt   using(   R^   Ri   t   report_unexpected_exceptionR   R   t   rollback_unless_managed(   RJ   t   outt   testt   examplet   exc_infot   conn(    (    s.   ../Django//lib/python/django/test/testcases.pyRm      s    
(   Rg   Rh   Rj   Rm   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyRi      s   	t   _AssertNumQueriesContextc           B  s#   e  Z d    Z d   Z d   Z RS(   c         C  s   | |  _  | |  _ | |  _ d  S(   N(   t	   test_caset   numR   (   RJ   Ru   Rv   R   (    (    s.   ../Django//lib/python/django/test/testcases.pyRj      s    		c         C  sA   |  j  j |  _ t |  j  _ t |  j  j  |  _ t j t	  |  S(   N(
   R   t   use_debug_cursort   old_debug_cursorRV   t   lent   queriest   starting_queriesR   t
   disconnectR   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt	   __enter__   s
    c         C  su   |  j  |  j _ t j t  | d  k	 r, d  St |  j j  } | |  j	 } |  j
 j | |  j d | |  j f  d  S(   Nu    %d queries executed, %d expected(   Rx   R   Rw   R   t   connectR   R1   Ry   Rz   R{   Ru   t   assertEqualRv   (   RJ   t   exc_typet	   exc_valuet	   tracebackt   final_queriest   executed(    (    s.   ../Django//lib/python/django/test/testcases.pyt   __exit__   s    	(   Rg   Rh   Rj   R}   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyRt      s   		t   _AssertTemplateUsedContextc           B  s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C  s4   | |  _  | |  _ g  |  _ g  |  _ t   |  _ d  S(   N(   Ru   t   template_namet   rendered_templatest   rendered_template_namesR$   t   context(   RJ   Ru   R   (    (    s.   ../Django//lib/python/django/test/testcases.pyRj      s
    				c         K  s=   |  j  j |  |  j j | j  |  j j t |   d  S(   N(   R   t   appendR   t   nameR   R   (   RJ   t   sendert   signalt   templateR   R7   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   on_template_render   s    c         C  s   |  j  |  j k S(   N(   R   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyRp      s    c         C  s   d |  j  S(   Nu   %s was not rendered.(   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   message   s    c         C  s   t  j |  j  |  S(   N(   R   R~   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR}      s    c         C  s   t  j |  j  | d  k	 r  d  S|  j   s |  j   } t |  j  d k rZ | d 7} n | d d j |  j	  7} |  j
 j |  n  d  S(   Ni    u    No template was rendered.u&    Following templates were rendered: %su   , (   R   R|   R   R1   Rp   R   Ry   R   t   joinR   Ru   RH   (   RJ   R   R   R   R   (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s    (   Rg   Rh   Rj   R   Rp   R   R}   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s   					t   _AssertTemplateNotUsedContextc           B  s   e  Z d    Z d   Z RS(   c         C  s   |  j  |  j k S(   N(   R   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyRp      s    c         C  s   d |  j  S(   Nu   %s was rendered.(   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s    (   Rg   Rh   Rp   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s   	t   SimpleTestCasec           B  s   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z d d  Z	 d d d d  Z
 d d	  Z d d
  Z d d d  Z d d  Z d d  Z d d  Z RS(   c         C  s	  t  |  |  j  } t  |  j d t  p6 t  | d t  } | s y |  j   Wq t t f k
 ri   q t k
 r | j |  t	 j
    d SXn  t t |   j |  | sy |  j   Wqt t f k
 r   qt k
 r| j |  t	 j
    d SXn  d S(   u   
        Wrapper around default __call__ method to perform common Django test
        set up. This means that user-defined Test Cases aren't required to
        include a call to super().setUp().
        u   __unittest_skip__N(   t   getattrt   _testMethodNamet	   __class__RW   t
   _pre_setupt   KeyboardInterruptt
   SystemExitRa   t   addErrort   sysRr   t   superR   t   __call__t   _post_teardown(   RJ   t   resultt
   testMethodt   skipped(    (    s.   ../Django//lib/python/django/test/testcases.pyR      s(    c         C  s   d  S(   N(    (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    c         C  s   d  S(   N(    (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    c         C  s   t    |  _ d S(   u8   
        Saves the state of the warnings module
        N(   R   t   _warnings_state(   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   save_warnings_state  s    c         C  s   t  |  j  d S(   uo   
        Restores the state of the warnings module to the state
        saved by save_warnings_state()
        N(   R    R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s    c         K  s
   t  |   S(   u   
        A context manager that temporarily sets a setting and reverts
        back to the original value when exiting the context.
        (   R!   (   RJ   R7   (    (    s.   ../Django//lib/python/django/test/testcases.pyR   &  s    c         O  s%   t  j |  | t j |  | | |  S(   ul  
        Asserts that the message in a raised exception matches the passed
        value.

        Args:
            expected_exception: Exception class expected to be raised.
            expected_message: expected error message string value.
            callable_obj: Function to be called.
            args: Extra args.
            kwargs: Extra kwargs.
        (   R'   t   assertRaisesRegexR+   t   escape(   RJ   t   expected_exceptiont   expected_messaget   callable_objR6   R7   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertRaisesMessage-  s    u    c         C  s  | d k r g  } n  | d k r* i  } n  | | |   } | | t | d t   } xL | j   D]> \ }	 }
 |  j | j |	  |
  |  j | j |	  |
  qa Wx | j   D] \ }	 } |  j t   } | j |	  Wd QX|  j | j j	 |  |  j t   } | j |	  Wd QX|  j | j j	 |  q Wt
 | j d  g } xb t D]Z } |  j t   } | j |  Wd QX|  j | j j	 |  |  j | j |  |  qSWt | t  r| j i d d 6d d 6 |  j t | | |   |   n  d S(   uQ  
        Asserts that a form field behaves correctly with various inputs.

        Args:
            fieldclass: the class of the field to be tested.
            valid: a dictionary mapping valid inputs to their expected
                    cleaned values.
            invalid: a dictionary mapping invalid inputs to one or more
                    raised error messages.
            field_args: the args passed to instantiate the field
            field_kwargs: the kwargs passed to instantiate the field
            empty_value: the expected clean output for inputs in EMPTY_VALUES

        t   requiredNu   requiredi   u
   min_lengthi   u
   max_length(   R1   t   dictRW   t   itemsR   t   cleant   assertRaisesR   t	   exceptiont   messagesR&   t   error_messagesR   t
   issubclassR   t   updatet
   assertTrueR2   (   RJ   t
   fieldclasst   validt   invalidt
   field_argst   field_kwargst   empty_valueR   t   optionalt   inputt   outputt   errorst   context_managert   error_requiredRN   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertFieldOutput=  s8    		c         C  s   t  |  | | d  } t  |  | | d  } | | k r d t | t  t | t  f } d d j t j t j |  j   t j |  j     } |  j	 | |  } |  j
 |  j | |   n  d S(   u   
        Asserts that two HTML snippets are semantically the same.
        Whitespace in most cases is ignored, and attribute ordering is not
        significant. The passed-in arguments must be valid HTML.
        u!   First argument is not valid HTML:u"   Second argument is not valid HTML:u   %s != %su   
N(   RP   R(   RV   R   t   difflibt   ndiffR'   t	   text_typet
   splitlinest   _truncateMessageRH   RI   (   RJ   t   html1t   html2RG   t   dom1t   dom2RO   t   diff(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertHTMLEqualo  s    		c         C  sx   t  |  | | d  } t  |  | | d  } | | k rt d t | t  t | t  f } |  j |  j | |   n  d S(   u?   Asserts that two HTML snippets are not semantically equivalent.u!   First argument is not valid HTML:u"   Second argument is not valid HTML:u   %s == %sN(   RP   R(   RV   RH   RI   (   RJ   R   R   RG   R   R   RO   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertHTMLNotEqual  s    		c         C  s   t  |  | d  d  } t  |  | d  d  } | j |  } | d  k	 rl |  j | | | d | | | f  n |  j | d k | d |  d  S(   Nu!   First argument is not valid HTML:u"   Second argument is not valid HTML:u4   Found %d instances of '%s' in response (expected %d)i    u   Couldn't find '%s' in response(   RP   R1   t   countR   R   (   RJ   t   needlet   haystackR   t
   msg_prefixt
   real_count(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertInHTML  s    		c         C  s   y t  j |  } Wn" t k
 r7 |  j d |  n Xt | t j  r y t  j |  } Wq t k
 r |  j d |  q Xn  |  j | | d | d  S(   Nu$   First argument is not valid JSON: %ru%   Second argument is not valid JSON: %rRG   (   Rc   Rd   t
   ValueErrorRH   R2   R'   t   string_typesR   (   RJ   t   rawt   expected_dataRG   t   data(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertJSONEqual  s    c         C  s   y t  | |  } Wn6 t k
 rK } d | } |  j |  j | |   nE X| s d t | t  t | t  f } |  j |  j | |   n  d S(   u   
        Asserts that two XML snippets are semantically the same.
        Whitespace in most cases is ignored, and attribute ordering is not
        significant. The passed-in arguments must be valid XML.
        u,   First or second argument is not valid XML
%su   %s != %sN(   R"   Ra   RH   RI   R(   RV   (   RJ   t   xml1t   xml2RG   R   RN   RO   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertXMLEqual  s    
"c         C  s   y t  | |  } Wn6 t k
 rK } d | } |  j |  j | |   nE X| r d t | t  t | t  f } |  j |  j | |   n  d S(   u   
        Asserts that two XML snippets are not semantically equivalent.
        Whitespace in most cases is ignored, and attribute ordering is not
        significant. The passed-in arguments must be valid XML.
        u,   First or second argument is not valid XML
%su   %s == %sN(   R"   Ra   RH   RI   R(   RV   (   RJ   R   R   RG   R   RN   RO   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertXMLNotEqual  s    
"N(   Rg   Rh   R1   R   R   R   R   R    R   R   R   R   R   R   R   R   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR      s   					1t   TransactionTestCasec           B  s   e  Z e Z e Z d    Z e d  Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d d	 d d
 d  Z d d	 d
 e d  Z d	 d
 e d  Z d
 d  Z d d d
 d  Z d d d
 d  Z e e d  Z d d  Z RS(   c         C  s0   |  j    |  _ |  j   |  j   g  t _ d S(   u\  Performs any pre-test setup. This includes:

            * Flushing the database.
            * If the Test Case class has a 'fixtures' member, installing the
              named fixtures.
            * If the Test Case class has a 'urls' member, replace the
              ROOT_URLCONF with it.
            * Clearing the mail test outbox.
        N(   t   client_classt   clientt   _fixture_setupt   _urlconf_setupR   t   outbox(   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    


c         C  sL   t  |  d t  rA g  t D]$ } | s7 t | j d r | ^ q St g Sd  S(   Nu   multi_dbu   TEST_MIRROR(   R   RW   R   t   settings_dictR   (   RJ   t   include_mirrorst   alias(    (    s.   ../Django//lib/python/django/test/testcases.pyt   _databases_names  s    "c         C  s   t  | } | j j r | j j t   | j j    } | r y. | j   } x | D] } | j	 |  qS WWn$ t
 k
 r t j d |    n Xt j d |  q n  d  S(   NRl   (   R   t   featurest   supports_sequence_resett   opst   sequence_reset_by_name_sqlR   t   introspectiont   sequence_listt   cursort   executeRa   R   Rn   t   commit_unless_managed(   RJ   t   db_nameRs   t   sql_listR   t   sql(    (    s.   ../Django//lib/python/django/test/testcases.pyt   _reset_sequences  s    
c         C  st   xm |  j  d t  D]Y } |  j r2 |  j |  n  t |  d  r t d |  j i d d 6| d 6t d 6 q q Wd  S(   NR   u   fixturesu   loaddatai    u	   verbosityu   databaseu   skip_validation(   R   RW   t   reset_sequencesR   t   hasattrR   t   fixturesRV   (   RJ   R   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    	c         C  s5   t  |  d  r1 t j |  _ |  j t _ t   n  d  S(   Nu   urls(   R   R   t   ROOT_URLCONFt   _old_root_urlconft   urlsR   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    c         C  s9   |  j    |  j   x t j   D] } | j   q! Wd S(   u    Performs any post-test things. This includes:

            * Putting back the original ROOT_URLCONF if it was changed.
            * Force closing the connection, so that the next test gets
              a clean cursor.
        N(   t   _fixture_teardownt   _urlconf_teardownR   t   allt   close(   RJ   Rs   (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s    

c         C  sj   x t  j   D] } | j   q WxB |  j d t  D]. } t d d d d t d | d t d t q4 Wd  S(	   NR   u   flusht	   verbosityi    t   interactivet   databaset   skip_validationR   (   R   R   Rn   R   RW   R   RV   (   RJ   Rs   t   db(    (    s.   ../Django//lib/python/django/test/testcases.pyR   !  s
    c         C  s)   t  |  d  r% |  j t _ t   n  d  S(   Nu   _old_root_urlconf(   R   R   R   R   R   (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyR   +  s    i.  i   u    c      	   C  s  | r | d 7} n  t  | d  r |  j t | j  d k | d | j | f  |  j | j d d | | d | j d d | f  | j d \ } } |  j | j | | d | j | f  n |  j | j | | d | j | f  | d	 } t |  \ } }	 }
 } } | j j |
 t	 |   } |  j | j | | d
 |
 | j | f  t |  \ } } } } } | p{| st
 d | pd | | | f  } n  |  j | | | d | | f  d S(   u   Asserts that a response redirected to a specific URL, and that the
        redirect URL can be loaded.

        Note that assertRedirects won't work for external links since it uses
        TestClient to do a request.
        u   : u   redirect_chaini    uH   Response didn't redirect as expected: Response code was %d (expected %d)i   uP   Initial response didn't redirect as expected: Response code was %d (expected %d)iuN   Response didn't redirect as expected: Final Response code was %d (expected %d)u   LocationuK   Couldn't retrieve redirection page '%s': response code was %d (expected %d)u   httpu
   testserveru*   Response redirected to '%s', expected '%s'N(   R   R   Ry   t   redirect_chaint   status_codeR   R   R   t   getR   R   (   RJ   t   responset   expected_urlR  t   target_status_codet   hostR   t   urlt   schemet   netloct   patht   queryt   fragmentt   redirect_responset   e_schemet   e_netloct   e_patht   e_queryt
   e_fragment(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertRedirects0  s<    
c   	   
   C  sT  t  | d  r5 t | j  r5 | j r5 | j   n  | rH | d 7} n  |  j | j | | d | j | f  t | d | j } | j r d j	 | j
  } n	 | j } | j | j  } | r t |  | d d  } t |  | d d  } n  | j |  } | d k	 r2|  j | | | d | | | f  n |  j | d	 k | d
 |  d S(   uq  
        Asserts that a response indicates that some content was retrieved
        successfully, (i.e., the HTTP status code was as expected), and that
        ``text`` occurs ``count`` times in the content of the response.
        If ``count`` is None, the count doesn't matter - the assertion is true
        if the text occurs at least once in the response.
        u   renderu   : u=   Couldn't retrieve content: Response code was %d (expected %d)t   encodingt    u%   Response's content is not valid HTML:u"   Second argument is not valid HTML:u4   Found %d instances of '%s' in response (expected %d)i    u   Couldn't find '%s' in responseN(   R   t   callablet   rendert   is_renderedR   R  R&   t   _charsett	   streamingR   t   streaming_contentt   contentt   decodeRP   R1   R   R   (	   RJ   R  t   textR   R  R   RK   R!  R   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertContainsk  s2    
			c      	   C  s   t  | d  r5 t | j  r5 | j r5 | j   n  | rH | d 7} n  |  j | j | | d | j | f  t | d | j } | j j	 | j  } | r t
 |  | d	 d  } t
 |  | d	 d  } n  |  j | j |  d | d |  d	 S(
   u   
        Asserts that a response indicates that some content was retrieved
        successfully, (i.e., the HTTP status code was as expected), and that
        ``text`` doesn't occurs in the content of the response.
        u   renderu   : u=   Couldn't retrieve content: Response code was %d (expected %d)R  u%   Response's content is not valid HTML:u"   Second argument is not valid HTML:i    u    Response should not contain '%s'N(   R   R  R  R  R   R  R&   R  R!  R"  RP   R1   R   (   RJ   R  R#  R  R   RK   R!  (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertNotContains  s"    

	c         C  s  | r | d 7} n  t  | j  } | s< |  j | d  n  t  |  } t } x2t |  D]$\ } }	 | |	 k ry q[ n  t } x | D] }
 | rA| |	 | j k r |	 | j | } |  j |
 | k | d | | | |
 t |  f  q{| |	 | j	 k r |  j | d | | | f  q{|  j | d | | | f  q |	 | j
   } |  j |
 | k | d | | |
 | f  q Wq[ W| s|  j | d |  n  d S(	   ue   
        Asserts that a form used to render the response has a specific field
        error.
        u   : u8   Response did not use any contexts to render the responseu]   The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)u<   The field '%s' on form '%s' in context %d contains no errorsu;   The form '%s' in context %d does not contain the field '%s'uY   The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)u1   The form '%s' was not used to render the responseN(   R5   R   RH   RW   t	   enumerateRV   R   R   t   reprt   fieldst   non_field_errors(   RJ   R  t   formt   fieldR   R   t   contextst
   found_formt   iR   t   errt   field_errorsR)  (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertFormError  s<    !c      
   C  s   | d k r' | d k r' t d   n  | r: | d 7} n  t | d  s\ | d k r | r | rq | } d } n  t |  |  } | Sg  | j D] } | j ^ q } | s |  j | d  n  |  j | | k | d | d j |  f  d S(   u   
        Asserts that the template with the provided name was used in rendering
        the response. Also usable as context manager.
        u7   response and/or template_name argument must be providedu   : u	   templatesu(   No templates used to render the responseuY   Template '%s' was not a template used to render the response. Actual template(s) used: %su   , N(	   R1   t	   TypeErrorR   R   t	   templatesR   RH   R   R   (   RJ   R  R   R   R   t   tt   template_names(    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertTemplateUsed  s     "	c         C  s   | d k r' | d k r' t d   n  | r: | d 7} n  t | d  s\ | d k r | r | rq | } d } n  t |  |  } | Sg  | j D] } | j ^ q } |  j | | k | d |  d S(   u   
        Asserts that the template with the provided name was NOT used in
        rendering the response. Also usable as context manager.
        u7   response and/or template_name argument must be providedu   : u	   templatesu=   Template '%s' was used unexpectedly in rendering the responseN(   R1   R2  R   R   R3  R   t   assertFalse(   RJ   R  R   R   R   R4  R5  (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertTemplateNotUsed  s    "	c         C  sM   t  j j | |  } | s7 |  j t |  t |   S|  j t |  |  S(   N(   R'   t   movest   mapR   t   setR3   (   RJ   t   qst   valuest	   transformt   orderedR   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertQuerysetEqual  s    c         O  s\   | j  d t  } t | } t |  | |  } | d  k r> | S|  | | |   Wd  QXd  S(   Nu   using(   t   popR   R   Rt   R1   (   RJ   Rv   t   funcR6   R7   Rl   Rs   R   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   assertNumQueries  s    
N(   Rg   Rh   R   R   RW   R   R   RV   R   R   R   R   R   R   R   R1   R  R$  R%  R1  R6  R8  R'  R@  RC  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR     s*   							
	:)3c           C  s   t  d   t j    D  S(   u?   
    Returns True if all connections support transactions.
    c         s  s   |  ] } | j  j Vq d  S(   N(   R   t   supports_transactions(   t   .0Rs   (    (    s.   ../Django//lib/python/django/test/testcases.pys	   <genexpr>/  s   (   R   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyt    connections_support_transactions+  s    	t   TestCasec           B  s    e  Z d  Z d   Z d   Z RS(   uO  
    Does basically the same as TransactionTestCase, but surrounds every test
    with a transaction, monkey-patches the real transaction management routines
    to do nothing, and rollsback the test transaction at the end of the test.
    You have to use TransactionTestCase, if you need transaction management
    inside a test.
    c         C  s   t    s t t |   j   S|  j s2 t d   x7 |  j   D]) } t j d |  t j	 t
 d | q? Wt   d d l m } | j j   x[ |  j d t  D]G } t |  d  r t d |  j i d d	 6t d
 6| d 6t
 d 6 q q Wd  S(   Nu4   reset_sequences cannot be used on TestCase instancesRl   i(   t   SiteR   u   fixturesu   loaddatai    u	   verbosityu   commitu   databaseu   skip_validation(   RF  R   RG  R   R   t   AssertionErrorR   R   R;   R=   RV   R?   t   django.contrib.sites.modelsRH  t   objectst   clear_cacheRW   R   R   R   (   RJ   R   RH  R  (    (    s.   ../Django//lib/python/django/test/testcases.pyR   <  s"    	c         C  s^   t    s t t |   j   St   x4 |  j   D]& } t j d |  t j d |  q0 Wd  S(   NRl   (	   RF  R   RG  R   RF   R   R   R:   R<   (   RJ   R  (    (    s.   ../Django//lib/python/django/test/testcases.pyR   T  s    	(   Rg   Rh   t   __doc__R   R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyRG  3  s   	c           s      f d   } | S(   Nc           s[   t    t  o t   t  sH t        f d    } | } n   }  | _ | S(   Nc            s(      r t  j    n    |  |   S(   N(   t   ut2t   SkipTest(   R6   R7   (   t	   test_funct   reasont	   condition(    s.   ../Django//lib/python/django/test/testcases.pyt   skip_wrapperb  s    	(   R2   t   typeR   RG  R   t   __unittest_skip_why__(   RP  RS  t	   test_item(   RQ  RR  (   RP  s.   ../Django//lib/python/django/test/testcases.pyt	   decorator_  s    !		(    (   RR  RQ  RW  (    (   RQ  RR  s.   ../Django//lib/python/django/test/testcases.pyt   _deferredSkip^  s    c           s   t    f d   d    S(   u9   
    Skip a test if a database has the named feature
    c             s   t  t j    S(   N(   R   R   R   (    (   t   feature(    s.   ../Django//lib/python/django/test/testcases.pyR.   s  s    u   Database has feature %s(   RX  (   RY  (    (   RY  s.   ../Django//lib/python/django/test/testcases.pyt   skipIfDBFeatureo  s    c           s   t    f d   d    S(   u=   
    Skip a test unless a database has the named feature
    c             s   t  t j    S(   N(   R   R   R   (    (   RY  (    s.   ../Django//lib/python/django/test/testcases.pyR.   {  s    u#   Database doesn't support feature %s(   RX  (   RY  (    (   RY  s.   ../Django//lib/python/django/test/testcases.pyt   skipUnlessDBFeaturew  s    t   QuietWSGIRequestHandlerc           B  s   e  Z d  Z d   Z RS(   u   
    Just a regular WSGIRequestHandler except it doesn't log to the standard
    output any of the requests received, so as to not clutter the output for
    the tests' results.
    c          G  s   d  S(   N(    (   R6   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   log_message  s    (   Rg   Rh   RM  R]  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR\    s   i   i    i   i   t   _ImprovedEventc           B  s   e  Z d  Z d d  Z RS(   u  
        Does the same as `threading.Event` except it overrides the wait() method
        with some code borrowed from Python 2.7 to return the set state of the
        event (see: http://hg.python.org/cpython/rev/b5aa8aa78c0f/). This allows
        to know whether the wait() method exited normally or because of the
        timeout. This class can be removed when Django supports only Python >= 2.7.
        c         C  sI   |  j  j   z' |  j s, |  j  j |  n  |  j SWd  |  j  j   Xd  S(   N(   t   _Event__condt   acquiret   _Event__flagt   waitt   release(   RJ   t   timeout(    (    s.   ../Django//lib/python/django/test/testcases.pyRb    s    	N(   Rg   Rh   RM  R1   Rb  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR^    s   t   StoppableWSGIServerc           B  s>   e  Z d  Z d   Z d d  Z d   Z d   Z d   Z RS(   uW  
    The code in this class is borrowed from the `SocketServer.BaseServer` class
    in Python 2.6. The important functionality here is that the server is non-
    blocking and that it can be shut down at any moment. This is made possible
    by the server regularly polling the socket and checking if it has been
    asked to stop.
    Note for the future: Once Django stops supporting Python 2.6, this class
    can be removed as `WSGIServer` will have this ability to shutdown on
    demand and will not require the use of the _ImprovedEvent class whose code
    is borrowed from Python 2.7.
    c         O  s2   t  t |   j | |   t   |  _ t |  _ d  S(   N(   R   Re  Rj   R^  t"   _StoppableWSGIServer__is_shut_downRW   t   _StoppableWSGIServer__serving(   RJ   R6   R7   (    (    s.   ../Django//lib/python/django/test/testcases.pyRj     s    g      ?c         C  sn   t  |  _ |  j j   xD |  j r\ t j |  g g  g  |  \ } } } | r |  j   q q W|  j j   d S(   uw   
        Handle one request at a time until shutdown.

        Polls for shutdown every poll_interval seconds.
        N(   RV   Rg  Rf  t   cleart   selectt   _handle_request_noblockR;  (   RJ   t   poll_intervalt   rt   wRN   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   serve_forever  s    	$c         C  s.   t  |  _ |  j j d  s* t d   n  d S(   u   
        Stops the serve_forever loop.

        Blocks until the loop has finished. This must be called while
        serve_forever() is running in another thread, or it will
        deadlock.
        i   un   Failed to shutdown the live test server in 2 seconds. The server might be stuck or generating a slow response.N(   RW   Rg  Rf  Rb  t   RuntimeError(   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   shutdown  s    	c         C  s7   t  j  |  g g  g  d  } | d s) d S|  j   d S(   u/   Handle one request, possibly blocking.
        i    N(   Ri  R1   Rj  (   RJ   t   fd_sets(    (    s.   ../Django//lib/python/django/test/testcases.pyt   handle_request  s    
c         C  s   y |  j    \ } } Wn t j k
 r- d SX|  j | |  r y |  j | |  Wq t k
 r |  j | |  |  j |  q Xn  d S(   u   
        Handle one request, without blocking.

        I assume that select.select has returned that the socket is
        readable before this function was called, so there should be
        no risk of blocking in get_request().
        N(   t   get_requestt   sockett   errort   verify_requestt   process_requestRa   t   handle_errort   close_request(   RJ   t   requestt   client_address(    (    s.   ../Django//lib/python/django/test/testcases.pyRj    s    (   Rg   Rh   RM  Rj   Rn  Rp  Rr  Rj  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyRe    s   			t   _MediaFilesHandlerc           B  s)   e  Z d  Z d   Z d   Z d   Z RS(   u   
    Handler for serving the media files. This is a private class that is
    meant to be used solely as a convenience by LiveServerThread.
    c         C  s   t  j S(   N(   R   t
   MEDIA_ROOT(   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   get_base_dir  s    c         C  s   t  j S(   N(   R   t	   MEDIA_URL(   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   get_base_url  s    c         C  s3   | j  t |  j d  } t | | d |  j   S(   Ni   t   document_root(   R  Ry   t   base_urlR*   R~  (   RJ   Rz  t   relative_url(    (    s.   ../Django//lib/python/django/test/testcases.pyR*     s    (   Rg   Rh   RM  R~  R  R*   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR|    s   		t   LiveServerThreadc           B  s/   e  Z d  Z d d  Z d   Z d d  Z RS(   uL   
    Thread for running a live http server while the tests are running.
    c         C  sS   | |  _  d  |  _ | |  _ t j   |  _ d  |  _ | |  _ t	 t
 |   j   d  S(   N(   R  R1   t   portt   possible_portst	   threadingt   Eventt   is_readyRu  t   connections_overrideR   R  Rj   (   RJ   R  R  R  (    (    s.   ../Django//lib/python/django/test/testcases.pyRj     s    					c         C  si  |  j  rF d d l m } x* |  j  j   D] \ } } | | | <q) Wn  y t t t     } x t |  j  D] \ } } y t	 |  j
 | f t  |  _ Wnd t k
 r } | d t |  j  k  r t | j d d  r | j d j t j k r qn q
  qn X| |  _ Pqn W|  j j |  |  j j   |  j j   Wn) t k
 rd} | |  _ |  j j   n Xd S(   ul   
        Sets up the live server and databases, and then loops over handling
        http requests.
        i(   R   i   i    u   errnoN(   R  t	   django.dbR   R   R   R|  R
   R&  R  Re  R  R\  t   httpdR   Ry   R   R6   t   errnot
   EADDRINUSER  t   set_appR  R;  Rn  Ra   Ru  (   RJ   R   R   Rs   t   handlert   indexR  RN   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   run  s0    			c         C  sF   t  |  d  r, |  j j   |  j j   n  t t |   j |  d  S(   Nu   httpd(   R   R  Rp  t   server_closeR   R  R   (   RJ   Rd  (    (    s.   ../Django//lib/python/django/test/testcases.pyR   8  s    N(   Rg   Rh   RM  R1   Rj   R  R   (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR    s   		-t   LiveServerTestCasec           B  s;   e  Z d  Z e d    Z e d    Z e d    Z RS(   u	  
    Does basically the same as TransactionTestCase but also launches a live
    http server in a separate thread so that the tests may use another testing
    framework, such as Selenium for example, instead of the built-in dummy
    client.
    Note that it inherits from TransactionTestCase instead of TestCase because
    the threads do not share the same transactions (unless if using in-memory
    sqlite) and each thread needs to commit all their transactions so that the
    other thread can see the changes.
    c         C  s   d |  j  j |  j  j f S(   Nu   http://%s:%s(   t   server_threadR  R  (   RJ   (    (    s.   ../Django//lib/python/django/test/testcases.pyt   live_server_urlL  s    c   
      C  s  i  } xc t  j   D]U } | j d j d d  d d k r | j d d k r t | _ | | | j <q q Wt j j	 d	 d
  } g  } y | j
 d  \ } } x | j
 d  D] } t t t | j
 d    } t |  d k s t  t |  d k r| j | d  q x0 t | d | d d  D] }	 | j |	  q-Wq WWn! t k
 rlt d |   n Xt | | |  |  _ t |  j _ |  j j   |  j j j   |  j j r|  j j  n  t t |   j   d  S(   Nu   ENGINEu   .i   iu   sqlite3u
   spatialiteu   NAMEu   :memory:u   DJANGO_LIVE_TEST_SERVER_ADDRESSu   localhost:8081u   :u   ,u   -i   i    u'   Invalid address ("%s") for live server.(   u   sqlite3u
   spatialite(   i   i   (   R   R   R   t   rsplitRV   t   allow_thread_sharingR   t   ost   environR  t   splitR3   R:  t   intRy   RI  R   t   rangeRa   R	   R  R  t   daemont   startR  Rb  Ru  R   R  t
   setUpClass(
   t   clsR  Rs   t   specified_addressR  R  t   port_rangest
   port_ranget   extremesR  (    (    s.   ../Django//lib/python/django/test/testcases.pyR  Q  s:    #		"c         C  s   t  |  d  r |  j j   n  xV t j   D]H } | j d j d d  d d
 k r, | j d d	 k r, t | _ q, q, Wt	 t
 |   j   d  S(   Nu   server_threadu   ENGINEu   .i   iu   sqlite3u
   spatialiteu   NAMEu   :memory:(   u   sqlite3u
   spatialite(   R   R  R   R   R   R   R  RW   R  R   R  t   tearDownClass(   R  Rs   (    (    s.   ../Django//lib/python/django/test/testcases.pyR    s    #(   Rg   Rh   RM  t   propertyR  t   classmethodR  R  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyR  @  s   
0(~   t
   __future__R    R   Rc   R  R+   R   R   t	   functoolsR   t   urllib.parseR   R   t   ImportErrort   urlparseRi  Rt  R  R  t   django.confR   t#   django.contrib.staticfiles.handlersR   t   django.coreR   t   django.core.exceptionsR   R	   t   django.core.handlers.wsgiR
   t   django.core.managementR   t   django.core.management.colorR   t   django.core.signalsR   t   django.core.servers.basehttpR   R   R   t   django.core.urlresolversR   t   django.core.validatorsR   R  R   R   R   R   R   t   django.forms.fieldsR   t   django.httpR   t   django.testR   R^   t   django.test.clientR   t   django.test.htmlR   R   t   django.test.signalsR   t   django.test.utilsR   R    R!   R"   R#   R$   t   django.utilsR%   RN  t   django.utils.encodingR&   R'   t   django.utils.unittest.utilR(   t   django.utils.unittestR)   t   django.views.staticR*   t   __all__R`   R_   R5   R9   R@   R:   RA   R;   RB   R<   RC   R=   RD   R>   RE   R8   R?   RF   RP   RQ   Ri   t   objectRt   R   R   RG  R   R   RF  RX  RZ  R[  R\  t   version_infoR  R^  t   _EventRe  R|  t   ThreadR  R  (    (    (    s.   ../Django//lib/python/django/test/testcases.pyt   <module>   s   ((															5& b	+			JC