a
    Pf)                     @   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   _/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/io/parser/test_textreader.pycsv_path   s    zTestTextReader.csv_pathc                 C   s>   t |d }t|}|  W d    n1 s00    Y  d S )Nrbopenr   readr   r   freaderr   r   r   test_file_handle    s    zTestTextReader.test_file_handlec                 C   sB   t |d$}t|d d}|  W d    n1 s40    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(0    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   sb   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   )br/   r/   r/   r   r   r   tmassert_numpy_array_equalnparrayobject_r'   r   r   r   test_skipinitialspace9   s    z$TestTextReader.test_skipinitialspacec                 C   s4   d}t t|d d}| }|d jtjks0J d S )NzTrue
False
True
Truer   r   )r   r   r   r-   r3   Zbool_r'   r   r   r   test_parse_booleansF   s    z"TestTextReader.test_parse_booleansc                 C   sb   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"	 	 bT)delim_whitespacer   r   )r+   r+   r+   r,   r.   )r/   r/   r/   r0   r'   r   r   r   test_delimit_whitespaceN   s    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   r3   r4   r5   r1   r2   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   r3   r4   r1   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,500r>   r?   r@   Z	thousandsr   @ 0  r,   r   )r   r   r   r3   r4   int64r1   rB   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.500r>   .rD   rE   rF   )r   r   r   r   r1   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 sH0    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    n1 s0    Y  d S )Nz'a:b:c
d:e:f
g:h:i
j:k:l:m
l:m:n
o:p:q:rr>   r@   r   zCError tokenizing data\. C error: Expected 3 fields in line 4, saw 4)matchr"   )r@   r   Zon_bad_lines)r+   dglr,   )r/   ehm)cr   inr   r.   r"   zSkipping liner.   )r   r   pytestZraisesparserZParserErrorr   r3   r4   objectassert_array_dicts_equalr1   Z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s2J | }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,6r?   r"   rL   r+   r/   rT   r.      r,            rW   )r   r   r   r   r3   r4   rG   r[   )r   r   r   r   r<   Z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"r?   \)r@   r   
escapecharr   z"hello world"ra   r,   )r   r   r   r3   r4   rZ   r[   r;   r   r   r   test_escapechar   s
    zTestTextReader.test_escapecharc                 C   s   d S Nr   r   r   r   r   test_eof_has_eol   s    zTestTextReader.test_eof_has_eolc                 C   s   d S rg   r   rh   r   r   r   test_na_substitution   s    z#TestTextReader.test_na_substitutionc                    s   d  fdd}|dd}|  }|d jdks4J tjg ddd}|d |k sZJ |d	 jd
kslJ |dd}|  }|d jdksJ tjg ddd}|d |k sJ |d	 jdksJ 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-   r?   rL   r	   r   r   kwdsr   r   r   _make_reader   s    z<TestTextReader.test_numpy_string_dtype.<locals>._make_readerzS5,i4r,   r   ZS5)r+   aaaaaaaaaZaaaaar.   i4ZS4)r+   rp   rq   rr   rr   )r   r-   r3   r4   all)r   ro   r   r(   Z	ex_valuesr   rn   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sLJ |tjtdd}|  }|d jdksxJ |d	 jd
ksJ |tdtd
dd}|  }|d jdksJ |d	 jd
ksJ 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-   r@   r?   rk   rl   rn   r   r   ro      s    z4TestTextReader.test_pass_dtype.<locals>._make_readeru1ZS1)oner.   r,   r   r.   O)r   r-   r3   Zuint8rZ   )r   ro   r   r(   r   rn   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 sTJ |d |d k slJ 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 )Nr@   r?   )r   r   rl   rn   r   r   ro      s    z1TestTextReader.test_usecols.<locals>._make_reader)r.   r"   )Zusecolsr"   r.   )r   r#   rt   )r   ro   r   r(   expr   rn   r   test_usecols   s    

zTestTextReader.test_usecolsztext, kwargsz a,b,c1,2,34,5,67,8,910,11,12r@   r?   z*a  b  c1  2  34  5  67  8  910  11  12r8   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   r[   )r   r   kwargsZ	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,,r?   )r@   r.   r_   r,   2 3rW   )r   r   r   r3   r4   rG   rZ   r[   )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 )Nr/   r+   rT   )columnsindex)r.   r.   r.   r   abcdr.   )r.   r"   ra   r_   rb   )   	   r            )r   r`         za,b
c
r   )Z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   r3   nanlistr   r   r1   rJ   )r   r   r+   r/   rT   dfr   r   r   #test_empty_field_eof_mem_access_bug-  s.    "z2TestTextReader.test_empty_field_eof_mem_access_bugc                 C   sH   t t dd g dd}t|ts&J W d    n1 s:0    Y  d S )N   r^   )	chunksizer   r   )r   r   
isinstancer   )r   r   r   r   r   test_empty_csv_inputL  s
    z#TestTextReader.test_empty_csv_inputN) __name__
__module____qualname__rX   Zfixturer   r   r   r!   r)   r6   r7   r9   r=   rC   rH   rK   r]   rc   rf   ri   rj   ru   ry   r{   markZparametrizer   r   ranger   r   r   r   r   r   r
      sP   
			





r
   c                 C   s2   |   D ]$\}}tt|t||  qd S rg   )itemsr1   r2   r3   Zasarray)leftrightkvr   r   r   r[   T  s    r[   )__doc__r   r   r   numpyr3   rX   Zpandas._libs.parsersZ_libsparsersrY   r   Zpandas.errorsr   Zpandasr   Zpandas._testingZ_testingr1   Zpandas.io.parsersr   r   Z"pandas.io.parsers.c_parser_wrapperr	   r
   r[   r   r   r   r   <module>   s     ;