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 #sjrdd|Dn|ttjfdd}td| dVWdQRX|rf|rftjsfdS)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.warn)setwarningswarnrpatchrpy3k)r4rr7r Zpy2konlyr9r)r4r5r6r7r8rrsrcCs 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_assertionssr@cCsnttjr0td7atdttjttjr^ttj}tjdat d|n tdkrjdadS)Nrz2Encountered a stray connection in test cleanup: %srz8Stray connection refused to leave after gc.collect(): %s ) testutilZlazy_gcr Z_refs_STRAY_CONNECTION_FAILURESprintr/Z gc_collectclearr;r<)errrrrr?s   r?cCsdS)Nr)abr-rrreq_regexsrIcCsdS)z.Assert a == b, with repr messaging on failure.Nr)rGrHr-rrreq_srJcCsdS)z.Assert a != b, with repr messaging on failure.Nr)rGrHr-rrrne_srKcCsdS)z.Assert a <= b, with repr messaging on failure.Nr)rGrHr-rrrle_srLcCst|d|ddS)NT)r-)is_)rGr-rrris_truesrNcCst|d|ddS)NF)r-)rM)rGr-rrris_falsesrOcCsdS)z.Assert a is b, with repr messaging on failure.Nr)rGrHr-rrrrMsrMcCsdS)z2Assert a is not b, with repr messaging on failure.Nr)rGrHr-rrris_not_srPcCsdS)z.Assert a in b, with repr messaging on failure.Nr)rGrHr-rrrin_ srQcCsdS)z2Assert a in not b, with repr messaging on failure.Nr)rGrHr-rrrnot_in_srRcCsdS)z>Assert a.startswith(fragment), with repr messaging on failure.Nr)rGZfragmentr-rrr startswith_srScCs<tdd|}tdd|}tdd|}tdd|}dS)Nz^\s+?|\nz {2,} )r(sub)rGrHr-rrreq_ignore_whitespaces rWcOs0y|||d}Wn|k r*d}YnXdS)NFTr) except_cls callable_r"rsuccessrrr assert_raises's   r[c OsJy|||Wn6|k rD}ztt|dWdd}~XYnXdS)Nzutf-8)rDr text_typeencode)rXr-rYr"kwargserrrassert_raises_message2s  r`c @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]rTz%r != %r on dialect %rcsg|] }|qSrr)r,x)prrr.sz5AssertsCompiledSQL.assert_compile..)(rZDefaultDialectgetattrrrreZStrCompileDialectrrrrZURLZ get_dialectlistr ZQueryZ_compile_contextZ statementZ use_labelsZ persistenceZBulkUDrr=objectZexec_Z mock_callsr)reprr>r]decoderDr\r(rVrJZconstruct_paramstupleZ positiontupZprefetch)selfZclauseresultrfZ checkparamsreZcheckpositionalZcheck_prefetchZuse_default_dialectZallow_dialect_selectrcrbrrdcontextZ stmt_mockcZ param_strZccr)rjrassert_compile?sd             z!AssertsCompiledSQL.assert_compile) NNNNNFFFN)__name__ __module__ __qualname__rurrrrra>srac@seZdZdddZddZdS)ComparesTablesFcCsxt|j|jD]\}}t|j|jt|j|jt|j|j|rLd}n |||t|jt j rxt|jj |jj tdd|j Ddd|j D|j rqWx|jD]}qWdS)Nz)Type '%s' doesn't correspond to type '%s'cSsh|] }|jjqSr)columnname)r,frrr sz5ComparesTables.assert_tables_equal..cSsh|] }|jjqSr)rzr{)r,r|rrrr}s)ziprtrJr{Z primary_keyZnullableassert_types_basertypesqltypesStringlengthZ foreign_keysZserver_default)rqtableZreflected_tableZ strict_typesrtZ reflected_cr-rrrassert_tables_equals"  z"ComparesTables.assert_tables_equalcCsdS)Nr)rqZc1Zc2rrrrsz ComparesTables.assert_types_baseN)F)rvrwrxrrrrrrrys !ryc@sleZdZddZddZddZddZdd d Zd d ZddZ ddZ ddZ e j ddZddZd S)AssertsExecutionResultscGs&t|}tt|||||dS)N)rlrDrn assert_list)rqrrclass_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 lenrvrange assert_row)rqrrrZlist_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__rnitemsrrprlrrkr)rqrZrowobjZdesckeyvaluerrrrs  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)rqrrr__hash__szOAssertsExecutionResults.assert_unordered_result..immutabledict.__hash__N)rvrwrxrrrrr immutabledictsrcsh|] }|qSrr)r,r_)rrrr}szBAssertsExecutionResults.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)rrrpassert_unordered_resultrkAssertionError)objrrr)NOVALUErqrr _compare_items  zFAssertsExecutionResults.assert_unordered_result.._compare_itemz2Expected %s instance with attributes %s not found.T) dictrZ IdentitySetZitertools_filterfalser rrvrrmremovern) rqrrrZexpectedfoundZwrongrZ expected_itemZ found_itemr)rrrrqrrs.     z/AssertsExecutionResults.assert_unordered_resultNcCs|dkrddlm}t|S)Nr)r)rTrrZ assert_engine)rqrrrrsql_execution_asserters z.AssertsExecutionResults.sql_execution_asserterc Gs*|| }|}WdQRX|j||S)N)rr )rqrrYrulesasserterrrrrrassert_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)rqrrYrZnewrulesZruleZnewrulerrr assert_sql#s   z"AssertsExecutionResults.assert_sqlcCs|||t|dS)N)rrCountStatements)rqrrYcountrrrassert_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)rqrrr.8szEAssertsExecutionResults.assert_multiple_sql_count..)r~r __enter____exit__r rr) rqZdbsrYZcountsZrecsZ assertersZctxrrrr)rqrassert_multiple_sql_count6s z1AssertsExecutionResults.assert_multiple_sql_countc gs*|| }dVWdQRX|j|dS)N)rr )rqrrrrrrassert_executionEs z(AssertsExecutionResults.assert_executioncCs||t|S)N)rrr)rqrrrrrassert_statement_countKsz.AssertsExecutionResults.assert_statement_count)N)rvrwrxrrrrrrrrr contextlibcontextmanagerrrrrrrrs 8 r)TTF)N)N)N)N)N)N)N)N)N)N)N)N)7Z __future__rrr(r;rTrrrrrBZ exclusionsrr r rr r rrrZenginerrrrrrrr$r%r&r'rr@rCr?rIrJrKrLrNrOrMrPrQrRrSrWr[r`rmraryrrrrrs^                  , *              W)