U
    ÌZjP4  ã                   @   s$  d dl mZ d dlZd dlZd dlZd dlZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ G dd„ deƒZeƒ Ze	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZd&dd„ZG dd„ deƒZd'dd„Zd(dd„Zd)dd„ZG d d!„ d!eƒZG d"d#„ d#eƒZeefd$d%„ZdS )*é    )Úabsolute_importNé   )Úconfig)Ú	decorator)Ú
gc_collecté   )Úevent)Úpool)Ú
await_onlyc                   @   s”   e Z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#S )$ÚConnectionKillerc                 C   s"   t  ¡ | _t t¡| _tƒ | _d S ©N)ÚweakrefÚWeakKeyDictionaryÚ
proxy_refsÚcollectionsÚdefaultdictÚsetÚtesting_enginesÚdbapi_connections©Úself© r   ú]/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/testing/engines.pyÚ__init__   s    
zConnectionKiller.__init__c                 C   sD   t  |d| j¡ t  |d| j¡ t  |d| j¡ t  |d| j¡ d S )NÚcheckoutZcheckinÚcloseZclose_detached)r   ÚlistenÚ	_add_connÚ_remove_conn)r   r	   r   r   r   Úadd_pool   s    zConnectionKiller.add_poolc                 C   s   | j  |¡ d| j|< d S )NT)r   Úaddr   )r   Z	dbapi_conZ
con_recordZ	con_proxyr   r   r   r   %   s    zConnectionKiller._add_connc                 G   s   | j  |¡ d S r   )r   Údiscard)r   Ú
dbapi_connÚargr   r   r   r   )   s    zConnectionKiller._remove_connc                 C   s,   |   |j¡ |dkst‚| j|  |¡ d S )N)ÚclassÚglobalÚfunctionZfixture)r   r	   ÚAssertionErrorr   r    )r   ÚengineÚscoper   r   r   Ú
add_engine,   s    zConnectionKiller.add_enginec              
   C   s@   z
|ƒ  W n0 t k
r: } zt d| ¡ W 5 d }~X Y nX d S )Nz5testing_reaper couldn't rollback/close connection: %s©Ú	ExceptionÚwarningsÚwarn©r   ÚfnÚer   r   r   Ú_safe2   s    
ÿzConnectionKiller._safec                 C   s.   t | jƒD ]}|d k	r
|jr
|  |j¡ q
d S r   )Úlistr   Úis_validr2   Úrollback©r   Úrecr   r   r   Úrollback_all:   s    zConnectionKiller.rollback_allc                 C   s^   t | jƒD ],}|d k	r
|jr
| j |j¡ |  |j¡ q
| jD ]}|  |j¡ q>| j 	¡  d S r   )
r3   r   r4   r   r!   Údbapi_connectionr2   Ú_checkinr5   Úclear)r   r7   Úconr   r   r   Úcheckin_all?   s    
zConnectionKiller.checkin_allc                 C   s   |   ¡  d S r   )r=   r   r   r   r   Ú	close_allN   s    zConnectionKiller.close_allc                 C   s*   t js
d S ddlm} | |jj|¡ d S ©Nr   )Ú	provision)r   Úbootstrapped_as_sqlalchemyÚ r@   Úprepare_for_drop_tablesr(   Úurl)r   Ú
connectionr@   r   r   r   rC   Q   s    z(ConnectionKiller.prepare_for_drop_tablesc                 C   s„   | j | }t|ƒD ]d}t| jƒD ]4}|d k	r |jr |jd k	r |j|jkr |  |j¡ q t|dƒrnt	| 
¡ ƒ q| 
¡  q| ¡  d S )NÚsync_engine)r   r3   r   r4   Ú_poolr	   r2   r:   Úhasattrr
   Údisposer;   )r   r)   Zengr7   Z	proxy_refr   r   r   Ú_drop_testing_enginesZ   s    
ÿ
þ

z&ConnectionKiller._drop_testing_enginesc                 C   s   |   d¡ d S )Nr&   )rJ   r   r   r   r   Ú
after_testj   s    zConnectionKiller.after_testc              	   C   sT   t js
d S |jjrd S |  ¡  ddlm} t j ¡ �}| 	|j
j|¡ W 5 Q R X d S r?   )r   rA   Ú	__class__Z"__leave_connections_for_teardown__r=   rB   r@   ÚdbÚconnectrC   r(   rD   )r   Útestr@   Úconnr   r   r   Úafter_test_outside_fixturesm   s    z,ConnectionKiller.after_test_outside_fixturesc                 C   s    |   ¡  |  d¡ |  d¡ d S )Nr&   r$   )r=   rJ   r   r   r   r   Ústop_test_class_inside_fixtures   s    
z0ConnectionKiller.stop_test_class_inside_fixturesc                 C   sB   t jjr>tƒ  t jjr>tt jjƒ}t jj ¡  ds>td| ƒ‚d S )NFz/%d connection recs not cleared after test suite)r	   ÚbaseZ_strong_ref_connection_recordsr   Úlenr;   r'   )r   Úlnr   r   r   Ú stop_test_class_outside_fixtures„   s    ÿþz1ConnectionKiller.stop_test_class_outside_fixturesc                 C   s"   |   ¡  | jD ]}|  |¡ qd S r   )r=   r   rJ   )r   r)   r   r   r   Úfinal_cleanup‘   s    
zConnectionKiller.final_cleanupc                 C   s   | j D ]}|jrdst‚qd S ©NF)r   r4   r'   r6   r   r   r   Úassert_all_closed–   s    
z"ConnectionKiller.assert_all_closedN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r*   r2   r8   r=   r>   rC   rJ   rK   rQ   rR   rV   rW   rY   r   r   r   r   r      s"   	r   c                 O   s   z| ||Ž W 5 t  ¡  X d S r   )Útesting_reaperrY   ©r0   ÚargsÚkwr   r   r   Úassert_conns_closedŸ   s    ra   c                 O   s   z| ||Ž W 5 t  ¡  X dS )zBDecorator that rolls back all open connections after fn execution.N)r]   r8   r^   r   r   r   Úrollback_open_connections§   s    rb   c                 O   s   t  ¡  | ||Ž dS )z:Decorator that closes all connections before fn execution.N©r]   r=   r^   r   r   r   Úclose_first±   s    rd   c                 O   s   z| ||Ž W 5 t  ¡  X dS )z9Decorator that closes all connections after fn execution.Nrc   r^   r   r   r   Úclose_open_connections¹   s    re   c                 c   sX   dd l m} |jD ]@}| r$|| kr$qt||d ƒ}|sHttd| ƒj|ƒ}| ¡ V  qd S )Nr   zsqlalchemy.dialects.%s)Zsqlalchemy.dialectsZdialectsÚ__all__ÚgetattrÚ
__import__Údialect)ÚexcludeÚdÚnameÚmodr   r   r   Úall_dialectsÂ   s    
 ÿrn   c                   @   s>   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„Zdd„ ZdS )ÚReconnectFixturec                 C   s   || _ g | _d| _d S rX   )ÚdbapiÚconnectionsÚ
is_stopped)r   rp   r   r   r   r   Ò   s    zReconnectFixture.__init__c                 C   s   t | j|ƒS r   )rg   rp   ©r   Úkeyr   r   r   Ú__getattr__×   s    zReconnectFixture.__getattr__c                 O   sT   | j j||Ž}| jr@|  |j¡ | ¡ }| d¡ dsPtdƒ‚n| j 	|¡ |S d S )Nzselect 1Fz%simulated connect failure didn't work)
rp   rN   rr   r2   r   ÚcursorÚexecuter'   rq   Úappend)r   r_   ÚkwargsrP   Zcursr   r   r   rN   Ú   s    
zReconnectFixture.connectc              
   C   s@   z
|ƒ  W n0 t k
r: } zt d| ¡ W 5 d }~X Y nX d S )Nz.ReconnectFixture couldn't close connection: %sr+   r/   r   r   r   r2   è   s    
zReconnectFixture._safeFc                 C   s,   || _ t| jƒD ]}|  |j¡ qg | _d S r   )rr   r3   rq   r2   r   )r   ÚstopÚcr   r   r   Úshutdownî   s    zReconnectFixture.shutdownc                 C   s
   d| _ d S rX   )rr   r   r   r   r   Úrestartù   s    zReconnectFixture.restartN)F)	rZ   r[   r\   r   ru   rN   r2   r|   r}   r   r   r   r   ro   Ñ   s   
ro   c                    sj   | p
t jj} t jjj}|si }t|ƒ|d< t| |ƒ‰ˆj‰ ‡ ‡fdd„}ˆjjjˆ_	ˆjjj
ˆ_|ˆ_ˆS )NÚmodulec                      s    ˆj j ¡  dˆj j_ˆ ƒ  d S rX   )ri   rp   r|   rr   r   ©Z_disposer(   r   r   rI     s    
z$reconnecting_engine.<locals>.dispose)r   rM   rD   ri   rp   ro   Útesting_enginerI   r|   Ztest_shutdownr}   Ztest_restart)rD   Úoptionsrp   rI   r   r   r   Úreconnecting_engineý   s    

r‚   Fc                 C   sä  |r|rt ‚ddlm} n4|s4tjrBtjjrB|dk	rBddlm} nddlm} ddl	m
} |sld}d}	d}
n$| dd¡}| d	d¡}	| d
d¡}
| pštjj} || ƒ} |dkrÔtjdksÆ| jtjjjkrÎtj}ni }n2tjdk	�r| jtjjjk�rtj ¡ }| |¡ || f|Ž}|
�rL|jdk�rLt |d¡dd„ ƒ}t |d¡dd„ ƒ}|�rŽddlm} tjdk	�rŽttjj|ƒ�rŽd}|j tjj¡ |	dk�r®|�r¨d|j_nd|_t|jtjƒ�rÎd|j_d|j_|�ràt ||	¡ |S )zBProduce an engine configured by --options with optional overrides.r   )Úcreate_async_engineF)Úcreate_engine)Úmake_urlTr&   Ú
use_reaperr)   Úsqlite_savepointNZsqliterN   c                 S   s
   d | _ d S r   )Zisolation_level)r9   Zconnection_recordr   r   r   Ú
do_connectA  s    z"testing_engine.<locals>.do_connectÚbeginc                 S   s   |   d¡ d S )NZBEGIN)Zexec_driver_sql©rP   r   r   r   Údo_beginE  s    z testing_engine.<locals>.do_begin)Ú
StaticPoolr%   ) r'   Zsqlalchemy.ext.asynciorƒ   r   rM   Z
_is_futureZsqlalchemy.futurer„   Ú
sqlalchemyZsqlalchemy.engine.urlr…   ÚpoprD   Z
drivernameZdb_optsÚcopyÚupdaterl   r   Zlistens_forZsqlalchemy.poolrŒ   Ú
isinstancer	   Z_transfer_fromrF   Z_has_eventsZ	QueuePoolÚ_timeoutZ_max_overflowr]   r*   )rD   r�   ÚfutureZasyncioZtransfer_staticpoolZ_sqlite_savepointr„   r…   r†   r)   r‡   Zdefault_optr(   rˆ   r‹   rŒ   r   r   r   r€     sf    
ÿÿÿ







ÿr€   c                    sr   ddl m} | stjj} g ‰ ‡ fdd„}‡ fdd„}‡fdd„}|| d	 |ƒ‰tˆd
ƒr\t‚ˆ ˆ_|ˆ_|ˆ_	ˆS )a'  Provides a mocking engine based on the current testing.db.

    This is normally used to test DDL generation flow as emitted
    by an Engine.

    It should not be used in other cases, as assert_compile() and
    assert_sql_execution() are much better choices with fewer
    moving parts.

    r   )Úcreate_mock_enginec                    s   ˆ   | ¡ d S r   )rx   )ÚsqlÚar`   ©Úbufferr   r   Úexecutort  s    zmock_engine.<locals>.executorc                    s"   dd„ ˆ D ƒ}|| kst |ƒ‚d S )Nc                 S   s   g | ]}t  d dt|ƒ¡‘qS )z[\n\t]rB   )ÚreÚsubÚstr©Ú.0Úsr   r   r   Ú
<listcomp>x  s     z3mock_engine.<locals>.assert_sql.<locals>.<listcomp>)r'   )ZstmtsÚrecvr—   r   r   Ú
assert_sqlw  s    zmock_engine.<locals>.assert_sqlc                      s    ˆj ‰ d ‡ fdd„ˆjD ƒ¡S )NÚ
c                 3   s   | ]}t |jˆ d �ƒV  qdS ))ri   N)rœ   Úcompiler�   ©rk   r   r   Ú	<genexpr>}  s     z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)ri   ÚjoinÚmockr   )r(   r¥   r   Ú	print_sql{  s    zmock_engine.<locals>.print_sqlz://r¨   )
r�   r”   r   rM   rl   rH   r'   r¨   r¢   r©   )Zdialect_namer”   r™   r¢   r©   r   )r˜   r(   r   Úmock_enginea  s    rª   c                   @   s:   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚDBAPIProxyCursorzuProxy a DBAPI cursor.

    Tests can provide subclasses of this to intercept
    DBAPI-level cursor operations.

    c                 O   s   || _ || _|j||Ž| _d S r   )r(   rE   rv   )r   r(   rP   r_   ry   r   r   r   r   �  s    zDBAPIProxyCursor.__init__Nc                 K   s*   |r| j j||f|ŽS | j j|f|ŽS d S r   )rv   rw   )r   ÚstmtÚ
parametersr`   r   r   r   rw   ”  s    zDBAPIProxyCursor.executec                 K   s   | j j||f|ŽS r   )rv   Úexecutemany)r   r¬   Úparamsr`   r   r   r   r®   š  s    zDBAPIProxyCursor.executemanyc                 C   s
   t | jƒS r   )Úiterrv   r   r   r   r   Ú__iter__�  s    zDBAPIProxyCursor.__iter__c                 C   s   t | j|ƒS r   )rg   rv   rs   r   r   r   ru      s    zDBAPIProxyCursor.__getattr__)N)	rZ   r[   r\   Ú__doc__r   rw   r®   r±   ru   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 )ÚDBAPIProxyConnectionz}Proxy a DBAPI connection.

    Tests can provide subclasses of this to intercept
    DBAPI-level connection operations.

    c                 C   s   |j  ¡ | _|| _|| _d S r   )r	   Z_creatorrP   r(   Ú
cursor_cls)r   r(   r´   r   r   r   r   ¬  s    zDBAPIProxyConnection.__init__c                 O   s   | j | j| jf|ž|ŽS r   )r´   r(   rP   )r   r_   ry   r   r   r   rv   ±  s    zDBAPIProxyConnection.cursorc                 C   s   | j  ¡  d S r   )rP   r   r   r   r   r   r   ´  s    zDBAPIProxyConnection.closec                 C   s   t | j|ƒS r   )rg   rP   rs   r   r   r   ru   ·  s    z DBAPIProxyConnection.__getattr__N)rZ   r[   r\   r²   r   rv   r   ru   r   r   r   r   r³   ¤  s
   r³   c                    s&   ‡ ‡fdd„}dd„ }t ||dœd�S )zIProduce an engine that provides proxy hooks for
    common methods.

    c                      s   ˆ t jˆƒS r   )r   rM   r   ©Úconn_clsr´   r   r   Ú	mock_connÃ  s    z"proxying_engine.<locals>.mock_connc                    s   ‡ fdd„}|S )Nc                    s
   ˆ | j ƒS r   rŠ   )r"   ©Údo_on_connectr   r   ÚgoÇ  s    z8proxying_engine.<locals>._wrap_do_on_connect.<locals>.gor   )r¹   rº   r   r¸   r   Ú_wrap_do_on_connectÆ  s    z,proxying_engine.<locals>._wrap_do_on_connect)Zcreatorr»   )r�   )r€   )r¶   r´   r·   r»   r   rµ   r   Úproxying_engine»  s    þÿr¼   )N)NN)NNNFFF)N)Ú
__future__r   r   rš   r-   r   rB   r   Úutilr   r   r   r	   r
   Úobjectr   r]   ra   rb   rd   re   rn   ro   r‚   r€   rª   r«   r³   r¼   r   r   r   r   Ú<module>   sJ    

	


,
      ú
P
& ÿ