
    daf2                         d dl Z d dlZd dlZd dlmZmZ d dlmZm	Z	 d dl
mZ  G d dej                        Z G d dej                        Z G d	 d
ej                        Zedk(  r ej"                          yy)    N)TESTFNunlink)memory_databasecx_limit)with_tracebacksc                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
CollationTestsc                     t        j                  d      }| j                  t              5  |j	                  d d        d d d        y # 1 sw Y   y xY w)N:memory:c                     | |kD  | |k  z
  S N xys     7/root/Python-3.12.4/Lib/test/test_sqlite3/test_hooks.py<lambda>zACollationTests.test_create_collation_not_string.<locals>.<lambda>%       QUq1u4E     )sqliteconnectassertRaises	TypeErrorcreate_collationselfcons     r    test_create_collation_not_stringz/CollationTests.test_create_collation_not_string"   sH    nnZ(y) 	G  'EF	G 	G 	Gs   AAc                     t        j                  d      }| j                  t              5 }|j	                  dd       d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nr   X*   zparameter must be callable)r   r   r   r   r   assertEqualstr	exceptionr   r   cms      r   "test_create_collation_not_callablez1CollationTests.test_create_collation_not_callable'   s_    nnZ(y) 	*R  b)	*R\\*,HI	* 	*s   A,,A5c                 T    t        j                  d      }|j                  dd        y )Nr   u   colläc                     | |kD  | |k  z
  S r   r   r   s     r   r   z@CollationTests.test_create_collation_not_ascii.<locals>.<lambda>/   r   r   )r   r   r   r   s     r   test_create_collation_not_asciiz.CollationTests.test_create_collation_not_ascii-   s!    nnZ(X'EFr   c                      G d dt               }t        j                  d      }d }|j                   |d      |       |j	                  d      j                         }| j                  |d   d   d       | j                  |d	   d   d
       y )Nc                       e Zd Zd Zy)CCollationTests.test_create_collation_bad_upper.<locals>.BadUpperStrc                      y r   r   r   s    r   upperzICollationTests.test_create_collation_bad_upper.<locals>.BadUpperStr.upper3   s    r   N)__name__
__module____qualname__r0   r   r   r   BadUpperStrr-   2   s    r   r4   r   c                     | |kD  | |k  z
   S r   r   r   s     r   r   z@CollationTests.test_create_collation_bad_upper.<locals>.<lambda>6   s    Q1q5 12 r   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   r4   r   r6   results        r   test_create_collation_bad_upperz.CollationTests.test_create_collation_bad_upper1   s    	# 	 nnZ(2[2F;    	 	1s+1s+r   c                    d }t        j                  d      }|j                  d|       d}|j                  |      j	                         }| j                  |g dd       |j                  dd        | j                  t         j                        5 }|j                  |      j	                         }d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)	Nc                     | |kD  | |k  z
   S r   r   r   s     r   r6   z5CollationTests.test_collation_is_used.<locals>.mycollC   s    !eA&''r   r   r6   
            select x from (
            select 'a' as x
            union
            select 'b' as x
            union
            select 'c' as x
            ) order by x collate mycoll
            )c)r7   )r9   #the expected order was not returnedmsg"no such collation sequence: mycoll)
r   r   r   r:   r;   r"   r   OperationalErrorr#   r$   )r   r6   r   sqlr<   r&   s         r   test_collation_is_usedz%CollationTests.test_collation_is_usedB   s    	( nnZ(Xv. S!**,!9B 	 	D 	Xt,v667 	12[[%..0F	1R\\*,PQ	1 	1s    C!!C*c                     d }t        j                  d      }|j                  d|       d}|j                  |      j	                         }| j                  |g dd       y )Nc                      | |kD  | |k  z
   dz  S )Nl        r   r   s     r   r6   zCCollationTests.test_collation_returns_large_integer.<locals>.mycoll\   s    !eA&'%//r   r   r6   r@   rA   rC   rD   r   r   r   r:   r;   r"   )r   r6   r   rH   r<   s        r   $test_collation_returns_large_integerz3CollationTests.test_collation_returns_large_integer[   sc    	0 nnZ(Xv. S!**,!9B 	 	Dr   c                    t        j                  d      }|j                  dd        |j                  dd        |j                  d      j	                         }| j                  |d   d   d       | j                  |d   d   d	       y
)z
        Register two different collation functions under the same name.
        Verify that the last one is actually used.
        r   r6   c                     | |kD  | |k  z
  S r   r   r   s     r   r   z>CollationTests.test_collation_register_twice.<locals>.<lambda>t   r   r   c                     | |kD  | |k  z
   S r   r   r   s     r   r   z>CollationTests.test_collation_register_twice.<locals>.<lambda>u   s    q1uQ6G4H r   zi
            select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
            r   r7   r8   r9   NrL   )r   r   r<   s      r   test_collation_register_twicez,CollationTests.test_collation_register_twicen   s    
 nnZ(X'EFX'HI   	 	1s+1s+r   c                 L   t        j                  d      }|j                  dd        |j                  dd       | j                  t         j                        5 }|j                  d       ddd       | j                  t        j                        d       y# 1 sw Y   /xY w)zu
        Register a collation, then deregister it. Make sure an error is raised if we try
        to use it.
        r   r6   c                     | |kD  | |k  z
  S r   r   r   s     r   r   z:CollationTests.test_deregister_collation.<locals>.<lambda>   r   r   Nz?select 'a' as x union select 'b' as x order by x collate mycollrF   )	r   r   r   r   rG   r:   r"   r#   r$   r%   s      r   test_deregister_collationz(CollationTests.test_deregister_collation|   s    
 nnZ(X'EFXt,v667 	[2KKYZ	[R\\*,PQ	[ 	[s   BB#N)r1   r2   r3   r   r'   r*   r=   rI   rM   rQ   rT   r   r   r   r	   r	   !   s0    G
JG,"R2D&,
Rr   r	   c                   d    e Zd Zd Zd Zd Zd Z eed      d        Z	 eed      d        Z
y	)
ProgressTestsc                     t        j                  d      }g fd}|j                  |d       |j                  d       | j	                         y)zK
        Test that the progress handler is invoked once it is set.
        r   c                  (     j                  d        yNr   appendprogress_callss   r   progressz:ProgressTests.test_progress_handler_used.<locals>.progress       !!$'r   r8   z0
            create table foo(a, b)
            N)r   r   set_progress_handlerr:   
assertTrue)r   r   r^   r]   s      @r   test_progress_handler_usedz(ProgressTests.test_progress_handler_used   sN     nnZ(	 	  1-  	 	'r   c                 >   t        j                  d      }g fd}|j                  |d       |j                         }|j	                  d       t              }g |j                  |d       |j	                  d       t              }| j                  ||       y)z=
        Test that the opcode argument is respected.
        r   c                  (     j                  d        yrY   rZ   r\   s   r   r^   z1ProgressTests.test_opcode_count.<locals>.progress   r_   r   r8   z1
            create table foo (a, b)
               z1
            create table bar (a, b)
            N)r   r   r`   cursorr:   lenassertGreaterEqual)r   r   r^   cursfirst_countsecond_countr]   s         @r   test_opcode_countzProgressTests.test_opcode_count   s     nnZ(	 	  1-zz|  	 .)  1-  	 >*\:r   c                     t        j                  d      }d }|j                  |d       |j                         }| j	                  t         j
                  |j                  d       y)zW
        Test that returning a non-zero value stops the operation in progress.
        r   c                       y)Nr8   r   r   r   r   r^   z5ProgressTests.test_cancel_operation.<locals>.progress   s    r   r8   zcreate table bar (a, b)N)r   r   r`   rf   r   rG   r:   )r   r   r^   ri   s       r   test_cancel_operationz#ProgressTests.test_cancel_operation   sS     nnZ(	  1-zz|##LL%	'r   c                     t        j                  d      }dfd}|j                  |d       |j                  dd       |j                  d      j	                          | j                  dd       y)zc
        Test that setting the progress handler to None clears the previously set handler.
        r   r   c                  
    d yNr8   r   r   )actions   r   r^   z2ProgressTests.test_clear_handler.<locals>.progress   s    Fr   r8   Nz&select 1 union select 2 union select 3z progress handler was not cleared)r   r   r`   r:   r;   r"   )r   r   r^   rs   s      @r   test_clear_handlerz ProgressTests.test_clear_handler   sh     nnZ(	 	  1-  q)<=FFH$FGr   bad_progress)namec                     t        j                  d      }d }|j                  |d       | j                  t         j                        5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr   c                      ddz   y rr   r   r   r   r   ru   zBProgressTests.test_error_in_progress_handler.<locals>.bad_progress   s	    Er   r8   8
                create table foo(a, b)
                r   r   r`   r   rG   r:   )r   r   ru   s      r   test_error_in_progress_handlerz,ProgressTests.test_error_in_progress_handler   s_    nnZ(	  q1v667 	KK  	 	 	s   
A%%A.c                     t        j                  d      } G d d      fd}|j                  |d       | j                  t         j                        5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr   c                       e Zd Zd Zy)DProgressTests.test_error_in_progress_handler_result.<locals>.BadBoolc                     ddz   y rr   r   r/   s    r   __bool__zMProgressTests.test_error_in_progress_handler_result.<locals>.BadBool.__bool__   s	    Ar   N)r1   r2   r3   r   r   r   r   BadBoolr~      s    r   r   c                               S r   r   )r   s   r   ru   zIProgressTests.test_error_in_progress_handler_result.<locals>.bad_progress   s    9r   r8   ry   rz   )r   r   ru   r   s      @r   %test_error_in_progress_handler_resultz3ProgressTests.test_error_in_progress_handler_result   sj    nnZ(	 		  q1v667 	KK  	 	 	s   A22A;N)r1   r2   r3   rb   rl   ro   rt   r   ZeroDivisionErrorr{   r   r   r   r   rV   rV      sO    (;.'H &^< = &^< =r   rV   c                       e Zd Zej                  d        Zd Zd Zd Zd Z	d Z
 eej                  d      d	        Z eed
      d        Zy)TraceCallbackTestsc              #      K   	 g |j                  fd       d  | j                  |       |j                  d        y # | j                  |       |j                  d        w xY ww)Nc                 &    j                  |       S r   rZ   )stmttraceds    r   r   z5TraceCallbackTests.check_stmt_trace.<locals>.<lambda>   s    v}}T/B r   )set_trace_callbackr"   )r   cxexpectedr   s      @r   check_stmt_tracez#TraceCallbackTests.check_stmt_trace   sb     	(F!!"BCVX.!!$' VX.!!$'s   A+A $A+%A((A+c                     t        j                  d      }g fd}|j                  |       |j                  d       | j	                         | j	                  t        d D                     y)zI
        Test that the trace callback is invoked once it is set.
        r   c                 (    j                  |        y r   rZ   	statementtraced_statementss    r   tracez:TraceCallbackTests.test_trace_callback_used.<locals>.trace       $$Y/r   create table foo(a, b)c              3   $   K   | ]  }d |v  
 yw)zcreate table fooNr   ).0r   s     r   	<genexpr>z>TraceCallbackTests.test_trace_callback_used.<locals>.<genexpr>   s     U4.$6Us   N)r   r   r   r:   ra   anyr   r   r   r   s      @r   test_trace_callback_usedz+TraceCallbackTests.test_trace_callback_used   s^     nnZ(	0u%,-)*UCTUUVr   c                     t        j                  d      }g fd}|j                  |       |j                  d       |j                  d       | j	                  d       y)zb
        Test that setting the trace callback to None clears the previously set callback.
        r   c                 (    j                  |        y r   rZ   r   s    r   r   z;TraceCallbackTests.test_clear_trace_callback.<locals>.trace  r   r   Nr   ztrace callback was not cleared)r   r   r   r:   assertFalser   s      @r   test_clear_trace_callbackz,TraceCallbackTests.test_clear_trace_callback   sY     nnZ(	0u%t$,-*,LMr   c                 p   dt        j                  d      }g fd}|j                  |       |j                  d       |j                  dz         |j	                          | j                  t        fdD              dt              dd	j                  t        t                           y
)zG
        Test that the statement can contain unicode literals.
        u   öäüÖÄÜß€r   c                 (    j                  |        y r   rZ   r   s    r   r   z6TraceCallbackTests.test_unicode_content.<locals>.trace  r   r   create table foo(x)z insert into foo(x) values ('%s')c              3   &   K   | ]  }|v  
 y wr   r   )r   r   unicode_values     r   r   z:TraceCallbackTests.test_unicode_content.<locals>.<genexpr>  s     PdMT1Ps   zUnicode data z garbled in trace callback: z, N)
r   r   r   r:   commitra   r   asciijoinmap)r   r   r   r   r   s      @@r   test_unicode_contentz'TraceCallbackTests.test_unicode_content  s     =nnZ(	0u%)*6FG

P>OPP /3uFW;X1Y[	\r   c                    g fd}ddg}| j                  t        t               t        j                  t        d       }t        j                  t              }	 |j                  |       |j                         }|j                  |d          |j                  d       |j                  |d          |j                          |j                          | j                  |       y # |j                          |j                          w xY w)Nc                 (    j                  |        y r   rZ   r   s    r   r   z=TraceCallbackTests.test_trace_callback_content.<locals>.trace  r   r   r   zinsert into foo(x) values(1))isolation_levelr   zcreate table bar(x)r8   )

addCleanupr   r   r   r   r   rf   r:   closer"   )r   r   queriescon1con2curr   s         @r   test_trace_callback_contentz.TraceCallbackTests.test_trace_callback_content  s    	0 )13'~~fd;~~f%	##E*++-CKK
#LL./KK
#JJLJJL*G4 JJLJJLs   AC) )"Dc           
      ,   g d}t               5 }| j                  ||      5  |5  |j                  d       |j                  dd t	        d      D               d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N)create table t(t)zBEGIN zinsert into t values(0)zinsert into t values(1)zinsert into t values(2)COMMITr   zinsert into t values(?)c              3   "   K   | ]  }|f 	 y wr   r   )r   vs     r   r   z=TraceCallbackTests.test_trace_expanded_sql.<locals>.<genexpr>>  s     :RAA4:Rs      )r   r   r:   executemanyrange)r   r   r   s      r   test_trace_expanded_sqlz*TraceCallbackTests.test_trace_expanded_sql2  s    
  	T"d&;&;B&I 	T T

./8:Rq:RST	T 	T 	TT T	T 	T 	T 	Ts9   B
A>4A2A>!B
2A;7A>>B	B

Bz5Expanded SQL string exceeds the maximum string length)regexc                    d}t         j                  }t               5 }t        ||      5 }d}d|z  }|dz   }|g}t         j                  dk  rg }| j                  ||      5  |j                  ||f       d d d        | d| d}	| j                  ||	g      5  |j                  ||f       d d d        d d d        d d d        y # 1 sw Y   QxY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   y xY w)Nzselect 1 as a where a=)categoryr9   ?)r      r   ')r   SQLITE_LIMIT_LENGTHr   r   sqlite_version_infor   r:   )
r   templater   r   limok_param	bad_paramunexpanded_queryr   expanded_querys
             r    test_trace_too_much_expanded_sqlz3TraceCallbackTests.test_trace_too_much_expanded_sql@  s    ,-- 	:"hrH&E 	:Hc	I'#~()H))J6&&r84 ;

+i\:; !)z8*A6N&&rN+;< :

+h[9:	: 	: 	:; ;: :	: 	: 	: 	:sR   C*7C"C6#CC-C5C*CCCCC'	#C**C3zdivision by zeroc                     t               5 }|j                  d        |j                  d       d d d        y # 1 sw Y   y xY w)Nc                     ddz  S )N   r   r   )r   s    r   r   z;TraceCallbackTests.test_trace_bad_handler.<locals>.<lambda>]  s
    qs r   zselect 1)r   r   r:   )r   r   s     r   test_trace_bad_handlerz)TraceCallbackTests.test_trace_bad_handlerZ  s:     	#"!!"23JJz"	# 	# 	#s	   $8AN)r1   r2   r3   
contextlibcontextmanagerr   r   r   r   r   r   r   r   	DataErrorr   r   r   r   r   r   r   r      sz    ( (WN\"5,T E:	:, &.@A# B#r   r   __main__)r   sqlite3r   unittesttest.support.os_helperr   r   test.test_sqlite3.test_dbapir   r   $test.test_sqlite3.test_userfunctionsr   TestCaser	   rV   r   r1   mainr   r   r   <module>r      st   .    1 B @eRX&& eRN[H%% [|x#** x#v zHMMO r   