o
    V=ºi©*  ã                   @   sT  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	  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 d1dd„Zdad	d
„ Zdd„ ZdZdZdZdZg 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(G d#d$„ d$ƒZ)G d%d&„ d&ƒZ*G d'd(„ d(ƒZ+G d)d*„ d*ƒZ,G d+d,„ d,ƒZ-G d-d.„ d.ƒZ.d/d0„ Z/dS )2é    N)ÚmkdtempÚmkstempÚNamedTemporaryFile)Úrmtree)Úassert_Úassert_equalÚassert_raises)Úurlparse)ÚURLErrorc                 C   s    | t ƒ krtdd�}|S tdƒ‚)z$Stub to replace urlopen for testing.Zurltmp_)ÚprefixzName or service not knownN)Úvalid_httpurlr   r
   )ÚurlÚdataÚtmpfile© r   úfC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\numpy/lib/tests/test__datasource.pyÚurlopen_stub   s   

r   c                   C   s   t jatt _d S ©N)Úurllib_requestÚurlopenÚold_urlopenr   r   r   r   r   Úsetup_module   s   
r   c                   C   s
   t t_d S r   )r   r   r   r   r   r   r   Úteardown_module!   s   
r   zhttp://www.google.com/z
index.htmlzhttp://fake.abc.web/site/zfake.txt)z/etc/shadowz../../shadowz..\system.datzc:\windows\system.dats   three is the magic numberc                 C   s"   t dd| dd�\}}t |¡ |S )Nú.txtÚdstmp_T)Úsuffixr   ÚdirÚtext)r   ÚosÚclose©ZfiledirÚfdÚpathr   r   r   Úvalid_textfile2   s   
r#   c                 C   s*   t dd| d�\}}t |¡ t |¡ |S )Nr   r   )r   r   r   )r   r   r   Úremover    r   r   r   Úinvalid_textfile9   s   

r%   c                   C   ó   t t S r   )Ú	http_pathÚ	http_filer   r   r   r   r   A   ó   r   c                   C   r&   r   )Úhttp_fakepathÚhttp_fakefiler   r   r   r   Úinvalid_httpurlE   r)   r,   c                   C   ó   t S r   )r'   r   r   r   r   Úvalid_baseurlI   ó   r.   c                   C   r-   r   )r*   r   r   r   r   Úinvalid_baseurlM   r/   r0   c                   C   r-   r   )r(   r   r   r   r   Úvalid_httpfileQ   r/   r1   c                   C   r-   r   )r+   r   r   r   r   Úinvalid_httpfileU   r/   r2   c                   @   sT   e 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 )ÚTestDataSourceOpenc                 C   ó   t ƒ | _t | j¡| _d S r   ©r   ÚtmpdirÚ
datasourceÚ
DataSourceÚds©Úselfr   r   r   Úsetup_methodZ   ó   zTestDataSourceOpen.setup_methodc                 C   ó   t | jƒ | `d S r   ©r   r6   r9   r:   r   r   r   Úteardown_method^   ó   
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j  tƒ ¡}t|ƒ | ¡  d S r   )r9   Úopenr   r   r   )r;   Úfhr   r   r   Útest_ValidHTTPb   s   z!TestDataSourceOpen.test_ValidHTTPc              
   C   s^   t ƒ }tt| jj|ƒ z	| j |¡ W d S  ty. } zt|jd u ƒ W Y d }~d S d }~ww r   )r,   r   ÚOSErrorr9   rB   r   Úerrno)r;   r   Úer   r   r   Útest_InvalidHTTPg   s   €þz#TestDataSourceOpen.test_InvalidHTTPc                 C   s   t t| jjtƒ ƒ d S r   )r   r
   r9   Ú_cacher,   r:   r   r   r   Útest_InvalidHTTPCacheURLErrorp   ó   z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | jƒ}| j |¡}t|ƒ | ¡  d S r   )r#   r6   r9   rB   r   r   )r;   Ú
local_filerC   r   r   r   Útest_ValidFiles   s   
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | jƒ}tt| jj|ƒ d S r   )r%   r6   r   rE   r9   rB   )r;   Zinvalid_filer   r   r   Útest_InvalidFiley   s   
z#TestDataSourceOpen.test_InvalidFilec                 C   s€   zdd l }W n ty   t ¡  Y nw tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.gzÚw)ÚgzipÚImportErrorÚpytestÚskipr   r"   Újoinr6   rB   ÚwriteÚ
magic_liner   r9   Úreadliner   )r;   rP   ÚfilepathÚfpÚresultr   r   r   Útest_ValidGzipFile}   ó   þ
z%TestDataSourceOpen.test_ValidGzipFilec                 C   s€   zdd l }W n ty   t ¡  Y nw tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.bz2rO   )Úbz2rQ   rR   rS   r   r"   rT   r6   ÚBZ2FilerU   rV   r   r9   rB   rW   r   )r;   r]   rX   rY   rZ   r   r   r   Útest_ValidBz2File�   r\   z$TestDataSourceOpen.test_ValidBz2FileN)Ú__name__Ú
__module__Ú__qualname__r<   r@   rD   rH   rJ   rM   rN   r[   r_   r   r   r   r   r3   Y   s    	r3   c                   @   ó<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTestDataSourceExistsc                 C   r4   r   r5   r:   r   r   r   r<   Ÿ   r=   z!TestDataSourceExists.setup_methodc                 C   r>   r   r?   r:   r   r   r   r@   £   rA   z$TestDataSourceExists.teardown_methodc                 C   ó   t | j tƒ ¡ƒ d S r   )r   r9   Úexistsr   r:   r   r   r   rD   §   rK   z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | j tƒ ¡dƒ d S ©NF)r   r9   rf   r,   r:   r   r   r   rH   ª   s   z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | jƒ}t| j |¡ƒ tƒ }t |ƒ}t| j |¡ƒ t|ƒ d S r   )r#   r6   r   r9   rf   r   r   )r;   r   Zlocaldirr   r   r   rM   ­   s   
z#TestDataSourceExists.test_ValidFilec                 C   ó    t | jƒ}t| j |¡dƒ d S rg   )r%   r6   r   r9   rf   ©r;   r   r   r   r   rN   ·   ó   
z%TestDataSourceExists.test_InvalidFileN)	r`   ra   rb   r<   r@   rD   rH   rM   rN   r   r   r   r   rd   ž   s    
rd   c                   @   sL   e 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S )ÚTestDataSourceAbspathc                 C   s"   t j tƒ ¡| _t | j¡| _d S r   )r   r"   Úabspathr   r6   r7   r8   r9   r:   r   r   r   r<   ½   s   z"TestDataSourceAbspath.setup_methodc                 C   r>   r   r?   r:   r   r   r   r@   Á   rA   z%TestDataSourceAbspath.teardown_methodc                 C   sN   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡ƒ d S ©Nú/)r	   r   r   r"   rT   r6   ÚstripÚsepr   r9   rl   )r;   ÚschemeÚnetlocÚupathÚpmsÚqryÚfrgÚ
local_pathr   r   r   rD   Å   s
   ÿz$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | jƒ}tj |¡d }t|| j |¡ƒ t|| j |¡ƒ d S ©Néÿÿÿÿ)r#   r6   r   r"   Úsplitr   r9   rl   )r;   r   Útmpfilenamer   r   r   rM   Ë   s   
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡kƒ d S rm   )r	   r,   r   r"   rT   r6   ro   rp   r   r9   rl   r   )r;   rq   rr   rs   rt   ru   rv   Zinvalidhttpr   r   r   rH   Ó   s
   ÿz&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | jƒ}t | jƒ}tj |¡d }t|| j |¡kƒ t|| j |¡kƒ d S rx   )r#   r6   r   r"   rz   r   r9   rl   )r;   Zinvalidfiler   r{   r   r   r   rN   Ù   s
   

z&TestDataSourceAbspath.test_InvalidFilec                    s´   t ˆ jƒ}tj |¡d }‡ fdd„}t|tƒ ƒ ˆ j¡ƒ t|tƒ ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ t	D ]}t|t
| ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ q?d S )Nry   c                    ó   t j ˆ j | ¡¡S r   )r   r"   rl   r9   ©Úxr:   r   r   Ú<lambda>æ   ó    z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r#   r6   r   r"   rz   r   r   Ú
startswithr,   Úmalicious_filesr'   )r;   r   r{   Útmp_pathÚfnr   r:   r   Útest_sandboxingâ   s   
þz%TestDataSourceAbspath.test_sandboxingc                 C   sJ   t j}zdt _|  ¡  |  ¡  |  ¡  |  ¡  |  ¡  W |t _d S |t _w ©Nú\)r   rp   rD   rM   rH   rN   r…   ©r;   Zorig_os_sepr   r   r   Útest_windows_os_sepð   s   
z)TestDataSourceAbspath.test_windows_os_sepN)r`   ra   rb   r<   r@   rD   rM   rH   rN   r…   r‰   r   r   r   r   rk   ¼   s    	rk   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestRepositoryAbspathc                 C   s&   t j tƒ ¡| _t tƒ | j¡| _d S r   )	r   r"   rl   r   r6   r7   Ú
Repositoryr.   Úreposr:   r   r   r   r<   þ   s   z"TestRepositoryAbspath.setup_methodc                 C   r>   r   ©r   r6   rŒ   r:   r   r   r   r@     rA   z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t tƒ ƒ\}}}}}}tj | jj|| tj¡ d¡¡}| j 	t
ƒ ¡}t||ƒ d S rm   )r	   r   r   r"   rT   rŒ   Ú	_destpathro   rp   rl   r1   r   )	r;   rq   rr   rs   rt   ru   rv   rw   rX   r   r   r   rD     s   ÿz$TestRepositoryAbspath.test_ValidHTTPc                    s\   ‡ fdd„}t |tƒ ƒ ˆ j¡ƒ tD ]}t |t| ƒ ˆ j¡ƒ t ||ƒ ˆ j¡ƒ qd S )Nc                    r|   r   )r   r"   rl   rŒ   r}   r:   r   r   r     r€   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r1   r�   r6   r‚   r'   )r;   rƒ   r„   r   r:   r   r…     s   þz%TestRepositoryAbspath.test_sandboxingc                 C   s2   t j}zdt _|  ¡  |  ¡  W |t _d S |t _w r†   )r   rp   rD   r…   rˆ   r   r   r   r‰     s   
z)TestRepositoryAbspath.test_windows_os_sepN)r`   ra   rb   r<   r@   rD   r…   r‰   r   r   r   r   rŠ   ý   s    rŠ   c                   @   rc   )ÚTestRepositoryExistsc                 C   s   t ƒ | _t tƒ | j¡| _d S r   )r   r6   r7   r‹   r.   rŒ   r:   r   r   r   r<     s   z!TestRepositoryExists.setup_methodc                 C   r>   r   r�   r:   r   r   r   r@   #  rA   z$TestRepositoryExists.teardown_methodc                 C   s   t | jƒ}t| j |¡ƒ d S r   )r#   r6   r   rŒ   rf   ri   r   r   r   rM   '  s   
z#TestRepositoryExists.test_ValidFilec                 C   rh   rg   )r%   r6   r   rŒ   rf   ri   r   r   r   rN   ,  rj   z%TestRepositoryExists.test_InvalidFilec                 C   re   r   )r   rŒ   rf   r   r:   r   r   r   Útest_RemoveHTTPFile0  rK   z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t ƒ }t|ƒ\}}}}}}tj | jj|¡}t |d¡ t|ƒ}	t	| j 
|	¡ƒ d S )NiÀ  )r   r	   r   r"   rT   rŒ   rŽ   Úmkdirr#   r   rf   )
r;   Ú	localfilerq   rr   rs   rt   ru   rv   rw   r   r   r   r   Útest_CachedHTTPFile3  s   z(TestRepositoryExists.test_CachedHTTPFileN)	r`   ra   rb   r<   r@   rM   rN   r�   r“   r   r   r   r   r�     s    r�   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestOpenFuncc                 C   s   t ƒ | _d S r   )r   r6   r:   r   r   r   r<   @  s   zTestOpenFunc.setup_methodc                 C   s   t | jƒ d S r   )r   r6   r:   r   r   r   r@   C  s   zTestOpenFunc.teardown_methodc                 C   sH   t | jƒ}tj|| jd�}t|ƒ | ¡  t |¡}t|ƒ | ¡  d S )N)Zdestpath)r#   r6   r7   rB   r   r   )r;   rL   rY   r   r   r   Útest_DataSourceOpenF  s   

z TestOpenFunc.test_DataSourceOpenN)r`   ra   rb   r<   r@   r•   r   r   r   r   r”   ?  s    r”   c                  C   s   t  ¡ } | `|  ¡  d S r   )r7   r8   Z
_istmpdestÚ__del__)r9   r   r   r   Útest_del_attr_handlingQ  s   r—   r   )0r   rR   Útempfiler   r   r   Úshutilr   Znumpy.lib._datasourceÚlibZ_datasourcer7   Znumpy.testingr   r   r   Úurllib.requestÚrequestr   Úurllib.parser	   Úurllib.errorr
   r   r   r   r   r'   r(   r*   r+   r‚   rV   r#   r%   r   r,   r.   r0   r1   r2   r3   rd   rk   rŠ   r�   r”   r—   r   r   r   r   Ú<module>   sD    
	EA!!