o
    Zh5                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlZd dl	Z	d dl
Zd dlmZ d dlmZ d dlmZmZ d dlmZ zd dlmZ W n	 eyK   Y nw d	d
 Zdd Ze	jjG dd dZG dd deZdd Ze	jje	jje	jj e	jj!dd Z"dS )    N)BytesIO)join)util)_test_dataframe)&_test_write_to_dataset_with_partitions$_test_write_to_dataset_no_partitions)guid)assert_frame_equalc                  C   s8   t  sd} tjdrt|  d S t|  d S d S )NzNo libhdfs available on systemZ!PYARROW_HDFS_TEST_LIBHDFS_REQUIRE)paZhave_libhdfsosenvirongetpytestZfailskip)message r   N/var/www/html/lang_env/lib/python3.10/site-packages/pyarrow/tests/test_hdfs.pycheck_libhdfs_present-   s   r   c                  C   s   t jdd} t jdd }ztt jdd}W n ty$   tdw tt tj	
| ||W  d    S 1 s=w   Y  d S )NARROW_HDFS_TEST_HOSTdefaultZARROW_HDFS_TEST_USERARROW_HDFS_TEST_PORTr   4Env variable ARROW_HDFS_TEST_PORT was not an integer)r   r   r   int
ValueErrorr   ZwarnsFutureWarningr
   hdfsconnect)hostuserportr   r   r   hdfs_test_client6   s   $r    c                   @   sl  e Zd Zdd Zedd Zedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zejjd)ed*ejjejjd+d, Zejjejjd-d. Zejjd)ed*ejjejjd/d0 Z ejjd)ed*ejjejjd1d2 Z!ejjd)ed*ejjejjd3d4 Z"d5S )6HdfsTestCasesc                 C   s\   t | j|}|| t ||}||d}|| W d    |S 1 s'w   Y  |S )Nwb)pjointmp_pathmkdiropenwrite)selfr   Z	test_nameZ	test_path	test_data	base_path	full_pathfr   r   r   _make_test_fileF   s   


zHdfsTestCases._make_test_filec                 C   s6   |    t | _dtdd| _| j| j d S )Nz/tmp/pyarrow-test-{}r   i  )check_driverr    r   formatrandomrandintr$   r%   clsr   r   r   setup_classQ   s   zHdfsTestCases.setup_classc                 C   s    | j j| jdd | j   d S )NT	recursive)r   deleter$   closer2   r   r   r   teardown_classX   s   zHdfsTestCases.teardown_classc                 C   sx   | | j}||}|jsJ |j| jjksJ |j| jjks"J |j| jjks+J |j| jjks4J || j	 d S N)
dumpsr   loadsis_openr   r   r   Zkerb_ticketlsr$   )r(   Zpickle_modulesZh2r   r   r   test_pickle]   s   

zHdfsTestCases.test_picklec                 C   sd   t | jd}d}| j|d}|| W d    n1 sw   Y  | j|}||ks0J d S )Nzcat-test	   foobarbazr"   )r#   r$   r   r&   r'   cat)r(   pathdatar,   contentsr   r   r   test_cath   s   zHdfsTestCases.test_catc                 C   sJ   | j  }| j  }| j  }|dksJ ||ksJ ||| ks#J d S )Nr   )r   Zget_capacityZget_space_useddf)r(   capacityZ
space_usedZ	disk_freer   r   r   test_capacity_spacer   s   


z!HdfsTestCases.test_capacity_spacec                 C   s\   t  }|jsJ |  |jrJ tt |d W d    d S 1 s'w   Y  d S )N/)r    r=   r8   r   raises	Exceptionr>   )r(   clientr   r   r   
test_close{   s   

"zHdfsTestCases.test_closec                 C   sX   t | jd}t | jd}| j| | j|sJ | jj|dd | j|r*J d S )Nztest-dir/test-dirztest-dirTr5   )r#   r$   r   r%   existsr7   )r(   rC   parent_pathr   r   r   
test_mkdir   s   zHdfsTestCases.test_mkdirc                 C   s   t | jd}t | jd}d}| j|d}|| W d    n1 s%w   Y  | j|s2J | j|| | j|rAJ | j|sIJ | j||ksSJ | j|| | j||ksdJ d S )Nzmv-testzmv-new-testrA   r"   )	r#   r$   r   r&   r'   rO   mvrB   rename)r(   rC   new_pathrD   r,   r   r   r   test_mv_rename   s   zHdfsTestCases.test_mv_renamec                 C   s   t | jd}t |d}| j| d}| j|d}|| W d    n1 s*w   Y  | j|}| j|}|d dksCJ |d dksKJ |d t|ksUJ d S )	N	info-baseexrA   r"   kind	directoryfilesize)r#   r$   r   r%   r&   r'   infolen)r(   rC   	file_pathrD   r,   Z	path_infoZfile_path_infor   r   r   	test_info   s   
zHdfsTestCases.test_infoc                 C   s   t | jd}t |d}t |d}| j| | j|d}|d W d    n1 s-w   Y  | j|s:J | j|sBJ | j|rJJ | j|sRJ | j|rZJ | j|rbJ | j|rjJ | j|srJ | j|rzJ d S )NrV   rW   zthis-path-is-missingr"   rA   )	r#   r$   r   r%   r&   r'   rO   isdirisfile)r(   dir_pathr^   Zmissing_pathr,   r   r   r   test_exists_isdir_isfile   s    

z&HdfsTestCases.test_exists_isdir_isfilec           	   	   C   s   t | jd}t |d}t |d}t |d}t |d}| j|r'| j|d | j| | j| d}|||fD ]}| j|d}|| W d    n1 sSw   Y  q:| j|t	|d	 ksgJ d S )
Nzdisk-usage-basep1p2subdirp3TrA   r"      )
r#   r$   r   rO   r7   r%   r&   r'   
disk_usager]   )	r(   rC   rd   re   rf   rg   rD   r^   r,   r   r   r   test_disk_usage   s    



 zHdfsTestCases.test_disk_usagec                 C   sv   t | jd}| j| t |d}t |d}| j| | j|d}|d t| j|d}|||gks9J d S )Nzls-testza-dirza-file-1r"   
   aaaaaaaaaaF)r#   r$   r   r%   r&   r'   sortedr>   )r(   r*   rb   Zf1_pathr,   rE   r   r   r   test_ls   s   


zHdfsTestCases.test_lsc                 C   sJ   t | jd}| j|d}|d W d    d S 1 sw   Y  d S )Nz
chmod-testr"   rk   )r#   r$   r   r&   r'   )r(   rC   r,   r   r   r   test_chmod_chown   s   "zHdfsTestCases.test_chmod_chownc                 C   sb   t | jd}d}t|}|d | j|| t }| j|| |d | |ks/J d S )Nzupload-testrA   r   )r#   r$   r   seekr   Zuploaddownloadgetvalue)r(   r*   rD   bufZout_bufr   r   r   test_download_upload   s   

z"HdfsTestCases.test_download_uploadc                 C   s   t | jd}d}| j|d}|| W d    n1 sw   Y  | j|d}| dks4J |d}||ks?J W d    d S 1 sJw   Y  d S )Nzctx-managers   foor"   rbrh   
   )r#   r$   r   r&   r'   r[   readr(   rC   rD   r,   resultr   r   r   test_file_context_manager   s   
"z'HdfsTestCases.test_file_context_managerc                 C   sH   t | jd}tt | j| W d    d S 1 sw   Y  d S )Nzdoes-not-exist-123)r#   r$   r   rK   FileNotFoundErrorr   r&   )r(   rC   r   r   r   test_open_not_exist
  s   "z!HdfsTestCases.test_open_not_existc                 C   sB   t ttf | jdd W d    d S 1 sw   Y  d S )NrJ   r"   )r   rK   FileExistsErrorIsADirectoryErrorr   r&   )r(   r   r   r   test_open_write_error  s   "z#HdfsTestCases.test_open_write_errorc                 C   s   t | jd}d}| j|d}|| W d    n1 sw   Y  | j|d}| }W d    n1 s:w   Y  ||ksEJ d S )Nzread-whole-files  foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoor"   rt   )r#   r$   r   r&   r'   rv   rw   r   r   r   test_read_whole_file  s   
z"HdfsTestCases.test_read_whole_filec              	   C   s   dd l m} d}d}g }t|D ]R}t||d}t|| |d | |d< |d tj|d< t|d	|}t
jj|d	d
}	| j|d}
||	|
 W d    n1 sXw   Y  ||	 qt
|}|S )Nr   ru      seed   indexuint32z
{}.parquetFZpreserve_indexr"   )pyarrow.parquetparquetranger   npZarangeastypeint64r#   r/   r
   Tablefrom_pandasr   r&   write_tableappendZconcat_tables)r(   tmpdirpqZnfilesr[   r)   irG   rC   tabler,   expectedr   r   r   _write_multiple_hdfs_pq_files   s    
z+HdfsTestCases._write_multiple_hdfs_pq_fileszglegacy.FileSystem not supported with ParquetDataset due to legacy path being removed in PyArrow 15.0.0.)reasonrK   c                 C   sZ   t | jdt  }| j| | |}| j|}t| j	ddj
dd|  d S )Nzmulti-parquet-r   ZbyTZdrop)r#   r$   r   r   r%   r   Zread_parquetr	   	to_pandassort_valuesreset_index)r(   r   r   rx   r   r   r    test_read_multiple_parquet_files8  s   
z.HdfsTestCases.test_read_multiple_parquet_filesc                 C   sl   dd l m} t| jdt  }| j| | |}t|}|	|}t
| jddjdd|  d S )Nr   zmulti-parquet-uri-r   r   Tr   )r   r   r#   r$   r   r   r%   r   _get_hdfs_uri
read_tabler	   r   r   r   )r(   r   r   r   rC   rx   r   r   r   )test_read_multiple_parquet_files_with_uriK  s   

z7HdfsTestCases.test_read_multiple_parquet_files_with_uric                 C   s   dd l m} t| jdt  }| j| tt|d}d}t|dd}|d 	t
j|d< tjj|dd}|j||| jd	 |j|| jd	 }t|| d S )
Nr   zuri-parquet-ztest.parquetr   r   r   Fr   
filesystem)r   r   r#   r$   r   r   r%   r   r   r   r   r   r
   r   r   r   r   r   r	   )r(   r   r   rC   r[   rG   r   rx   r   r   r   &test_read_write_parquet_files_with_uri]  s   z4HdfsTestCases.test_read_write_parquet_files_with_uric                 C   0   t | jdt  }| j| t|| jd d S )Nzwrite-partitions-r   )r#   r$   r   r   r%   r   r(   r   r   r   r   %test_write_to_dataset_with_partitionsu  
   
z3HdfsTestCases.test_write_to_dataset_with_partitionsc                 C   r   )Nzwrite-no_partitions-r   )r#   r$   r   r   r%   r   r   r   r   r   #test_write_to_dataset_no_partitions  r   z1HdfsTestCases.test_write_to_dataset_no_partitionsN)#__name__
__module____qualname__r-   classmethodr4   r9   r@   rF   rI   rN   rQ   rU   r_   rc   rj   rm   rn   rs   ry   r{   r~   r   r   r   markZxfail	TypeErrorpandasr   r   r   r   r   r   r   r   r   r   r!   C   sd    


		
r!   c                   @   s    e Zd Zedd Zdd ZdS )TestLibHdfsc                 C   s
   t   d S r:   )r   r2   r   r   r   r.     s   
zTestLibHdfs.check_driverc                 C   s,   t  }| |ddd}||}d }d }d S )NZorphaned_file_testfnamerA   )r    r-   r&   )r(   r   r^   r,   r   r   r   test_orphaned_file  s   

zTestLibHdfs.test_orphaned_fileN)r   r   r   r   r.   r   r   r   r   r   r     s    
r   c                 C   sN   t jdd}ztt jdd}W n ty   tdw d||| }|S )Nr   	localhostr   r   r   zhdfs://{}:{}{})r   r   r   r   r   r/   )rC   r   r   urir   r   r   r     s   r   c            
      C   s   t   zdd l} W n ty   td Y nw dd lm} td}t }t	
 }tj|}d}||d}||| W d    n1 sIw   Y  |j||jd}| }	t|	| d S )Nr   z fastparquet test requires snappyfastparquetz/tmp/testing.parquetr"   )Z	open_with)r   snappyImportErrorr   r   r   r   Zimportorskipr    r   Zmake_dataframer
   r   r   r&   r   ZParquetFiler   r	   )
r   r   r   fsrG   r   rC   r,   Zparquet_filerx   r   r   r   test_fastparquet_read_with_hdfs  s$   
r   )#r   r0   ior   Zos.pathr   r#   numpyr   r   Zpyarrowr
   Zpyarrow.testsr   Zpyarrow.tests.parquet.commonr   Z"pyarrow.tests.parquet.test_datasetr   r   Zpyarrow.utilr   Zpandas.testingr	   r   r   r    r   r   r!   r   r   r   r   r   r   r   r   r   r   <module>   s:   	  J