3
®ybŽ4  ã               @   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   ú?/tmp/pip-install-4cy230dp/pandas/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.picklez‰ignore: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_htmlZ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s®t
‚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ƒ   Z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   Z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