U
    W[$f                     @   s  d Z ddlm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mZmZ ddlmZ ddlmZ d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zeede  eede  G d d! d!eZG d"d# d#ej Z!G d$d% d%ej Z"G d&d' d'eZ#G d(d) d)eZ$G d*d+ d+eZ%G d,d- d-ej Z&dS ).z&
Tests for twisted.enterprise.adbapi.
    )unittestN)ConnectionPoolConnectionLost)
ConnectionTransaction)reactordefer
interfaces)FailurerequireModulez%
CREATE TABLE simple (
  x integer
)
c                   @   s   e Zd ZdZi Zeeddkr$dZdd Z	dd Z
dd	 Zd2d
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d0d1 ZdS )3ADBAPITestBasez,
    Test the asynchronous DB-API code.
    N5ADB-API requires threads, no way to test without themc                 C   s&   |    | j| jd| _| j  dS )zR
        Set up the database and create a connection pool pointing at it.
        )Z
cp_openfunN)startDBmakePoolopenfundbpoolstartself r   :/usr/lib/python3/dist-packages/twisted/test/test_adbapi.py
extraSetUp%   s    zADBAPITestBase.extraSetUpc                    s4    j d}| fdd | fdd |S )NDROP TABLE simplec                    s
    j  S Nr   closeresr   r   r   <lambda>0       z)ADBAPITestBase.tearDown.<locals>.<lambda>c                    s      S r   stopDBr   r   r   r   r   1   r    r   runOperationaddCallbackr   dr   r   r   tearDown.   s    zADBAPITestBase.tearDownc                 C   s   d| j |< d S )NT)openfun_calledr   connr   r   r   r   5   s    zADBAPITestBase.openfunc                 C   s$   |s|  | j n| || j d S r   )
assertTruer)   ZassertInr*   r   r   r   checkOpenfunCalled9   s    z!ADBAPITestBase.checkOpenfunCalledc                    s    j t} jrT| j | j | j | j | fdd | j	 | j
 | j | j | j | j | j | j |S )Nc                    s      S r   )flushLoggedErrorsr   r   r   r   r   G   r    z*ADBAPITestBase.test_pool.<locals>.<lambda>)r   r$   simple_table_schematest_failuresr%   _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_9r&   r   r   r   	test_pool@   s     zADBAPITestBase.test_poolc                    s,   t  jjd}| fdddd  |S )Nselect * from NOTABLEc                    s
     dS Nzno exceptionZfailr   r   r   r   r   U   r    z.ADBAPITestBase._testPool_1_1.<locals>.<lambda>c                 S   s   d S r   r   fr   r   r   r   V   r    r   maybeDeferredr   runQueryaddCallbacksr   r   r'   r   r   r   r1   S   s
    zADBAPITestBase._testPool_1_1c                    s,   t  jjd}| fdddd  |S )Nzdeletexxx from NOTABLEc                    s
     dS r?   r@   r   r   r   r   r   ]   r    z.ADBAPITestBase._testPool_1_2.<locals>.<lambda>c                 S   s   d S r   r   rA   r   r   r   r   ^   r    )r   rD   r   r$   rF   rG   r   r   r   r2   Z   s    
zADBAPITestBase._testPool_1_2c                    s.   t  jj j}| fdddd  |S )Nc                    s
     dS r?   r@   r   r   r   r   r   e   r    z.ADBAPITestBase._testPool_1_3.<locals>.<lambda>c                 S   s   d S r   r   rA   r   r   r   r   f   r    )r   rD   r   runInteractionbad_interactionrF   rG   r   r   r   r3   b   s    
zADBAPITestBase._testPool_1_3c                    s.   t  jj j}| fdddd  |S )Nc                    s
     dS r?   r@   r   r   r   r   r   m   r    z.ADBAPITestBase._testPool_1_4.<locals>.<lambda>c                 S   s   d S r   r   rA   r   r   r   r   n   r    )r   rD   r   runWithConnectionbad_withConnectionrF   rG   r   r   r   r4   j   s    
zADBAPITestBase._testPool_1_4c                    s*   d} j |} fdd}|| |S )Nselect count(1) from simplec                    s(     t| d d dkd    d S )Nr   zInteraction not rolled back)r,   intr-   rowr   r   r   _checkv   s    z*ADBAPITestBase._testPool_2.<locals>._checkr   rE   r%   r   r   sqlr'   rP   r   r   r   r5   r   s
    
zADBAPITestBase._testPool_2c                    sl   d}g }t  jD ]}d| }| j| qt|} fdd}||  fdd}|| |S )NrL   z insert into simple(x) values(%d)c                    s   d} j |}|S )Nselect x from simple order by x)r   rE   )r   rS   r'   r   r   r   _select   s    z+ADBAPITestBase._testPool_3.<locals>._selectc                    sZ     t|  jkd t jD ]4}  t| | dkd   | | d |kd q d S )NzWrong number of rows   Wrong size rowr   zValues not returned.)r,   lennum_iterationsrange)Zrowsir   r   r   rP      s    z*ADBAPITestBase._testPool_3.<locals>._check)rZ   rY   appendr   r$   r   ZgatherResultsr%   )r   r   rS   Zinsertsr[   r'   rU   rP   r   r   r   r6   }   s    


zADBAPITestBase._testPool_3c                    s$    j  j}| fdd |S )Nc                    s     | dS NdoneassertEqualr   r   r   r   r      r    z,ADBAPITestBase._testPool_4.<locals>.<lambda>)r   rH   interactionr%   rG   r   r   r   r7      s    zADBAPITestBase._testPool_4c                    s$    j  j}| fdd |S )Nc                    s     | dS r]   r_   r   r   r   r   r      r    z,ADBAPITestBase._testPool_5.<locals>.<lambda>)r   rJ   withConnectionr%   rG   r   r   r   r8      s    zADBAPITestBase._testPool_5c                 C   s   | j | j}|S r   )r   rJ   close_withConnectionrG   r   r   r   r9      s    zADBAPITestBase._testPool_6c                    sV   g }t  jD ]}d| }| j| qtj|dd} fdd}|| |S )Nz!select x from simple where x = %dTZfireOnOneErrbackc                    s4   t  jD ]$} | | d d d |kd q
d S )NrV   r   zValue not returned)rZ   rY   r,   )resultr[   r   r   r   rP      s    z*ADBAPITestBase._testPool_7.<locals>._check)rZ   rY   r\   r   rE   r   DeferredListr%   )r   r   dsr[   rS   dlistrP   r   r   r   r:      s    
zADBAPITestBase._testPool_7c                 C   s@   g }t | jD ]}d| }|| j| qtj|dd}|S )Nzdelete from simple where x = %dTrd   )rZ   rY   r\   r   r$   r   rf   )r   r   rg   r[   rS   rh   r   r   r   r;      s    zADBAPITestBase._testPool_8c                    s*   d} j |} fdd}|| |S )NrL   c                    s(     t| d d dkd    d S )Nr   z)Didn't successfully delete table contents)r,   rM   checkConnectrN   r   r   r   rP      s    z*ADBAPITestBase._testPool_9.<locals>._checkrQ   rR   r   r   r   r<      s
    
zADBAPITestBase._testPool_9c                 C   s   | j  }| | | }|d |d | }| t|d | t|d d | |d d d |d |d | t| d |  | j 	| dS )z/Check the connect/disconnect synchronous calls.zinsert into simple(x) values(1)zselect x from simplerV   r   zdelete from simpleN)
r   connectr-   cursorexecuteZfetchallr`   rX   r   
disconnect)r   r+   cursr   r   r   r   ri      s    





zADBAPITestBase.checkConnectc                 C   s^   | d t| jD ]4}| }| t|dkd | |d |kd q| | d dS )NrT   rV   rW   r   Value not returned.zToo many rowsr^   )rl   rZ   rY   fetchoner,   rX   ZassertIsNone)r   transactionr[   rO   r   r   r   ra      s    
zADBAPITestBase.interactionc                 C   s   | j r|d |d d S )Nzinsert into simple(x) values(0)r>   )can_rollbackrl   )r   rq   r   r   r   rI      s    
zADBAPITestBase.bad_interactionc                 C   sf   |  }zN|d t| jD ]4}| }| t|dkd | |d |kd qW 5 |  X dS )NrT   rV   rW   r   ro   r^   )rk   r   rl   rZ   rY   rp   r,   rX   )r   r+   rn   r[   rO   r   r   r   rb      s    

zADBAPITestBase.withConnectionc                 C   s   |   d S r   )r   r*   r   r   r   rc      s    z#ADBAPITestBase.close_withConnectionc                 C   s&   |  }z|d W 5 |  X d S )Nr>   )rk   r   rl   )r   r+   rn   r   r   r   rK     s    z!ADBAPITestBase.bad_withConnection)N) __name__
__module____qualname____doc__r)   r	   IReactorThreadsr   skipr   r(   r   r-   r=   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   ri   ra   rI   rb   rc   rK   r   r   r   r   r      s6   	


r   c                   @   sd   e Zd ZdZeeddkr 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S )ReconnectTestBasez;
    Test the asynchronous DB-API code with reconnect.
    Nr   c                 C   sF   | j dkrtd|   | jdd| j d| _| 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.
        Nzno good sql for reconnect testrV   T)cp_maxZcp_reconnectZcp_good_sql)	good_sqlr   SkipTestr   r   r   r   r$   r/   r   r   r   r   r     s    


zReconnectTestBase.extraSetUpc                    s4    j d}| fdd | fdd |S )Nr   c                    s
    j  S r   r   r   r   r   r   r   '  r    z,ReconnectTestBase.tearDown.<locals>.<lambda>c                    s      S r   r!   r   r   r   r   r   (  r    r#   r&   r   r   r   r(   %  s    zReconnectTestBase.tearDownc                 C   sP   t d }|| j || j | js4|| j || j || j |S r   )	r   Zsucceedr%   _testPool_1r5   early_reconnectr6   r7   r8   r&   r   r   r   r=   ,  s    
zReconnectTestBase.test_poolc                    s*   d} j |} fdd}|| |S )NrL   c                    s      t| d d dkd d S Nr   zTable not emptyr,   rM   rN   r   r   r   rP   :  s    z-ReconnectTestBase._testPool_1.<locals>._checkrQ   rR   r   r   r   r}   7  s
    
zReconnectTestBase._testPool_1c                 C   s   t | jj d   d S )Nr   )listr   Zconnectionsvaluesr   )r   r   r   r   r   r5   @  s    zReconnectTestBase._testPool_2c                    s0   d}t  jj|}| fdddd  |S )NrL   c                    s
     dS r?   r@   r   r   r   r   r   H  r    z/ReconnectTestBase._testPool_3.<locals>.<lambda>c                 S   s   d S r   r   rA   r   r   r   r   I  r    rC   r   r   rS   r'   r   r   r   r6   E  s    zReconnectTestBase._testPool_3c                    s*   d} j |} fdd}|| |S )NrL   c                    s      t| d d dkd d S r   r   rN   r   r   r   rP   P  s    z-ReconnectTestBase._testPool_4.<locals>._checkrQ   rR   r   r   r   r7   M  s
    
zReconnectTestBase._testPool_4c                    s<       d}t jj|}| fdd fdd |S )Nr>   c                    s
     dS r?   r@   r   r   r   r   r   Z  r    z/ReconnectTestBase._testPool_5.<locals>.<lambda>c                    s     | tS r   )assertFalseZcheckr   rA   r   r   r   r   [  r    )r.   r   rD   r   rE   rF   r   r   r   r   r8   V  s    
zReconnectTestBase._testPool_5)rs   rt   ru   rv   r	   rw   r   rx   r   r(   r=   r}   r5   r6   r7   r8   r   r   r   r   ry     s   		ry   c                   @   sz   e 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ejZdZdZdZdd Zdd	 Zd
d Zdd Zdd Zdd ZdS )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.
    NZtwisted_testT2   c                 C   s6   |   | _t| j |  s.td| j |  S )Nz%s: Cannot access db)	ZmktempDB_DIRosmkdircan_connectr   r|   TEST_PREFIXr   r   r   r   r   setUp}  s
    
zDBTestConnector.setUpc                 C   s
   t  dS )zXReturn true if this database is present on the system
        and can be used in a test.NNotImplementedErrorr   r   r   r   r     s    zDBTestConnector.can_connectc                 C   s   dS )z+Take any steps needed to bring database up.Nr   r   r   r   r   r     s    zDBTestConnector.startDBc                 C   s   dS )zBring database down, if needed.Nr   r   r   r   r   r"     s    zDBTestConnector.stopDBc                 K   s(   |   \}}| }|| t||S )z;Create a connection pool with additional keyword arguments.)getPoolArgscopyupdater   )r   Znewkwargskwr   r   r   r     s    
zDBTestConnector.makePoolc                 C   s
   t  dS )zReturn a tuple (args, kw) of list and keyword arguments
        that need to be passed to ConnectionPool to create a connection
        to this database.Nr   r   r   r   r   r     s    zDBTestConnector.getPoolArgs)rs   rt   ru   rv   r   DB_NAMEDB_USERDB_PASSr   Znulls_oktrailing_spaces_okrr   r0   escape_slashesr   r{   r~   	can_clearrY   r   r   r   r"   r   r   r   r   r   r   r   `  s*   r   c                   @   s4   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dS )SQLite3ConnectorzB
    Connector that uses the stdlib SQLite3 database support.
    ZSQLite3FrV   c                 C   s   t dd krdS dS d S )Nsqlite3FTr   r   r   r   r   r     s    zSQLite3Connector.can_connectc                 C   s2   t j| j| j| _t j| jr.t | j d S r   r   pathjoinr   r   databaseexistsunlinkr   r   r   r   r     s    zSQLite3Connector.startDBc                 C   s   d}| j ddd}||fS )N)r   rV   Fr   rz   Zcheck_same_threadr   r   r   r   r   r   r   r     s    zSQLite3Connector.getPoolArgsN
rs   rt   ru   rv   r   r   rY   r   r   r   r   r   r   r   r     s   r   c                   @   s4   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dS )PySQLite2ConnectorzA
    Connector that uses pysqlite's SQLite database support.
    Z	pysqlite2FrV   c                 C   s   t dd krdS dS d S )Npysqlite2.dbapi2FTr   r   r   r   r   r     s    zPySQLite2Connector.can_connectc                 C   s2   t j| j| j| _t j| jr.t | j d S r   r   r   r   r   r   r     s    zPySQLite2Connector.startDBc                 C   s   d}| j ddd}||fS )N)r   rV   Fr   r   r   r   r   r   r     s    zPySQLite2Connector.getPoolArgsNr   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )PyPgSQLConnectorZPyPgSQLc                 C   sX   zddl m} W n   Y dS X z$|j| j| j| jd}|  W dS    Y dS X d S )Nr   )PgSQLFr   userpasswordT)ZpyPgSQLr   rj   r   r   r   r   )r   r   r+   r   r   r   r     s      zPyPgSQLConnector.can_connectc                 C   s    d}| j | j| jdd}||fS )N)zpyPgSQL.PgSQLr   r   r   r   Zcp_minr   r   r   r   r   r   r   r     s     zPyPgSQLConnector.getPoolArgsNrs   rt   ru   r   r   r   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )PsycopgConnectorZPsycopgc                 C   sT   zdd l }W n   Y dS X z$|j| j| j| jd}|  W dS    Y dS X d S )Nr   Fr   T)psycopgrj   r   r   r   r   )r   r   r+   r   r   r   r     s      zPsycopgConnector.can_connectc                 C   s    d}| j | j| jdd}||fS )N)r   r   r   r   r   r   r   r   r     s     zPsycopgConnector.getPoolArgsNr   r   r   r   r   r     s   r   c                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )MySQLConnectorZMySQLFc                 C   sT   zdd l }W n   Y dS X z$|j| j| j| jd}|  W dS    Y dS X d S )Nr   FZdbr   ZpasswdT)MySQLdbrj   r   r   r   r   )r   r   r+   r   r   r   r     s      zMySQLConnector.can_connectc                 C   s   d}| j | j| jd}||fS )N)r   r   r   r   r   r   r   r   "  s    zMySQLConnector.getPoolArgsN)	rs   rt   ru   r   r   rr   r~   r   r   r   r   r   r   r     s   r   c                   @   sD   e Zd ZdZdZdZdZdZdZdd Z	dd Z
d	d
 Zdd ZdS )FirebirdConnectorZFirebirdFN   c                 C   s:   t dd krdS z|   |   W dS    Y dS X d S )NkinterbasdbFT)r   r   r"   r   r   r   r   r   4  s    zFirebirdConnector.can_connectc                 C   sf   dd l }tj| jtj| _t| jtj	tj
 tj  d}|| j| j| jf; }||}|  d S )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   Zcreate_databaser   )r   r   rS   r+   r   r   r   r   ?  s    
zFirebirdConnector.startDBc                 C   s    d}| j d| j| jd}||fS )N)r   	127.0.0.1r   Zhostr   r   r   r   r   r   r   r   I  s     zFirebirdConnector.getPoolArgsc                 C   s,   dd l }|j| jd| j| jd}|  d S )Nr   r   r   )r   rj   r   r   r   Zdrop_database)r   r   r+   r   r   r   r"   P  s     zFirebirdConnector.stopDB)rs   rt   ru   r   r0   r   r{   r   rY   r   r   r   r"   r   r   r   r   r   )  s   
r   c                    s   t tttttg}i }|D ]`  j| }G  fddd | tj}||_	t
 drpd j dd |g |_|||< q|| dS )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                       s   e Zd Z jZdS )zmakeSQLTests.<locals>.testcaseN)rs   rt   ru   r   Z	connclassr   r   testcaseh  s   r   ru   .r   N)r   r   r   r   r   r   r   r   TestCasers   hasattrr   ru   splitr   )basesuffixglobalsZ
connectorsZtestsnamer   r   r   r   makeSQLTestsY  s     	  


r   ZADBAPITestsZReconnectTestsc                   @   s0   e Zd ZdZdZdZdd Zdd Zdd Zd	S )
FakePoolz
    A fake L{ConnectionPool} for tests.

    @ivar connectionFactory: factory for making connections returned by the
        C{connect} method.
    @type connectionFactory: any callable
    Tc                 C   s
   || _ d S r   connectionFactory)r   r   r   r   r   __init__  s    zFakePool.__init__c                 C   s   |   S )zB
        Return an instance of C{self.connectionFactory}.
        r   r   r   r   r   rj     s    zFakePool.connectc                 C   s   dS )z
        Do nothing.
        Nr   )r   
connectionr   r   r   rm     s    zFakePool.disconnectN)	rs   rt   ru   rv   	reconnectZnoisyr   rj   rm   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )ConnectionTestsz,
    Tests for the L{Connection} class.
    c                 C   sd   G dd dt }t|}t|}| t|j | t}| t	|d | |d j
jd d dS )z|
        If an error happens during rollback, L{ConnectionLost} is raised but
        the original error is logged.
        c                   @   s   e Zd Zdd ZdS )zIConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaisec                 S   s   t dd S Nproblem!RuntimeErrorr   r   r   r   rollback  s    zRConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaise.rollbackN)rs   rt   ru   r   r   r   r   r   ConnectionRollbackRaise  s   r   rV   r   r   N)objectr   r   ZassertRaisesr   r   r.   r   r`   rX   valuer   )r   r   poolr   errorsr   r   r   test_rollbackErrorLogged  s    
z(ConnectionTests.test_rollbackErrorLoggedN)rs   rt   ru   rv   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )TransactionTestsz-
    Tests for the L{Transaction} class.
    c                 C   sb   G dd dt }td}t|| }|  | t}| t|d | |d jj	d d dS )z
        If the cursor creation raises an error in L{Transaction.reopen}, it
        reconnects but log the error occurred.
        c                   @   s    e Zd ZdZdd Zdd ZdS )zNTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaiser   c                 S   s   d S r   r   r   r   r   r   r     s    zXTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.reconnectc                 S   s$   | j dkr |  j d7  _ tdd S )Nr   rV   r   )countr   r   r   r   r   rk     s    
zUTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.cursorN)rs   rt   ru   r   r   rk   r   r   r   r   ConnectionCursorRaise  s   r   NrV   r   r   )
r   r   r   Zreopenr.   r   r`   rX   r   r   )r   r   r   rq   r   r   r   r   test_reopenLogErrorIfReconnect  s    
z/TransactionTests.test_reopenLogErrorIfReconnectN)rs   rt   ru   rv   r   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd ZdS )NonThreadPoolc                 O   s@   d}z|||}W n t k
r0   d}t }Y nX ||| d S )NTF)	Exceptionr
   )r   ZonResultrB   ar   Zsuccessre   r   r   r   callInThreadWithCallback  s    z&NonThreadPool.callInThreadWithCallbackN)rs   rt   ru   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZe Zdd ZdS )DummyConnectionPoolz'
    A testable L{ConnectionPool};
    c                 C   s
   t | _dS )z*
        Don't forward init call.
        N)r   Z_reactorr   r   r   r   r     s    zDummyConnectionPool.__init__N)rs   rt   ru   rv   r   Z
threadpoolr   r   r   r   r   r     s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )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                 C   s   || _ g | _d S r   )_runningtriggers)r   runningr   r   r   r     s    zEventReactor.__init__c                 C   s    | j r|  n| dd|S d S )Nafterstartup)r   addSystemEventTrigger)r   Zfunctionr   r   r   callWhenRunning  s    zEventReactor.callWhenRunningc                 C   s   |||f}| j | |S r   )r   r\   )r   ZphaseZeventZtriggerhandler   r   r   r     s    
z"EventReactor.addSystemEventTriggerc                 C   s   | j | d S r   )r   remove)r   r   r   r   r   removeSystemEventTrigger  s    z%EventReactor.removeSystemEventTriggerN)rs   rt   ru   rv   r   r   r   r   r   r   r   r   r     s
   	r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )ConnectionPoolTestsz+
    Unit tests for L{ConnectionPool}.
    c                    sT   G dd dt }dd }t }||_||} |t} fdd}|| |S )z
        If rollback fails, L{ConnectionPool.runWithConnection} raises the
        original exception and log the error of the rollback.
        c                   @   s   e Zd Zdd Zdd ZdS )z]ConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                 S   s   d S r   r   r   r   r   r   r   r     s    zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__c                 S   s   t dd S r   r   r   r   r   r   r     s    zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollbackNrs   rt   ru   r   r   r   r   r   r   r     s   r   c                 S   s   t dd S NZfoo
ValueError)r   r   r   r   raisingFunction  s    zUConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.raisingFunctionc                    s6     t} t|d  |d jjd d d S NrV   r   r   r.   r   r`   rX   r   r   Zignoredr   r   r   r   cbFailed$  s    
zNConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.cbFailed)r   r   r   rJ   assertFailurer   r%   )r   r   r   r   r'   r   r   r   r   (test_runWithConnectionRaiseOriginalError  s    

z<ConnectionPoolTests.test_runWithConnectionRaiseOriginalErrorc                 C   sX   G dd dt }t }||  | t}| t|d | |d jjd d dS )z;
        L{ConnectionPool._close} logs exceptions.
        c                   @   s   e Zd Zdd ZdS )zDConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaisec                 S   s   t dd S r   r   r   r   r   r   r   1  s    zJConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaise.closeN)rs   rt   ru   r   r   r   r   r   ConnectionCloseRaise0  s   r   rV   r   r   N)	r   r   Z_closer.   r   r`   rX   r   r   )r   r   r   r   r   r   r   test_closeLogError,  s    
z&ConnectionPoolTests.test_closeLogErrorc                    sj   G dd dt }G dd dt }dd }t }||_||_||} |t} fdd}|| |S )	z
        If rollback fails, L{ConnectionPool.runInteraction} raises the
        original exception and log the error of the rollback.
        c                   @   s   e Zd Zdd Zdd ZdS )z^ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                 S   s   d S r   r   r   r   r   r   r   B  s    zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__c                 S   s   t dd S r   r   r   r   r   r   r   E  s    zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollbackNr   r   r   r   r   r   A  s   r   c                   @   s   e Zd Zdd ZdS )zWConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransactionc                 S   s   d S r   r   )r   r   r   r   r   r   r   I  s    z`ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransaction.__init__N)rs   rt   ru   r   r   r   r   r   DummyTransactionH  s   r   c                 S   s   t dd S r   r   )rq   r   r   r   r   L  s    zVConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.raisingFunctionc                    s6     t} t|d  |d jjd d d S r   r   r   r   r   r   r   U  s    
zOConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.cbFailed)r   r   r   ZtransactionFactoryrH   r   r   r%   )r   r   r   r   r   r'   r   r   r   r   )test_runWithInteractionRaiseOriginalError<  s    

z=ConnectionPoolTests.test_runWithInteractionRaiseOriginalErrorc                 C   sD   t d}td|d}| |jdd|jfg |  | |j dS )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Z
cp_reactorr   r   N)r   r   r`   r   Z_startr   r   r   r   r   r   r   r   test_unstartedClose]  s
    z'ConnectionPoolTests.test_unstartedClosec                 C   sD   t d}td|d}| |jdd|jfg |  | |j dS )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  ZduringZshutdownN)r   r   r`   r   Z
finalCloser   r   r  r   r   r   test_startedClosek  s    z%ConnectionPoolTests.test_startedCloseN)	rs   rt   ru   rv   r   r   r  r  r  r   r   r   r   r     s   !r   )'rv   Ztwisted.trialr   r   r   Ztwisted.enterprise.adbapir   r   r   r   Ztwisted.internetr   r   r	   Ztwisted.python.failurer
   Ztwisted.python.reflectr   r/   r   r   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:    sSE0!