B 4]aF@sddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddl m Z dd l m Z d d lmZd d lmZd d lmZd dlmZd dlmZd dlm Z d dlmZd dlmZd dl mZd dl mZddZejddZddZddZddZddZ ejdJd"d#Z!d$d%Z"da#d&d'Z$dKd(d)Z%dLd*d+Z&dMd,d-Z'dNd.d/Z(dOd0d1Z)dPd2d3Z*dQd4d5Z+dRd6d7Z,dSd8d9Z-dTd:d;Z.dUdd?Z0d@dAZ1dBdCZ2GdDdEdEe3Z4GdFdGdGe3Z5GdHdIdIe3Z6dS)W)absolute_importN) assertsql)config)mock)util)db_spec)fail)exc)orm)pool)schema)types)default)url)compat) decoratorcOsttj|f|S)aContext manager which expects one or more warnings. With no arguments, squelches all SAWarnings emitted via sqlalchemy.util.warn and sqlalchemy.util.warn_limited. Otherwise pass string expressions that will match selected warnings via regex; all non-matching warnings are sent through. The expect version **asserts** that the warnings were in fact seen. Note that the test suite sets SAWarning warnings to raise exceptions. )_expect_warningssa_excZ SAWarning)messageskwrP/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/testing/assertions.pyexpect_warnings s rc osFt|}t|tjr&|tjs&dVnt|| dVWdQRXdS)zContext manager which expects one or more warnings on specific dialects. The expect version **asserts** that the warnings were in fact seen. N)r isinstancer string_typesrZ_currentr)dbrrspecrrrexpect_warnings_on0s  rcstfdd}|S)zDecorator form of expect_warnings(). Note that emits_warning does **not** assert that the warnings were in fact seen. c s$tddi |||SQRXdS)Nassert_F)r)fnargsr)rrrdecorateIszemits_warning..decorate)r)rr#r)rr emits_warningAsr$cOsttj|f|S)N)rrZSADeprecationWarning)rrrrrexpect_deprecatedQsr%cstfdd}|S)aVMark a test as emitting a warning on a specific dialect. With no arguments, squelches all SAWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). Note that emits_warning_on does **not** assert that the warnings were in fact seen. c s*tfddi |||SQRXdS)Nr F)r)r!r"r)rrrrr#asz"emits_warning_on..decorate)r)rrr#r)rrremits_warning_onUs r&cstfdd}|S)a+Mark a test as immune from fatal deprecation warnings. With no arguments, squelches all SADeprecationWarning failures. Or pass one or more strings; these will be matched to the root of the warning description by warnings.filterwarnings(). As a special case, you may pass a function name prefixed with // and it will be re-written as needed to match the standard warning verbiage emitted by the sqlalchemy.util.deprecated decorator. Note that uses_deprecated does **not** assert that the warnings were in fact seen. c s$tddi |||SQRXdS)Nr F)r%)r!r"r)rrrr#ysz!uses_deprecated..decorate)r)rr#r)rruses_deprecatedisr'TFc #srdd|Dn|ttjfdd}td| dVWdQRX|r|rftjsrtddfdd DdS) NcSs g|]}t|tjtjBqSr)recompileIS).0msgrrr sz$_expect_warnings..cst|r|}t|}n|r&|d}nd}|r8t|sH|f||SsPdSxBD]*}rh||stsV||krV|PqVW|f||dS)Nr)rstr issubclassmatchdiscard)r-argrZ exceptionZfilter_)exc_clsfilters real_warnregexseenrrour_warns       z"_expect_warnings..our_warnz warnings.warnzWarnings were not seen: %sz, c3s |]}dr|jn|VqdS)z%rN)pattern)r,s)r7rr sz#_expect_warnings..) setwarningswarnrpatchrpy3kAssertionErrorjoin)r4rr7r Zpy2konlyr9r)r4r5r6r7r8rrs rcCs tdS)zCheck things that have to be finalized at the end of a test suite. Hardcoded at the moment, a modular system can be built here to support things like PG prepared transactions, tables all dropped, etc. N)!_assert_no_stray_pool_connectionsrrrrglobal_cleanup_assertionssrEcCszttjr0td7atdttjttjr^ttj}tjdat d|ntdkrvdsrt ddadS)Nrz2Encountered a stray connection in test cleanup: %srz8Stray connection refused to leave after gc.collect(): %s Fz*Encountered more than 10 stray connections) testutilZlazy_gcr Z_refs_STRAY_CONNECTION_FAILURESprintr/Z gc_collectclearr>r?rB)errrrrrDs     rDcCs$t||s t|pd||fdS)Nz%r !~ %r)r(r1rB)abr-rrreq_regexsrNcCs ||kst|pd||fdS)z.Assert a == b, with repr messaging on failure.z%r != %rN)rB)rLrMr-rrreq_srOcCs ||kst|pd||fdS)z.Assert a != b, with repr messaging on failure.z%r == %rN)rB)rLrMr-rrrne_srPcCs ||kst|pd||fdS)z.Assert a <= b, with repr messaging on failure.z%r != %rN)rB)rLrMr-rrrle_srQcCst|d|ddS)NT)r-)is_)rLr-rrris_truesrScCst|d|ddS)NF)r-)rR)rLr-rrris_falsesrTcCs ||kst|pd||fdS)z.Assert a is b, with repr messaging on failure.z %r is not %rN)rB)rLrMr-rrrrRsrRcCs ||k st|pd||fdS)z2Assert a is not b, with repr messaging on failure.z%r is %rN)rB)rLrMr-rrris_not_srUcCs ||kst|pd||fdS)z.Assert a in b, with repr messaging on failure.z %r not in %rN)rB)rLrMr-rrrin_ srVcCs ||kst|pd||fdS)z2Assert a in not b, with repr messaging on failure.z %r is in %rN)rB)rLrMr-rrrnot_in_srWcCs"||st|pd||fdS)z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN) startswithrB)rLZfragmentr-rrr startswith_srYcCsXtdd|}tdd|}tdd|}tdd|}||ksTt|pPd||fdS)Nz^\s+?|\nz {2,} z%r != %r)r(subrB)rLrMr-rrreq_ignore_whitespaces r]cOs<y|||d}Wn|k r*d}YnX|s8tddS)NFTz#Callable did not raise an exception)rB) except_cls callable_r"rsuccessrrr assert_raises's   rac Os|y|||dstdWn\|k rv}z>t|t|tjsRtd||ftt|dWdd}~XYnXdS)NFz#Callable did not raise an exceptionz%r !~ %szutf-8)rBr(searchr text_typeUNICODErIencode)r^r-r_r"kwargserrrassert_raises_message2s  rhc @seZdZdddZdS)AssertsCompiledSQLNFc  s0|rt}np| rd}nf|dkr,t|dd}|dkr>tjj}n@|dkrPt}n.|dkrbt}nt|tj r~t | }i} i} | r| | d<|dk rt || d<| rd| d<t|tjr|}d|j_|j}nDt|tjjrtj|d }||jd d d }WdQRX| r(| | d <|jfd |i| }tt|d i}tjr|ddd}tdt ||dntdt |d|t!"ddt |}t#||d|||f|dk rt#|$|||dk r|$|t#t%fdd|j&D||dk r,t#|j'|dS)NZ __dialect__rZdefault_enhancedschema_translate_mapZ column_keysT literal_bindsZ _execute_stmtrrcompile_kwargsdialectparamszutf-8asciiignorez SQL String: z[\n\t]rZz%r != %r on dialect %rcsg|] }|qSrr)r,x)prrr.sz5AssertsCompiledSQL.assert_compile..)(rZDefaultDialectgetattrrrrmZStrCompileDialectrrrrZURLZ get_dialectlistr ZQueryZ_compile_contextZ statementZ use_labelsZ persistenceZBulkUDrr@objectZexec_Z mock_callsr)reprrAredecoderIrcr(r\rOZconstruct_paramstupleZ positiontupZprefetch)selfZclauseresultrnZ checkparamsrmZcheckpositionalZcheck_prefetchZuse_default_dialectZallow_dialect_selectrkrjrrlcontextZ stmt_mockcZ param_strZccr)rrrassert_compile?sd             z!AssertsCompiledSQL.assert_compile) NNNNNFFFN)__name__ __module__ __qualname__r}rrrrri>sric@seZdZdddZddZdS)ComparesTablesFcCsPt|jt|jkstxt|j|jD]\}}t|j|j||j|jksRtt|j|jt|j|j|rd}t|j t |j st||j |j fn | ||t|j t j rt|j j |j j tdd|jDdd|jD|jr(t|jtjs(tq(Wt|jt|jks"tx(|jD]}|jj|jdk s*tq*WdS)Nz)Type '%s' doesn't correspond to type '%s'cSsh|] }|jjqSr)columnname)r,frrr sz5ComparesTables.assert_tables_equal..cSsh|] }|jjqSr)rr)r,rrrrrs)lenr|rBziprOrZ primary_keyZnullablertypeassert_types_basesqltypesStringlengthZ foreign_keysZserver_defaultrZ FetchedValuecolumns)rytableZreflected_tableZ strict_typesr|Z reflected_cr-rrrassert_tables_equals.  z"ComparesTables.assert_tables_equalcCs*|j|js&td|j|j|jfdS)Nz7On column %r, type '%s' doesn't correspond to type '%s')rZ_compare_type_affinityrBr)ryZc1Zc2rrrrsz ComparesTables.assert_types_baseN)F)r~rrrrrrrrrs !rc@sleZdZddZddZddZddZdd d Zd d ZddZ ddZ ddZ e j ddZddZd S)AssertsExecutionResultscGs&t|}tt|||||dS)N)rtrIrv assert_list)ryrzclass_Zobjectsrrr assert_results z%AssertsExecutionResults.assert_resultcCsP|t|t|kd|jx,tdt|D]}||||||q.WdS)Nz9result list is not the same size as test list, for class r)r rr~range assert_row)ryrzrZlist_irrrrs  z#AssertsExecutionResults.assert_listc Cs||j|kdt|x|D]\}}t|trt|dtrb|t|||d|dq| |dt|||dq$|t|||kd|t|||fq$WdS)Nzitem class is not rrz'attribute %s value %s does not match %s) r __class__rvitemsrrxrtrrsr)ryrZrowobjZdesckeyvaluerrrrs  z"AssertsExecutionResults.assert_rowc sGdddtt|}fdd|D}x4tfdd|D]}tdt|jjfqBWt|t|krtdt|t|ftfd d }xF|D]>}x8|D]}|||r| |PqWtd jt |fqWd S) zAs assert_result, but the order of objects is not considered. The algorithm is very expensive but not a big deal for the small numbers of rows that the test suite manipulates. c@seZdZddZdS)zFAssertsExecutionResults.assert_unordered_result..immutabledictcSst|S)N)id)ryrrr__hash__szOAssertsExecutionResults.assert_unordered_result..immutabledict.__hash__N)r~rrrrrrr immutabledictsrcsh|] }|qSrr)r,rg)rrrrszBAssertsExecutionResults.assert_unordered_result..cs t|S)N)r)o)clsrrzAAssertsExecutionResults.assert_unordered_result..z#Unexpected type "%s", expected "%s"z+Unexpected object count "%s", expected "%s"c stxn|D]b\}}t|trXy$jt|||df|dWqltk rTdSXq t|||kr dSq WdS)NrrFT)rrrxassert_unordered_resultrsrB)objrrr)NOVALUEryrr _compare_items  zFAssertsExecutionResults.assert_unordered_result.._compare_itemz2Expected %s instance with attributes %s not found.T) dictrZ IdentitySetZitertools_filterfalser rr~rruremoverv) ryrzrZexpectedfoundZwrongrZ expected_itemZ found_itemr)rrrryrrs.     z/AssertsExecutionResults.assert_unordered_resultNcCs|dkrddlm}t|S)Nr)r)rZrrZ assert_engine)ryrrrrsql_execution_asserters z.AssertsExecutionResults.sql_execution_asserterc Gs*|| }|}WdQRX|j||S)N)rr )ryrr_rulesasserterrzrrrassert_sql_executions  z,AssertsExecutionResults.assert_sql_executioncCs\g}xD|D]<}t|tr2tjdd|D}n tj|}||q W|j||f|S)NcSsg|]\}}t||qSr)r CompiledSQL)r,kvrrrr.)sz6AssertsExecutionResults.assert_sql..)rrrZAllOfrrappendr)ryrr_rZnewrulesZruleZnewrulerrr assert_sql#s   z"AssertsExecutionResults.assert_sqlcCs|||t|dS)N)rrCountStatements)ryrr_countrrrassert_sql_count1sz(AssertsExecutionResults.assert_sql_countc sfddt||D}g}x |D]\}}}||q"Wz|Sx:t||D],\} \}}}|ddd| t|qRWXdS)Ncs g|]\}}|||fqSr)r)r,rr)ryrrr.8szEAssertsExecutionResults.assert_multiple_sql_count..)rr __enter____exit__r rr) ryZdbsr_ZcountsZrecsZ assertersZctxrrrr)ryrassert_multiple_sql_count6s z1AssertsExecutionResults.assert_multiple_sql_countc gs*|| }dVWdQRX|j|dS)N)rr )ryrrrrrrassert_executionEs z(AssertsExecutionResults.assert_executioncCs||t|S)N)rrr)ryrrrrrassert_statement_countKsz.AssertsExecutionResults.assert_statement_count)N)r~rrrrrrrrrrr contextlibcontextmanagerrrrrrrrs 8 r)TTF)N)N)N)N)N)N)N)N)N)N)N)N)7Z __future__rrr(r>rZrrrrrGZ exclusionsrr r rr r rrrZenginerrrrrrrr$r%r&r'rrErHrDrNrOrPrQrSrTrRrUrVrWrYr]rarhrurirrrrrrs^                  , *              W)