o
    [h )                     @   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   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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   Z
dbapi_conn
con_recordr   r   r   connect      zConnectionKiller.connectc                 C   s   d| j |< d S r   )r   )r   	dbapi_conr   Z	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   r/   r   )r0   r   r1   r-   _closer3   r   r   r   	close_all4   r6   zConnectionKiller.close_allc                 C   s0   t | jD ]\}}|jd u rq| |j qd S r   )r0   r   
connectionr-   r2   r   connr4   r   r   r   _after_test_ctx9   s
   
z ConnectionKiller._after_test_ctxc                 C   s    t jjr
|   d S |   d S r   )r   optionsZ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   )r8   r   r   r0   r   r   dbdisposer3   r   r   r   r>   N   s   
z'ConnectionKiller._stop_test_ctx_minimalc                 C   sR   |    t| jD ]\}}| |j d |_q	t | _t| jD ]}|  q d S r   )	r8   r0   r   r-   closer9   r   r   rB   r:   r   r   r   r?   X   s   
z*ConnectionKiller._stop_test_ctx_aggressivec                 C   s   | j D ]}|jr
J qd S r   )r   r1   r3   r   r   r   assert_all_closedc   s
   
z"ConnectionKiller.assert_all_closedN)__name__
__module____qualname__r   r   r   r!   r$   r-   r5   r8   r<   r@   r   r>   r?   rD   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 )NrC   r   )
assertionszCan't sort tablesF)Zassert_)testing_reaperr8   hasattrrC   r   rA   dialectZsupports_alter rH   Zexpect_warningsZdrop_all)metadatabindrH   r   r   r   drop_all_tablesl   s   

"rO   c                 O   s(   z| |i | W t   d S t   w r   )rI   rD   r+   argskwr   r   r   assert_conns_closedz   s   rS   c                 O   (   z| |i | W t   dS t   w )zBDecorator that rolls back all open connections after fn execution.N)rI   r5   rP   r   r   r   rollback_open_connections   s   rU   c                 O   s   t   | |i | dS )z:Decorator that closes all connections before fn execution.NrI   r8   rP   r   r   r   close_first   s   rW   c                 O   rT   )z9Decorator that closes all connections after fn execution.NrV   rP   r   r   r   close_open_connections   s   rX   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)Zsqlalchemy.databasesZ	databases__all__getattr
__import__rK   )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   rc   r   r   r   r      s   
zReconnectFixture.__init__c                 C      t | j|S r   )rZ   rc   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)	rc   r   re   r-   rC   cursorexecuterd   append)r   rQ   kwargsr;   Zcursr   r   r   r      s   
zReconnectFixture.connectc              
   C   r%   )Nz.ReconnectFixture couldn't close connection: %sr&   r*   r   r   r   r-      r.   zReconnectFixture._safeFc                 C   s,   || _ t| jD ]}| |j qg | _d S r   )re   r0   rd   r-   rC   )r   stopcr   r   r   shutdown   s   
zReconnectFixture.shutdownc                 C   s
   d| _ d S rb   )re   r   r   r   r   restart   s   
zReconnectFixture.restartN)F)	rE   rF   rG   r   ri   r   r-   rq   rr   r   r   r   r   ra      s    
ra   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 rb   )rK   rc   rq   re   r   Z_disposer   r   r   rB      s   

z$reconnecting_engine.<locals>.dispose)r   rA   urlrK   rc   ra   testing_enginerB   rq   Ztest_shutdownrr   Ztest_restart)ru   r=   rc   rB   r   rt   r   reconnecting_engine   s   

rw   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$   )
sqlalchemyry   Zsqlalchemy.engine.urlrz   popr   rA   ru   Z
drivernameZdb_optscopyupdateZ_has_events
isinstancer	   Z	QueuePool_timeoutZ_max_overflowr   listenrI   r   r!   r$   r   )ru   r=   ry   rz   r{   Zdefault_optr   r   r   r   rv      s2   


rv   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   rx   c                    s     |  d S r   )rm   )sqlarR   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]rL   )resubstr.0sr   r   r   
<listcomp>+  s    z3mock_engine.<locals>.assert_sql.<locals>.<listcomp>r   )Z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 ))rK   N)r   compiler   r]   r   r   	<genexpr>0  s    z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)rK   joinmockr   )r   r   r   	print_sql.  s   zmock_engine.<locals>.print_sqlz://r   )Zstrategyr   )	r|   ry   r   rA   r^   rJ   r   r   r   )Zdialect_namery   r   r   r   r   )r   r   r   mock_engine  s   
r   c                   @   sB   e Zd ZdZdd ZdddZ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   r9   rk   )r   r   r;   rQ   rn   r   r   r   r   D  s   zDBAPIProxyCursor.__init__Nc                 K   s.   |r| j j||fi |S | j j|fi |S r   )rk   rl   )r   stmt
parametersrR   r   r   r   rl   I  s   zDBAPIProxyCursor.executec                 K   s   | j j||fi |S r   )rk   executemany)r   r   paramsrR   r   r   r   r   O  s   zDBAPIProxyCursor.executemanyc                 C   rf   r   )rZ   rk   rg   r   r   r   ri   R  rj   zDBAPIProxyCursor.__getattr__c                 C   s   | S r   r   r   r   r   r   	__enter__U     zDBAPIProxyCursor.__enter__c                 G   s   d S r   r   )r   rQ   r   r   r   __exit__X  r   zDBAPIProxyCursor.__exit__r   )
rE   rF   rG   __doc__r   rl   r   ri   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"   |j   | _| _|| _|| _d S r   )r	   Z_creatorr;   Z_sqla_unwrapr   
cursor_cls)r   r   r   r   r   r   r   d  s   
zDBAPIProxyConnection.__init__c                 O   s   | j | j| jg|R i |S r   )r   r   r;   )r   rQ   rn   r   r   r   rk   i  s   zDBAPIProxyConnection.cursorc                 C   s   | j   d S r   )r;   rC   r   r   r   r   rC   l  r   zDBAPIProxyConnection.closec                 C   rf   r   )rZ   r;   rg   r   r   r   ri   o  rj   z DBAPIProxyConnection.__getattr__N)rE   rF   rG   r   r   rk   rC   ri   r   r   r   r   r   \  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   rA   r   conn_clsr   r   r   	mock_conn{  rj   z"proxying_engine.<locals>.mock_connZcreator)r=   )rv   )r   r   r   r   r   r   proxying_engines  s   r   r   )NN)
__future__r   r   r(   r   rL   r   r   utilr   r   r	   objectr
   rI   rO   rS   rU   rW   rX   r`   ra   rw   rv   r   r   r   r   r   r   r   r   <module>   s:   T

	



.

&( 