o
    d*                  	   @   s  d dl Z d dlZd dlZ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Zd dlmZ d dlZd dlmZ d dlm  mZ dd ej D Zejdejdg dg d	g g d
dejdg d ddgejdg ddd Zejdejdg dg d	g g d
dejdg d ddgejdddgdd Zejddddiejfdi ej fdi ej!fgdd Z"ejdddddejddifdi ej d d!ifdi ej!i fgd"d# Z#d$d% Z$d&d' Z%d(d) Z&d*d+ Z'd,d- Z(d.d/ Z)ejdejdg dg d	g g d
dejdg d ddgejdg dd0d1 Z*ejdejdg dg d	g g d
dejdg d ddgejdg dd2d3 Z+ejd4d5e
j,fd6ej-fgd7d8 Z.d9d: Z/d;d< Z0d=d> Z1d?d@ Z2dS )A    N)Path)is_platform_windowsc                 C   s   i | ]\}}||qS  r   ).0keyvaluer   r   T/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/io/test_compression.py
<dictcomp>   s    
r	   objd   g~Ϛ?g9DܜJ?g>D)?g.4i(@g33333@g̤AXYZcolumnsr   namemethod)	to_pickleto_jsonto_csvc                 C   s   |dkr	ddd}t  -}t| |||d tj|}t| ||d d tj|}||ks2J W d    d S 1 s=w   Y  d S )Ntarw:gzr   modecompressiontmensure_cleangetattrospathgetsize)r
   r   compression_onlyr%   compressed_sizeuncompressed_sizer   r   r   test_compression_size   s   

"r*   r   r   c              	   C   s  t  :}tj||dkrdnd|d}t| ||j |jjr"J W d    n1 s,w   Y  tj	|}W d    n1 sAw   Y  t  ;}tj|dd d}t| ||j |jjrbJ W d    n1 slw   Y  tj	|}||ks}J W d    d S 1 sw   Y  d S )Nr   r   wr   )
r!   r"   icom
get_handler#   handleclosedr$   r%   r&   )r
   r   r'   r%   handlesr(   r)   r   r   r   test_compression_size_fh1   s(   

	"r1   z'write_method, write_kwargs, read_methodindexFr   c                 C   s   t jg dg dgg dd}t| }td| }t|| |fi | |||d}W d    n1 s6w   Y  t|| d S )N)g      ?r   )g333333@      r   r   
compressedr   )pd	DataFrame_compression_to_extensionr!   r"   r#   assert_frame_equal)write_methodwrite_kwargsread_methodr'   input	extensionr%   outputr   r   r   ,test_dataframe_compression_defaults_to_inferN   s   rA   z1write_method,write_kwargs,read_method,read_kwargsT)r2   headersqueezetypZseriesc           
      C   s   t jg ddd}t| }td| 6}t|| |fi | d|v r9| }|d= ||fd|i|d}	n
||fd|i|}	W d    n1 sMw   Y  tj|	|dd	 d S )
N)r   r4   
   r   r   r6   rC   r   r   F)Zcheck_names)	r7   Seriesr9   r!   r"   r#   copyrC   Zassert_series_equal)
r;   r<   r=   Zread_kwargsr'   r>   r?   r%   kwargsr@   r   r   r   )test_series_compression_defaults_to_inferb   s   
rJ   c              
   C   s   t jdg dg dg g dd}t F}tj|d| d%}tt |j|j	| d W d    n1 s7w   Y  W d    n1 sFw   Y  W d    d S W d    d S 1 s^w   Y  d S )Nr   r   r   r   r   r+   r   )
r7   r8   r!   r"   r,   r-   Zassert_produces_warningRuntimeWarningr   r.   )r'   dfr%   r0   r   r   r   test_compression_warning}   s   
"rM   c              	   C   s   t  }t  7}t|dd}|j|d| d |d W d   n1 s'w   Y  t |tj|d| d W d   n1 sBw   Y  t	
 }|j|d| d |d t |tj|d| d dS )z?
    Binary file handles support compression.

    GH22555
    wb)r   )r   r   r   N)Z	index_colr   )r!   makeDataFramer"   openr   seekr:   r7   read_csvioBytesIO)r'   rL   r%   filer   r   r   test_compression_binary   s    
	
rV   c                  C   s   t  } ddd}t  ,}t|}| j||d td | }| j||d || ks1J W d   dS 1 s<w   Y  dS )z
    Gzip should create reproducible archives with mtime.

    Note: Archives created with different filenames will still be different!

    GH 28103
    gzip   r   mtimer   r5   N)r!   rO   r"   r   r   timesleep
read_bytes)rL   compression_optionsr%   r@   r   r   r   #test_gzip_reproducibility_file_name   s   


"r_   c                  C   sh   t  } ddd}t }| j||dd | }td t }| j||dd || ks2J dS )zL
    Gzip should create reproducible archives with mtime.

    GH 28103
    rW   rX   rY   rN   )r   r   r5   N)r!   rO   rS   rT   r   getvaluer[   r\   )rL   r^   bufferr@   r   r   r   %test_gzip_reproducibility_file_object   s   

rb   c                  C   &   t d} tjtjd| gtjd dS )z6Tests if import pandas works when lzma is not present.zT        import sys
        sys.modules['lzma'] = None
        import pandas
        -cstderrNtextwrapdedent
subprocesscheck_outputsys
executablePIPEcoder   r   r   test_with_missing_lzma   s   rq   c                  C   rc   )z\Tests if RuntimeError is hit when calling lzma without
    having the module available.
    z
        import sys
        import pytest
        sys.modules['lzma'] = None
        import pandas as pd
        df = pd.DataFrame()
        with pytest.raises(RuntimeError, match='lzma module'):
            df.to_csv('foo.csv', compression='xz')
        rd   re   Nrg   ro   r   r   r   test_with_missing_lzma_runtime   s   rr   c                 C   s|   t  0}t| ||dd tj|}t| ||dddd tj|}||k s,J W d    d S 1 s7w   Y  d S )NrW   r   rX   r   compresslevelr    )r
   r   r%   Zcompressed_size_defaultZcompressed_size_fastr   r   r   test_gzip_compression_level   s   
"ru   c                 C   sF   t  }t| ||dddd W d   dS 1 sw   Y  dS )zGH33196 bzip needs file size > 100k to show a size difference between
    compression levels, so here we just check if the call works when
    compression is passed as a dict.
    bz2rX   rs   r   N)r!   r"   r#   )r
   r   r%   r   r   r   test_bzip_compression_level   s   
"rw   zsuffix,archive.zipz.tarc              	   C   s   t j| dB}||d W d    n1 sw   Y  tjtdd t| W d    n1 s3w   Y  W d    d S W d    d S 1 sKw   Y  d S )Nfilenamer+   zZero files foundmatch)r!   r"   pytestraises
ValueErrorr7   rR   )suffixarchiver%   r   r   r   test_empty_archive_zip  s   "r   c               	   C   s   t jddO} t| d}|dd |dd W d    n1 s$w   Y  tjtdd t	|  W d    n1 s@w   Y  W d    d S W d    d S 1 sXw   Y  d S )	Nrx   ry   r+   a.csvzfoo,barb.csvz Multiple files found in ZIP filer{   )
r!   r"   zipfileZipFilewritestrr}   r~   r   r7   rR   )r%   rU   r   r   r   test_ambiguous_archive_zip#  s   "r   c                 C   s  | d }t |d}|d W d    n1 sw   Y  | d }t |d}|d W d    n1 s7w   Y  | d }t|d}||d ||d W d    n1 s]w   Y  tjtdd t	| W d    d S 1 szw   Y  d S )Nr   r+   zfoo,bar
r   zarchive.tarz#Multiple files found in TAR archiver{   )
rP   writetarfileTarFileaddr}   r~   r   r7   rR   )Ztmp_pathZcsvAPathaZcsvBPathbZtarpathr   r   r   r   test_ambiguous_archive_tar,  s    "r   c               
   C   s  t jddw} tjddggddgdj| dd	d
dd t| D}tj|d.}|	 }t
|dks5J ||d  d}t rGd}nd}||ksOJ W d    n1 sYw   Y  W d    n1 shw   Y  W d    d S W d    d S 1 sw   Y  d S )Nz.foory   12Zfoobarr   r   r   r   F)r   r2   )fileobjrX   r   utf8zfoo,bar
1,2
zfoo,bar
1,2
)r!   r"   r7   r8   r   rW   rP   r   r   
getmemberslenextractfilereaddecoder   )rU   Zuncompressedr   memberscontentexpectedr   r   r   !test_tar_gz_to_different_filename=  s,   "r   c               	   C   sl   t  (} tj| dd W d    n1 sw   Y  W d    d S W d    d S 1 s/w   Y  d S )Nr+   )r   r   )rS   rT   r,   Z_BytesTarFile)ra   r   r   r   test_tar_no_error_on_closeQ  s   
"r   )3rW   rS   r$   pathlibr   rj   rl   r   rh   r[   r   r}   Zpandas.compatr   Zpandasr7   Zpandas._testingZ_testingr!   Zpandas.io.commoncommonr,   Zextension_to_compressionitemsr9   markZparametrizer8   rG   r*   r1   rR   	read_jsonZread_picklerA   rJ   rM   rV   r_   rb   rq   rr   ru   rw   r   r   r   r   r   r   r   r   r   r   r   <module>   s    








	
	