o
    i(                     @   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mZ G d
d deZe Zdd 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)uses_deprecated)	decorator   )event)poolc                   @   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e dd Ze dd Zdd ZdS )ConnectionKillerc                 C   s    t  | _t  | _t | _d S N)weakrefWeakKeyDictionary
proxy_refstesting_enginessetconnsself r   U/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/sqlalchemy/testing/engines.py__init__   s   

zConnectionKiller.__init__c                 C   s   d| j |< d S NT)r   )r   enginer   r   r   
add_engine      zConnectionKiller.add_enginec                 C      | j ||f d S r   )r   add)r   
dbapi_conn
con_recordr   r   r   connect      zConnectionKiller.connectc                 C   s   d| j |< d S r   )r   )r   	dbapi_conr   	con_proxyr   r   r   checkout!   r   zConnectionKiller.checkoutc                 C   r   r   )r   discard)r   r!   r   	exceptionr   r   r   
invalidate$   r    zConnectionKiller.invalidatec              
   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'      zConnectionKiller._safec                 C   .   t | jD ]}|d ur|jr| |j qd S r   )listr   is_validr/   rollbackr   recr   r   r   rollback_all/   
   zConnectionKiller.rollback_allc                 C   r1   r   )r2   r   r3   r/   _closer5   r   r   r   	close_all4   r8   zConnectionKiller.close_allc                 C   s0   t | jD ]\}}|jd u rq| |j qd S r   )r2   r   
connectionr/   r4   r   connr6   r   r   r   _after_test_ctx9   s
   
z ConnectionKiller._after_test_ctxc                 C   s    t jjr
|   d S |   d S r   )r   optionslow_connections_stop_test_ctx_minimal_stop_test_ctx_aggressiver   r   r   r   _stop_test_ctxH   s   zConnectionKiller._stop_test_ctxc                 C   s6   |    t | _t| jD ]}|tjur|  qd S r   )r:   r   r   r2   r   r   dbdisposer5   r   r   r   rA   N   s   
z'ConnectionKiller._stop_test_ctx_minimalc                 C   sR   |    t| jD ]\}}| |j d |_q	t | _t| jD ]}|  q d S r   )	r:   r2   r   r/   closer;   r   r   rE   r<   r   r   r   rB   X   s   
z*ConnectionKiller._stop_test_ctx_aggressivec                 C   s   | j D ]}|jr
J qd S r   )r   r3   r5   r   r   r   assert_all_closedc   s
   
z"ConnectionKiller.assert_all_closedN)__name__
__module____qualname__r   r   r   r#   r&   r/   r7   r:   r>   rC   r   rA   rB   rG   r   r   r   r   r
      s     
	

r
   c                 C   s|   t   t|dr|  tjjjs7ddlm	} |j
ddd | | W d    d S 1 s0w   Y  d S | | d S )NrF   r   )
assertionszCan't sort tablesF)assert_)testing_reaperr:   hasattrrF   r   rD   dialectsupports_alter rK   expect_warningsdrop_all)metadatabindrK   r   r   r   drop_all_tablesl   s   

"rV   c                 O   s(   z| |i | W t   d S t   w r   )rM   rG   r-   argskwr   r   r   assert_conns_closedz   s   rZ   c                 O   (   z| |i | W t   dS t   w )zBDecorator that rolls back all open connections after fn execution.N)rM   r7   rW   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rM   r:   rW   r   r   r   close_first   s   r^   c                 O   r[   )z9Decorator that closes all connections after fn execution.Nr]   rW   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.databases.%s)sqlalchemy.databases	databases__all__getattr
__import__rO   )excludednamemodr   r   r   all_dialects   s   
ri   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   rl   r   r   r   r      s   
zReconnectFixture.__init__c                 C      t | j|S r   )rc   rl   r   keyr   r   r   __getattr__      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)	rl   r   rn   r/   rF   cursorexecuterm   append)r   rX   kwargsr=   cursr   r   r   r      s   
zReconnectFixture.connectc              
   C   r'   )Nz.ReconnectFixture couldn't close connection: %sr(   r,   r   r   r   r/      r0   zReconnectFixture._safeFc                 C   s,   || _ t| jD ]}| |j qg | _d S r   )rn   r2   rm   r/   rF   )r   stopcr   r   r   shutdown   s   
zReconnectFixture.shutdownc                 C   s
   d| _ d S rk   )rn   r   r   r   r   restart   s   
zReconnectFixture.restartN)F)	rH   rI   rJ   r   rr   r   r/   r{   r|   r   r   r   r   rj      s    
rj   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 rk   )rO   rl   r{   rn   r   _disposer   r   r   rE      s   

z$reconnecting_engine.<locals>.dispose)r   rD   urlrO   rl   rj   testing_enginerE   r{   test_shutdownr|   test_restart)r   r?   rl   rE   r   r~   r   reconnecting_engine   s   

r   c                 C   s  ddl m} ddlm} |sd}n|dd}| ptjj} || } |du r9tjdu s2| jtjjjkr6tj	}ni }ntjdurP| jtjjjkrPtj	
 }|| || fi |}d|_t|jtjrjd|j_d|j_|rt|jdtj t|jdtj t|jd	tj t| |S )
zBProduce an engine configured by --options with optional overrides.r   create_engine)make_urlT
use_reaperNr   r#   r&   )
sqlalchemyr   sqlalchemy.engine.urlr   popr   rD   r   
drivernamedb_optscopyupdate_has_events
isinstancer	   	QueuePool_timeout_max_overflowr   listenrM   r   r#   r&   r   )r   r?   r   r   r   default_optr   r   r   r   r      s2   


r   c                    sv   ddl m} | stjj} g   fdd} fdd}fdd}|| d	 d
|dtd
r0J  _|_|_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   r   c                    s     |  d S r   )rv   )sqlarY   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]rQ   )resubstr.0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 ))rO   N)r   compiler   rf   r   r   	<genexpr>0  s    z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)rO   joinmockr   )r   r   r   	print_sql.  s   zmock_engine.<locals>.print_sqlz://r   )strategyr   )	r   r   r   rD   rg   rN   r   r   r   )dialect_namer   r   r   r   r   )r   r   r   mock_engine  s   
r   c                   @   s2   e Zd ZdZdd Zd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   r;   rt   )r   r   r=   rX   rw   r   r   r   r   D  s   zDBAPIProxyCursor.__init__Nc                 K   s.   |r| j j||fi |S | j j|fi |S r   )rt   ru   )r   stmt
parametersrY   r   r   r   ru   I  s   zDBAPIProxyCursor.executec                 K   s   | j j||fi |S r   )rt   executemany)r   r   paramsrY   r   r   r   r   O  s   zDBAPIProxyCursor.executemanyc                 C   ro   r   )rc   rt   rp   r   r   r   rr   R  rs   zDBAPIProxyCursor.__getattr__r   )rH   rI   rJ   __doc__r   ru   r   rr   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	   _creatorr=   _sqla_unwrapr   
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   r=   )r   rX   rw   r   r   r   rt   c  s   zDBAPIProxyConnection.cursorc                 C   s   | j   d S r   )r=   rF   r   r   r   r   rF   f  r   zDBAPIProxyConnection.closec                 C   ro   r   )rc   r=   rp   r   r   r   rr   i  rs   z DBAPIProxyConnection.__getattr__N)rH   rI   rJ   r   r   rt   rF   rr   r   r   r   r   r   V  s    r   c                    s    fdd}t d|idS )zIProduce an engine that provides proxy hooks for
    common methods.

    c                      s    t jS r   )r   rD   r   conn_clsr   r   r   	mock_connu  rs   z"proxying_engine.<locals>.mock_conncreator)r?   )r   )r   r   r   r   r   r   proxying_enginem  s   r   r   )NN)
__future__r   r   r*   r   rQ   r   r   utilr   r   r	   objectr
   rM   rV   rZ   r\   r^   r_   ri   rj   r   r   r   r   r   r   r   r   r   r   <module>   s:   T

	



.

&(