B
    18™\2)  ã               @   sx   d dl Z d dlZd dlmZmZ G dd„ de jƒZG dd„ de jƒZG dd„ de jƒZ	d	d
„ Z
dd„ Zedkrteƒ  dS )é    N)ÚTESTFNÚunlinkc               @   s^   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	j
d	k d
¡dd„ ƒZdd„ Zdd„ Zdd„ ZdS )ÚCollationTestsc          	   C   s4   t  d¡}|  t¡ | d dd„ ¡ W d Q R X d S )Nz:memory:c             S   s   | |k| |k  S )N© )ÚxÚyr   r   úV/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/sqlite3/test/hooks.pyÚ<lambda>!   ó    z>CollationTests.CheckCreateCollationNotString.<locals>.<lambda>)ÚsqliteÚconnectÚassertRaisesÚ	TypeErrorÚcreate_collation)ÚselfÚconr   r   r   ÚCheckCreateCollationNotString   s    
z,CollationTests.CheckCreateCollationNotStringc          	   C   sB   t  d¡}|  t¡}| dd¡ W d Q R X |  t|jƒd¡ d S )Nz:memory:ÚXé*   zparameter must be callable)r   r   r   r   r   ÚassertEqualÚstrÚ	exception)r   r   Úcmr   r   r   ÚCheckCreateCollationNotCallable#   s    
z.CollationTests.CheckCreateCollationNotCallablec          	   C   s6   t  d¡}|  t j¡ | ddd„ ¡ W d Q R X d S )Nz:memory:u   collÃ¤c             S   s   | |k| |k  S )Nr   )r   r   r   r   r   r	   ,   r
   z=CollationTests.CheckCreateCollationNotAscii.<locals>.<lambda>)r   r   r   ÚProgrammingErrorr   )r   r   r   r   r   ÚCheckCreateCollationNotAscii)   s    
z+CollationTests.CheckCreateCollationNotAsciic             C   sl   G dd„ dt ƒ}t d¡}dd„ }| |dƒ|¡ | d¡ ¡ }|  |d d d	¡ |  |d
 d d¡ d S )Nc               @   s   e Zd Zdd„ ZdS )z@CollationTests.CheckCreateCollationBadUpper.<locals>.BadUpperStrc             S   s   d S )Nr   )r   r   r   r   Úupper0   s    zFCollationTests.CheckCreateCollationBadUpper.<locals>.BadUpperStr.upperN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚBadUpperStr/   s   r    z:memory:c             S   s   | |k| |k   S )Nr   )r   r   r   r   r   r	   3   r
   z=CollationTests.CheckCreateCollationBadUpper.<locals>.<lambda>Úmycollz›
            select x from (
            select 'a' as x
            union
            select 'b' as x
            ) order by x collate mycoll
            r   Úbé   Úa)r   r   r   r   ÚexecuteÚfetchallr   )r   r    r   r!   Úresultr   r   r   ÚCheckCreateCollationBadUpper.   s    

z+CollationTests.CheckCreateCollationBadUpper)é   é   r#   z&old SQLite versions crash on this testc          	   C   sŽ   dd„ }t  d¡}| d|¡ d}| |¡ ¡ }| j|dddgd	d
 | dd ¡ |  t j¡}| |¡ ¡ }W d Q R X |  t|j	ƒd¡ d S )Nc             S   s   | |k| |k   S )Nr   )r   r   r   r   r   r!   B   s    z3CollationTests.CheckCollationIsUsed.<locals>.mycollz:memory:r!   zÉ
            select x from (
            select 'a' as x
            union
            select 'b' as x
            union
            select 'c' as x
            ) order by x collate mycoll
            )Úc)r"   )r$   z#the expected order was not returned)Úmsgz"no such collation sequence: mycoll)
r   r   r   r%   r&   r   r   ÚOperationalErrorr   r   )r   r!   r   Úsqlr'   r   r   r   r   ÚCheckCollationIsUsed?   s    
	z#CollationTests.CheckCollationIsUsedc             C   sJ   dd„ }t  d¡}| d|¡ d}| |¡ ¡ }| j|dddgd	d
 d S )Nc             S   s   | |k| |k   d S )Nl        r   )r   r   r   r   r   r!   [   s    z@CollationTests.CheckCollationReturnsLargeInteger.<locals>.mycollz:memory:r!   zÉ
            select x from (
            select 'a' as x
            union
            select 'b' as x
            union
            select 'c' as x
            ) order by x collate mycoll
            )r+   )r"   )r$   z#the expected order was not returned)r,   )r   r   r   r%   r&   r   )r   r!   r   r.   r'   r   r   r   Ú!CheckCollationReturnsLargeIntegerZ   s    
	z0CollationTests.CheckCollationReturnsLargeIntegerc             C   sd   t  d¡}| ddd„ ¡ | ddd„ ¡ | d¡ ¡ }|  |d d d¡ |  |d	 d d
¡ dS )z„
        Register two different collation functions under the same name.
        Verify that the last one is actually used.
        z:memory:r!   c             S   s   | |k| |k  S )Nr   )r   r   r   r   r   r	   s   r
   z<CollationTests.CheckCollationRegisterTwice.<locals>.<lambda>c             S   s   | |k| |k   S )Nr   )r   r   r   r   r   r	   t   r
   zi
            select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
            r   r"   r#   r$   N)r   r   r   r%   r&   r   )r   r   r'   r   r   r   ÚCheckCollationRegisterTwicem   s    

z*CollationTests.CheckCollationRegisterTwicec          	   C   s^   t  d¡}| ddd„ ¡ | dd¡ |  t j¡}| d¡ W dQ R X |  t|jƒd¡ dS )zu
        Register a collation, then deregister it. Make sure an error is raised if we try
        to use it.
        z:memory:r!   c             S   s   | |k| |k  S )Nr   )r   r   r   r   r   r	      r
   z9CollationTests.CheckDeregisterCollation.<locals>.<lambda>Nz?select 'a' as x union select 'b' as x order by x collate mycollz"no such collation sequence: mycoll)	r   r   r   r   r-   r%   r   r   r   )r   r   r   r   r   r   ÚCheckDeregisterCollation{   s    
z'CollationTests.CheckDeregisterCollationN)r   r   r   r   r   r   r(   ÚunittestÚskipIfr   Úsqlite_version_infor/   r0   r1   r2   r   r   r   r   r      s   r   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚProgressTestsc                s>   t  d¡}g ‰ ‡ fdd„}| |d¡ | d¡ |  ˆ ¡ dS )zK
        Test that the progress handler is invoked once it is set.
        z:memory:c                  s   ˆ   d ¡ dS )Nr   )Úappendr   )Úprogress_callsr   r   ÚprogressŽ   s    
z8ProgressTests.CheckProgressHandlerUsed.<locals>.progressr#   z0
            create table foo(a, b)
            N)r   r   Úset_progress_handlerr%   Ú
assertTrue)r   r   r9   r   )r8   r   ÚCheckProgressHandlerUsedˆ   s    
z&ProgressTests.CheckProgressHandlerUsedc                sr   t  d¡}g ‰ ‡ fdd„}| |d¡ | ¡ }| d¡ tˆ ƒ}g ‰ | |d¡ | d¡ tˆ ƒ}|  ||¡ dS )	z=
        Test that the opcode argument is respected.
        z:memory:c                  s   ˆ   d ¡ dS )Nr   )r7   r   )r8   r   r   r9   ž   s    
z0ProgressTests.CheckOpcodeCount.<locals>.progressr#   z1
            create table foo (a, b)
            r*   z1
            create table bar (a, b)
            N)r   r   r:   Úcursorr%   ÚlenÚassertGreaterEqual)r   r   r9   ÚcursZfirst_countZsecond_countr   )r8   r   ÚCheckOpcodeCount˜   s    
zProgressTests.CheckOpcodeCountc             C   s<   t  d¡}dd„ }| |d¡ | ¡ }|  t j|jd¡ dS )zW
        Test that returning a non-zero value stops the operation in progress.
        z:memory:c               S   s   dS )Nr#   r   r   r   r   r   r9   ´   s    z4ProgressTests.CheckCancelOperation.<locals>.progressr#   zcreate table bar (a, b)N)r   r   r:   r=   r   r-   r%   )r   r   r9   r@   r   r   r   ÚCheckCancelOperation¯   s    
z"ProgressTests.CheckCancelOperationc                sR   t  d¡}d‰ ‡ fdd„}| |d¡ | dd¡ | d¡ ¡  |  ˆ dd¡ dS )	zc
        Test that setting the progress handler to None clears the previously set handler.
        z:memory:r   c                  s   d‰ dS )Nr#   r   r   r   )Úactionr   r   r9   Ã   s    z1ProgressTests.CheckClearHandler.<locals>.progressr#   Nz&select 1 union select 2 union select 3z progress handler was not cleared)r   r   r:   r%   r&   r   )r   r   r9   r   )rC   r   ÚCheckClearHandler½   s    
zProgressTests.CheckClearHandlerN)r   r   r   r<   rA   rB   rD   r   r   r   r   r6   ‡   s   r6   c               @   s>   e Zd Zdd„ Zdd„ Zdd„ Ze ej	dk d¡d	d
„ ƒZ
dS )ÚTraceCallbackTestsc                sT   t  d¡}g ‰ ‡ fdd„}| |¡ | d¡ |  ˆ ¡ |  tdd„ ˆ D ƒƒ¡ dS )zI
        Test that the trace callback is invoked once it is set.
        z:memory:c                s   ˆ   | ¡ d S )N)r7   )Ú	statement)Útraced_statementsr   r   ÚtraceÓ   s    z8TraceCallbackTests.CheckTraceCallbackUsed.<locals>.tracezcreate table foo(a, b)c             s   s   | ]}d |kV  qdS )zcreate table fooNr   )Ú.0Ústmtr   r   r   ú	<genexpr>Ø   s    z<TraceCallbackTests.CheckTraceCallbackUsed.<locals>.<genexpr>N)r   r   Úset_trace_callbackr%   r;   Úany)r   r   rH   r   )rG   r   ÚCheckTraceCallbackUsedÍ   s    



z)TraceCallbackTests.CheckTraceCallbackUsedc                sH   t  d¡}g ‰ ‡ fdd„}| |¡ | d¡ | d¡ |  ˆ d¡ dS )zb
        Test that setting the trace callback to None clears the previously set callback.
        z:memory:c                s   ˆ   | ¡ d S )N)r7   )rF   )rG   r   r   rH   à   s    z9TraceCallbackTests.CheckClearTraceCallback.<locals>.traceNzcreate table foo(a, b)ztrace callback was not cleared)r   r   rL   r%   ÚassertFalse)r   r   rH   r   )rG   r   ÚCheckClearTraceCallbackÚ   s    



z*TraceCallbackTests.CheckClearTraceCallbackc          
      s‚   d‰t  d¡}g ‰ ‡ fdd„}| |¡ | d¡ | dˆ ¡ | ¡  |  t‡fdd„ˆ D ƒƒd	tˆƒd
 t	tˆ ƒ¡f ¡ dS )zG
        Test that the statement can contain unicode literals.
        u   Ã¶Ã¤Ã¼Ã–Ã„ÃœÃŸâ‚¬z:memory:c                s   ˆ   | ¡ d S )N)r7   )rF   )rG   r   r   rH   î   s    z5TraceCallbackTests.CheckUnicodeContent.<locals>.tracezcreate table foo(x)z insert into foo(x) values ("%s")c             3   s   | ]}ˆ |kV  qd S )Nr   )rI   rJ   )Úunicode_valuer   r   rK   ÷   s    z9TraceCallbackTests.CheckUnicodeContent.<locals>.<genexpr>z-Unicode data %s garbled in trace callback: %sz, N)
r   r   rL   r%   Úcommitr;   rM   ÚasciiÚjoinÚmap)r   r   rH   r   )rG   rQ   r   ÚCheckUnicodeContentç   s    


z&TraceCallbackTests.CheckUnicodeContent)r)   r)   é	   z#sqlite3_prepare_v2 is not availablec                s„   g ‰ ‡ fdd„}ddg}|   tt¡ tjtd d}t t¡}| |¡ | ¡ }| |d ¡ | d¡ | |d ¡ |  ˆ |¡ d S )	Nc                s   ˆ   | ¡ d S )N)r7   )rF   )rG   r   r   rH   ÿ   s    z;TraceCallbackTests.CheckTraceCallbackContent.<locals>.tracezcreate table foo(x)zinsert into foo(x) values(1))Úisolation_levelr   zcreate table bar(x)r#   )	Ú
addCleanupr   r   r   r   rL   r=   r%   r   )r   rH   ZqueriesÚcon1Úcon2Úcurr   )rG   r   ÚCheckTraceCallbackContentû   s    


z,TraceCallbackTests.CheckTraceCallbackContentN)r   r   r   rN   rP   rV   r3   r4   r   r5   r]   r   r   r   r   rE   Ì   s   rE   c              C   s4   t  td¡} t  td¡}t  td¡}t  | ||f¡S )NÚCheck)r3   Ú	makeSuiter   r6   rE   Ú	TestSuite)Zcollation_suiteZprogress_suiteZtrace_suiter   r   r   Úsuite  s    ra   c              C   s   t  ¡ } |  tƒ ¡ d S )N)r3   ÚTextTestRunnerÚrunra   )Úrunnerr   r   r   Útest  s    re   Ú__main__)r3   Úsqlite3r   Útest.supportr   r   ÚTestCaser   r6   rE   ra   re   r   r   r   r   r   Ú<module>   s   jEC