o
    Û­j°)  ã                   @   sœ   d Z ddlmZm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 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)ÚParserWarning)Ú	DataFrame)ÚTextFileReaderÚread_csv)Úensure_dtype_objsc                   @   s.  e Zd Zej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ej d)d*d+d,ifd-d.d/ifd0d+d,ifd1d+d,ifd2d.d/ifd3d.d/ifg¡d4d5„ ƒZd6d7„ Zej d8ed9ƒ¡d:d;„ ƒZd<d=„ Zd>S )?ÚTestTextReaderc                 C   s   |ddddƒS )NÚioÚdataÚcsvz	test1.csv© )ÚselfÚdatapathr   r   úc/var/www/html/CropPilot/venv/lib/python3.10/site-packages/pandas/tests/io/parser/test_textreader.pyÚcsv_path   s   zTestTextReader.csv_pathc                 C   s@   t |dƒ�}t|ƒ}| ¡  W d   ƒ d S 1 sw   Y  d S )NÚrb©Úopenr   Úread©r   r   ÚfÚreaderr   r   r   Útest_file_handle    s   
"þzTestTextReader.test_file_handlec                 C   sD   t |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   sR   t |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_factorize2   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   )Úar,   r,   r,   ©Údtypeé   )Úbr0   r0   r0   ©r   r   r   ÚtmÚassert_numpy_array_equalÚnpÚarrayÚobject_r'   r   r   r   Útest_skipinitialspace9   ó   ÿÿ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   r.   r4   Úbool_r'   r   r   r   Útest_parse_booleansF   s   z"TestTextReader.test_parse_booleansc                 C   r*   )	Nza  b
a		 "b"
"a"	 	 bT)Údelim_whitespacer   r   )r,   r,   r,   r-   r/   )r0   r0   r0   r1   r'   r   r   r   Útest_delimit_whitespaceN   r8   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   )r,   zhello
thereÚthisr-   r   )r   r   r   r4   r5   r6   r2   r3   ©r   r   r   r(   Ú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   r4   r5   r2   Úassert_almost_equalr>   r   r   r   Útest_euro_decimald   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,500rA   rB   ©rC   Ú	thousandsr   é@â éÔ0  r-   r   )r   r   r   r4   r5   Úint64r2   rE   r>   r   r   r   Útest_integer_thousandsm   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.500rA   Ú.rG   rI   rJ   )r   r   r   r   r2   Úassert_frame_equalr>   r   r   r   Útest_integer_thousands_altv   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jtdd�� t t|ƒdd dd�}| ¡  W d   ƒ d S 1 sxw   Y  d S )Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rrA   ©rC   r   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)Úmatchr"   )rC   r   Úon_bad_lines)r,   ÚdÚgÚlr-   )r0   ÚeÚhÚm)Úcr   ÚiÚn©r   r/   r"   zSkipping liner/   )r   r   ÚpytestÚraisesÚparserÚParserErrorr   r4   r5   ÚobjectÚassert_array_dicts_equalr2   Úassert_produces_warningr   )r   r   r   Úmsgr(   r?   r   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,6rB   r"   rP   ©r,   r0   rY   r/   é   r-   é   é   é   r\   )r   r   r   r   r4   r5   rK   rb   )r   r   r   r   r?   Ú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"rB   ú\)rC   r   Ú
escapecharr   z"hello world"ri   r-   )r   r   r   r4   r5   ra   rb   r>   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   rp   rq   r   rr   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 )Nr.   rB   rP   ©r	   r   r   ©Úkwds©r   r   r   Ú_make_readerÂ   s   z<TestTextReader.test_numpy_string_dtype.<locals>._make_readerzS5,i4r-   r   ÚS5)r,   ÚaaÚaaaÚaaaaÚaaaaar/   Úi4ÚS4)r,   r{   r|   r}   r}   )r   r.   r4   r5   Úall)r   ry   r   r(   Ú	ex_valuesr   rx   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 )Nr.   rC   rB   ru   rv   rx   r   r   ry   ß   s   z4TestTextReader.test_pass_dtype.<locals>._make_readerÚu1ÚS1)Úoner/   r-   r   r/   ÚO)r   r.   r4   Úuint8ra   )r   ry   r   r(   r   rx   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 )NrC   rB   )r   r   rv   rx   r   r   ry   û   s   z1TestTextReader.test_usecols.<locals>._make_reader)r/   r"   )Úusecolsr"   r/   )r   r#   r�   )r   ry   r   r(   Úexpr   rx   r   Útest_usecolsó   s   

zTestTextReader.test_usecolsztext, kwargsz a,b,c1,2,34,5,67,8,910,11,12rC   rB   z*a  b  c1  2  34  5  67  8  910  11  12r;   Tz 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 6c                 C   sJ   |  dd¡}tt|ƒfi |¤Ž ¡ }tt|ƒfi |¤Ž ¡ }t||ƒ d S )Núz
)Úreplacer   r   r   rb   )r   r   ÚkwargsÚ	nice_textr(   r?   r   r   r   Útest_cr_delimited  s   z TestTextReader.test_cr_delimitedc                 C   s^   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||ƒ d S )Nza,b,c
1,2,3
4,,rB   )rC   r/   rg   r-   Ú2Ú Ú3r\   )r   r   r   r4   r5   rK   ra   rb   )r   r   r(   r?   r   r   r   Útest_empty_field_eof!  s   ýz#TestTextReader.test_empty_field_eofÚrepeaté
   c                 C   sê   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tdƒddgdd�}t ||¡ ttdƒtdƒdd�}t ||¡ ttdƒtdƒdd�}t ||¡ d S )Nr0   r,   rY   )ÚcolumnsÚindex)r/   r/   r/   r   Úabcdr/   )r/   r"   ri   rg   rj   )é   é	   r—   é   é   é   )r   rh   é   é   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   r4   ÚnanÚlistr   r   r2   rN   )r   r–   r,   r0   rY   Údfr   r   r   Ú#test_empty_field_eof_mem_access_bug-  s.   "üøÿýz2TestTextReader.test_empty_field_eof_mem_access_bugc                 C   sJ   t tƒ dd g d¢d��}t|tƒsJ ‚W d   ƒ d S 1 sw   Y  d S )Né   rf   )Ú	chunksizer   r£   )r   r   Ú
isinstancer   )r   r§   r   r   r   Útest_empty_csv_inputL  s   ÿ"ýz#TestTextReader.test_empty_csv_inputN) Ú__name__Ú
__module__Ú__qualname__r]   Úfixturer   r   r   r!   r)   r7   r:   r<   r@   rF   rL   rO   re   rl   ro   rs   rt   rƒ   r‰   rŒ   ÚmarkÚparametrizer‘   r•   Úranger¨   r¬   r   r   r   r   r
      sR    
			
þ
ú

ðþ

r
   c                 C   s2   |   ¡ D ]\}}t t |¡t || ¡¡ qd S rq   )Úitemsr2   r3   r4   Úasarray)ÚleftÚrightÚkÚvr   r   r   rb   T  s   ÿrb   )Ú__doc__r   r   r   Únumpyr4   r]   Úpandas._libs.parsersÚ_libsÚparsersr_   r   Úpandas.errorsr   Úpandasr   Úpandas._testingÚ_testingr2   Úpandas.io.parsersr   r   Ú"pandas.io.parsers.c_parser_wrapperr	   r
   rb   r   r   r   r   Ú<module>   s      ;