o
    E6d 6                     @  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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rkd
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?d2d.Z%			"	"	"	"d@d3d.Z%d:d4d5Z&G d6d7 d7Z'G d8d9 d9Z(dS )A    )annotationsN)Any)Dict)Optional   )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#S )$ConnectionKillerc                 C  s"   t  | _tt| _t | _d S N)weakrefWeakKeyDictionary
proxy_refscollectionsdefaultdictsettesting_enginesdbapi_connectionsself r   qC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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$   2   s   zConnectionKiller._add_connc                 G  s   | j | d S r   )r   discard)r   Z
dbapi_connargr   r   r   r%   6   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_engine9   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_validr8   rollbackr   recr   r   r   rollback_allG   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   )
r9   r   r:   r   r(   dbapi_connectionr8   _checkinr;   clear)r   r=   conr   r   r   checkin_allL   s   
zConnectionKiller.checkin_allc                 C  s   |    d S r   )rC   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 rG   prepare_for_drop_tablesr-   url)r   
connectionrG   r   r   r   rJ   ^   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   r9   r   r:   _poolr   r8   r@   hasattrr   disposerA   )r   r.   Zengr=   Z	proxy_refr   r   r   _drop_testing_enginesg   s   



z&ConnectionKiller._drop_testing_enginesc                 C     |  d d S )Nr,   )rQ   r   r   r   r   
after_testx      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 rF   )r   rH   	__class__Z"__leave_connections_for_teardown__rC   rI   rG   dbconnectrJ   r-   rK   )r   testrG   connr   r   r   after_test_outside_fixtures{   s   "z,ConnectionKiller.after_test_outside_fixturesc                 C  s    |    | d | d d S )Nr,   r*   )rC   rQ   r   r   r   r   stop_test_class_inside_fixtures   s   
z0ConnectionKiller.stop_test_class_inside_fixturesc                 C  sD   t jjrt  t jjr tt jj}t jj  	J d| d S d S )NFz/%d connection recs not cleared after test suite)r   baseZ_strong_ref_connection_recordsr	   lenrA   )r   lnr   r   r    stop_test_class_outside_fixtures   s   z1ConnectionKiller.stop_test_class_outside_fixturesc                 C  s"   |    | jD ]}| | qd S r   )rC   r   rQ   )r   r.   r   r   r   final_cleanup   s   
zConnectionKiller.final_cleanupc                 C  s   | j D ]}|jr
J qd S r   )r   r:   r<   r   r   r   assert_all_closed   s
   
z"ConnectionKiller.assert_all_closedN)__name__
__module____qualname__r    r&   r$   r%   r/   r8   r>   rC   rD   rJ   rQ   rS   rZ   r[   r_   r`   ra   r   r   r   r   r   $   s$    	r   c                 O  s(   z| |i | W t   d S t   w r   )testing_reaperra   r6   argskwr   r   r   assert_conns_closed   s   ri   c                 O  (   z| |i | W t   dS t   w )zBDecorator that rolls back all open connections after fn execution.N)re   r>   rf   r   r   r   rollback_open_connections   s   rk   c                 O  s   t   | |i | dS )z:Decorator that closes all connections before fn execution.Nre   rC   rf   r   r   r   close_first   s   rm   c                 O  rj   )z9Decorator that closes all connections after fn execution.Nrl   rf   r   r   r   close_open_connections   s   rn   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)Zsqlalchemy.dialectsZdialects__all__getattr
__import__dialect)excludednamemodr   r   r   all_dialects   s   
rw   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   rz   r   r   r   r          
zReconnectFixture.__init__c                 C     t | j|S r   )rp   rz   r   keyr   r   r   __getattr__   rE   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)	rz   rW   r|   r8   r"   cursorexecuter{   append)r   rg   kwargsrY   Zcursr   r   r   rW      s   
zReconnectFixture.connectc              
   C  r0   )Nz.ReconnectFixture couldn't close connection: %sr1   r5   r   r   r   r8      s   zReconnectFixture._safeFc                 C  s,   || _ t| jD ]}| |j qg | _d S r   )r|   r9   r{   r8   r"   )r   stopcr   r   r   shutdown   s   
zReconnectFixture.shutdownc                 C  s
   d| _ d S ry   )r|   r   r   r   r   restart     
zReconnectFixture.restartN)F)	rb   rc   rd   r    r   rW   r8   r   r   r   r   r   r   rx      s    
rx   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 ry   )rr   rz   r   r|   r   Z_disposer-   r   r   rP     s   

z$reconnecting_engine.<locals>.dispose)r   rV   rK   rr   rz   rx   testing_enginerP   r   Ztest_shutdownr   Ztest_restart)rK   optionsrz   rP   r   r   r   reconnecting_engine
  s   

r   FrK   Optional[URL]r   Optional[Dict[str, Any]]asyncioLiteral[False]transfer_staticpoolboolreturnr   c                 C     d S r   r   rK   r   r   r   r   r   r   r        r   TLiteral[True]r   c                 C  r   r   r   r   r   r   r   r   (  r   c                 C  s  |r|rJ ddl m} nddlm} ddlm} |s"d}d}	d}
n|dd}|d	d}	|d
d}
| p9tjj	} || } |d u rVtjd u sO| j
tjj	j
krStj}ni }ntjd urm| j
tjj	j
krmtj }|| || fi |}|
r|jdkrt|ddd }t|ddd }|rddlm} tjd urttjj|rd}|jtjj n|rtjj|_|	dkr|rd|j_nd|_t|jtjrd|j_d|j_|rt||	 |S )Nr   )create_async_engine)create_engine)make_urlTr,   F
use_reaperr.   sqlite_savepointZsqliterW   c                 S  s
   d | _ d S r   )Zisolation_level)r?   Zconnection_recordr   r   r   
do_connect\  s   
z"testing_engine.<locals>.do_connectbeginc                 S  rR   )NZBEGIN)Zexec_driver_sql)rY   r   r   r   do_begin`  s   z testing_engine.<locals>.do_begin)
StaticPoolr+   )Zsqlalchemy.ext.asyncior   
sqlalchemyr   Zsqlalchemy.engine.urlr   popr   rV   rK   Z
drivernameZdb_optscopyupdateru   r   Zlistens_forZsqlalchemy.poolr   
isinstancer   Z_transfer_fromrM   Z_has_eventsZ	QueuePool_timeoutZ_max_overflowre   r/   )rK   r   r   r   Z
share_poolZ_sqlite_savepointr   r   r   r.   r   Zdefault_optr-   r   r   r   r   r   r   r   2  s\   







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arh   bufferr   r   executor  rT   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]rI   )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 ))rr   N)r   compiler   rt   r   r   	<genexpr>  s    z1mock_engine.<locals>.print_sql.<locals>.<genexpr>)rr   joinmockr   )r-   r   r   	print_sql  s   zmock_engine.<locals>.print_sqlz://r   )	r   r   r   rV   ru   rO   r   r   r   )Zdialect_namer   r   r   r   r   )r   r-   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   )r-   rL   r   )r   r-   rY   rg   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
parametersrh   r   r   r   r     s   zDBAPIProxyCursor.executec                 K  s   | j j||fi |S r   )r   executemany)r   r   paramsrh   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   )rp   r   r   r   r   r   r     rE   zDBAPIProxyCursor.__getattr__r   )	rb   rc   rd   __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   )rY   r-   
cursor_cls)r   r-   rY   r   r   r   r   r      r}   zDBAPIProxyConnection.__init__c                 O  s   | j | j| jg|R i |S r   )r   r-   rY   )r   rg   r   r   r   r   r     s   zDBAPIProxyConnection.cursorc                 C  s   | j   d S r   )rY   r"   r   r   r   r   r"     rT   zDBAPIProxyConnection.closec                 C  r~   r   )rp   rY   r   r   r   r   r     rE   z DBAPIProxyConnection.__getattr__N)rb   rc   rd   r   r    r   r"   r   r   r   r   r   r     s    r   r   )NN)NNFF)
rK   r   r   r   r   r   r   r   r   r   )NNTF)
rK   r   r   r   r   r   r   r   r   r   )NNFFFF))
__future__r   r   r   typingr   r   r   r3   r   rI   r   utilr   r	   r   r   r   Zutil.typingr   TYPE_CHECKINGr-   r   Z
engine.urlr   Zext.asyncior   r   re   ri   rk   rm   rn   rw   rx   r   overloadr   r   r   r   r   r   r   r   <module>   sp   	 

	



+	


L&