
    Ej                        d Z ddlmZ ddlZddlmc mZ ddl	m
Z
 ddlZddlmZmZ  e
e          j        j        dz  dz  Zdd
ZddZd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )uO   TDD tests for compiler/stages/acquire.py — all offline via recorded fixtures.    )annotationsN)Path)acquire_allextract_textfixtureshttpreturnhttpx.MockTransportc                 2    dd} t          j        |           S )zAHTTPX mock transport that returns pre-recorded fixture responses.requesthttpx.Requestr	   httpx.Responsec                &   t          | j                  }d|v r6d|v r2t          dz                                  }t	          j        d|          S d|v r6d|v r2t          dz                                  }t	          j        d|          S d|v r6d|v r2t          d	z                                  }t	          j        d|          S d
|v r6d|v r2t          dz                                  }t	          j        d|          S t	          j        dd          S )Nzgutenberg.org2680gutenberg_2680.txt   content55317zwikipedia.org/w/api.phpMarcus_Aureliuswikipedia_marcus_aurelius.jsonzwikiquote.orgwikiquote_marcus_aurelius.htmli  zNot found in fixture transport)text)strurlFIXTURES_HTTP
read_byteshttpxResponse)r   r   bodys      D/Users/jameslopez/projects/MentorCore/tests/compiler/test_acquire.pyhandlerz#_fixture_transport.<locals>.handler   s)   '+c!!fmm!$88DDFFD>#t4444c!!gnn!$88DDFFD>#t4444$++0AS0H0H!$DDPPRRD>#t4444c!!&73&>&>!$DDPPRRD>#t4444~c(HIIII    r   r   r	   r   r   MockTransport)r"   s    r!   _fixture_transportr'      s-    J J J J& w'''r#   httpx.Clientc                 D    t          j        t                                S )N	transport)r   Clientr'    r#   r!   _mock_clientr.   &   s    <"4"6"67777r#   c                .   ddddg}t          || dz  t                                }d}||v }|st          j        d|fd||f          t          j        |          d	t          j                    v st          j        |          rt          j        |          nd	d
z  }dd|iz  }t          t          j	        |                    d x}}|d         }|j
        } |            }	|	sddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |	          dz  }
t          t          j	        |
                    d x}}	|j        }d}||k    }	|	st          j        d|	fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j	        |                    d x}x}	}d}|j        } |            }||v }|st          j        d|fd||f          t          j        |          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j	        |                    d x}x}x}}d S )Nmeditations-gutenberg	gutenbergx
  id
fetch_typegutenberg_idrawhttp_clientinz%(py1)s in %(py3)sresultpy1py3assert %(py5)spy5Aassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}pathpy0py2py4z.txt==z.%(py2)s
{%(py2)s = %(py0)s.suffix
} == %(py5)srF   rG   rB   assert %(py7)spy7s   Marcus Aurelius)zI%(py1)s in %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.read_bytes
}()
}r?   r@   rB   rN   zassert %(py9)spy9)r   r.   
@pytest_ar_call_reprcompare	_saferepr@py_builtinslocals_should_repr_global_nameAssertionError_format_explanationexistssuffixr   )tmp_pathsourcesr=   @py_assert0@py_assert2@py_format4@py_format6rD   @py_assert1@py_assert3@py_format5@py_assert4@py_format8@py_assert6@py_format10s                  r!   test_acquire_gutenbergrh   -   s    *% 	
 	
G (U"2OOOF","f,,,,,,,,,,"f,,,,,",,,,,,,,,,,f,,,,,f,,,,,,,,,,,,,,,,,,,,,,,,,,,)*D;;;===44;=; & ;&          ;&            4     4      ;    &                           22!2!22!22222222222!22222222222222222222222222222222!22222222222222222222222222222222r#   c                ,   ddddg}t          || dz  t                                }|d         }|j        } |            }|sddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          d	z  }t          t          j	        |                    d x}}|j
        }d
}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j	        |	                    d x}x}}d S )Nwikipedia-marcus-aurelius	wikipediaMarcus Aureliusr4   r5   wikipedia_titler7   r8   rC   rD   rE   z.jsonrI   rK   rL   rM   rN   r   r.   rY   rT   rU   rQ   rV   rS   rW   rX   rZ   rR   
r[   r\   r=   rD   ra   rb   rc   rd   r`   re   s
             r!   test_acquire_wikipediarq   =   s    .%0	
 	
G (U"2OOOF-.D;;;===44;=;!'!;'!!!!!!!!!!;'!!!!!!!!!!!!4!!!!!4!!!!!!;!!!!'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!r#   c                  	
 i 
d
fdt           j        		fd}|                    d|           t          dd	d
dg| dz             
d         }|j        }d} ||          }|ssdt          j        |          t          j        |          t          j        |          t          j        |          dz  }t          t          j        |                    dx}x}x}}dS )zCWikipedia can reject the anonymous default python-httpx user agent.r   r   r	   r   c                                         | j                   t          dz                                  }t	          j        d|          S )Nr   r   r   )updateheadersr   r   r   r   )r   r    seen_headerss     r!   r"   zGtest_default_client_identifies_mentorcore_to_wikipedia.<locals>.handlerO   sE    GO,,, @@LLNN~c40000r#   c                 <     | dt          j                  i|S )Nr+   r%   )argskwargsr"   real_clients     r!   mock_clientzKtest_default_client_identifies_mentorcore_to_wikipedia.<locals>.mock_clientV   s(    {DSU%8%A%ASFSSSr#   z$compiler.stages.acquire.httpx.Clientrj   rk   rl   rm   r7   z
user-agentzMentorCore/zLassert %(py7)s
{%(py7)s = %(py3)s
{%(py3)s = %(py1)s.startswith
}(%(py5)s)
}rO   Nr$   )	r   r,   setattrr   
startswithrQ   rS   rW   rX   )r[   monkeypatchr{   r]   r^   rd   rf   re   r"   rz   rv   s           @@@r!   6test_default_client_identifies_mentorcore_to_wikipediar   K   sY   #%L1 1 1 1 1 1
 ,KT T T T T T >LLL 2)#4 	
 	5	 	 	 %?%0??00????????%????0??????????????????????????????????r#   c                ,   ddddg}t          || dz  t                                }|d         }|j        } |            }|sddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          d	z  }t          t          j	        |                    d x}}|j
        }d
}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j	        |	                    d x}x}}d S )Nzwikiquote-marcus-aureliusr   z-https://en.wikiquote.org/wiki/Marcus_Aurelius)r4   r5   r   r7   r8   rC   rD   rE   z.htmlrI   rK   rL   rM   rN   ro   rp   s
             r!   test_acquire_urlr   i   s    .B	
 	
G (U"2OOOF-.D;;;===44;=;!'!;'!!!!!!!!!!;'!!!!!!!!!!!!4!!!!!4!!!!!!;!!!!'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!r#   c                   | dz  }ddddg}t          ||t                                 t          j        t          j        d                     }t          |||          }|d         }|j        } |            }|s`d	t          j        |          t          j        |          t          j        |          d
z  }t          t          j	        |                    dx}x}}dS )z9Second call with same raw_dir should not hit the network.r7   r0   r1   r2   r3   r8   c                *    t          j        d          S )Ni  )r   r   )rs    r!   <lambda>z+test_acquire_skips_cached.<locals>.<lambda>   s    5>RUCVCV r#   r*   zAassert %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.exists
}()
})r?   r@   rB   N)
r   r.   r   r,   r&   rY   rQ   rS   rW   rX   )	r[   raw_dirr\   failingr=   r]   r^   rd   r`   s	            r!   test_acquire_skips_cachedr   w   s   G *% 	
 	
G lnn====lU%89V9V%W%WXXXG'w???F)*3*131133333333*333313333333333333333333333333333r#   c                  
 | dz  }ddddg}t          ||t                                 dg
d
fd}t          j        t          j        |                    }t          |||d           
d         }d}||k    }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }dd|iz  }	t          t          j	        |	                    dx}x}}dS )z2force=True must re-download even when file exists.r7   r0   r1   r2   r3   r8   r   r   r   r	   r   c                    dxx         dz  cc<   t           dz                                  }t          j        d|          S )Nr      r   r   r   )r   r   r   r   )r   r    
call_counts     r!   counting_handlerz6test_acquire_force_refetches.<locals>.counting_handler   sE    1 44@@BB~c40000r#   r*   T)r9   forcer   rI   )z%(py1)s == %(py4)s)r?   rH   zassert %(py6)spy6Nr$   )
r   r.   r   r,   r&   rQ   rR   rS   rW   rX   )r[   r   r\   r   counting_clientr]   rb   r^   rc   @py_format7r   s             @r!   test_acquire_force_refetchesr      sK   G *% 	
 	
G lnn====J1 1 1 1 1 1
 lU-@AQ-R-RSSSOoTJJJJa=A=A=A=Ar#   c                   t           dz  }t          |d          }d}||v }|st          j        d|fd||f          t          j        |          dt          j                    v st          j        |          rt          j        |          nddz  }dd	|iz  }t          t          j	        |                    d x}}t          |          }d
}||k    }|st          j        d|fd||f          dt          j                    v st          j        t                    rt          j        t                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }	dd|	iz  }
t          t          j	        |
                    d x}x}}d S )Nr   r1   rl   r:   r<   r   r>   rA   rB   d   >z/%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} > %(py6)slenrF   r?   r@   r   assert %(py8)spy8r   r   rQ   rR   rS   rT   rU   rV   rW   rX   r   r[   srcr   r]   r^   r_   r`   @py_assert5rd   r   @py_format9s              r!   test_extract_text_gutenbergr      s   
.
.C[))D$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$t99s9s?9s33tt9sr#   c                   t           dz  }t          |d          }d}||v }|st          j        d|fd||f          t          j        |          dt          j                    v st          j        |          rt          j        |          nddz  }dd	|iz  }t          t          j	        |                    d x}}t          |          }d
}||k    }|st          j        d|fd||f          dt          j                    v st          j        t                    rt          j        t                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }	dd|	iz  }
t          t          j	        |
                    d x}x}}d S )Nr   rk   Stoicr:   r<   r   r>   rA   rB   2   r   r   r   r   r   r   r   r   s              r!   test_extract_text_wikipediar      s   
:
:C[))D7d?7d7ddt99r9r>9r33tt9rr#   c                   t           dz  }t          |d          }d }||u}|st          j        d|fd||f          dt	          j                    v st          j        |          rt          j        |          ndt          j        |          dz  }dd|iz  }t          t          j	        |                    d x}}t          |          }d	}||k    }|st          j        d
|fd||f          dt	          j                    v st          j        t                    rt          j        t                    nddt	          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }	dd|	iz  }
t          t          j	        |
                    d x}x}}d S )Nr   r   )is not)z%(py0)s is not %(py3)sr   )rF   r@   rA   rB   r   r   r   r   r   r   r   )r   r   rQ   rR   rT   rU   rV   rS   rW   rX   r   )r[   r   r   r^   ra   r_   r`   r   rd   r   r   s              r!   test_extract_text_url_htmlr      s   
:
:CU##D4t4t44tt99q9q=9q33tt9qr#   )r	   r
   )r	   r(   )__doc__
__future__r   builtinsrT   _pytest.assertion.rewrite	assertionrewriterQ   pathlibr   r   compiler.stages.acquirer   r   __file__parentr   r'   r.   rh   rq   r   r   r   r   r   r   r   r-   r#   r!   <module>r      sD   U U " " " " " "                      = = = = = = = =X%,z9FB( ( ( (28 8 8 83 3 3 " " "@ @ @<" " "4 4 4$  4        r#   