
    g                        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
mZmZmZmZmZmZ d dlmZ d dlmZ ej.                  j1                  d      Zej4                  d        Zej.                  j8                  ej.                  j;                  dg d      d	               Zd
edede fdZ!ej.                  j;                  dddg      d        Z"d Z# ejH                  d      d        Z%y)    )BytesION)	DataFrameIndex
date_rangeread_csv
read_excel	read_jsonread_parquet)_test_decoratorsz=ignore:Passing a BlockManager to DataFrame:DeprecationWarningc                      t        j                  d       t        j                  d      } t               d _         G fdd| j                        }| j                  d|d       S )	z"Emulate GCS using a binary buffer.gcsfsfsspecc                       y)NT r       U/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/pandas/tests/io/test_gcs.py<lambda>zgcs_buffer.<locals>.<lambda>"   s    r   c                   (    e Zd Ze fd       Zd Zy)%gcs_buffer.<locals>.MockGCSFileSystemc                  *    j                  d       S )Nr   )seek)argskwargs
gcs_buffers     r   openz*gcs_buffer.<locals>.MockGCSFileSystem.open%   s    OOAr   c                     |ddgS )Nfile)nametyper   )selfpathr   s      r   lsz(gcs_buffer.<locals>.MockGCSFileSystem.ls*   s    !6233r   N)__name__
__module____qualname__staticmethodr   r"   )r   s   r   MockGCSFileSystemr   $   s    		 
		4r   r'   gsT)clobber)pytestimportorskipr   closeAbstractFileSystemregister_implementation)r   r'   r   s     @r   r   r      sb        *FJ#J4F55 4 ""4):D"Ir   format)csvjsonparquetexcelmarkdownc           	      X  
 t        ddgdt        j                  gddgt        dd      d	      }d
| }|dk(  r$|j	                  |d       t        |dgd      }n*|dk(  r$d}|j                  |       t        |dgd      }n|dk(  r |j                  |       t        |dg      }n|dk(  rt        j                  d       t        j                  d      
 G 
fdd
j                        }|j                         5 }|j                  
d|       |j                  |       t!        |      }ddd       |j#                         }	|	j$                  dk(  s/J |dk(  r(t        j                  d       |j'                  |       |}t)        j*                  |       y# 1 sw Y   nxY w)zE
    Test that many to/read functions support GCS.

    GH 33987
                 @ts
2018-06-18   periodsintfloatstrdtzgs://test/test.r0   T)indexrC   r   )parse_dates	index_colr3   zgs://test/test.xlsxr1   )convert_datesr2   pyarrowz
pyarrow.fsc                   "    e Zd Ze fd       Zy)(test_to_read_gcs.<locals>.MockFileSystemc                     t        d       t        j                  | j                  dd            j	                         j                         }j                  |      S )NzUsing pyarrow filesystemzgs:// )printpathlibPathreplaceabsoluteas_uriLocalFileSystem)r!   to_localpa_fss     r   from_uriz1test_to_read_gcs.<locals>.MockFileSystem.from_uriX   sH    01"<<Wb(ABKKMTTV,,X66r   N)r#   r$   r%   r&   rV   )rU   s   r   MockFileSystemrJ   W   s    7 7r   rW   
FileSystemNz2Using pyarrow filesystem
Using pyarrow filesystem
r4   tabulate)r   npnanr   to_csvr   to_excelr   to_jsonr	   r*   r+   rX   contextsetattr
to_parquetr
   
readouterroutto_markdowntmassert_frame_equal)r   r/   monkeypatchcapsysdf1r!   df2rW   mcapturedrU   s             @r   test_to_read_gcsrm   5   s    q6266]:\15		
C VH%D

4t
$t$1=	7	$TD6Q?	6	DTF3	9	I&##L1	7U-- 	7   " 	%aIIe\>:NN4 t$C	% $$&||UUUU	:	J'#s#	% 	%s   0F  F)resultexpectedcompressionc                 l   |dk(  rt        j                  t        |             5 }t        j                  t        |            5 }t        |j	                         |j	                               D ]   \  }}|j
                  |j
                  k(  r J  	 ddd       ddd       y|dk(  rt        j                  t        |             5 }t        j                  t        |            5 }t        |j                         |j                               D ][  \  }	}
|j                  |	      }|j                  |
      }|du |du k(  sJ |6|9|j                         |j                         k(  r[J  	 ddd       ddd       y| |k(  sJ y# 1 sw Y   xY w# 1 sw Y   yxY w# 1 sw Y   2xY w# 1 sw Y   yxY w)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   rr   infolistCRCtarfiler   
getmembersextractfileread)rn   ro   rp   expresres_infoexp_infotar_exptar_restar_res_infotar_exp_infoactual_fileexpected_files                r   assert_equal_zip_safer   l   s    e__WV_- 	4gooH7
 	4&)#,,.#,,.&I 4"(||x||3334	4 	4 	4
 
	\\'&/2 
	Fgw||H%@
 
	F.1""$g&8&8&:/ F*l &11,? ' 3 3L A#t+$1FGGG*}/H&++-1C1C1EEEEF
	F 
	F 
	F !!!%	4 	4 	4 	4
	F 
	F 
	F 
	Fsa   FAFFF F*%AFF!F)F-F*F	FFF'	#F**F3encodingutf-8cp1251c                    t        dt        j                  d      j                  d      z  t	        t        d      t              t	        t        d      D cg c]  }d| 	 c}t                    }d	|i}|d
k(  rd|d<   t               }|j                  |||d       d}|j                  |||       | j                         }	|j                         }
t        |	|
|       t        |d||      }t        j                  ||       ||   }d|d	<   |d| z  }|j                  |||       | j                         }	|j                         }
t        |	|
|       t        |dd|      }t        j                  ||       yc c}w )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   )      ABCD)dtyper   zi-)columnsrD   methodgzipr6   mtimewb)rp   r   modegs://test/test.csv)rp   r   r   )rF   rp   r   infer.N)r   rZ   arangereshaper   listobjectranger   r\   getvaluer   r   re   rf   )r   compression_onlyr   compression_to_extensionidfrp   bufferpath_gcsr~   ro   read_dffile_exts                r   $test_to_csv_compression_encoding_gcsr      s|    
biin$$W--d6l&1uRy1!r!X1@
B -.K6! GYFIIf+tIL $HIIhK(IC



C H#x)9:A+;hG "g& ((89H#K!H:HIIhK(IC



C H#x)9:x1'HUG"g&C 2s   Fc           	      ,   t        j                  d       t        j                  d       ddlm} t	        ddgdt
        j                  gdd	gt        d
d      d      } G fdd|      }| j                  d|       |j                  dddd       y)zCRegression test for writing to a not-yet-existent GCS Parquet file.fastparquetr   r   )r-   r6   r7   r8   r9   r:   r;   r<   r=   r?   c                       e Zd Zd fd	Zy)7test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystemc                 p    d|vrt         t        t        j                  j	                  d      |d      S )Nwztest.parquetr   )r   )FileNotFoundErrorr   osr!   join)r    r!   r   r   tmpdirs       r   r   z<test_to_parquet_gcs_new_file.<locals>.MockGCSFileSystem.open   s.    $''V^<dWUUr   N)r)r#   r$   r%   r   )r   s   r   r'   r      s	    	Vr   r'   zgcsfs.GCSFileSystemr   TN)rD   enginerp   )
r*   r+   r   r-   r   rZ   r[   r   r`   ra   )rg   r   r-   ri   r'   s    `   r   test_to_parquet_gcs_new_filer      s    
&
 )
q6266]:\15		
CV. V -/@ANNDD  r   r   c                  v    t        j                  t              5  t        d       d d d        y # 1 sw Y   y xY w)Nr   )re   external_error_raisedImportErrorr   r   r   r   test_gcs_not_present_exceptionr      s.    		!	!+	. '%&' ' 's   /8)&ior   r   rN   ry   ru   numpyrZ   r*   pandasr   r   r   r   r   r	   r
   pandas._testing_testingre   pandas.utilr   tdmarkfilterwarnings
pytestmarkfixturer   
single_cpuparametrizerm   bytesrB   r   r   r   skip_if_installedr   r   r   r   <module>r      s    	         .[[''C

  2 #RS2$ T 2$j"% "5 "s "> gx%89-' :-'`8 g' 'r   