o
    LDiK5                     @  s  d dl m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	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 ddlmZ ejrqddlmZ ddlmZ ddlmZ G dd d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 Z#d=d!d"Z$ej%	#	#d>d?d,d-Z&ej%	#	#d>d@d0d-Z&		d=d1d2dAd5d-Z&d<d6d7Z'G d8d9 d9Z(G d:d; d;Z)dS )B    )annotationsN)Any)Dict)Optional)Union   )config)	decorator)
gc_collect   )event)pool)
await_only)Literal)Engine)URL)AsyncEnginec                   @  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#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   f/var/www/www-root/data/www/77.83.87.30/venv/lib/python3.10/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checkoutcheckinclose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   	dbapi_con
con_record	con_proxyr   r   r    r'   3   s   zConnectionKiller._add_connc                 G  s   | j | d S r   )r   discard)r   
dbapi_connargr   r   r    r(   7   s   zConnectionKiller._remove_connc                 C  s,   |  |j |dv sJ | j| | d S )N)classglobalfunctionfixture)r)   r   r   r*   )r   enginescoper   r   r    
add_engine:   s   zConnectionKiller.add_enginec              
   C  B   z|  W d S  t y  } ztd|  W Y d }~d S d }~ww )Nz5testing_reaper couldn't rollback/close connection: %s	Exceptionwarningswarnr   fner   r   r    _safe@   s   zConnectionKiller._safec                 C  s.   t | jD ]}|d ur|jr| |j qd S r   )listr   is_validr@   rollbackr   recr   r   r    rollback_allH   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   )
rA   r   rB   r   r.   dbapi_connectionr@   _checkinrC   clear)r   rE   conr   r   r    checkin_allM   s   
zConnectionKiller.checkin_allc                 C  s   |    d S r   )rK   r   r   r   r    	close_all\      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 rP   prepare_for_drop_tablesr5   url)r   
connectionrP   r   r   r    rS   _   s   z(ConnectionKiller.prepare_for_drop_tablesc                 C  s   | j | }t|D ]2}t| jD ]}|d ur*|jr*|jd ur*|j|ju r*| |j qt|dr7t	|
  q	|
  q	|  d S Nsync_engine)r   rA   r   rB   _poolr   r@   rH   hasattrr   disposerI   )r   r6   engrE   	proxy_refr   r   r    _drop_testing_enginesh   s   



z&ConnectionKiller._drop_testing_enginesc                 C  s<   | j | }t|D ]}t|drt|  q	|  q	d S rV   )r   rA   rY   r   rZ   )r   r6   r[   rE   r   r   r    _dispose_testing_enginesz   s   


z)ConnectionKiller._dispose_testing_enginesc                 C  s   |  d d S )Nr3   )r]   r   r   r   r    
after_test      zConnectionKiller.after_testc                 C  sj   t jsd S |jjrd S |   ddlm} t j }|	|j
j| W d    d S 1 s.w   Y  d S rN   )r   rQ   	__class__"__leave_connections_for_teardown__rK   rR   rP   dbconnectrS   r5   rT   )r   testrP   connr   r   r    after_test_outside_fixtures   s   "z,ConnectionKiller.after_test_outside_fixturesc                 C  s    |    | d | d d S )Nr3   r1   )rK   r]   r   r   r   r    stop_test_class_inside_fixtures   s   
z0ConnectionKiller.stop_test_class_inside_fixturesc                 C  s`   t jjrt  t jjrtt jj}t jj  	J d| tjr,tjjr.| 	d d S d S d S )NFz/%d connection recs not cleared after test suiter2   )
r   base_strong_ref_connection_recordsr
   lenrI   r   optionslow_connectionsr^   )r   lnr   r   r     stop_test_class_outside_fixtures   s   z1ConnectionKiller.stop_test_class_outside_fixturesc                 C  s"   |    | jD ]}| | qd S r   )rK   r   r]   )r   r6   r   r   r    final_cleanup   s   
zConnectionKiller.final_cleanupc                 C  s   | j D ]}|jr
J qd S r   )r   rB   rD   r   r   r    assert_all_closed   s
   
z"ConnectionKiller.assert_all_closedN)__name__
__module____qualname__r!   r)   r'   r(   r7   r@   rF   rK   rL   rS   r]   r^   r_   rg   rh   ro   rp   rq   r   r   r   r    r   %   s&    	r   c                 O  s(   z| |i | W t   d S t   w r   )testing_reaperrq   r>   argskwr   r   r    assert_conns_closed   s   ry   c                 O  (   z| |i | W t   dS t   w )zBDecorator that rolls back all open connections after fn execution.N)ru   rF   rv   r   r   r    rollback_open_connections   s   r{   c                 O  s   t   | |i | dS )z:Decorator that closes all connections before fn execution.Nru   rK   rv   r   r   r    close_first   s   r}   c                 O  rz   )z9Decorator that closes all connections after fn execution.Nr|   rv   r   r   r    close_open_connections   s   r~   c                 c  sZ    dd l m} |jD ] }| r|| v rq
t||d }|s%ttd| j|}| V  q
d S )Nr   zsqlalchemy.dialects.%s)sqlalchemy.dialectsdialects__all__getattr
__import__dialect)excludednamemodr   r   r    all_dialects   s   
r   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 NF)dbapiconnections
is_stopped)r   r   r   r   r    r!         
zReconnectFixture.__init__c                 C     t | j|S r   )r   r   r   keyr   r   r    __getattr__   rM   zReconnectFixture.__getattr__c                 O  sN   | j j|i |}| jr| |j | }|d J d| j| |S )Nzselect 1Fz%simulated connect failure didn't work)	r   rd   r   r@   r$   cursorexecuter   append)r   rw   kwargsrf   cursr   r   r    rd      s   
zReconnectFixture.connectc              
   C  r8   )Nz.ReconnectFixture couldn't close connection: %sr9   r=   r   r   r    r@     s   zReconnectFixture._safeFc                 C  s,   || _ t| jD ]}| |j qg | _d S r   )r   rA   r   r@   r$   )r   stopcr   r   r    shutdown	  s   
zReconnectFixture.shutdownc                 C  s
   d| _ d S r   )r   r   r   r   r    restart     
zReconnectFixture.restartN)F)	rr   rs   rt   r!   r   rd   r@   r   r   r   r   r   r    r      s    
r   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 r   )r   r   r   r   r   _disposer5   r   r    rZ   !  s   

z$reconnecting_engine.<locals>.dispose)r   rc   rT   r   r   r   testing_enginerZ   r   test_shutdownr   test_restart)rT   rl   r   rZ   r   r   r    reconnecting_engine  s   

r   .rT   Optional[URL]rl   Optional[Dict[str, Any]]asyncioLiteral[False]returnr   c                C     d S r   r   rT   rl   r   r   r   r    r   ,     r   Literal[True]r   c                C  r   r   r   r   r   r   r    r   5  r   F)r   boolUnion[Engine, AsyncEngine]c                C  s<  |r	ddl m} nddlm} ddlm} || r| ntjj} |s#i }i }tj	|fD ]}|s/q*|
dd | D  q*|| fi |}tjrPtjjrPtd |dd	}|dkrd|rad
|j_nd
|_ddlm}	 |	|j||| |dd
}
|
rt|| t|jtjrd|vrd|vrd|vrd|j_d|j_|S )Nr   )create_async_engine)create_engine)make_urlc                 S  s(   i | ]\}}|d vr| ds||qS ))r6   
use_reapersqlite_)
startswith).0optvaluer   r   r    
<dictcomp>X  s    z"testing_engine.<locals>.<dictcomp>r2   r6   r3   Tr   rO   r   r   pool_timeoutmax_overflow)sqlalchemy.ext.asyncior   
sqlalchemyr   sqlalchemy.engine.urlr   r   rc   rT   db_optsupdateitemsrl   rm   ru   r^   getrW   _has_eventsrR   rP   post_configure_testing_enginer7   
isinstancer   	QueuePool_timeout_max_overflow)rT   rl   r   r   r   use_optionsopt_dictr5   r6   rP   r   r   r   r    r   >  sL   	

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   )r   )sqlarx   bufferr   r    executor  r`   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]rR   )resubstrr   sr   r   r    
<listcomp>  s    z3mock_engine.<locals>.assert_sql.<locals>.<listcomp>r   )stmtsrecvr   r   r    
assert_sql  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 ))r   N)r   compiler   r   r   r    	<genexpr>  s    z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)r   joinmockr   )r5   r   r    	print_sql  s   zmock_engine.<locals>.print_sqlz://r   )	r   r   r   rc   r   rY   r   r   r   )dialect_namer   r   r   r   r   )r   r5   r    mock_engine  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   )r5   rU   r   )r   r5   rf   rw   r   r   r   r    r!     s   zDBAPIProxyCursor.__init__Nc                 K  s.   |r| j j||fi |S | j j|fi |S r   )r   r   )r   stmt
parametersrx   r   r   r    r     s   zDBAPIProxyCursor.executec                 K  s   | j j||fi |S r   )r   executemany)r   r   paramsrx   r   r   r    r     s   zDBAPIProxyCursor.executemanyc                 C  s
   t | jS r   )iterr   r   r   r   r    __iter__  r   zDBAPIProxyCursor.__iter__c                 C  r   r   )r   r   r   r   r   r    r     rM   zDBAPIProxyCursor.__getattr__r   )	rr   rs   rt   __doc__r!   r   r   r   r   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   || _ || _|| _d S r   )rf   r5   
cursor_cls)r   r5   rf   r   r   r   r    r!     r   zDBAPIProxyConnection.__init__c                 O  s   | j | j| jg|R i |S r   )r   r5   rf   )r   rw   r   r   r   r    r     s   zDBAPIProxyConnection.cursorc                 C  s   | j   d S r   )rf   r$   r   r   r   r    r$     r`   zDBAPIProxyConnection.closec                 C  r   r   )r   rf   r   r   r   r    r     rM   z DBAPIProxyConnection.__getattr__N)rr   rs   rt   r   r!   r   r$   r   r   r   r   r    r     s    r   r   )NN)..)rT   r   rl   r   r   r   r   r   )rT   r   rl   r   r   r   r   r   )rT   r   rl   r   r   r   r   r   )*
__future__r   r   r   typingr   r   r   r   r;   r   rR   r   utilr	   r
   r   r   r   util.typingr   TYPE_CHECKINGr5   r   
engine.urlr   ext.asyncior   r   ru   ry   r{   r}   r~   r   r   r   overloadr   r   r   r   r   r   r   r    <module>   sf   	 

	



+	
I&