a
    j=ic                     @   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	g d
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                    sN   ddl m}m} |j  t  dd  _G  fddd|}| d|  S )z"Emulate GCS using a binary buffer.r   AbstractFileSystemregistryc                   S   s   dS )NT r   r   r   i/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pandas/tests/io/test_gcs.py<lambda>!       zgcs_buffer.<locals>.<lambda>c                       s    e Zd Z 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__r   r   r   r   r   r   MockGCSFileSystem#   s   r!   gcsfs.GCSFileSystem)fsspecr   r   targetclearr   closesetattr)monkeypatchr   r   r!   r   r   r   r      s    

	r   Zgcsfsformat)csvjsonparquetexcelmarkdownc                 C   s  ddl m} |j  tddgdtjgddgtdd	d
d}d| }|dkrp|j|dd t	|dgdd}n|dkrd}|
| t|dgdd}nj|dkr|| t|dgd}nH|dkrtd || t|}n"|dkrtd || |}t|| dS )zE
    Test that many to/read functions support GCS.

    GH 33987
    r   r                @ts
2018-06-18   Zperiodsintfloatstrdtzgs://test/test.r*   T)indexr<   )Zparse_dates	index_colr-   zgs://test/test.xlsr+   )Zconvert_datesr,   Zpyarrowr.   tabulateN)r#   r   r$   r%   r   npnanr   to_csvr   Zto_excelr   to_jsonr   pytestZimportorskip
to_parquetr   Zto_markdowntmassert_frame_equal)r   r)   r   df1r   Zdf2r   r   r   test_to_read_gcs1   s:    

	








rI   )resultexpectedcompressionc              	   C   s~  |dkrt t| j}t t|<}t| | D ]\}}|j|jks:J q:W d   n1 sh0    Y  W d   n1 s0    Y  n|dkrltjt| d}tjt|dz}t| | D ]V\}	}
|	|	}|	|
}|du |du ksJ |dur|dur|
 |
 ksJ qW d   n1 s@0    Y  W d   n1 s`0    Y  n| |kszJ 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
    zipNtar)fileobj)zipfileZipFiler   rM   infolistCRCtarfiler   
getmembersextractfileread)rJ   rK   rL   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_safe^   s*    P


XrZ   encodingzutf-8cp1251c                 C   s  ddl m} |j  t }d|i}|dkr6d|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)
    r   r/   methodgzipr0   mtimewb)rL   r[   modegs://test/test.csv)rL   r[   )r>   rL   r[   Zinfer.N)r#   r   r$   r%   rF   ZmakeDataFramer   rB   getvaluerZ   r   rG   r	   )r   Zcompression_onlyr[   r   dfrL   bufferZpath_gcsrY   rK   Zread_dfZfile_extr   r   r   $test_to_csv_compression_encoding_gcs}   s4    	
rg   fastparquetc                    sx   ddl m}m} |j  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   r0   r1   r2   r3   r4   r5   r6   r7   r8   c                       s   e Zd Zd fdd	ZdS )z7test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystemrc                    s    d|vrt ttj d|S )Nwztest.parquet)FileNotFoundErrorr   osr   join)r   r   ra   r   tmpdirr   r   r      s    z<test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystem.openN)ri   )r   r   r    r   r   rn   r   r   r!      s   r!   r"   rb   Trh   N)r=   ZenginerL   )r#   r   r   r$   r%   r   r@   rA   r   r'   rE   )r(   ro   r   r   rH   r!   r   rn   r   test_to_parquet_gcs_new_file   s    

	rp   c                   C   s6   t t td W d    n1 s(0    Y  d S )Nrb   )rF   Zexternal_error_raisedImportErrorr   r   r   r   r   test_gcs_not_present_exception   s    rr   )$ior   rl   rT   rP   numpyr@   rD   Zpandasr   r   r   r   r   r   Zpandas._testingZ_testingrF   Z pandas.tests.io.test_compressionr	   Zpandas.utilr
   tdZfixturer   Z
skip_if_nomarkZparametrizerI   bytesr;   rZ   rg   rp   Zskip_if_installedrr   r   r   r   r   <module>   s.    
+-