
2!V^                 @   s  d  d l  m Z 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 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 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# e j$ rJd  d l m% Z% n9 y d  d l& m% Z% Wn" e' k
 rd  d l% m% Z% Yn Xe j( d  d k oe j( d d k Z) d d   Z* Gd d   d e+  Z, Gd d   d e, e  Z- Gd d   d e, e  Z. Gd d   d e, e  Z/ Gd d   d e, e  Z0 Gd  d!   d! e, e  Z1 Gd" d#   d# e, e  Z2 Gd$ d%   d% e, e  Z3 Gd& d'   d' e, e  Z4 Gd( d)   d) e, e  Z5 Gd* d+   d+ e, e  Z6 Gd, d-   d- e, e  Z7 Gd. d/   d/ e, e  Z8 Gd0 d1   d1 e8  Z9 Gd2 d3   d3 e, e  Z: Gd4 d5   d5 e:  Z; Gd6 d7   d7 e, e  Z< Gd8 d9   d9 e, e  Z= Gd: d;   d; e, e  Z> Gd< d=   d= e, e  Z? Gd> d?   d? e, e  Z@ Gd@ dA   dA e, e  ZA GdB dC   dC e, e  ZB d S)D    )with_statementunicode_literalsN)import_module)patch)SkipTest)CommandError)TemplateContext)TestCase)six)flush_offline_manifestget_offline_manifest)settings)OfflineGenerationError)Command)default_storage)get_mod_func)StringIO         c              c   s.   x' t  d d  D] }  i d |  d 6Vq Wd  S)Nr      zOK %d!content)range)i r   J/tmp/pip-build-0sboevxh/django-compressor/compressor/tests/test_offline.pyoffline_context_generator)   s    r   c               @   s   e  Z d  Z d Z d Z d Z d Z e r3 d Z n d Z d Z	 d d   Z
 d	 d
   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d S)OfflineTestCaseMixinztest_compressor_offline.htmlr    djangojinja2Nc                s  t      _ t j t j  } t j j | d d d   j	  } t j j | d d d   j	  } | g | d d <| g | d d <i | d 6d d 6d d 6} d   j
 k r   f d	 d
   | d <n    j d  k	 r | j   j  n    j |     _   j j   d   j
 k rrt j j |   j    _ t j   j d t j  } t | j      _ Wd  QXn  d   j
 k rt j j |   j    _ | d   } t j   j d t j  } | j | j      _ Wd  QXn  d  S)Nr   DIRSr   	TEMPLATESTCOMPRESS_ENABLEDCOMPRESS_OFFLINEr!   c                  s
     j    S)N)_get_jinja2_envr   )selfr   r   <lambda>X   s    z,OfflineTestCaseMixin.setUp.<locals>.<lambda>ZCOMPRESS_JINJA2_GET_ENVIRONMENTr    encoding)r   logcopydeepcopyr   r#   ospathjointemplates_direnginesadditional_test_settingsupdateoverride_settings	__enter__template_nametemplate_pathioopenFILE_CHARSETr   readtemplateZtemplate_path_jinja2Zfrom_stringtemplate_jinja2)r'   r#   Zdjango_template_dirZjinja2_template_dirr4   file_Z
jinja2_envr   )r'   r   setUp;   s>    
		
		zOfflineTestCaseMixin.setUpc             C   sN   |  j  j d  d  d   t j j d d  } t j |  rJ t j |  n  d  S)NCACHEzmanifest.json)r4   __exit__r-   r.   r/   r   existsdelete)r'   manifest_pathr   r   r   tearDownq   s    zOfflineTestCaseMixin.tearDownc             C   s6   | d k r t  t j  g S| d k r2 t j g Sd  S)Nr    r!   )r	   r   COMPRESS_OFFLINE_CONTEXT)r'   enginer   r   r   _prepare_contextsx   s
    
z&OfflineTestCaseMixin._prepare_contextsc                so     j  |  } | d k r; d j   f d d   | D  S| d k rk d j   f d d   | D  d Sd  S)Nr    r   c             3   s!   |  ] }   j  j |  Vq d  S)N)r<   render).0c)r'   r   r   	<genexpr>   s    z8OfflineTestCaseMixin._render_template.<locals>.<genexpr>r!   
c             3   s!   |  ] }   j  j |  Vq d  S)N)r=   rI   )rJ   rK   )r'   r   r   rL      s    )rH   r/   )r'   rG   contextsr   )r'   r   _render_template   s     z%OfflineTestCaseMixin._render_templatec             C   s   |  j  } t | t t f  s* | g } n  t   j d |  j d |  j d |  \ } } |  j t	 |  |  |  j d d   | D |  |  j
 |  } |  j | d j |  d  d  S)Nr*   	verbosityrG   c             S   s   g  |  ] } d  |  q S)zE<script type="text/javascript" src="/static/CACHE/js/%s.js"></script>r   )rJ   hr   r   r   
<listcomp>   s   	z6OfflineTestCaseMixin._test_offline.<locals>.<listcomp>rM   )expected_hash
isinstancelisttupleCompressCommandcompressr*   rP   assertEquallenrO   r/   )r'   rG   hashescountresultrendered_templater   r   r   _test_offline   s    	!	z"OfflineTestCaseMixin._test_offlinec             C   s2   d |  j  k r t d   n  |  j d d  d  S)Nr    z/This test class does not support django engine.rG   )r1   r   r_   )r'   r   r   r   test_offline_django   s    z(OfflineTestCaseMixin.test_offline_djangoc             C   s2   d |  j  k r t d   n  |  j d d  d  S)Nr!   z/This test class does not support jinja2 engine.rG   )r1   r   r_   )r'   r   r   r   test_offline_jinja2   s    z(OfflineTestCaseMixin.test_offline_jinja2c             C   s   d d  l  } d d  l } d d l m } m } d d l m } | | | j j | j j	 g } |  j
   } | j d | d |  } | | j d <| S)Nr   )url_forSpacelessExtension)CompressorExtension
extensionsloaderrb   )r!   Z
jinja2.extZcompressor.offline.jinja2rb   rc   Zcompressor.contrib.jinja2extrd   extwith_Zdo_get_jinja2_loaderEnvironmentglobals)r'   r!   rb   rc   rd   re   rf   envr   r   r   r&      s    	z$OfflineTestCaseMixin._get_jinja2_envc             C   s3   d d  l  } | j t j d d d t j } | S)Nr   r   r"   r)   )r!   FileSystemLoaderr   r#   r:   )r'   r!   rf   r   r   r   ri      s    z'OfflineTestCaseMixin._get_jinja2_loader)djangozjinja2)rn   )__name__
__module____qualname__r6   rP   r0   rS   _TEST_JINJA2r1   r2   r?   rE   rH   rO   r_   r`   ra   r&   ri   r   r   r   r   r   .   s"   	6	r   c               @   s   e  Z d  Z d Z d Z e j e d  d d    Z e j e d  d d    Z	 e j e d  d d	    Z
 e j e d  d
 d    Z d d   Z e j e d  d d    Z d d   Z d d   Z d d   Z d S)OfflineCompressBasicTestCasebasicf5e179b8eca4rX   c             C   s$   t    j   |  j | j d  d  S)Nr   )rW   handlerY   
call_count)r'   compress_mockr   r   r   test_handle_no_args   s    z0OfflineCompressBasicTestCase.test_handle_no_argsc             C   sS   |  j  d d  ( |  j t   t   j   Wd  QXWd  QX|  j | j d  d  S)Nr$   Fr   )r   assertRaisesr   rW   rv   rY   rw   )r'   rx   r   r   r   test_handle_compress_disabled   s    z:OfflineCompressBasicTestCase.test_handle_compress_disabledc             C   sS   |  j  d d  ( |  j t   t   j   Wd  QXWd  QX|  j | j d  d  S)Nr%   Fr   )r   rz   r   rW   rv   rY   rw   )r'   rx   r   r   r   %test_handle_compress_offline_disabled   s    zBOfflineCompressBasicTestCase.test_handle_compress_offline_disabledc          
   C   sC   |  j  d d   t   j d d  Wd  QX|  j | j d  d  S)Nr%   FforceTr   )r   rW   rv   rY   rw   )r'   rx   r   r   r   +test_handle_compress_offline_disabled_force   s    zHOfflineCompressBasicTestCase.test_handle_compress_offline_disabled_forcec             C   s*   t    |  j t |  j j t i    d  S)N)r   rz   r   r<   rI   r	   )r'   r   r   r   0test_rendering_without_manifest_raises_exception   s    	zMOfflineCompressBasicTestCase.test_rendering_without_manifest_raises_exceptionzNo Jinja2 testingc             C   s$   t    |  j t |  j j i   d  S)N)r   rz   r   r=   rI   )r'   r   r   r   7test_rendering_without_manifest_raises_exception_jinja2   s    	zTOfflineCompressBasicTestCase.test_rendering_without_manifest_raises_exception_jinja2c             C   s   t    j d |  j d |  j d |  \ } } t   t j j d d  } t j	 |  rh t j
 |  n  |  j d |  |  j d |  j f g |  |  j |  } |  j | d j |  d	  d  S)
Nr*   rP   rG   r@   zmanifest.jsonr   zE<script type="text/javascript" src="/static/CACHE/js/%s.js"></script>r   rM   )rW   rX   r*   rP   r   r-   r.   r/   r   rB   rC   rY   rS   rO   )r'   rG   r\   r]   rD   r^   r   r   r   1_test_deleting_manifest_does_not_affect_rendering   s    !zNOfflineCompressBasicTestCase._test_deleting_manifest_does_not_affect_renderingc             C   s%   x |  j  D] } |  j |  q
 Wd  S)N)r1   r   )r'   rG   r   r   r   0test_deleting_manifest_does_not_affect_rendering   s    zMOfflineCompressBasicTestCase.test_deleting_manifest_does_not_affect_renderingc             C   s   d
 } |  j  d |  h d d l m } d d l m } t   j   } |  j t | d |   |  j t | d |   Wd  QXd  S)N%django.template.loaders.cached.Loader)django.template.loaders.filesystem.Loader.django.template.loaders.app_directories.LoaderTEMPLATE_LOADERSr   )Loaderr   r   r   r   r   )r   )r   Z"django.template.loaders.filesystemr   Z'django.template.loaders.app_directoriesrW   Zget_loadersZ
assertTruerT   )r'   r   rm   ZAppDirectoriesLoaderloadersr   r   r   test_get_loaders   s      z-OfflineCompressBasicTestCase.test_get_loadersN)ro   rp   rq   r0   rS   r   objectrW   ry   r{   r|   r~   r   unittestZskipIfrr   r   r   r   r   r   r   r   r   rs      s   rs   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)%OfflineCompressSkipDuplicatesTestCaseZtest_duplicater    c             C   s   t    j d |  j d |  j d |  \ } } |  j d |  |  j d g |  |  j |  } |  j | d j | d  d  d  S)	Nr*   rP   rG   r   zO<script type="text/javascript" src="/static/CACHE/js/f5e179b8eca4.js"></script>r   r   rM   )rW   rX   r*   rP   rY   rO   r/   )r'   rG   r\   r]   r^   r   r   r   r_     s    !z3OfflineCompressSkipDuplicatesTestCase._test_offlineN)zdjango)ro   rp   rq   r0   r1   r_   r   r   r   r   r     s   r   c               @   s"   e  Z d  Z d Z d Z d Z d S)!OfflineCompressBlockSuperTestCaseZtest_block_superZ7c02d201f69dr    N)zdjango)ro   rp   rq   r0   rS   r1   r   r   r   r   r     s   r   c               @   s"   e  Z d  Z d Z d Z d Z d S))OfflineCompressBlockSuperMultipleTestCaseZtest_block_super_multiplef8891c416981r    N)zdjango)ro   rp   rq   r0   rS   r1   r   r   r   r   r   #  s   r   c               @   s/   e  Z d  Z d Z d Z d	 Z i d d 6Z d S)5OfflineCompressBlockSuperMultipleCachedLoaderTestCaseZ test_block_super_multiple_cachedZ2f6ef61c488er    %django.template.loaders.cached.Loader)django.template.loaders.filesystem.Loader.django.template.loaders.app_directories.Loaderr   N)zdjangor   r   r   r   )r   )ro   rp   rq   r0   rS   r1   r2   r   r   r   r   r   +  s     r   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)1OfflineCompressBlockSuperTestCaseWithExtraContentZtest_block_super_extrar    c             C   s   t    j d |  j d |  j d |  \ } } |  j d |  |  j d d g |  |  j |  } |  j | d j |  d  d  S)	Nr*   rP   rG   r   zO<script type="text/javascript" src="/static/CACHE/js/ced14aec5856.js"></script>zO<script type="text/javascript" src="/static/CACHE/js/7c02d201f69d.js"></script>r   rM   )rW   rX   r*   rP   rY   rO   r/   )r'   rG   r\   r]   r^   r   r   r   r_   A  s    !z?OfflineCompressBlockSuperTestCaseWithExtraContent._test_offlineN)zdjango)ro   rp   rq   r0   r1   r_   r   r   r   r   r   ;  s   r   c               @   s0   e  Z d  Z d Z d Z i i d d 6d 6Z d S) OfflineCompressConditionTestCaseZtest_conditionZ4e3758d50224Zred	conditionrF   N)ro   rp   rq   r0   rS   r2   r   r   r   r   r   O  s
   r   c               @   s   e  Z d  Z d Z d Z d S)"OfflineCompressTemplateTagTestCaseZtest_templatetagZa27e1d3a619aN)ro   rp   rq   r0   rS   r   r   r   r   r   Y  s   r   c               @   s   e  Z d  Z d Z d Z d S)(OfflineCompressStaticTemplateTagTestCaseZtest_static_templatetagZdfa2bb387fa8N)ro   rp   rq   r0   rS   r   r   r   r   r   ^  s   r   c               @   s0   e  Z d  Z d Z d Z i i d d 6d 6Z d S)"OfflineCompressTestCaseWithContexttest_with_contextZ5838e2fd66afzOK!r   rF   N)ro   rp   rq   r0   rS   r2   r   r   r   r   r   c  s
   r   c               @   s6   e  Z d  Z d Z d Z i i d d 6d 6Z d Z d S)	'OfflineCompressTestCaseWithContextSupertest_with_context_superZb1d0a333a4efzOK!r   rF   r    N)zdjango)ro   rp   rq   r0   rS   r2   r1   r   r   r   r   r   m  s   r   c               @   sG   e  Z d  Z d Z d d d g Z i e e    d 6Z d d   Z d S)	&OfflineCompressTestCaseWithContextListr   f8bcaea049b3db12749b1e80e9f4a0054a06rF   c             C   s7   | d k r  d d   t  j D S| d k r3 t  j Sd  S)Nr    c             S   s   g  |  ] } t  |   q Sr   )r	   )rJ   rK   r   r   r   rR     s   	 zLOfflineCompressTestCaseWithContextList._prepare_contexts.<locals>.<listcomp>r!   )r   rF   )r'   rG   r   r   r   rH     s
    z8OfflineCompressTestCaseWithContextList._prepare_contextsN)	ro   rp   rq   r0   rS   rU   r   r2   rH   r   r   r   r   r   y  s
   r   c               @   sA   e  Z d  Z d Z d d d g Z i e e    d 6Z d Z d S)	+OfflineCompressTestCaseWithContextListSuperr   b11543f1e174aedf6d2a7ec70dbb8c29f23arF   r    N)zdjango)	ro   rp   rq   r0   rS   rU   r   r2   r1   r   r   r   r   r     s
   r   c               @   s>   e  Z d  Z d Z d d d g Z i d d 6Z d d   Z d	 S)
+OfflineCompressTestCaseWithContextGeneratorr   r   r   r   z7compressor.tests.test_offline.offline_context_generatorrF   c             C   s^   t  t j  \ } } t t |  |    } | d k rJ d d   | D S| d k rZ | Sd  S)Nr    c             s   s   |  ] } t  |  Vq d  S)N)r	   )rJ   rK   r   r   r   rL     s    zPOfflineCompressTestCaseWithContextGenerator._prepare_contexts.<locals>.<genexpr>r!   )r   r   rF   getattrr   )r'   rG   modulefunctionrN   r   r   r   rH     s    z=OfflineCompressTestCaseWithContextGenerator._prepare_contextsN)ro   rp   rq   r0   rS   r2   rH   r   r   r   r   r     s
   
r   c               @   s8   e  Z d  Z d Z d d d g Z i d d 6Z d	 Z d S)
0OfflineCompressTestCaseWithContextGeneratorSuperr   r   r   r   z7compressor.tests.test_offline.offline_context_generatorrF   r    N)zdjango)ro   rp   rq   r0   rS   r2   r1   r   r   r   r   r     s
   
r   c               @   s"   e  Z d  Z d Z d d   Z d S)6OfflineCompressTestCaseWithContextGeneratorImportErrorr   c          *   C   s0  |  j  d d  ! |  j t t   j d | Wd  QX|  j  d d  ! |  j t t   j d | Wd  QX|  j  d d  ! |  j t t   j d | Wd  QX|  j  d d  ! |  j t t   j d | Wd  QXd } |  j  d |  > y t   j d |  Wn t k
 r%|  j d  Yn XWd  QXd  S)	NrF   zinvalid_mod.invalid_funcrG   Z
compressorz!compressor.tests.invalid_functionzcompressor.tests.test_offlinez7compressor.tests.test_offline.offline_context_generatorzCValid path to offline context generator must not raise ImportError.)r   rz   ImportErrorrW   rX   Zfail)r'   rG   Z
valid_pathr   r   r   r_     s*    	
	
	
zDOfflineCompressTestCaseWithContextGeneratorImportError._test_offlineN)ro   rp   rq   r0   r_   r   r   r   r   r     s   r   c               @   s"   e  Z d  Z d Z d d   Z d S)OfflineCompressTestCaseErrorstest_error_handlingc             C   s   t    j d |  j d |  j d |  \ } } | d k rL |  j d |  n@ |  j d |  |  j | d  |  j d |  |  j d	 |  |  j d
 |  |  j d |  d  S)Nr*   rP   rG   r    r   r   r!   zS<link rel="stylesheet" href="/static/CACHE/css/78bd7a762e2d.css" type="text/css" />zS<link rel="stylesheet" href="/static/CACHE/css/e31030430724.css" type="text/css" />zO<script type="text/javascript" src="/static/CACHE/js/3872c9ae3f42.js"></script>zO<script type="text/javascript" src="/static/CACHE/js/cd8870829421.js"></script>)rW   rX   r*   rP   rY   assertIn)r'   rG   r\   r]   r   r   r   r_     s$    !z+OfflineCompressTestCaseErrors._test_offlineN)ro   rp   rq   r0   r_   r   r   r   r   r     s   r   c               @   s/   e  Z d  Z d Z i d	 d 6Z d d   Z d S)
 OfflineCompressTestCaseWithErrorr   text/coffeescriptnonexisting-binaryZCOMPRESS_PRECOMPILERSc             C   sn   |  j  d d  ! |  j t t   j d | Wd QX|  j  d d  ! |  j t t   j d | Wd QXd S)z
        Test that a CommandError is raised with DEBUG being False as well as
        True, as otherwise errors in configuration will never show in
        production.
        DEBUGTrG   NF)r   rz   r   rW   rX   )r'   rG   r   r   r   r_     s    z.OfflineCompressTestCaseWithError._test_offlineNr   r   )r   )ro   rp   rq   r0   r2   r_   r   r   r   r   r     s   
r   c               @   s4   e  Z d  Z d Z d Z d Z d Z d d   Z d S)	OfflineCompressEmptyTaga  
        In case of a compress template tag with no content, an entry
        will be added to the manifest with an empty string as value.
        This test makes sure there is no recompression happening when
        compressor encounters such an emptystring in the manifest.
    rt   ru   r    c             C   sg   t    j d |  j d |  j d |  \ } } t   } d | t |  d <|  j |  j |  d  d  S)Nr*   rP   rG   r   r   rM   )rW   rX   r*   rP   r   rU   rY   rO   )r'   rG   r\   r]   manifestr   r   r   r_     s
    !	z%OfflineCompressEmptyTag._test_offlineN)zdjango)ro   rp   rq   __doc__r0   rS   r1   r_   r   r   r   r   r     s
   r   c                   sd   e  Z d  Z d d d g Z d Z d d d g Z d Z   f d	 d
   Z d d   Z d d   Z	   S)'OfflineCompressBlockSuperBaseCompressedz	base.htmlz
base2.htmlztest_compressor_offline.htmlZ test_block_super_base_compressedZ028c3fc42232Z2e9d3f5545a6r   r    c          
      s   t  t |   j   g  |  _ g  |  _ x |  j D]} } t j j t	 j
 d d d |  } |  j j |  t j | d t	 j  } t | j    } Wd  QX|  j j |  q/ Wd  S)Nr   r"   r)   )superr   r?   Ztemplate_paths	templatestemplate_namesr-   r.   r/   r   r#   appendr8   r9   r:   r   r;   )r'   r6   r7   r>   r<   )	__class__r   r   r?   ,  s    			z-OfflineCompressBlockSuperBaseCompressed.setUpc             C   sJ   | d k r" | j  t t j   S| d k rB | j  t j  d Sd  Sd  S)Nr    r!   rM   )rI   r	   r   rF   )r'   r<   rG   r   r   r   rO   :  s
    z8OfflineCompressBlockSuperBaseCompressed._render_templatec             C   s   t    j d |  j d |  j d |  \ } } |  j t |  j  |  xf t |  j |  j  D]O \ } } d | f } |  j	 | |  |  j
 | |  } |  j | | d  q\ Wd  S)Nr*   rP   rG   zE<script type="text/javascript" src="/static/CACHE/js/%s.js"></script>rM   )rW   rX   r*   rP   rY   rZ   rS   zipr   r   rO   )r'   rG   r\   r]   rS   r<   expectedr^   r   r   r   r_   B  s    !"
z5OfflineCompressBlockSuperBaseCompressed._test_offline)zdjango)
ro   rp   rq   r   r0   rS   r1   r?   rO   r_   r   r   )r   r   r   $  s   	r   c               @   s6   e  Z d  Z d Z i i d d 6d 6Z d d   Z d S)%OfflineCompressInlineNonAsciiTestCaseZtest_inline_non_asciiu   —Ztest_non_ascii_valuerF   c             C   s]   t    j d |  j d |  j d |  \ } } |  j |  } |  j | d j |  d  d  S)Nr*   rP   rG   r   rM   )rW   rX   r*   rP   rO   rY   r/   )r'   rG   r\   r]   r^   r   r   r   r_   V  s    !z3OfflineCompressInlineNonAsciiTestCase._test_offlineN)ro   rp   rq   r0   r2   r_   r   r   r   r   r   N  s
   r   c               @   s=   e  Z d  Z d Z i i d d 6d d 6d 6Z d d	   Z d
 S)OfflineCompressComplexTestCaseZtest_complexzOK!r   	js/one.jsjs/nonasc.jsZmy_namesrF   c             C   s   t    j d |  j d |  j d |  \ } } |  j d |  |  j d d d g |  |  j |  } | d | d	 f } |  j | d
 j |  d  d  S)Nr*   rP   rG   r   zO<script type="text/javascript" src="/static/CACHE/js/0e8807bebcee.js"></script>zO<script type="text/javascript" src="/static/CACHE/js/eed1d222933e.js"></script>zO<script type="text/javascript" src="/static/CACHE/js/00b4baffe335.js"></script>r   r   r   rM   )rW   rX   r*   rP   rY   rO   r/   )r'   rG   r\   r]   r^   r   r   r   r_   i  s    !z,OfflineCompressComplexTestCase._test_offlineN)r   r   )ro   rp   rq   r0   r2   r_   r   r   r   r   r   ]  s   r   )C
__future__r   r   r+   r8   r-   sysr   	importlibr   Zmockr   r   Zdjango.core.management.baser   Zdjango.templater   r	   Zdjango.testr
   Zdjango.utilsr   Zcompressor.cacher   r   Zcompressor.confr   Zcompressor.exceptionsr   Z'compressor.management.commands.compressr   rW   Zcompressor.storager   Zcompressor.utilsr   PY3r   	cStringIOr   version_inforr   r   r   r   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp   	'M

	
	**