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_aggressivecCs"x|jD]}|jrdstqWdS)NF)r r%AssertionError)rr'rrrassert_all_closedcs z"ConnectionKiller.assert_all_closedN)__name__ __module__ __qualname__rrrrrr#r(r)r,r0rr.r/r5rrrrr s  r c Csdtt|dr|tjjjsVddlm }|j ddd| |WdQRXn | |dS)Nr3r) assertionszCan't sort tablesF)Zassert_) testing_reaperr)hasattrr3rr1dialectZsupports_alterr9Zexpect_warningsZdrop_all)ZmetadataZbindr9rrrdrop_all_tablesls   r>cOsz|||WdtXdS)N)r:r5)r!argskwrrrassert_conns_closedzsrAcOsz|||WdtXdS)zBDecorator that rolls back all open connections after fn execution.N)r:r()r!r?r@rrrrollback_open_connectionssrBcOst|||dS)z:Decorator that closes all connections before fn execution.N)r:r))r!r?r@rrr close_firstsrCcOsz|||WdtXdS)z9Decorator that closes all connections after fn execution.N)r:r))r!r?r@rrrclose_open_connectionssrDccs\ddlm}xJ|jD]@}|r&||kr&qt||d}|sJttd|j|}|VqWdS)Nrzsqlalchemy.databases.%s)Zsqlalchemy.databasesZ databases__all__getattr __import__r<)Zexcludednamemodrrr all_dialectss    rKc@s>eZdZddZddZddZddZdd d Zd d ZdS)ReconnectFixturecCs||_g|_d|_dS)NF)dbapi connections is_stopped)rrMrrrrszReconnectFixture.__init__cCs t|j|S)N)rFrM)rkeyrrr __getattr__szReconnectFixture.__getattr__cOsT|jj||}|jr@||j|}|ddsPtdn|j ||SdS)Nzselect 1Fz%simulated connect failure didn't work) rMrrOr#r3cursorexecuter4rNappend)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)rOr$rNr#r3)rstopcrrrshutdownszReconnectFixture.shutdowncCs d|_dS)NF)rO)rrrrrestartszReconnectFixture.restartN)F) r6r7r8rrQrr#rXrYrrrrrLs  rLcsj|p tjj}tjjj}|si}t||d<t||jfdd}jjj_ jjj _ |_S)Nmodulecs jjdjj_dS)NF)r<rMrXrOr)_disposerrrr2s  z$reconnecting_engine..dispose) rr1urlr<rMrLtesting_enginer2rXZ test_shutdownrYZ test_restart)r\r-rMr2r)r[rrreconnecting_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_overflowrZlistenr:rrrr)r\r-r_r`raZ default_optrrrrr]s2        r]csvddlm}|stjj}gfdd}fdd}fdd}||d d |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)r_cs|dS)N)rT)Zsqlar@)bufferrrexecutor'szmock_engine..executorcs"ddD}||kst|dS)NcSsg|]}tddt|qS)z[\n\t]r=)resubstr).0srrr +sz3mock_engine..assert_sql..)r4)ZstmtsZrecv)rhrr assert_sql*szmock_engine..assert_sqlcs jdfddjDS)N c3s|]}t|jdVqdS))r<N)rlcompile)rmrn)rHrr 0sz1mock_engine..print_sql..)r<joinmockr)r)rHr print_sql.szmock_engine..print_sqlz://ru)Zstrategyri) rbr_rr1rIr;r4rurprv)Z dialect_namer_rirprvr)rhrr mock_engines    rwc@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*rR)rrr+r?rUrrrrDszDBAPIProxyCursor.__init__NcKs*|r|jj||f|S|jj|f|SdS)N)rRrS)rstmtZ parametersr@rrrrSIszDBAPIProxyCursor.executecKs|jj||f|S)N)rR executemany)rryZparamsr@rrrrzOszDBAPIProxyCursor.executemanycCs t|j|S)N)rFrR)rrPrrrrQRszDBAPIProxyCursor.__getattr__)N)r6r7r8__doc__rrSrzrQrrrrrx<s  rxc@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?rUrrrrRcszDBAPIProxyConnection.cursorcCs|jdS)N)r+r3)rrrrr3fszDBAPIProxyConnection.closecCs t|j|S)N)rFr+)rrPrrrrQisz DBAPIProxyConnection.__getattr__N)r6r7r8r{rrRr3rQrrrrr|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}rr)r~r}rproxying_enginemsr)N)NN)NN)N)Z __future__rrjrr r=rrutilrrr objectr r:r>rArBrCrDrKrLr^r]rwrxr|rrrrrs.      T   .  & (