o
    Eh                     @   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Zd dl	m
Z
 d dlmZmZmZmZmZmZmZ d dlmZ d dlmZ ejdZejdd Zejjejd	g d
dd Z de!de!de"fddZ#ejdddgdd Z$dd Z%e&ddd Z'dS )    )BytesION)pa_version_under17p0)	DataFrameIndex
date_rangeread_csv
read_excel	read_jsonread_parquet)_test_decoratorsz=ignore:Passing a BlockManager to DataFrame:DeprecationWarningc                     sN   t d t d} t  dd  _G  fddd| j}| jd|dd	  S )
z"Emulate GCS using a binary buffer.gcsfsfsspecc                   S   s   dS )NT r   r   r   8lib/python3.10/site-packages/pandas/tests/io/test_gcs.py<lambda>$   s    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)Zclobber)pytestimportorskipr   closeAbstractFileSystemZregister_implementation)r   r!   r   r   r   r      s   


r   format)csvjsonparquetexcelmarkdownc                    s  t ddgdtjgddgtdddd	}d
| }|dkr-|j|dd t|dgdd}n|dkrAd}|| t|dgdd}nw|dkrR|| t	|dgd}nf|dkrt
d t
d G  fddd j}|t
jjt tdd | }	|	 d| || t|}W d   n1 sw   Y  | }
|
jdksJ n|dkrt
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)indexr8   r   )Zparse_dates	index_colr*   zgs://test/test.xlsxr(   )Zconvert_datesr)   Zpyarrowz
pyarrow.fsc                       s   e Zd Ze fddZdS )z(test_to_read_gcs.<locals>.MockFileSystemc                    s,   t d t| dd  } |S )NzUsing pyarrow filesystemzgs:// )printpathlibZPathreplaceZabsoluteZas_uriZLocalFileSystem)r   Zto_localZpa_fsr   r   from_uriZ   s   
z1test_to_read_gcs.<locals>.MockFileSystem.from_uriN)r   r   r   r    r@   r   r?   r   r   MockFileSystemY   s    rA   z&pyarrow 17 broke the mocked filesystem)Zraisesreason
FileSystemNz2Using pyarrow filesystem
Using pyarrow filesystem
r+   Ztabulate)r   npnanr   to_csvr   Zto_excelr   Zto_jsonr	   r"   r#   rC   ZapplymarkermarkZxfailr   	TypeErrorcontextsetattr
to_parquetr
   Z
readouterroutZto_markdowntmassert_frame_equal)r   r&   monkeypatchZcapsysZrequestdf1r   Zdf2rA   mZcapturedr   r?   r   test_to_read_gcs7   sR   	

	








rR   resultexpectedcompressionc              	   C   s  |dkrSt t| >}t t|}t| | D ]\}}|j|jks)J qW d   n1 s4w   Y  W d   dS W d   dS 1 sLw   Y  dS |dkrtjt| d]}tjt|d<}t| | D ]*\}	}
|	|	}|	|
}|du |du ksJ |dur|dur|
 |
 ksJ qrW d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS | |ksJ 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)Zfileobj)zipfileZipFiler   rV   infolistZCRCtarfiler   Z
getmembersZextractfileread)rS   rT   rU   Z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_safeu   s4   P

Pr^   encodingutf-8cp1251c                 C   s  t dtdd ttdtdd tdD d}d	|i}|d
kr(d|d< t }|j|||dd d}|j|||d | 	 }|	 }	t
||	| t|d||d}
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)
    g?x   )      ZABCDc                 S   s   g | ]}d | qS )zi-r   ).0ir   r   r   
<listcomp>   s    z8test_to_csv_compression_encoding_gcs.<locals>.<listcomp>rc   )columnsr9   methodgzipr,   mtimewb)rU   r_   modegs://test/test.csv)rU   r_   r   )r:   rU   r_   Zinfer.N)r   rD   ZarangeZreshaper   listranger   rF   getvaluer^   r   rM   rN   )r   Zcompression_onlyr_   Zcompression_to_extensionZdfrU   bufferZpath_gcsr]   rT   Zread_dfZfile_extr   r   r   $test_to_csv_compression_encoding_gcs   s8   

rt   c                    s~   t d t d 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.Zfastparquetr   r   )r%   r,   r-   r.   r/   r0   r1   r2   r3   r4   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|ddS )Nwztest.parquetr`   )r_   )FileNotFoundErrorr   osr   join)r   r   rm   r   tmpdirr   r   r      s   z<test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystem.openN)ru   )r   r   r   r   r   rz   r   r   r!      s    r!   zgcsfs.GCSFileSystemrn   TN)r9   ZenginerU   )
r"   r#   r   r%   r   rD   rE   r   rJ   rK   )rO   r{   r%   rP   r!   r   rz   r   test_to_parquet_gcs_new_file   s   


	
r|   r   c                   C   s8   t t td W d    d S 1 sw   Y  d S )Nrn   )rM   Zexternal_error_raisedImportErrorr   r   r   r   r   test_gcs_not_present_exception   s   
"r~   )(ior   rx   r=   r[   rX   ZnumpyrD   r"   Zpandas.compat.pyarrowr   Zpandasr   r   r   r   r   r	   r
   Zpandas._testingZ_testingrM   Zpandas.utilr   ZtdrG   filterwarningsZ
pytestmarkZfixturer   Z
single_cpuZparametrizerR   bytesr7   r^   rt   r|   Zskip_if_installedr~   r   r   r   r   <module>   s2    $	
<
0