B 4](@sddlmZddlZddlZddlZddlmZddlmZddlm Z ddlm Z dd lm Z Gd d d e Z e Zd d Ze ddZe ddZe ddZe ddZd&ddZGddde Zd'ddZd(ddZd)ddZGd d!d!e ZGd"d#d#e Zeefd$d%ZdS)*)absolute_importN)config)uses_deprecated) decorator)event)poolc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e ddZe ddZddZdS)ConnectionKillercCs t|_t|_t|_dS)N)weakrefWeakKeyDictionary proxy_refstesting_enginessetconns)selfrM/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/testing/engines.py__init__s  zConnectionKiller.__init__cCsd|j|<dS)NT)r)renginerrr add_engineszConnectionKiller.add_enginecCs|j||fdS)N)radd)rZ dbapi_conn con_recordrrrconnectszConnectionKiller.connectcCsd|j|<dS)NT)r )r dbapi_conrZ con_proxyrrrcheckout!szConnectionKiller.checkoutcCs|j||fdS)N)rdiscard)rrrZ exceptionrrr invalidate$szConnectionKiller.invalidatec Cs@y |Wn0tk r:}ztd|Wdd}~XYnXdS)Nz5testing_reaper couldn't rollback/close connection: %s) Exceptionwarningswarn)rfnerrr_safe's  zConnectionKiller._safecCs2x,t|jD]}|dk r |jr ||jq WdS)N)listr is_validr#rollback)rrecrrr rollback_all/szConnectionKiller.rollback_allcCs2x,t|jD]}|dk r |jr ||jq WdS)N)r$r r%r#Z_close)rr'rrr close_all4szConnectionKiller.close_allcCs4x.t|jD] \}}|jdkr q ||jq WdS)N)r$r connectionr#r&)rconnr'rrr_after_test_ctx9s z ConnectionKiller._after_test_ctxcCstjjr|n|dS)N)roptionsZlow_connections_stop_test_ctx_minimal_stop_test_ctx_aggressive)rrrr_stop_test_ctxHs zConnectionKiller._stop_test_ctxcCs:|t|_x$t|jD]}|tjk r|qWdS)N)r)rrr$rrdbdispose)rr'rrrr.Ns  z'ConnectionKiller._stop_test_ctx_minimalcCsZ|x(t|jD]\}}||jd|_qWt|_xt|jD] }|qFWdS)N) r)r$rr#closer*rrr2)rr+r'rrrr/Xs  z*ConnectionKiller._stop_test_ctx_aggressivecCsx|jD] }|jrqWdS)N)r r%)rr'rrrassert_all_closedcs z"ConnectionKiller.assert_all_closedN)__name__ __module__ __qualname__rrrrrr#r(r)r,r0rr.r/r4rrrrr s  r c Csdtt|dr|tjjjsVddlm }|j ddd| |WdQRXn | |dS)Nr3r) assertionszCan't sort tablesF)Zassert_) testing_reaperr)hasattrr3rr1dialectZsupports_alterr8Zexpect_warningsZdrop_all)ZmetadataZbindr8rrrdrop_all_tablesls   r=cOsz|||WdtXdS)N)r9r4)r!argskwrrrassert_conns_closedzsr@cOsz|||WdtXdS)zBDecorator that rolls back all open connections after fn execution.N)r9r()r!r>r?rrrrollback_open_connectionssrAcOst|||dS)z:Decorator that closes all connections before fn execution.N)r9r))r!r>r?rrr close_firstsrBcOsz|||WdtXdS)z9Decorator that closes all connections after fn execution.N)r9r))r!r>r?rrrclose_open_connectionssrCccs\ddlm}xJ|jD]@}|r&||kr&qt||d}|sJttd|j|}|VqWdS)Nrzsqlalchemy.databases.%s)Zsqlalchemy.databasesZ databases__all__getattr __import__r;)Zexcludednamemodrrr all_dialectss    rJc@s>eZdZddZddZddZddZdd d Zd d ZdS)ReconnectFixturecCs||_g|_d|_dS)NF)dbapi connections is_stopped)rrLrrrrszReconnectFixture.__init__cCs t|j|S)N)rErL)rkeyrrr __getattr__szReconnectFixture.__getattr__cOsH|jj||}|jr4||j|}|dn|j||SdS)Nzselect 1) rLrrNr#r3cursorexecuterMappend)rr>kwargsr+Zcursrrrrs   zReconnectFixture.connectc Cs@y |Wn0tk r:}ztd|Wdd}~XYnXdS)Nz.ReconnectFixture couldn't close connection: %s)rrr )rr!r"rrrr#s  zReconnectFixture._safeFcCs0||_xt|jD]}||jqWg|_dS)N)rNr$rMr#r3)rstopcrrrshutdownszReconnectFixture.shutdowncCs d|_dS)NF)rN)rrrrrestartszReconnectFixture.restartN)F) r5r6r7rrPrr#rWrXrrrrrKs  rKcsj|p tjj}tjjj}|si}t||d<t||jfdd}jjj_ jjj _ |_S)Nmodulecs jjdjj_dS)NF)r;rLrWrNr)_disposerrrr2s  z$reconnecting_engine..dispose) rr1urlr;rLrKtesting_enginer2rWZ test_shutdownrXZ test_restart)r[r-rLr2r)rZrrreconnecting_engines      r]cCsddlm}ddlm}|s"d}n |dd}|p8tjj}||}|dkrrtjdksd|jtjjjkrltj }qi}n.tjdk r|jtjjjkrtj }| |||f|}d|_ t |jtjrd|j_d|j_|rt|jdtjt|jdtjt|jd tjt||S) zBProduce an engine configured by --options with optional overrides.r) create_engine)make_urlT use_reaperNrrr) sqlalchemyr^Zsqlalchemy.engine.urlr_poprr1r[Z drivernameZdb_optscopyupdateZ _has_events isinstancer Z QueuePoolZ_timeoutZ _max_overflowrZlistenr9rrrr)r[r-r^r_r`Z default_optrrrrr\s2        r\cshddlm}|stjj}gfdd}fdd}fdd}||d d |d _|_|_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^cs|dS)N)rS)Zsqlar?)bufferrrexecutor'szmock_engine..executorcsddD}dS)NcSsg|]}tddt|qS)z[\n\t]r<)resubstr).0srrr +sz3mock_engine..assert_sql..r)ZstmtsZrecv)rgrr assert_sql*szmock_engine..assert_sqlcs jdfddjDS)N c3s|]}t|jdVqdS))r;N)rkcompile)rlrm)rGrr 0sz1mock_engine..print_sql..)r;joinmockr)r)rGr print_sql.szmock_engine..print_sqlz://rt)Zstrategyrh)rar^rr1rHrtroru)Z dialect_namer^rhrorur)rgrr mock_engines    rvc@s2eZdZdZddZd ddZddZd d ZdS) DBAPIProxyCursorzuProxy a DBAPI cursor. Tests can provide subclasses of this to intercept DBAPI-level cursor operations. cOs||_||_|j|||_dS)N)rr*rQ)rrr+r>rTrrrrDszDBAPIProxyCursor.__init__NcKs*|r|jj||f|S|jj|f|SdS)N)rQrR)rstmtZ parametersr?rrrrRIszDBAPIProxyCursor.executecKs|jj||f|S)N)rQ executemany)rrxZparamsr?rrrryOszDBAPIProxyCursor.executemanycCs t|j|S)N)rErQ)rrOrrrrPRszDBAPIProxyCursor.__getattr__)N)r5r6r7__doc__rrRryrPrrrrrw<s  rwc@s0eZdZdZddZddZddZdd Zd S) DBAPIProxyConnectionz}Proxy a DBAPI connection. Tests can provide subclasses of this to intercept DBAPI-level connection operations. cCs"|j|_|_||_||_dS)N)r Z_creatorr+Z _sqla_unwrapr cursor_cls)rrr|rrrr^szDBAPIProxyConnection.__init__cOs|j|j|jf||S)N)r|rr+)rr>rTrrrrQcszDBAPIProxyConnection.cursorcCs|jdS)N)r+r3)rrrrr3fszDBAPIProxyConnection.closecCs t|j|S)N)rEr+)rrOrrrrPisz DBAPIProxyConnection.__getattr__N)r5r6r7rzrrQr3rPrrrrr{Vs r{csfdd}td|idS)zIProduce an engine that provides proxy hooks for common methods. cs tjS)N)rr1r)conn_clsr|rr mock_connusz"proxying_engine..mock_connZcreator)r-)r\)r}r|r~r)r}r|rproxying_enginemsr)N)NN)NN)N)Z __future__rrirr r<rrutilrrr objectr r9r=r@rArBrCrJrKr]r\rvrwr{rrrrrs.      T   .  & (