
    Vhe                     N   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZ ddlmZ d	Z G d
 d      Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Z eed e                eed e                G d d       Z! G d! d"ejD                        Z# G d# d$ejD                        Z$ G d% d&      Z% G d' d(e	      Z& G d) d*      Z' G d+ d,ejD                        Z(y)-z&
Tests for twisted.enterprise.adbapi.
    N)DictOptional)
ConnectionConnectionLostConnectionPoolTransaction)defer
interfacesreactor)FailurerequireModule)unittestz%
CREATE TABLE simple (
  x integer
)
c                       e Zd ZU dZi Zeeef   ed<    e	j                  ed      dZd Zd Zd ZddZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#y)ADBAPITestBasez,
    Test the asynchronous DB-API code.
    openfun_calledN5ADB-API requires threads, no way to test without themc                     | j                          | j                  | j                        | _        | j                  j	                          y)zR
        Set up the database and create a connection pool pointing at it.
        )
cp_openfunN)startDBmakePoolopenfundbpoolstartselfs    H/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_adbapi.py
extraSetUpzADBAPITestBase.extraSetUp(   s3     	mmt||m<    c                       j                   j                  d      }|j                   fd       |j                   fd       |S )NDROP TABLE simplec                 8    j                   j                         S Nr   closeresr   s    r   <lambda>z)ADBAPITestBase.tearDown.<locals>.<lambda>2       $++"3"3"5 r   c                 $    j                         S r#   stopDBr&   s    r   r(   z)ADBAPITestBase.tearDown.<locals>.<lambda>3       $++- r   r   runOperationaddCallbackr   ds   ` r   tearDownzADBAPITestBase.tearDown0   7    KK$$%89	56	/0r   c                 "    d| j                   |<   y )NT)r   r   conns     r   r   zADBAPITestBase.openfun6   s    $(D!r   c                 x    |s| j                  | j                         y | j                  || j                         y r#   )
assertTruer   assertInr6   s     r   checkOpenfunCalledz!ADBAPITestBase.checkOpenfunCalled9   s+    OOD//0MM$ 3 34r   c                      j                   j                  t              } j                  r|j	                   j
                         |j	                   j                         |j	                   j                         |j	                   j                         |j	                   fd       |j	                   j                         |j	                   j                         |j	                   j                         |j	                   j                         |j	                   j                         |j	                   j                         |j	                   j                         |j	                   j                          |S )Nc                 $    j                         S r#   )flushLoggedErrorsr&   s    r   r(   z*ADBAPITestBase.test_pool.<locals>.<lambda>F   s    d&<&<&> r   )r   r/   simple_table_schematest_failuresr0   _testPool_1_1_testPool_1_2_testPool_1_3_testPool_1_4_testPool_2_testPool_3_testPool_4_testPool_5_testPool_6_testPool_7_testPool_8_testPool_9r1   s   ` r   	test_poolzADBAPITestBase.test_pool?   s   KK$$%89MM$,,-MM$,,-MM$,,-MM$,,-MM>?	d&&'	d&&'	d&&'	d&&'	d&&'	d&&'	d&&'	d&&'r   c                      t        j                   j                  j                  d      }|j	                   fdd        |S )Nselect * from NOTABLEc                 &    j                  d      S Nzno exceptionfailr&   s    r   r(   z.ADBAPITestBase._testPool_1_1.<locals>.<lambda>S       499^#< r   c                      y r#    fs    r   r(   z.ADBAPITestBase._testPool_1_1.<locals>.<lambda>S       r   r	   maybeDeferredr   runQueryaddCallbacksr   r'   r2   s   `  r   rA   zADBAPITestBase._testPool_1_1Q   s4     4 46MN	<nMr   c                      t        j                   j                  j                  d      }|j	                   fdd        |S )Nzdeletexxx from NOTABLEc                 &    j                  d      S rQ   rR   r&   s    r   r(   z.ADBAPITestBase._testPool_1_2.<locals>.<lambda>X   rT   r   c                      y r#   rV   rW   s    r   r(   z.ADBAPITestBase._testPool_1_2.<locals>.<lambda>X   rY   r   )r	   r[   r   r/   r]   r^   s   `  r   rB   zADBAPITestBase._testPool_1_2V   s4     8 8:RS	<nMr   c                      t        j                   j                  j                   j                        }|j                   fdd        |S )Nc                 &    j                  d      S rQ   rR   r&   s    r   r(   z.ADBAPITestBase._testPool_1_3.<locals>.<lambda>]   rT   r   c                      y r#   rV   rW   s    r   r(   z.ADBAPITestBase._testPool_1_3.<locals>.<lambda>]   rY   r   )r	   r[   r   runInteractionbad_interactionr]   r^   s   `  r   rC   zADBAPITestBase._testPool_1_3[   s9     : :D<P<PQ	<nMr   c                      t        j                   j                  j                   j                        }|j                   fdd        |S )Nc                 &    j                  d      S rQ   rR   r&   s    r   r(   z.ADBAPITestBase._testPool_1_4.<locals>.<lambda>b   rT   r   c                      y r#   rV   rW   s    r   r(   z.ADBAPITestBase._testPool_1_4.<locals>.<lambda>b   rY   r   )r	   r[   r   runWithConnectionbad_withConnectionr]   r^   s   `  r   rD   zADBAPITestBase._testPool_1_4`   s9     = =t?V?VW	<nMr   c                 n     d} j                   j                  |      } fd}|j                  |       |S )Nselect count(1) from simplec                 n    j                  t        | d   d         dk(  d       j                          y )Nr   zInteraction not rolled back)r9   intr;   rowr   s    r   _checkz*ADBAPITestBase._testPool_2.<locals>._checkj   s/    OOCAq	Na/1NO##%r   r   r\   r0   r   r'   sqlr2   rr   s   `    r   rE   zADBAPITestBase._testPool_2e   s4    +KK  %	& 	
fr   c                 $    d}g }t         j                        D ]1  }d|z  }|j                   j                  j	                  |             3 t        j                  |      } fd}|j                  |        fd}|j                  |       |S )Nrm   z insert into simple(x) values(%d)c                 B    d}j                   j                  |      }|S )Nselect x from simple order by x)r   r\   )r'   ru   r2   r   s      r   _selectz+ADBAPITestBase._testPool_3.<locals>._selectz   s!    3C$$S)AHr   c                    j                  t        |       j                  k(  d       t        j                        D ]>  }j                  t        | |         dk(  d       j                  | |   d   |k(  d       @ y )NzWrong number of rows   Wrong size rowr   zValues not returned.)r9   lennum_iterationsrange)rowsir   s     r   rr   z*ADBAPITestBase._testPool_3.<locals>._check   st    OOCI)<)<<>TU4../ IDG 13CDQ
a1GHIr   )r   r~   appendr   r/   r	   gatherResultsr0   )r   r'   ru   insertsr   r2   ry   rr   s   `       r   rF   zADBAPITestBase._testPool_3q   s    +t**+ 	:A4q8CNN4;;33C89	: (	 	
g	I 	
fr   c                 z      j                   j                   j                        }|j                   fd       |S )Nc                 (    j                  | d      S NdoneassertEqualr&   s    r   r(   z,ADBAPITestBase._testPool_4.<locals>.<lambda>       $"2"23"? r   )r   re   interactionr0   r^   s   `  r   rG   zADBAPITestBase._testPool_4   s/    KK&&t'7'78	?@r   c                 z      j                   j                   j                        }|j                   fd       |S )Nc                 (    j                  | d      S r   r   r&   s    r   r(   z,ADBAPITestBase._testPool_5.<locals>.<lambda>   r   r   )r   rj   withConnectionr0   r^   s   `  r   rH   zADBAPITestBase._testPool_5   s/    KK))$*=*=>	?@r   c                 P    | j                   j                  | j                        }|S r#   )r   rj   close_withConnectionr^   s      r   rI   zADBAPITestBase._testPool_6   s!    KK))$*C*CDr   c                      g }t         j                        D ]1  }d|z  }|j                   j                  j	                  |             3 t        j                  |d      } fd}|j                  |       |S )Nz!select x from simple where x = %dTfireOnOneErrbackc                 |    t        j                        D ]#  }j                  | |   d   d   d   |k(  d       % y )Nr{   r   zValue not returned)r   r~   r9   )resultr   r   s     r   rr   z*ADBAPITestBase._testPool_7.<locals>._check   sD    4../ Oq	!Q 2a 79MNOr   )r   r~   r   r   r\   r	   DeferredListr0   )r   r'   dsr   ru   dlistrr   s   `      r   rJ   zADBAPITestBase._testPool_7   sv    t**+ 	1A59CIIdkk**3/0	1 ""2=	O 	&!r   c                     g }t        | j                        D ]1  }d|z  }|j                  | j                  j	                  |             3 t        j                  |d      }|S )Nzdelete from simple where x = %dTr   )r   r~   r   r   r/   r	   r   )r   r'   r   r   ru   r   s         r   rK   zADBAPITestBase._testPool_8   s_    t**+ 	5A3a7CIIdkk..s34	5 ""2=r   c                 n     d} j                   j                  |      } fd}|j                  |       |S )Nrm   c                 n    j                  t        | d   d         dk(  d       j                          y )Nr   z)Didn't successfully delete table contents)r9   ro   checkConnectrp   s    r   rr   z*ADBAPITestBase._testPool_9.<locals>._check   s4    OOCF1I!#%P r   rs   rt   s   `    r   rL   zADBAPITestBase._testPool_9   s4    +KK  %	  	
fr   c                 l   | j                   j                         }| j                  |       |j                         }|j	                  d       |j	                  d       |j                         }| j                  t        |      d       | j                  t        |d         d       | j                  |d   d   d       |j	                  d       |j	                  d       | j                  t        |j                               d       |j                          | j                   j                  |       y)z/Check the connect/disconnect synchronous calls.zinsert into simple(x) values(1)zselect x from simpler{   r   zdelete from simpleN)
r   connectr;   cursorexecutefetchallr   r}   r%   
disconnect)r   r7   cursr'   s       r   r   zADBAPITestBase.checkConnect   s    {{""$%{{}67+,mmoS1%SVa(QA&)*+,T]]_-q1

t$r   c                 &   |j                  d       t        | j                        D ]H  }|j                         }| j	                  t        |      dk(  d       | j	                  |d   |k(  d       J | j                  |j                         d       y)Nrx   r{   r|   r   Value not returned.zToo many rowsr   )r   r   r~   fetchoner9   r}   assertIsNone)r   transactionr   rq   s       r   r   zADBAPITestBase.interaction   s    =>t**+ 	@A&&(COOCHM+;<OOCFaK)>?	@ 	+..0/Br   c                 `    | j                   r|j                  d       |j                  d       y )Nzinsert into simple(x) values(0)rO   )can_rollbackr   )r   r   s     r   rf   zADBAPITestBase.bad_interaction   s)     AB34r   c                 T   |j                         }	 |j                  d       t        | j                        D ]H  }|j	                         }| j                  t        |      dk(  d       | j                  |d   |k(  d       J 	 |j                          y# |j                          w xY w)Nrx   r{   r|   r   r   r   )r   r   r   r~   r   r9   r}   r%   )r   r7   r   r   rq   s        r   r   zADBAPITestBase.withConnection   s    {{}	LL:;4../ DmmoCA/?@A!-BCD
 JJL JJLs   A1B B'c                 $    |j                          y r#   )r%   r6   s     r   r   z#ADBAPITestBase.close_withConnection   s    

r   c                     |j                         }	 |j                  d       |j                          y # |j                          w xY w)NrO   )r   r   r%   )r   r7   r   s      r   rk   z!ADBAPITestBase.bad_withConnection   s2    {{}	LL01JJLDJJLs	   4 Ar#   )$__name__
__module____qualname____doc__r   r   objectbool__annotations__r
   IReactorThreadsr   skipr   r3   r   r;   rM   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r   r   rf   r   r   rk   rV   r   r   r   r      s     *,ND&+!z!!'408F)5$




6
%"5
r   r   c                   j    e Zd ZdZ ej
                  ed      dZd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zy)ReconnectTestBasez;
    Test the asynchronous DB-API code with reconnect.
    Nr   c                    | j                   t        j                  d      | j                          | j	                  dd| j                         | _        | j
                  j                          | j
                  j                  t              S )z
        Skip the test if C{good_sql} is unavailable.  Otherwise, set up the
        database, create a connection pool pointed at it, and set up a simple
        schema in it.
        zno good sql for reconnect testr{   T)cp_maxcp_reconnectcp_good_sql)	good_sqlr   SkipTestr   r   r   r   r/   r?   r   s    r   r   zReconnectTestBase.extraSetUp  sq     == ##$DEEmm4T]] $ 
 	{{''(;<<r   c                       j                   j                  d      }|j                   fd       |j                   fd       |S )Nr!   c                 8    j                   j                         S r#   r$   r&   s    r   r(   z,ReconnectTestBase.tearDown.<locals>.<lambda>  r)   r   c                 $    j                         S r#   r+   r&   s    r   r(   z,ReconnectTestBase.tearDown.<locals>.<lambda>  r-   r   r.   r1   s   ` r   r3   zReconnectTestBase.tearDown  r4   r   c                 V   t        j                  d       }|j                  | j                         |j                  | j                         | j
                  s|j                  | j                         |j                  | j                         |j                  | j                         |S r#   )	r	   succeedr0   _testPool_1rE   early_reconnectrF   rG   rH   r1   s     r   rM   zReconnectTestBase.test_pool  sx    MM$	d&&'	d&&'##MM$**+	d&&'	d&&'r   c                 n     d} j                   j                  |      } fd}|j                  |       |S )Nrm   c                 N    j                  t        | d   d         dk(  d       y Nr   zTable not emptyr9   ro   rp   s    r   rr   z-ReconnectTestBase._testPool_1.<locals>._check%  #    OOCAq	Na/1BCr   rs   rt   s   `    r   r   zReconnectTestBase._testPool_1!  5    +KK  %	D 	
fr   c                     t        | j                  j                  j                               d   j	                          y )Nr   )listr   connectionsvaluesr%   )r   r'   s     r   rE   zReconnectTestBase._testPool_2+  s+    T[[$$++-.q1779r   c                      d}t        j                   j                  j                  |      }|j	                   fdd        |S )Nrm   c                 &    j                  d      S rQ   rR   r&   s    r   r(   z/ReconnectTestBase._testPool_3.<locals>.<lambda>2  rT   r   c                      y r#   rV   rW   s    r   r(   z/ReconnectTestBase._testPool_3.<locals>.<lambda>2  rY   r   rZ   r   r'   ru   r2   s   `   r   rF   zReconnectTestBase._testPool_3/  s8    + 4 4c:	<nMr   c                 n     d} j                   j                  |      } fd}|j                  |       |S )Nrm   c                 N    j                  t        | d   d         dk(  d       y r   r   rp   s    r   rr   z-ReconnectTestBase._testPool_4.<locals>._check9  r   r   rs   rt   s   `    r   rG   zReconnectTestBase._testPool_45  r   r   c                       j                          d}t        j                   j                  j                  |      }|j                   fd fd       |S )NrO   c                 &    j                  d      S rQ   rR   r&   s    r   r(   z/ReconnectTestBase._testPool_5.<locals>.<lambda>D  s    		.1 r   c                 L    j                  | j                  t                    S r#   )assertFalsecheckr   )rX   r   s    r   r(   z/ReconnectTestBase._testPool_5.<locals>.<lambda>E  s    d&&qww~'>? r   )r>   r	   r[   r   r\   r]   r   s   `   r   rH   zReconnectTestBase._testPool_5?  sI     % 4 4c:	1?	
 r   )r   r   r   r   r
   r   r   r   r   r3   rM   r   rE   rF   rG   rH   rV   r   r   r   r      sK     "z!!'408F=:r   r   c                       e Zd ZU dZdZee   ed<   dZdZ	dZ
dZdZdZdZdZdZej$                  Zee   ed<   dZdZdZd Zd	 Zd
 Zd Zd Zd Zy)DBTestConnectora>  
    A class which knows how to test for the presence of
    and establish a connection to a relational database.

    To enable test cases  which use a central, system database,
    you must create a database named DB_NAME with a user DB_USER
    and password DB_PASS with full access rights to database DB_NAME.
    NTEST_PREFIXtwisted_testTr   2   c                     | j                         | _        t        j                  | j                         | j	                         s"t        j                  d| j                  z        | j                         S )Nz%s: Cannot access db)	mktempDB_DIRosmkdircan_connectr   r   r   r   r   s    r   setUpzDBTestConnector.setUpi  sU    kkm
!##$:T=M=M$MNN  r   c                     t               )zXReturn true if this database is present on the system
        and can be used in a test.NotImplementedErrorr   s    r   r   zDBTestConnector.can_connectp  s     "##r   c                      y)z+Take any steps needed to bring database up.NrV   r   s    r   r   zDBTestConnector.startDBu      r   c                      y)zBring database down, if needed.NrV   r   s    r   r,   zDBTestConnector.stopDBy  r   r   c                     | j                         \  }}|j                         }|j                  |       t        |i |S )z;Create a connection pool with additional keyword arguments.)getPoolArgscopyupdater   )r   newkwargskws       r   r   zDBTestConnector.makePool}  s;    ##%bWWY
		%t*r**r   c                     t               )zReturn a tuple (args, kw) of list and keyword arguments
        that need to be passed to ConnectionPool to create a connection
        to this database.r   r   s    r   r   zDBTestConnector.getPoolArgs  s     "##r   )r   r   r   r   r   r   strr   DB_NAMEDB_USERDB_PASSr   nulls_oktrailing_spaces_okr   r@   escape_slashesr   r   r   	can_clearr~   r   r   r   r,   r   r   rV   r   r   r   r   J  s     "&K#%GGGFHLMN,55Hhsm5OI N!$
+$r   r   c                   .    e Zd ZdZdZdZdZd Zd Zd Z	y)	SQLite3ConnectorzB
    Connector that uses the stdlib SQLite3 database support.
    SQLite3Fr{   c                     t        d      yy)Nsqlite3FTr   r   s    r   r   zSQLite3Connector.can_connect  s    #+r   c                    t         j                  j                  | j                  | j                        | _        t         j                  j                  | j
                        r t        j                  | j
                         y y r#   r   pathjoinr   r   databaseexistsunlinkr   s    r   r   zSQLite3Connector.startDB  I    T[[$,,?77>>$--(IIdmm$ )r   c                 .    d}| j                   ddd}||fS )N)r  r{   Fr  r   check_same_threadr  r   r   r   s      r   r   zSQLite3Connector.getPoolArgs  s     --15QRxr   N
r   r   r   r   r   r   r~   r   r   r   rV   r   r   r   r     s'     KNN%
r   r   c                   .    e Zd ZdZdZdZdZd Zd Zd Z	y)	PySQLite2ConnectorzA
    Connector that uses pysqlite's SQLite database support.
    	pysqlite2Fr{   c                     t        d      yy)Npysqlite2.dbapi2FTr   r   s    r   r   zPySQLite2Connector.can_connect  s    +,4r   c                    t         j                  j                  | j                  | j                        | _        t         j                  j                  | j
                        r t        j                  | j
                         y y r#   r  r   s    r   r   zPySQLite2Connector.startDB  r
  r   c                 .    d}| j                   ddd}||fS )N)r  r{   Fr  r  r  s      r   r   zPySQLite2Connector.getPoolArgs  s     $--15QRxr   Nr  rV   r   r   r  r    s'     KNN%
r   r  c                       e Zd ZdZd Zd Zy)PyPgSQLConnectorPyPgSQLc                     	 ddl m} 	 |j                  | j                  | j
                  | j                        }|j                          y# t        $ r Y yw xY w# t        $ r Y yw xY w)Nr   )PgSQLFr  userpasswordT)pyPgSQLr  BaseExceptionr   r   r   r   r%   )r   r  r7   s      r   r   zPyPgSQLConnector.can_connect  sj    	%	==DLL4<< ! D JJL  		  		s#   A AA 	AA	A'&A'c                 X    d}| j                   | j                  | j                  dd}||fS )N)zpyPgSQL.PgSQLr   r  r  r  cp_minr   r   r   r  s      r   r   zPyPgSQLConnector.getPoolArgs  s1    !LL	
 Rxr   Nr   r   r   r   r   r   rV   r   r   r  r        Kr   r  c                       e Zd ZdZd Zd Zy)PsycopgConnectorPsycopgc                     	 dd l }	 |j                  | j                  | j                  | j
                        }|j                          y# t        $ r Y yw xY w# t        $ r Y yw xY w)Nr   Fr  T)psycopgr!  r   r   r   r   r%   )r   r,  r7   s      r   r   zPsycopgConnector.can_connect  sj    		??DLL4<< # D JJL  		  		#   A
 AA 
	AA	A%$A%c                 X    d}| j                   | j                  | j                  dd}||fS )N)r,  r   r#  r%  r  s      r   r   zPsycopgConnector.getPoolArgs  s1    LL	
 Rxr   Nr&  rV   r   r   r)  r)    r'  r   r)  c                   (    e Zd ZdZdZdZdZd Zd Zy)MySQLConnectorMySQLFc                     	 dd l }	 |j                  | j                  | j                  | j
                        }|j                          y# t        $ r Y yw xY w# t        $ r Y yw xY w)Nr   Fdbr  passwdT)MySQLdbr!  r   r   r   r   r%   )r   r6  r7   s      r   r   zMySQLConnector.can_connect  sj    		??<<dll4<< # D JJL  		  		r-  c                 V    d}| j                   | j                  | j                  d}||fS )N)r6  r3  r%  r  s      r   r   zMySQLConnector.getPoolArgs  s(    LL$,,$,,ORxr   N)	r   r   r   r   r   r   r   r   r   rV   r   r   r0  r0    s!    KLOr   r0  c                   <    e Zd ZdZdZdZdZdZdZd Z	d Z
d Zd Zy)	FirebirdConnectorFirebirdFN   c                 |    t        d      y	 | j                          | j                          y# t        $ r Y yw xY w)NkinterbasdbFT)r   r   r,   r!  r   s    r   r   zFirebirdConnector.can_connect  s<    '/	LLNKKM 		s    / 	;;c                    dd l }t        j                  j                  | j                  t
        j                        | _        t        j                  | j                  t        j                  t        j                  z   t        j                  z          d}|| j                  | j                  | j                  fz  }|j                  |      }|j                          y )Nr   z,create database "%s" user "%s" password "%s")r=  r   r  r  r   r   r   chmodstatS_IRWXUS_IRWXGS_IRWXOr   r   create_databaser%   )r   r=  ru   r7   s       r   r   zFirebirdConnector.startDB&  s    ww||DKK1H1HI
dllT\\9DLLHI<dllDLL99**3/

r   c                 X    d}| j                   d| j                  | j                  d}||fS )N)r=  	127.0.0.1r  hostr  r  r%  r  s      r   r   zFirebirdConnector.getPoolArgs0  s1    LL	
 Rxr   c                     dd l }|j                  | j                  d| j                  | j                        }|j                          y )Nr   rF  rG  )r=  r   r   r   r   drop_database)r   r=  r7   s      r   r,   zFirebirdConnector.stopDB:  s@    ""\\\\	 # 
 	r   )r   r   r   r   r@   r   r   r   r~   r   r   r   r,   rV   r   r   r9  r9    s4    KMNHIN	r   r9  c                 l   t         t        t        t        t        t
        g}i }|D ]{  j                  |z   } G fdd| t        j                        }||_	        t        d      r5dj                  j                  j                         dd |gz         |_        |||<   } |j                  |       y)aQ  
    Make a test case for every db connector which can connect.

    @param base: Base class for test case. Additional base classes
                 will be a DBConnector subclass and unittest.TestCase
    @param suffix: A suffix used to create test case names. Prefixes
                   are defined in the DBConnector subclasses.
    c                   (    e Zd ZW  j                  Zy)makeSQLTests.<locals>.testcaseN)r   r   r   )	connclasss   r   testcaserM  [  s    "--Jr   rO  r   .r   N)r  r   r  r)  r0  r9  r   r   TestCaser   hasattrr  r   splitr   )basesuffixglobals
connectorstestsnamerO  rN  s          @r   makeSQLTestsr[  F  s     	J E 	$$v-	.y$(9(9 	. !9n-$'HH&&,,.q4v=%H! d NN5r   ADBAPITestsReconnectTestsc                   *    e Zd ZdZdZdZd Zd Zd Zy)FakePoolz
    A fake L{ConnectionPool} for tests.

    @ivar connectionFactory: factory for making connections returned by the
        C{connect} method.
    @type connectionFactory: any callable
    Tc                     || _         y r#   connectionFactory)r   rb  s     r   __init__zFakePool.__init__}  s
    !2r   c                 "    | j                         S )zB
        Return an instance of C{self.connectionFactory}.
        ra  r   s    r   r   zFakePool.connect  s     %%''r   c                      y)z
        Do nothing.
        NrV   )r   
connections     r   r   zFakePool.disconnect  rY   r   N)	r   r   r   r   	reconnectnoisyrc  r   r   rV   r   r   r_  r_  q  s"     IE3(r   r_  c                       e Zd ZdZd Zy)ConnectionTestsz,
    Tests for the L{Connection} class.
    c                 <    G d d      }t        |      }t        |      }| j                  t        |j                         | j                  t              }| j                  t        |      d       | j                  |d   j                  j                  d   d       y)z|
        If an error happens during rollback, L{ConnectionLost} is raised but
        the original error is logged.
        c                       e Zd Zd Zy)IConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaisec                     t        d      Nproblem!RuntimeErrorr   s    r   rollbackzRConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaise.rollback      ":..r   N)r   r   r   rs  rV   r   r   ConnectionRollbackRaiserm        /r   ru  r{   r   rp  N)r_  r   assertRaisesr   rs  r>   rr  r   r}   valuer   )r   ru  poolrf  errorss        r   test_rollbackErrorLoggedz(ConnectionTests.test_rollbackErrorLogged  s    	/ 	/ /0%
.**=*=>''5Va(--a0*=r   N)r   r   r   r   r{  rV   r   r   rj  rj    s    >r   rj  c                       e Zd ZdZd Zy)TransactionTestsz-
    Tests for the L{Transaction} class.
    c                 (    G d d      }t        d      }t        | |             }|j                          | j                  t              }| j                  t        |      d       | j                  |d   j                  j                  d   d       y)z
        If the cursor creation raises an error in L{Transaction.reopen}, it
        reconnects but log the error occurred.
        c                       e Zd ZdZd Zd Zy)NTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaiser   c                      y r#   rV   r   s    r   rg  zXTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.reconnect      r   c                 b    | j                   dk(  r | xj                   dz  c_         t        d      y )Nr   r{   rp  )countrr  r   s    r   r   zUTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.cursor  s*    ::?JJ!OJ&z22 #r   N)r   r   r   r  rg  r   rV   r   r   ConnectionCursorRaiser    s    E3r   r  Nr{   r   rp  )	r_  r   reopenr>   rr  r   r}   rx  r   )r   r  ry  r   rz  s        r   test_reopenLogErrorIfReconnectz/TransactionTests.test_reopenLogErrorIfReconnect  sz    		3 		3 ~!$(=(?@''5Va(--a0*=r   N)r   r   r   r   r  rV   r   r   r}  r}    s    >r   r}  c                       e Zd Zd Zy)NonThreadPoolc                 b    d}	  ||i |} |||       y # t         $ r d}t               }Y !w xY w)NTF)	Exceptionr   )r   onResultrX   ar   successr   s          r   callInThreadWithCallbackz&NonThreadPool.callInThreadWithCallback  sB    	[R[F 	&!  	GYF	s    ..N)r   r   r   r  rV   r   r   r  r    s    "r   r  c                   $    e Zd ZdZ e       Zd Zy)DummyConnectionPoolz'
    A testable L{ConnectionPool};
    c                     t         | _        y)z*
        Don't forward init call.
        N)r   _reactorr   s    r   rc  zDummyConnectionPool.__init__  s      r   N)r   r   r   r   r  
threadpoolrc  rV   r   r   r  r    s     J r   r  c                   (    e Zd ZdZd Zd Zd Zd Zy)EventReactora  
    Partial L{IReactorCore} implementation with simple event-related
    methods.

    @ivar _running: A C{bool} indicating whether the reactor is pretending
        to have been started already or not.

    @ivar triggers: A C{list} of pending system event triggers.
    c                      || _         g | _        y r#   )_runningtriggers)r   runnings     r   rc  zEventReactor.__init__  s    r   c                 P    | j                   r |        y | j                  dd|      S )Nafterstartup)r  addSystemEventTrigger)r   functions     r   callWhenRunningzEventReactor.callWhenRunning  s#    ==J--gy(KKr   c                 F    |||f}| j                   j                  |       |S r#   )r  r   )r   phaseeventtriggerhandles        r   r  z"EventReactor.addSystemEventTrigger  s%    (V$r   c                 :    | j                   j                  |       y r#   )r  remove)r   r  s     r   removeSystemEventTriggerz%EventReactor.removeSystemEventTrigger  s    V$r   N)r   r   r   r   rc  r  r  r  rV   r   r   r  r    s    L
%r   r  c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)ConnectionPoolTestsz+
    Unit tests for L{ConnectionPool}.
    c                       G d d      }d }t               }||_        |j                  |      } j                  |t              } fd}|j                  |       |S )z
        If rollback fails, L{ConnectionPool.runWithConnection} raises the
        original exception and log the error of the rollback.
        c                       e Zd Zd Zd Zy)]ConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                      y r#   rV   r   ry  s     r   rc  zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__  r  r   c                     t        d      ro  rq  r   s    r   rs  zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollback  rt  r   Nr   r   r   rc  rs  rV   r   r   ru  r        /r   ru  c                     t        d      Nfoo
ValueError)rf  s    r   raisingFunctionzUConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.raisingFunction
      U##r   c                     j                  t              }j                  t        |      d       j                  |d   j                  j
                  d   d       y Nr{   r   rp  r>   rr  r   r}   rx  r   ignoredrz  r   s     r   cbFailedzNConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.cbFailed  J    ++L9FS[!,VAY__11!4jAr   )r  rb  rj   assertFailurer  r0   )r   ru  r  ry  r2   r  s   `     r   (test_runWithConnectionRaiseOriginalErrorz<ConnectionPoolTests.test_runWithConnectionRaiseOriginalError  s_    	/ 	/	$ #$!8""?3q*-	B
 	
hr   c                     G d d      }t               }|j                   |              | j                  t              }| j	                  t        |      d       | j	                  |d   j                  j                  d   d       y)z;
        L{ConnectionPool._close} logs exceptions.
        c                       e Zd Zd Zy)DConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaisec                     t        d      ro  rq  r   s    r   r%   zJConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaise.close   rt  r   N)r   r   r   r%   rV   r   r   ConnectionCloseRaiser    rv  r   r  r{   r   rp  N)r  _closer>   rr  r   r}   rx  r   )r   r  ry  rz  s       r   test_closeLogErrorz&ConnectionPoolTests.test_closeLogError  sm    
	/ 	/ #$(*+''5Va(--a0*=r   c                       G d d      } G d d      }d }t               }||_        ||_        |j                  |      } j	                  |t
              } fd}|j                  |       |S )z
        If rollback fails, L{ConnectionPool.runInteraction} raises the
        original exception and log the error of the rollback.
        c                       e Zd Zd Zd Zy)^ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                      y r#   rV   r  s     r   rc  zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__1  r  r   c                     t        d      ro  rq  r   s    r   rs  zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollback4  rt  r   Nr  rV   r   r   ru  r  0  r  r   ru  c                       e Zd Zd Zy)WConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransactionc                      y r#   rV   )r   ry  rf  s      r   rc  z`ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransaction.__init__8  r  r   N)r   r   r   rc  rV   r   r   DummyTransactionr  7  s    r   r  c                     t        d      r  r  )r   s    r   r  zVConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.raisingFunction;  r  r   c                     j                  t              }j                  t        |      d       j                  |d   j                  j
                  d   d       y r  r  r  s     r   r  zOConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.cbFailedE  r  r   )r  rb  transactionFactoryre   r  r  r0   )r   ru  r  r  ry  r2   r  s   `      r   )test_runWithInteractionRaiseOriginalErrorz=ConnectionPoolTests.test_runWithInteractionRaiseOriginalError*  sq    	/ 	/	 		$ #$!8"20q*-	B
 	
hr   c                     t        d      }t        d|      }| j                  |j                  dd|j                  fg       |j                          | j                  |j                         y)z
        If L{ConnectionPool.close} is called without L{ConnectionPool.start}
        having been called, the pool's startup event is cancelled.
        Ftwisted.test.test_adbapi
cp_reactorr  r  N)r  r   r   r  _startr%   r   r   r   ry  s      r   test_unstartedClosez'ConnectionPoolTests.test_unstartedCloseM  s\    
 u%8WM))Wi,M+NO

))*r   c                     t        d      }t        d|      }| j                  |j                  dd|j                  fg       |j                          | j                  |j                         y)z
        If L{ConnectionPool.close} is called after it has been started, but
        not by its shutdown trigger, the shutdown trigger is cancelled.
        Tr  r  duringshutdownN)r  r   r   r  
finalCloser%   r   r  s      r   test_startedClosez%ConnectionPoolTests.test_startedCloseZ  s\    
 t$8WM))Xz4??,S+TU

))*r   N)	r   r   r   r   r  r  r  r  r  rV   r   r   r  r    s!    :> !F++r   r  ))r   r   r@  typingr   r   twisted.enterprise.adbapir   r   r   r   twisted.internetr	   r
   r   twisted.python.failurer   twisted.python.reflectr   twisted.trialr   r?   r   r   r   r   r  r  r)  r0  r9  r[  rW  r_  rR  rj  r}  r  r  r  r  rV   r   r   <module>r     s0   
  !  8 7 * 0 " Y YxM M`>$ >$B 4 4 8 8_ 61 1hH ^]GI 6  0') < 6>h'' >.>x(( ><" " .  % %<m+(++ m+r   