3
Ud4                 @   s  d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z	 ddl
jjZddlZddljZddljjZG dd dZeeegZyddlmZ eje W n ek
r   Y nX ejjejj e!Z"ej#j$d	G d
d dZ%ej&dd Z'G dd dZ(dd Z)dS )z0
Tests for the pandas.io.common functionalities
    )StringION)Path)is_platform_windowsc               @   s    e Zd ZdZdd Zdd ZdS )CustomFSPathz%For testing fspath on unknown objectsc             C   s
   || _ d S )N)path)selfr    r   O/var/www/html/virt/lib64/python3.6/site-packages/pandas/tests/io/test_common.py__init__   s    zCustomFSPath.__init__c             C   s   | j S )N)r   )r   r   r   r	   
__fspath__   s    zCustomFSPath.__fspath__N)__name__
__module____qualname____doc__r
   r   r   r   r   r	   r      s   r   )localz*ignore:can't resolve package:ImportWarningc               @   s  e Zd ZdZdd Zdd Zdd Zejdd	d
 Z	dd Z
ejjdd\d]d^d_d`dadbdcddg	ejjdedd Zdd  Zd!d" Zd#d$ Zejjd%ejd&ed'fejd&ed(fejd)ed*fejd+ed,fejd-ed.fejd&ed/fejd&ed0fejd&ed1fejd&ed2fg	d3d4 Z ejjd%ejd&ed'fej!d&ed'fejd&ed(fejd)ed*fejd+ed,fejd-ed.fejd&ed/fejd&ed0fejd&ed1fejd&ed2fg
d5d6 Z"ejjd7ejd&defej!d&dffejd&dgfejd)dhfejd+difejd-djfejd&dkfejd&dlfejd&dmfejd&dnfg
ejj#dHdIdJ Z$ejjdKdLi d&fdMdNdOidOfdPi d+fdQi d&fdRi d&fdSi d&fdTi d&fdUdVej%dWid&fgdXdY Z&dZd[ Z'dS )oTestCommonIOCapabilitiesziindex,A,B,C,D
foo,2,3,4,5
bar,7,8,9,10
baz,12,13,14,15
qux,12,13,14,15
foo2,12,13,14,15
bar2,12,13,14,15
c             C   sB   d}t j|}||ksttjj|s*ttjj||ks>td S )Nz
~/sometest)icom_expand_userAssertionErrorosr   isabs
expanduser)r   filenameexpanded_namer   r   r	   test_expand_user7   s
    
z)TestCommonIOCapabilities.test_expand_userc             C   s2   d}t j|}||ksttjj||ks.td S )Nz/somefolder/sometest)r   r   r   r   r   r   )r   r   r   r   r   r	   test_expand_user_normal_path?   s    
z5TestCommonIOCapabilities.test_expand_user_normal_pathc             C   sB   t jtd}|dkstt jtd}|tjjddks>td S )N.zfoo//barfoobar)r   stringify_pathr   r   r   r   join)r   rel_pathZredundant_pathr   r   r	   test_stringify_path_pathlibF   s    z4TestCommonIOCapabilities.test_stringify_path_pathlibzpy.pathc             C   s8   t jjdd}t jj|}t|}tj||ks4td S )Nr   r   )r   r   r    abspath	LocalPathr   r   r   )r   r   abs_pathZlpathr   r   r	   test_stringify_path_localpathL   s    z6TestCommonIOCapabilities.test_stringify_path_localpathc             C   s"   t d}tj|}|dkstd S )Nzfoo/bar.csv)r   r   r   r   )r   presultr   r   r	   test_stringify_path_fspathS   s    
z3TestCommonIOCapabilities.test_stringify_path_fspathzextension,expected N.gzgzip.bz2bz2.zipzip.xzxz.GZ.BZ2.ZIP.XZ	path_typec             C   s*   |d| }t j|dd}||ks&td S )Nzfoo/bar.csvZinfer)compression)r   Zinfer_compressionr   )r   	extensionexpectedr7   r   r8   r   r   r	    test_infer_compression_from_pathX   s    z9TestCommonIOCapabilities.test_infer_compression_from_pathc             C   sT   d}t j|\}}}}||ks"ttjj|s2ttjj||ksFt| sPtd S )Nz
~/sometest)r   get_filepath_or_bufferr   r   r   r   r   )r   r   filepath_or_buffer_should_closer   r   r	   %test_get_filepath_or_buffer_with_pathl   s    z>TestCommonIOCapabilities.test_get_filepath_or_buffer_with_pathc             C   s2   t  }tj|\}}}}||ks$t| s.td S )N)r   r   r<   r   )r   Zinput_bufferr=   r>   r?   r   r   r	   'test_get_filepath_or_buffer_with_buffert   s
    z@TestCommonIOCapabilities.test_get_filepath_or_buffer_with_bufferc             C   s   t jt| jdd}t j|dd}t jt| j}tj|| t jt| jdd}t|}tj||jdg  tjt j||jdd   d S )N   )	chunksizeT)Zignore_indexr   )	pdread_csvr   data1concattmassert_frame_equalnextZiloc)r   readerr(   r:   itfirstr   r   r	   test_iterator|   s    z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   csvtxtZxlrdZxlsxZpyarrowfeathertablesh5ZdtaZsas7bdatjsonpicklec             C   s   t j| tjjtdd| }d| d}d| d}d}d}	d	| d
| d}
d| d}d| d}d| }t j|d| d| d| d|	 d|
 d| d| d| dd || W d Q R X d S )Ndatazdoes_not_exist.zFile (b')?.+does_not_exist\.z'? does not existz:\[Errno 2\] No such file or directory: '.+does_not_exist\.'zExpected object or valuez7path_or_buf needs to be a string file path or file-likez#\[Errno 2\] File .+does_not_exist\.z$ does not exist: '.+does_not_exist\.u<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.z?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.z+Failed to open local file.+does_not_exist\.(|))match)pytestimportorskipr   r   r    HEREraises)r   rK   moduleerror_classfn_extr   msg1msg2msg3msg4msg5msg6msg7msg8r   r   r	   test_read_non_existent   s    

<z/TestCommonIOCapabilities.test_read_non_existentc             C   s   t j| tjjdd| }|jtddd  d| d}d| d	}d
}	d}
d| d| d	}d| d	}d| d	}d| }t j|d| d| d|	 d|
 d| d| d| d| dd || W d Q R X d S )N~zdoes_not_exist.r   c             S   s   t jjd| S )Nr   )r   r   r    )xr   r   r	   <lambda>   s    zJTestCommonIOCapabilities.test_read_expands_user_home_dir.<locals>.<lambda>zFile (b')?.+does_not_exist\.z'? does not existz:\[Errno 2\] No such file or directory: '.+does_not_exist\.rW   z0Unexpected character found when decoding 'false'z7path_or_buf needs to be a string file path or file-likez#\[Errno 2\] File .+does_not_exist\.z$ does not exist: '.+does_not_exist\.u<   \[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.z?\[Errno 2\] File o directory non esistente: '.+does_not_exist\.z+Failed to open local file.+does_not_exist\.rX   rY   rZ   )r[   )r\   r]   r   r   r    setattrr   r_   )r   rK   r`   ra   rb   Zmonkeypatchr   rc   rd   re   rf   rg   rh   ri   rj   r   r   r	   test_read_expands_user_home_dir   s    

<z8TestCommonIOCapabilities.test_read_expands_user_home_dirzreader, module, pathiorV   iris.csvfixed_widthfixed_width_format.txtexcel
test1.xlsxfeather-0_3_1.feather
legacy_hdfdatetimetz_object.h5statastata10_115.dtasastest1.sas7bdattsframe_v012.jsoncategorical.0.25.0.picklezignore:This method will be removed in future versions.  Use 'tree.iter\(\)' or 'list\(tree.iter\(\)\)' instead.:PendingDeprecationWarningc             C   sR   t j| || }t|}||}||}|jdrBtj|| ntj|| d S )Nz.pickle)r\   r]   r   endswithrH   Zassert_categorical_equalrI   )r   rK   r`   r   datapathmypathr(   r:   r   r   r	   test_read_fspath_all   s    %

z-TestCommonIOCapabilities.test_read_fspath_allz"writer_name, writer_kwargs, moduleto_csvZto_excelengineZxlwtZ
to_featherZto_htmlto_jsonZto_latexZ	to_pickleZto_stataZ
time_stampz2019-01-01 00:00c             C   s   t jd}t jd}tjdddgi}|}|}tj| t|}	t||}
|
|f| t|d}|j	 }W d Q R X |
|	f| t|d}|j	 }W d Q R X ||kst
W d Q R X W d Q R X d S )NstringfspathArB      rb)rH   ensure_cleanrD   	DataFramer\   r]   r   getattropenreadr   )r   Zwriter_nameZwriter_kwargsr`   p1p2dfr   r   r   writerfr:   r(   r   r   r	   test_write_fspath_all  s    



z.TestCommonIOCapabilities.test_write_fspath_allc       	      C   s   t jd tjdddgi}tjd}tjd}|V}|F}t|}|j|dd |j|dd tj|dd}tj|dd}W d Q R X W d Q R X tj	|| d S )	NrR   r   rB   r   r   r   r   )key)
r\   r]   rD   r   rH   r   r   Zto_hdfread_hdfrI   )	r   r   r   r   r   r   r   r(   r:   r   r   r	   test_write_fspath_hdf5,  s    


"z/TestCommonIOCapabilities.test_write_fspath_hdf5)r*   N)r+   r,   )r-   r.   )r/   r0   )r1   r2   )r3   r,   )r4   r.   )r5   r0   )r6   r2   )rq   rV   rO   rr   )rq   rV   rO   rr   )rq   rV   rs   rt   )rq   rV   ru   rv   )rq   rV   rQ   rw   )rq   rV   rx   ry   )rq   rV   rz   r{   )rq   r|   rV   r}   )rq   rT   rV   r~   )rq   rV   rU   r   )(r   r   r   rF   r   r   r"   tdZ
skip_if_nor&   r)   r\   markZparametrize
path_typesr;   r@   rA   rN   rD   rE   FileNotFoundErrorZread_fwfZ
read_excelZread_featherIOErrorr   Z
read_stataZread_sas	read_json
ValueErrorZread_picklerk   Z
read_tablerp   filterwarningsr   to_datetimer   r   r   r   r   r	   r   ,   s   	





 r   c             C   s   | ddddS )Nrq   rV   rO   ztest_mmap.csvr   )r   r   r   r	   	mmap_fileA  s    r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestMMapWrapperc             C   s   t d}dd |_t r"d}t}n
d}tj}tj||d tj	| W d Q R X t
|d}|j  d}tjt|d tj	| W d Q R X d S )	NzI am not a filec               S   s   dS )NrB   r   r   r   r   r	   rn   I  s    z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22])r[   rzI/O operation on closed file)r   filenor   OSErrormmaperrorr\   r_   r   _MMapWrapperr   closer   )r   r   Znon_filemsgerrtargetr   r   r	   test_constructor_bad_fileG  s    

z)TestMMapWrapper.test_constructor_bad_filec             C   sr   t |d}tj|}W d Q R X t|j}dd |D }|jd x|D ]}t||sHtqHW t|d sntd S )Nr   c             S   s   g | ]}|j d s|qS )__)
startswith).0attrr   r   r	   
<listcomp>b  s    z1TestMMapWrapper.test_get_attr.<locals>.<listcomp>__next__r   )r   r   r   dirr   appendhasattrr   )r   r   r   wrapperattrsr   r   r   r	   test_get_attr]  s    


zTestMMapWrapper.test_get_attrc             C   sx   t |d}tj|}|j }W d Q R X x(|D ] }t|}|j |j ks.tq.W tjt	dd t| W d Q R X d S )Nr   z^$)r[   )
r   r   r   	readlinesrJ   stripr   r\   r_   StopIteration)r   r   r   r   lineslineZ	next_liner   r   r	   	test_nextj  s    

zTestMMapWrapper.test_nextc             C   sR   t j @}t j }|j| tjtdd tj|dd W d Q R X W d Q R X d S )NzUnknown engine)r[   Zpyt)r   )	rH   r   ZmakeDataFramer   r\   r_   r   rD   rE   )r   r   r   r   r   r	   test_unknown_enginev  s
    

z#TestMMapWrapper.test_unknown_engineN)r   r   r   r   r   r   r   r   r   r   r	   r   F  s   r   c               C   s`   t jdstt jdstt jd s,tt jd s<tt jd sLtt jd s\td S )Nzgcs://pandas/somethingelse.comzgs://pandas/somethingelse.comzhttp://pandas/somethingelse.comzrandom:pandas/somethingelse.comz/local/pathzrelative/local/path)r   Zis_fsspec_urlr   r   r   r   r	   test_is_fsspec_url~  s    r   )*r   rq   r   r   r   pathlibr   r\   Zpandas.compatr   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   ZpandasrD   Zpandas._testingZ_testingrH   Zpandas.io.commoncommonr   r   strr   Zpy.pathr   r$   r   ImportErrorr   r#   dirname__file__r^   r   r   r   Zfixturer   r   r   r   r   r   r	   <module>   s0   

  8