a
    xˆcP4  ã                   @   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   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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v sJ ‚| j|  |¡ d S )N)ÚclassÚglobalÚfunctionZfixture)r   r	   r   r    )r   ÚengineÚscoper   r   r   Ú
add_engine,   s    zConnectionKiller.add_enginec              
   C   sB   z
|ƒ  W n2 t y< } zt d| ¡ W Y d }~n
d }~0 0 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 ur
|jr
|  |j¡ q
d S r   )Úlistr   Úis_validr1   Úrollback©r   Úrecr   r   r   Úrollback_all:   s    zConnectionKiller.rollback_allc                 C   s^   t | jƒD ],}|d ur
|jr
| j |j¡ |  |j¡ q
| jD ]}|  |j¡ q>| j 	¡  d S r   )
r2   r   r3   r   r!   Údbapi_connectionr1   Ú_checkinr4   Úclear)r   r6   Ú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   rB   Q   s    z(ConnectionKiller.prepare_for_drop_tablesc                 C   s„   | j | }t|ƒD ]d}t| jƒD ]4}|d ur |jr |jd ur |j|ju r |  |j¡ q t|dƒrnt	| 
¡ ƒ q| 
¡  q| ¡  d S )NÚsync_engine)r   r2   r   r3   Ú_poolr	   r1   r9   Úhasattrr
   Údisposer:   )r   r(   Zengr6   Z	proxy_refr   r   r   Ú_drop_testing_enginesZ   s    
ÿ
þ

z&ConnectionKiller._drop_testing_enginesc                 C   s   |   d¡ d S )Nr&   )rI   r   r   r   r   Ú
after_testj   s    zConnectionKiller.after_testc                 C   sh   t js
d S |jjrd S |  ¡  ddlm} t j ¡ � }| 	|j
j|¡ W d   ƒ n1 sZ0    Y  d S r>   )r   r@   Ú	__class__Z"__leave_connections_for_teardown__r<   rA   r?   ÚdbÚconnectrB   r'   rC   )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<   rI   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>J d| ƒ‚d S )NFz/%d connection recs not cleared after test suite)r	   ÚbaseZ_strong_ref_connection_recordsr   Úlenr:   )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   rI   )r   r(   r   r   r   Úfinal_cleanup‘   s    
zConnectionKiller.final_cleanupc                 C   s   | j D ]}|jrdsJ ‚qd S ©NF)r   r3   r5   r   r   r   Úassert_all_closed–   s    
z"ConnectionKiller.assert_all_closedN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r)   r1   r7   r<   r=   rB   rI   rJ   rP   rQ   rU   rV   rX   r   r   r   r   r      s"   	r   c                 O   s*   z| |i |¤Ž W t  ¡  n
t  ¡  0 d S r   )Útesting_reaperrX   ©r/   ÚargsÚkwr   r   r   Úassert_conns_closedŸ   s    r`   c                 O   s*   z| |i |¤Ž W t  ¡  n
t  ¡  0 dS )zBDecorator that rolls back all open connections after fn execution.N)r\   r7   r]   r   r   r   Úrollback_open_connections§   s    ra   c                 O   s   t  ¡  | |i |¤Ž dS )z:Decorator that closes all connections before fn execution.N©r\   r<   r]   r   r   r   Úclose_first±   s    rc   c                 O   s*   z| |i |¤Ž W t  ¡  n
t  ¡  0 dS )z9Decorator that closes all connections after fn execution.Nrb   r]   r   r   r   Úclose_open_connections¹   s    rd   c                 c   sX   dd l m} |jD ]@}| r$|| v 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    
ÿrm   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 rW   )ÚdbapiÚconnectionsÚ
is_stopped)r   ro   r   r   r   r   Ò   s    zReconnectFixture.__init__c                 C   s   t | j|ƒS r   )rf   ro   ©r   Úkeyr   r   r   Ú__getattr__×   s    zReconnectFixture.__getattr__c                 O   sX   | j j|i |¤Ž}| jrD|  |j¡ | ¡ }| d¡ dsTJ dƒ‚n| j |¡ |S d S )Nzselect 1Fz%simulated connect failure didn't work)	ro   rM   rq   r1   r   ÚcursorÚexecuterp   Úappend)r   r^   ÚkwargsrO   Zcursr   r   r   rM   Ú   s    
zReconnectFixture.connectc              
   C   sB   z
|ƒ  W n2 t y< } zt d| ¡ W Y d }~n
d }~0 0 d S )Nz.ReconnectFixture couldn't close connection: %sr*   r.   r   r   r   r1   è   s    
zReconnectFixture._safeFc                 C   s,   || _ t| jƒD ]}|  |j¡ qg | _d S r   )rq   r2   rp   r1   r   )r   ÚstopÚcr   r   r   Úshutdownî   s    zReconnectFixture.shutdownc                 C   s
   d| _ d S rW   )rq   r   r   r   r   Úrestartù   s    zReconnectFixture.restartN)F)	rY   rZ   r[   r   rt   rM   r1   r{   r|   r   r   r   r   rn   Ñ   s   
rn   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 rW   )rh   ro   r{   rq   r   ©Z_disposer'   r   r   rH     s    
z$reconnecting_engine.<locals>.dispose)r   rL   rC   rh   ro   rn   Útesting_enginerH   r{   Ztest_shutdownr|   Ztest_restart)rC   Úoptionsro   rH   r   r~   r   Úreconnecting_engineý   s    

r�   Fc                 C   sè  |r|rJ ‚ddl m} n4|s4tjrBtjjrB|du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u rÔtjdu sÆ| jtjjjkrÎtj}ni }n2tjdu�r| jtjjjk�rtj ¡ }| |¡ || fi |¤Ž}|
�rP|jdk�rPt |d¡dd„ ƒ}t |d¡dd„ ƒ}|�r’ddlm} tjdu�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sqliterM   c                 S   s
   d | _ d S r   )Zisolation_level)r8   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©rO   r   r   r   Údo_beginE  s    z testing_engine.<locals>.do_begin)Ú
StaticPoolr%   )Zsqlalchemy.ext.asyncior‚   r   rL   Z
_is_futureZsqlalchemy.futurerƒ   Ú
sqlalchemyZsqlalchemy.engine.urlr„   ÚpoprC   Z
drivernameZdb_optsÚcopyÚupdaterk   r   Zlistens_forZsqlalchemy.poolr‹   Ú
isinstancer	   Z_transfer_fromrE   Z_has_eventsZ	QueuePoolÚ_timeoutZ_max_overflowr\   r)   )rC   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\J ‚ˆ ˆ_|ˆ_|ˆ_ˆ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   )rw   )ZsqlÚar_   ©Úbufferr   r   Úexecutort  s    zmock_engine.<locals>.executorc                    s"   dd„ ˆ D ƒ}|| ksJ |ƒ‚d S )Nc                 S   s   g | ]}t  d dt|ƒ¡‘qS )z[\n\t]rA   )ÚreÚsubÚstr©Ú.0Úsr   r   r   Ú
<listcomp>x  ó    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 ))rh   N)rš   Úcompiler›   ©rj   r   r   Ú	<genexpr>}  rŸ   z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)rh   ÚjoinÚmockr   )r'   r¤   r   Ú	print_sql{  s    zmock_engine.<locals>.print_sqlz://r§   )	rŒ   r“   r   rL   rk   rG   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|i |¤Ž| _d S r   )r'   rD   ru   )r   r'   rO   r^   rx   r   r   r   r   �  s    zDBAPIProxyCursor.__init__Nc                 K   s2   |r| j j||fi |¤ŽS | j j|fi |¤ŽS d S r   )ru   rv   )r   ÚstmtÚ
parametersr_   r   r   r   rv   ”  s    zDBAPIProxyCursor.executec                 K   s   | j j||fi |¤ŽS r   )ru   Úexecutemany)r   r«   Úparamsr_   r   r   r   r­   š  s    zDBAPIProxyCursor.executemanyc                 C   s
   t | jƒS r   )Úiterru   r   r   r   r   Ú__iter__�  s    zDBAPIProxyCursor.__iter__c                 C   s   t | j|ƒS r   )rf   ru   rr   r   r   r   rt      s    zDBAPIProxyCursor.__getattr__)N)	rY   rZ   r[   Ú__doc__r   rv   r­   r°   rt   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_creatorrO   r'   Ú
cursor_cls)r   r'   r³   r   r   r   r   ¬  s    zDBAPIProxyConnection.__init__c                 O   s   | j | j| jg|¢R i |¤ŽS r   )r³   r'   rO   )r   r^   rx   r   r   r   ru   ±  s    zDBAPIProxyConnection.cursorc                 C   s   | j  ¡  d S r   )rO   r   r   r   r   r   r   ´  s    zDBAPIProxyConnection.closec                 C   s   t | j|ƒS r   )rf   rO   rr   r   r   r   rt   ·  s    z DBAPIProxyConnection.__getattr__N)rY   rZ   r[   r±   r   ru   r   rt   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   rL   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   rA   r   Úutilr   r   r   r	   r
   Úobjectr   r\   r`   ra   rc   rd   rm   rn   r�   r   r©   rª   r²   r»   r   r   r   r   Ú<module>   sH    

	


,
      ú
P
&ÿ