U
    ùQ”iÇ  ã                	   @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZ d dlmZ ejdd„ ƒZe d¡ej d	d
ddddg¡dd„ ƒƒZeeedœdd„Ze d¡ej dddg¡dd„ ƒƒZ e d¡e d¡dd„ ƒƒZ!e "d¡dd„ ƒZ#dS )é    )ÚBytesION)Ú	DataFrameÚ
date_rangeÚread_csvÚ
read_excelÚ	read_jsonÚread_parquet)Ú_compression_to_extension)Ú_test_decoratorsc                    sB   ddl }tƒ ‰ dd„ ˆ _G ‡ fdd„d|jƒ}|jd|dd	� ˆ S )
z"Emulate GCS using a binary buffer.r   Nc                   S   s   dS )NT© r   r   r   ú</tmp/pip-unpacked-wheel-58ppya8e/pandas/tests/io/test_gcs.pyÚ<lambda>   ó    zgcs_buffer.<locals>.<lambda>c                       s$   e Zd Ze‡ fdd„ƒZdd„ ZdS )z%gcs_buffer.<locals>.MockGCSFileSystemc                     s   ˆ   d¡ ˆ S )Nr   )Úseek)ÚargsÚkwargs©Ú
gcs_bufferr   r   Úopen   s    
z*gcs_buffer.<locals>.MockGCSFileSystem.openc                 [   s   |ddœgS )NÚfile)ÚnameÚtyper   )ÚselfÚpathr   r   r   r   Úls$   s    z(gcs_buffer.<locals>.MockGCSFileSystem.lsN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r   r   r   ÚMockGCSFileSystem   s   r   ZgsT)Úclobber)Úfsspecr   ÚcloseÚAbstractFileSystemZregister_implementation)Úmonkeypatchr!   r   r   r   r   r      s    
r   ZgcsfsÚformatÚcsvÚjsonÚparquetÚexcelÚmarkdownc                 C   sú   t ddgdtjgddgtddd�d	œƒ}d
|› �}|dkrZ|j|dd� t|dgdd�}n�|dkr‚d}| |¡ t|dgdd�}nh|dkr¤| |¡ t	|dgd�}nF|dkrÊt
 d¡ | |¡ t|ƒ}n |dkrêt
 d¡ | |¡ |}t ||¡ dS )zE
    Test that many to/read functions support GCS.

    GH 33987
    é   é   ç       @ÚtÚsú
2018-06-18é   ©Zperiods©ÚintÚfloatÚstrÚdtzgs://test/test.r&   T)Úindexr7   r   )Zparse_datesÚ	index_colr)   zgs://test/test.xlsxr'   )Zconvert_datesr(   Zpyarrowr*   ZtabulateN)r   ÚnpÚnanr   Úto_csvr   Zto_excelr   Zto_jsonr   ÚpytestZimportorskipÚ
to_parquetr   Zto_markdownÚtmÚassert_frame_equal)r   r%   Údf1r   Zdf2r   r   r   Útest_to_read_gcs.   s6    	
üÿ	







rB   )ÚresultÚexpectedÚcompressionc              
   C   s(  |dkrjt  t| ƒ¡�L}t  t|ƒ¡�2}t| ¡ | ¡ ƒD ]\}}|j|jks:t‚q:W 5 Q R X W 5 Q R X nº|dk�rtjt| ƒd��Š}tjt|ƒd��n}t| 	¡ | 	¡ ƒD ]T\}	}
| 
|	¡}| 
|
¡}|dk|dkksÚt‚|dk	rª|dk	rª| ¡ | ¡ ksªt‚qªW 5 Q R X W 5 Q R X n| |k�s$t‚dS )a  
    For zip compression, only compare the CRC-32 checksum of the file contents
    to avoid checking the time-dependent last-modified timestamp which
    in some CI builds is off-by-one

    See https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
    ÚzipÚtar)ÚfileobjN)ÚzipfileÚZipFiler   rF   ÚinfolistÚCRCÚAssertionErrorÚtarfiler   Ú
getmembersÚextractfileÚread)rC   rD   rE   ÚexpÚresZres_infoZexp_infoZtar_expZtar_resZtar_res_infoZtar_exp_infoZactual_fileZexpected_filer   r   r   Úassert_equal_zip_safeX   s,    ÿ(
ÿ ÿ

,rT   Úencodingzutf-8Úcp1251c                 C   sî   t  ¡ }d|i}|dkr d|d< tƒ }|j|||dd� d}|j|||d� |  ¡ }| ¡ }t|||ƒ t|d	||d
�}	t  ||	¡ t| }
d|d< |d|
› �7 }|j|||d� |  ¡ }| ¡ }t|||ƒ t|d	d|d
�}	t  ||	¡ dS )z›
    Compression and encoding should with GCS.

    GH 35677 (to_csv, compression), GH 26124 (to_csv, encoding), and
    GH 32392 (read_csv, encoding)
    ÚmethodÚgzipr+   ÚmtimeÚwb)rE   rU   Úmodeúgs://test/test.csv)rE   rU   r   )r9   rE   rU   ZinferÚ.N)	r?   ZmakeDataFramer   r<   ÚgetvaluerT   r   r@   r	   )r   Zcompression_onlyrU   ZdfrE   ÚbufferZpath_gcsrS   rD   Zread_dfZfile_extr   r   r   Ú$test_to_csv_compression_encoding_gcsw   s6    	   ÿr`   Úfastparquetc                    sj   ddl m} tddgdtjgddgtdd	d
�dœƒ}G ‡ fdd„d|ƒ}|  d|¡ |jddddd� dS )zCRegression test for writing to a not-yet-existent GCS Parquet file.r   )r#   r+   r,   r-   r.   r/   r0   r1   r2   r3   c                       s   e Zd Zd‡ fdd„	ZdS )z7test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystemÚrc                    s    d|krt ‚ttj ˆ d¡|ƒS )NÚwztest.parquet)ÚFileNotFoundErrorr   Úosr   Újoin)r   r   r[   r   ©Útmpdirr   r   r   ³   s    z<test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystem.openN)rb   )r   r   r   r   r   rg   r   r   r   ²   s   r   zgcsfs.GCSFileSystemr\   Tra   N)r8   ZenginerE   )r!   r#   r   r:   r;   r   Úsetattrr>   )r$   rh   r#   rA   r   r   rg   r   Útest_to_parquet_gcs_new_file£   s     
üÿ	   ÿrj   c                	   C   s"   t  t¡� tdƒ W 5 Q R X d S )Nr\   )r?   Zexternal_error_raisedÚImportErrorr   r   r   r   r   Útest_gcs_not_present_exception¾   s    rl   )$Úior   re   rN   rI   Znumpyr:   r=   Zpandasr   r   r   r   r   r   Zpandas._testingZ_testingr?   Z pandas.tests.io.test_compressionr	   Zpandas.utilr
   ÚtdZfixturer   Z
skip_if_noÚmarkZparametrizerB   Úbytesr6   rT   r`   rj   Zskip_if_installedrl   r   r   r   r   Ú<module>   s.    
(*