U kf4@sddlmZddlZddlZddlZddlmZddlmZddlmZddlZddl Z ddl m Z ddl m Z dd l mZd d l mZd d l mZd d l mZd dlmZejrd dlmZd dlmZd dlmZGdddZeZe ddZe ddZe ddZe ddZ d6ddZ!GdddZ"d7d d!Z#ej$d8d#d$d%d&d'd(d)d*Z%ej$d9d#d$d,d&d-d(d.d*Z%d:d/d*Z%d;d0d1Z&Gd2d3d3Z'Gd4d5d5Z(dS)<) annotationsN)Any)Dict)Optional)config) decorator) gc_collect)event)pool) await_only)Literal)Engine)URL) AsyncEnginec@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#S)$ConnectionKillercCs"t|_tt|_t|_dSN)weakrefWeakKeyDictionary proxy_refs collections defaultdictsettesting_enginesdbapi_connectionsselfrJ/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/testing/engines.py__init__%s  zConnectionKiller.__init__cCsDt|d|jt|d|jt|d|jt|d|jdS)NcheckoutZcheckincloseZclose_detached)r listen _add_conn _remove_conn)rr rrradd_pool*szConnectionKiller.add_poolcCs|j|d|j|<dS)NT)raddr)rZ dbapi_conZ con_recordZ con_proxyrrrr$2s zConnectionKiller._add_conncGs|j|dSr)rdiscard)rZ dbapi_connargrrrr%6szConnectionKiller._remove_conncCs,||j|dkst|j||dS)N)classglobalfunctionZfixture)r&r AssertionErrorrr')renginescoperrr add_engine9s  zConnectionKiller.add_enginec Cs@z |Wn0tk r:}ztd|W5d}~XYnXdS)Nz5testing_reaper couldn't rollback/close connection: %s Exceptionwarningswarnrfnerrr_safe?s  zConnectionKiller._safecCs.t|jD]}|dk r |jr ||jq dSr)listris_validr8rollbackrrecrrr rollback_allGszConnectionKiller.rollback_allcCs^t|jD],}|dk r |jr |j|j||jq |jD]}||jq>|j dSr) r9rr:rr(dbapi_connectionr8_checkinr;clear)rr=conrrr checkin_allLs zConnectionKiller.checkin_allcCs |dSr)rCrrrr close_all[szConnectionKiller.close_allcCs*tjs dSddlm}||jj|dSNr) provision)rbootstrapped_as_sqlalchemyrFprepare_for_drop_tablesr.url)r connectionrFrrrrI^s z(ConnectionKiller.prepare_for_drop_tablescCs|j|}t|D]d}t|jD]4}|dk r |jr |jdk r |j|jkr ||jq t|drnt | q| q| dS)N sync_engine) rr9rr:_poolr r8r@hasattrr disposerA)rr/Zengr=Z proxy_refrrr_drop_testing_enginesgs     z&ConnectionKiller._drop_testing_enginescCs|ddS)Nr,)rPrrrr after_testxszConnectionKiller.after_testc CsTtjs dS|jjrdS|ddlm}tj}| |j j |W5QRXdSrE) rrG __class__Z"__leave_connections_for_teardown__rCrHrFdbconnectrIr.rJ)rtestrFconnrrrafter_test_outside_fixtures{s  z,ConnectionKiller.after_test_outside_fixturescCs ||d|ddS)Nr,r*)rCrPrrrrstop_test_class_inside_fixturess z0ConnectionKiller.stop_test_class_inside_fixturescCsBtjjr>ttjjr>ttjj}tjjds>td|dS)NFz/%d connection recs not cleared after test suite)r baseZ_strong_ref_connection_recordsr lenrAr-)rlnrrr stop_test_class_outside_fixturess  z1ConnectionKiller.stop_test_class_outside_fixturescCs"||jD]}||qdSr)rCrrP)rr/rrr final_cleanups zConnectionKiller.final_cleanupcCs|jD]}|jrdstqdSNF)rr:r-r<rrrassert_all_closeds z"ConnectionKiller.assert_all_closedN)__name__ __module__ __qualname__r r&r$r%r0r8r>rCrDrIrPrQrWrXr\r]r_rrrrr$s"  rcOsz|||W5tXdSr)testing_reaperr_r6argskwrrrassert_conns_closedsrgcOsz|||W5tXdS)zBDecorator that rolls back all open connections after fn execution.N)rcr>rdrrrrollback_open_connectionssrhcOst|||dS)z:Decorator that closes all connections before fn execution.NrcrCrdrrr close_firstsrjcOsz|||W5tXdS)z9Decorator that closes all connections after fn execution.Nrirdrrrclose_open_connectionssrkccsXddlm}|jD]@}|r$||kr$qt||d}|sHttd|j|}|VqdS)Nrzsqlalchemy.dialects.%s)Zsqlalchemy.dialectsZdialects__all__getattr __import__dialect)excludednamemodrrr all_dialectss     rtc@s>eZdZddZddZddZddZdd d Zd d ZdS)ReconnectFixturecCs||_g|_d|_dSr^)dbapi connections is_stopped)rrvrrrr szReconnectFixture.__init__cCs t|j|Sr)rmrvrkeyrrr __getattr__szReconnectFixture.__getattr__cOsT|jj||}|jr@||j|}|ddsPtdn|j ||SdS)Nzselect 1Fz%simulated connect failure didn't work) rvrTrxr8r"cursorexecuter-rwappend)rrekwargsrVZcursrrrrTs   zReconnectFixture.connectc Cs@z |Wn0tk r:}ztd|W5d}~XYnXdS)Nz.ReconnectFixture couldn't close connection: %sr1r5rrrr8s zReconnectFixture._safeFcCs,||_t|jD]}||jqg|_dSr)rxr9rwr8r")rstopcrrrshutdownszReconnectFixture.shutdowncCs d|_dSr^)rxrrrrrestartszReconnectFixture.restartN)F) r`rarbr r{rTr8rrrrrrrus   rucsj|p tjj}tjjj}|si}t||d<t||jfdd}jjj_ jjj _ |_S)Nmodulecs jjdjj_dSr^)rorvrrxrZ_disposer.rrrOs  z$reconnecting_engine..dispose) rrSrJrorvrutesting_enginerOrZ test_shutdownrZ test_restart)rJoptionsrvrOrrrreconnecting_engine s      rFz Optional[URL]zOptional[Dict[str, Any]]zLiteral[False]boolr)rJrasynciotransfer_staticpoolreturncCsdSrrrJrrrrrrrsrTz Literal[True]rcCsdSrrrrrrr'scCs|r|r tddlm}n ddlm}ddlm}|sDd}d} d} n$|dd}|d d} |d d} |prtj j }||}|dkrtj dks|j tj j j krtj }qi}n.tj dk r|j tj j j krtj } | |||f|} | r | jd kr t| d d d} t| ddd}|rdddlm}tj dk rtttj j|rtd}| jtj jn|rttj j| _| dkr|rd| j_nd| _t| jtjrd|krd|krd|krd| j_d| j_|rt| | | S)Nr)create_async_engine) create_engine)make_urlTr,F use_reaperr/sqlite_savepointZsqliterTcSs d|_dSr)Zisolation_level)r?Zconnection_recordrrr do_connectZsz"testing_engine..do_connectbegincSs|ddS)NZBEGIN)Zexec_driver_sql)rVrrrdo_begin^sz testing_engine..do_begin) StaticPoolr+r pool_timeoutZ max_overflow)r-Zsqlalchemy.ext.asyncior sqlalchemyrZsqlalchemy.engine.urlrpoprrSrJZ drivernameZdb_optscopyupdaterrr Z listens_forZsqlalchemy.poolr isinstancer Z_transfer_fromrLZ _has_eventsZ QueuePool_timeoutZ _max_overflowrcr0)rJrrrZ share_poolZ_sqlite_savepointrrrr/rZ default_optr.rrrrrrr0sh                   csrddlm}|stjj}gfdd}fdd}fdd}||d |td r\t_|_|_ 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_enginecs|dSr)r~)sqlarfbufferrrexecutorszmock_engine..executorcs"ddD}||kst|dS)NcSsg|]}tddt|qS)z[\n\t]rH)resubstr.0srrr sz3mock_engine..assert_sql..)r-)Zstmtsrecvrrr assert_sqlszmock_engine..assert_sqlcs jdfddjDS)N c3s|]}t|jdVqdS))roN)rcompilerrqrr sz1mock_engine..print_sql..)rojoinmockr)r.rr print_sqlszmock_engine..print_sqlz://r) rrrrSrrrNr-rrr)Z dialect_namerrrrr)rr.r mock_engines    rc@s:eZdZdZddZd ddZddZd d Zd d ZdS)DBAPIProxyCursorzuProxy a DBAPI cursor. Tests can provide subclasses of this to intercept DBAPI-level cursor operations. cOs||_||_|j|||_dSr)r.rKr|)rr.rVrerrrrr szDBAPIProxyCursor.__init__NcKs*|r|jj||f|S|jj|f|SdSr)r|r})rstmt parametersrfrrrr}szDBAPIProxyCursor.executecKs|jj||f|Sr)r| executemany)rrparamsrfrrrrszDBAPIProxyCursor.executemanycCs t|jSr)iterr|rrrr__iter__szDBAPIProxyCursor.__iter__cCs t|j|Sr)rmr|ryrrrr{szDBAPIProxyCursor.__getattr__)N) r`rarb__doc__r r}rrr{rrrrrs  rc@s0eZdZdZddZddZddZdd Zd S) DBAPIProxyConnectionz}Proxy a DBAPI connection. Tests can provide subclasses of this to intercept DBAPI-level connection operations. cCs||_||_||_dSr)rVr. cursor_cls)rr.rVrrrrr szDBAPIProxyConnection.__init__cOs|j|j|jf||Sr)rr.rV)rrerrrrr|szDBAPIProxyConnection.cursorcCs|jdSr)rVr"rrrrr"szDBAPIProxyConnection.closecCs t|j|Sr)rmrVryrrrr{sz DBAPIProxyConnection.__getattr__N)r`rarbrr r|r"r{rrrrrs r)N)NN)NNFF)NNTF)NNFFFF)N)) __future__rrrtypingrrrr3rrHrutilrr r r r Z util.typingr TYPE_CHECKINGr.rZ engine.urlrZ ext.asynciorrrcrgrhrjrkrtruroverloadrrrrrrrr sn                   +   Q &