o
    g<*                     @   s   d Z ddlmZmZ ddlZddlZddlZddlm	  m
Z ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZ G dd	 d	Zd
d ZdS )z\
Tests the TextReader class in parsers.pyx, which
is integral to the C engine in parsers.py
    )BytesIOStringION)
TextReader)	DataFrame)TextFileReaderread_csv)ensure_dtype_objsc                   @   s   e Zd Zejdd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d'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1S )2TestTextReaderT)autousec                 C   sR   |ddd| _ |ddd}tj|d| _tj| j d| _tj| j d| _d S )Nioparserdatacsvz	test1.csvz	test2.csvztest.xls)dirpathospathjoincsv1csv2xls1)selfdatapathcsv1_dirpath r   d/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pandas/tests/io/parser/test_textreader.pysetup_method   s
   zTestTextReader.setup_methodc                 C   sB   t | jd}t|}|  W d    d S 1 sw   Y  d S )Nrbopenr   r   readr   freaderr   r   r   test_file_handle$   s   
"zTestTextReader.test_file_handlec                 C   sF   t | jd}t|d d}|  W d    d S 1 sw   Y  d S Nr   headerr   r    r   r   r   test_file_handle_mmap)   s   
"z$TestTextReader.test_file_handle_mmapc                 C   sT   t | jd}| }W d    n1 sw   Y  t|}t|d d}|  d S r$   )r   r   r   r   r   )r   r!   textsrcr"   r   r   r   test_StringIO/   s   
zTestTextReader.test_StringIOc                 C   s>   d}t t|d d}| }tttt|d dksJ d S )Nz	a
b
a
b
ar%   r      )r   r   r   lensetmapidr   r   r"   resultr   r   r   test_string_factorize6   s   "z$TestTextReader.test_string_factorizec                 C   b   d}t t|dd d}| }t|d tjg dtjd t|d tjg dtjd d S )	Nza,   b
a,   b
a,   b
a,   bT)skipinitialspacer&   r   )ar5   r5   r5   dtype   )br9   r9   r9   r   r   r   tmassert_numpy_array_equalnparrayobject_r0   r   r   r   test_skipinitialspace=      z$TestTextReader.test_skipinitialspacec                 C   s4   d}t t|d d}| }|d jtjksJ d S )NzTrue
False
True
Truer%   r   )r   r   r   r7   r=   bool_r0   r   r   r   test_parse_booleansJ   s   z"TestTextReader.test_parse_booleansc                 C   r3   )	Nza  b
a		 "b"
"a"	 	 bT)delim_whitespacer&   r   )r5   r5   r5   r6   r8   )r9   r9   r9   r:   r0   r   r   r   test_delimit_whitespaceR   rA   z&TestTextReader.test_delimit_whitespacec                 C   sD   d}t t|d d}| }tjg dtjd}t|d | d S )Nza
"hello
there"
thisr%   )r5   zhello
therethisr6   r   )r   r   r   r=   r>   r?   r;   r<   r   r   r"   r1   expectedr   r   r   test_embedded_newline_   s
   z$TestTextReader.test_embedded_newlinec                 C   sB   d}t t|ddd d}| }tddg}t|d | d S )Nz12345,67
345,678:,)	delimiterdecimalr&   g)\@g+ٚu@r   )r   r   r   r=   r>   r;   assert_almost_equalrG   r   r   r   test_euro_decimalh   s
   z TestTextReader.test_euro_decimalc                 C   sH   d}t t|ddd d}| }tjddgtjd}t|d | d S )	Nz123,456
12,500rJ   rK   rL   	thousandsr&   @ 0  r6   r   )r   r   r   r=   r>   int64r;   rN   rG   r   r   r   test_integer_thousandsq   s
   z%TestTextReader.test_integer_thousandsc                 C   s<   d}t t|ddd d}| }tddg}t|| d S )Nz123.456
12.500rJ   .rP   rR   rS   )r   r   r   r   r;   assert_frame_equalrG   r   r   r   test_integer_thousands_altz   s   z)TestTextReader.test_integer_thousands_altc                 C   s   d}t t|dd d}d}tjtj|d |  W d    n1 s$w   Y  t t|dd dd}| }tjg dt	d	tjg d
t	d	tjg dt	d	d}t
|| t t|dd dd}|  | }d|jv spJ d|jv swJ d S )Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rrJ   rL   r&   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)matchr+   )rL   r&   on_bad_lines)r5   dglr6   )r9   ehm)cr!   inr   r8   r+   r8   zSkipping line 4zSkipping line 6)r   r   pytestraisesr   ParserErrorr   r=   r>   objectassert_array_dicts_equal
readouterrerr)r   capsysr   r"   msgr1   rH   capturedr   r   r   test_skip_bad_lines   s,   

z"TestTextReader.test_skip_bad_linesc                 C   s   d}t t|ddd}|j}g dg}||ksJ | }tjddgtjdtjdd	gtjdtjd
dgtjdd}t|| d S )Nz%skip this
skip this
a,b,c
1,2,3
4,5,6rK   r+   rY   r5   r9   rb   r8      r6            re   )r   r   r&   r   r=   r>   rT   rj   )r   r   r"   r&   rH   recsr   r   r   test_header_not_enough_lines   s   
z+TestTextReader.test_header_not_enough_linesc                 C   sF   d}t t|dd dd}| }dtjdgd tdi}t|| d S )	Nz,\"hello world"
\"hello world"
\"hello world"rK   \)rL   r&   
escapecharr   z"hello world"rt   r6   )r   r   r   r=   r>   ri   rj   rG   r   r   r   test_escapechar   s
   zTestTextReader.test_escapecharc                 C      d S Nr   r   r   r   r   test_eof_has_eol   s   zTestTextReader.test_eof_has_eolc                 C   r{   r|   r   r}   r   r   r   test_na_substitution   s   z#TestTextReader.test_na_substitutionc                    s   d  fdd}|dd}|  }|d jdksJ tjg ddd}|d |k s-J |d	 jd
ks6J |dd}|  }|d jdksHJ tjg ddd}|d |k s[J |d	 jdksdJ d S )Nza,1
aa,2
aaa,3
aaaa,4
aaaaa,5c                     s2   d| v rt | d | d< tt fdd d| S )Nr7   rK   rY   r   r   r   kwdsr   r   r   _make_reader   s   z<TestTextReader.test_numpy_string_dtype.<locals>._make_readerzS5,i4r6   r   S5)r5   aaaaaaaaaaaaaar8   i4S4)r5   r   r   r   r   )r   r7   r=   r>   all)r   r   r"   r1   	ex_valuesr   r   r   test_numpy_string_dtype   s   

z&TestTextReader.test_numpy_string_dtypec                    s   d  fdd}|dddd}|  }|d jdksJ |d	 jdks&J |tjtdd}|  }|d jdks<J |d	 jd
ksEJ |tdtd
dd}|  }|d jdks`J |d	 jd
ksiJ d S )Nzone,two
1,a
2,b
3,c
4,dc                     s0   d| v rt | d | d< tt fddi| S )Nr7   rL   rK   r   r   r   r   r   r      s   z4TestTextReader.test_pass_dtype.<locals>._make_readeru1S1)oner8   r6   r   r8   O)r   r7   r=   uint8ri   )r   r   r"   r1   r   r   r   test_pass_dtype   s   zTestTextReader.test_pass_dtypec                    sp   d  fdd}|dd}|  }|   }t|dksJ |d |d k s*J |d |d k s6J d S )Nz a,b,c
1,2,3
4,5,6
7,8,9
10,11,12c                     s   t t fddi| S )NrL   rK   )r   r   r   r   r   r   r     s   z1TestTextReader.test_usecols.<locals>._make_reader)r8   r+   )usecolsr+   r8   )r   r,   r   )r   r   r"   r1   expr   r   r   test_usecols   s   

zTestTextReader.test_usecolsc                 C   sl   dd }d}||dd d}||dd d	}||dd d
}||dd d}||dd d}||dd d S )Nc                 [   sJ   |  dd}tt| fi | }tt|fi | }t|| d S )Nz
)replacer   r   r   rj   )r(   kwargs	nice_textr1   rH   r   r   r   _test  s   z/TestTextReader.test_cr_delimited.<locals>._testz a,b,c1,2,34,5,67,8,910,11,12rK   rL   z*a  b  c1  2  34  5  67  8  910  11  12T)rD   z a,b,c1,2,34,5,6,88,910,11,12zdA,B,C,D,E,F,G,H,I,J,K,L,M,N,OAAAAA,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0zA  B  C  2  34  5  6zA B C2 34 5 6r   )r   r   r   sampler   r   r   test_cr_delimited  s   z TestTextReader.test_cr_delimitedc           	      C   sR  d}t t|dd }tjddgtjdtjddgtdtjd	dgtdd
}t|| tdgtj	ggdgddgd}tg dg dgt
dddgd}tg ddtj	tj	tj	gg dddtj	tj	ggt
dg dd}tdD ]5}ttdddgdd}t|| ttdt
ddd}t|| ttdt
ddd}t|| qqd S )Nza,b,c
1,2,3
4,,rK   r   r8   rr   r6   2 3re   r9   r5   rb   )columnsindex)r8   r8   r8   r   abcd)r8   r+   rt   rr   ru   )   	   
            )r   rs         d   za,b
c
r   )skiprowsnamesenginez1,1,1,1,0
1,1,1,1,0


)r   r   z"0,1,2,3,4
5,6
7,8,9,10,11
12,13,14)r   r   r   r=   r>   rT   ri   rj   r   nanlistranger   r;   rW   )	r   r   r1   rH   r5   r9   rb   _dfr   r   r   test_empty_field_eof*  s@   
"z#TestTextReader.test_empty_field_eofc                 C   sJ   t t dd g dd}t|tsJ W d    d S 1 sw   Y  d S )N   rq   )	chunksizer&   r   )r   r   
isinstancer   )r   r   r   r   r   test_empty_csv_inputT  s   "z#TestTextReader.test_empty_csv_inputN)__name__
__module____qualname__rf   fixturer   r#   r'   r*   r2   r@   rC   rE   rI   rO   rU   rX   rp   rw   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r	      s2    

			*r	   c                 C   s2   |   D ]\}}tt|t||  qd S r|   )itemsr;   r<   r=   asarray)leftrightkvr   r   r   rj   \  s   rj   )__doc__r   r   r   r   numpyr=   rf   pandas._libs.parsers_libsparsersr   r   pandasr   pandas._testing_testingr;   pandas.io.parsersr   r   "pandas.io.parsers.c_parser_wrapperr   r	   rj   r   r   r   r   <module>   s      C