a
    GGb                     @   sJ  d Z ddlZddlmZ ddlZddlZddlmZ ddlm	Z
 ddlm  mZ ejdZejddgdd	d
 Zejdd Zeejdg ddd Zeejdddgdd Zedd Zedd Zeejdddgdd Zeejdg ddd  Zed!d" Zeejd#g d$d%d& Zd'd( ZdS ))zZ
Tests compressed data parsing functionality for all
of the parsers defined in parsers.py
    N)Path)	DataFrameZpyarrow_skipTF)paramsc                 C   s   | j S )N)Zparam)Zrequest r   Flib/python3.9/site-packages/pandas/tests/io/parser/test_compression.pybuffer   s    r   c                 C   sJ   | }t |d}| }W d    n1 s,0    Y  ||}|||fS )Nrb)openreadread_csv)all_parserscsv1parserfdataexpectedr   r   r   parser_and_data   s
    &
r   compression)zipinferzip2c           	   	   C   s   | \}}}t d}tj|dd}|d| W d    n1 sF0    Y  |dkrt|d}|j|dd}W d    q1 s0    Y  n|j||d}t || W d    n1 s0    Y  d S )	Ntest_file.zipwmode	test_filer   r   r   r   )tmensure_cleanzipfileZipFilewritestrr	   r   assert_frame_equal)	r   r   r   r   r   pathtmpr   resultr   r   r   test_zip$   s    
*.r&   r   r   c           	   	   C   s   | \}}}t d}ddg}tj|dd&}|D ]}||| q2W d    n1 sX0    Y  tjtdd |j||d W d    n1 s0    Y  W d    n1 s0    Y  d S )	Nzcombined_zip.zipr   Zsecond_filer   r   zMultiple filesmatchr   )	r   r   r   r    r!   pytestraises
ValueErrorr   )	r   r   r   r   r   r#   Zinner_file_namesr$   	file_namer   r   r   test_zip_error_multiple_files6   s    
,r-   c              	   C   s   | \}}}t  z}tj|dd W d    n1 s80    Y  tjtdd |j|dd W d    n1 st0    Y  W d    n1 s0    Y  d S )Nr   r   z
Zero filesr'   r   r   )r   r   r   r    r)   r*   r+   r   )r   r   _r#   r   r   r   test_zip_error_no_filesF   s    

r/   c              
   C   s   | \}}}t  x}t|dN}tjtjdd |j|dd W d    n1 sT0    Y  W d    n1 sr0    Y  W d    n1 s0    Y  d S )Nr   zFile is not a zip filer'   r   r   )r   r   r	   r)   r*   r   Z
BadZipfiler   )r   r   r.   r#   r   r   r   r   test_zip_error_invalid_zipR   s
    

r0   filenamez
test.{ext}c              	   C   s   | \}}}|}t j| }|d u r$|n
|j|d}|rB|rBtd tj|d}	t||	| |rfdn|}
|rt|	d}|j	||
d}W d    q1 s0    Y  n|j	|	|
d}t
|| W d    n1 s0    Y  d S )N)extz9Cannot deduce compression from buffer of compressed data.)r1   r   r   r   )icomZ_compression_to_extensionformatr)   skipr   r   Zwrite_to_compressedr	   r   r"   )r   Zcompression_onlyr   r1   r   r   r   Zcompress_typer2   r#   r   r   r%   r   r   r   test_compression\   s    


.r6   r2   )NZgzbz2c           	      C   s   | }ddd}|j |fi |}d|d< |rht|"}|j |fi |}W d    q1 s\0    Y  n&|rtd| nd}|j || fi |}t|| d S )Nr   T)Z	index_colZparse_datesr   r   . )r   r	   r   r"   )	r   r   r   r2   r   kwargsr   r   r%   r   r   r   test_infer_compressionu   s    

2r;   c           	      C   s\   | }| |}tj|d| d}|j||ddd}tddgddgd	}t|| d S )
Nutfz_ex_small.zipr   	)encodingr   sepZ	Venezuelau   Hugo Chávez FríaszHenrique Capriles R.)ZCountryZTwitter)r4   osr#   joinr   r   r   r"   )	r   Zcsv_dir_pathZ	utf_valueZencoding_fmtr   r>   r#   r%   r   r   r   r   test_compression_utf_encoding   s    
rB   invalid_compression)ZsfarkZbz3Zzipperc                 C   sX   | }d|i}d| }t jt|d  |jdi | W d    n1 sJ0    Y  d S )Nr   zUnrecognized compression type: r'   r   )r   )r)   r*   r+   r   )r   rC   r   Zcompress_kwargsmsgr   r   r   test_invalid_compression   s
    
rE   c              	   C   s   | }t dddgi}tdt}tdJ}|j|dd t|t|  t|j|d d| W d    n1 sz0    Y  W d    n1 s0    Y  d S )	Nar      ztest.csvztest.csv.zipF)indexr   )	r   r   r   Zto_csvr   Z
write_text	read_textr"   r   )r   r   ZdfZpath_csvZpath_zipr   r   r   !test_ignore_compression_extension   s    rJ   )__doc__r@   pathlibr   r   r)   Zpandasr   Zpandas._testingZ_testingr   Zpandas.io.commoniocommonr3   ZmarkZusefixturesZskip_pyarrowZfixturer   r   Zparametrizer&   r-   r/   r0   r6   r;   rB   rE   rJ   r   r   r   r   <module>   sD   




	

