3
Êb_0  ã               @   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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-build-jkwsxyiy/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dVdWdXdYdZgƒ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d+fejd"ed,fejd"ed-fg	ƒ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d*fejd"ed+fejd"ed,fejd"ed-fg
ƒd0d1„ ƒZ"ejjd2ejd"d[fej!d"d\fejd"d]fejd%d^fejd'd_fejd(d`fejd"dafejd"dbfejd"dcfejd"ddfg
ƒdCdD„ ƒZ#ejjdEdFi d"fdGdHdIidIfdJi d'fdKi d"fdLi d"fdMi d"fdNi d"fdOdPej$dQƒid"fgƒdRdS„ ƒZ%dTdU„ Z&dS )eÚ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Ú	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Úexpectedr3   r   r4   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_pathb   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   r8   r   )r   Zinput_bufferr9   r:   r;   r   r   r	   Ú'test_get_filepath_or_buffer_with_bufferj   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(   r6   ÚitÚfirstr   r   r	   Útest_iteratorr   s    z&TestCommonIOCapabilities.test_iteratorz#reader, module, error_class, fn_extr   ÚcsvÚtxtZxlrdZxlsxÚfeatherÚtablesÚh5ZdtaZsas7bdatÚjsonÚpicklec             C   s¸   t j|ƒ tjjtdd| ƒ}dj|ƒ}d|› d�}d}d}	d|› d	|› d�}
d
|› d�}d|› d�}t j|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\.{}'? 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\.ú(ú|ú))Úmatch)ÚpytestÚimportorskipr   r   r    ÚHEREÚformatÚraises)r   rG   ÚmoduleÚerror_classÚfn_extr   Úmsg1Úmsg2Úmsg3Úmsg4Úmsg5Úmsg6Úmsg7r   r   r	   Útest_read_non_existant~   s    

8z/TestCommonIOCapabilities.test_read_non_existantc             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	�}t j|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\.rS   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\.rT   rU   rV   )rW   )rX   rY   r   r   r    Úsetattrr   r\   )r   rG   r]   r^   r_   Zmonkeypatchr   r`   ra   rb   rc   rd   re   rf   r   r   r	   Útest_read_expands_user_home_dir¡   s    
8z8TestCommonIOCapabilities.test_read_expands_user_home_dirzreader, module, pathÚiorR   ú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.picklec             C   sR   t j|ƒ ||Ž }t|ƒ}||ƒ}||ƒ}|jdƒrBtj||ƒ ntj||ƒ d S )Nz.pickle)rX   rY   r   ÚendswithrD   Zassert_categorical_equalrE   )r   rG   r]   r   ÚdatapathÚmypathr(   r6   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ÚAr>   é   Úrb)rD   Úensure_cleanr@   Ú	DataFramerX   rY   r   ÚgetattrÚopenÚreadr   )r   Zwriter_nameZwriter_kwargsr]   Úp1Úp2Údfr‚   rƒ   r~   ÚwriterÚfr6   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 )	NrN   r„   r>   r…   r‚   rƒ   r   )Úkey)
rX   rY   r@   rˆ   rD   r‡   r   Zto_hdfÚread_hdfrE   )	r   rŽ   rŒ   r�   r‚   rƒ   r~   r(   r6   r   r   r	   Útest_write_fspath_hdf5  s    


"z/TestCommonIOCapabilities.test_write_fspath_hdf5)r*   N)r+   r,   )r-   r.   )r/   r0   )r1   r2   )rm   rR   rK   rn   )rm   rR   rK   rn   )rm   rR   ro   rp   )rm   rR   rq   rr   )rm   rR   rM   rs   )rm   rR   rt   ru   )rm   rR   rv   rw   )rm   rx   rR   ry   )rm   rP   rR   rz   )rm   rR   rQ   r{   )'r   r   r   rB   r   r   r"   ÚtdZ
skip_if_nor&   r)   rX   ÚmarkZparametrizeÚ
path_typesr7   r<   r=   rJ   r@   rA   ÚFileNotFoundErrorZread_fwfZ
read_excelZread_featherÚ	Exceptionr“   Z
read_stataZread_sasZ	read_jsonÚ
ValueErrorZread_picklerg   Z
read_tablerl   r   Zto_datetimer‘   r”   r   r   r   r	   r   ,   s†   	





 r   c             C   s   | ddddƒS )Nrm   rR   rK   ztest_mmap.csvr   )r}   r   r   r	   Ú	mmap_file-  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 )Nr>   éÿÿÿÿr   r   r   r   r	   rj   5  s    z;TestMMapWrapper.test_constructor_bad_file.<locals>.<lambda>zThe parameter is incorrectz
[Errno 22])rW   ÚrzI/O operation on closed file)r   Úfilenor   ÚOSErrorÚmmapÚerrorrX   r\   r   Ú_MMapWrapperrŠ   Úcloserš   )r   r›   Znon_fileÚmsgÚerrÚtargetr   r   r	   Útest_constructor_bad_file3  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>N  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_attrI  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^$)rW   )
rŠ   r   r£   Ú	readlinesrF   Ústripr   rX   r\   ÚStopIteration)r   r›   r§   r²   ÚlinesÚlineZ	next_liner   r   r	   Ú	test_nextV  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)rW   Zpyt)r�   )	rD   r‡   ZmakeDataFramer€   rX   r\   rš   r@   rA   )r   r   rŽ   r   r   r	   Útest_unknown_engineb  s
    

z#TestMMapWrapper.test_unknown_engineN)r   r   r   r¨   r´   rº   r»   r   r   r   r	   rœ   2  s   rœ   ))r   rm   r   r¡   r   Zpathlibr   rX   Zpandas.compatr   Zpandas.util._test_decoratorsÚutilZ_test_decoratorsr•   Zpandasr@   Zpandas._testingZ_testingrD   Zpandas.io.commonÚcommonr   r   Ústrr—   Zpy.pathr   r$   r°   ÚImportErrorr   r#   ÚdirnameÚ__file__rZ   r–   Úfilterwarningsr   Zfixturer›   rœ   r   r   r   r	   Ú<module>   s.   

  