U kfz@sddlmZddlmZddlZddlmZddlmZddlZddl Z ddl Z ddl m Z ddl m Z dd l mZdd l mZdd lmZdd lmZd dl mZd dl mZd dl mZd dl mZd dl mZd dlmZd dlmZd dlmZd dlmZddZ ej!ddZ"ddZ#ddZ$dd Z%d!d"Z&d#d$Z'da(da)da*dod'd(Z+ej!dpd)d*Z,d+d,Z-d-d.Z.d/d0Z/dqd1d2Z0drd3d4Z1dsd5d6Z2dtd7d8Z3dud9d:Z4dvd;d<Z5dwd=d>Z6dxd?d@Z7dydAdBZ8dzdCdDZ9d{dEdFZ:e:Z;d|dGdHZd~dKdLZ?ddMdNZ@dOdPZAdQdRZBdSdTZCdUdVZDdWdXZEdYdZZFd[d\ZGdd]d^ZHGd_d`d`ZIej!ddadbZJddcddZKddedfZLGdgdhdhZMGdidjdjZNGdkdldlZOGdmdndnZPdS)) annotations) defaultdictN)copy) filterfalse) assertsql)config)engines)mock)db_spec)fail)exc)schema)sql)types)util)default)url)LABEL_STYLE_TABLENAME_PLUS_COL decoratorcOsttj|f|S)aContext manager which expects one or more warnings. With no arguments, squelches all SAWarning 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_warnings_sqla_onlysa_excZ SAWarningmessageskwrM/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/testing/assertions.pyexpect_warnings%s rc osDt|}t|tr$|tjs$dVnt|| dVW5QRXdS)zContext manager which expects one or more warnings on specific dialects. The expect version **asserts** that the warnings were in fact seen. N)r isinstancestrr_currentr)dbrrspecrrrexpect_warnings_on5s  r%cstfdd}|S)zDecorator form of expect_warnings(). Note that emits_warning does **not** assert that the warnings were in fact seen. c s0tddi|||W5QRSQRXdSNassert_F)rfnargsrrrrdecorateNszemits_warning..decoraterrr,rr+r emits_warningFsr.cOsttj|f|SN)rrZSADeprecationWarningrrrrexpect_deprecatedVs r0cOsttj|f|Sr/)rrZBase20DeprecationWarningrrrrexpect_deprecated_20\s r1cstfdd}|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 s6tfddi|||W5QRSQRXdSr&)r%r(r#rrrr,nsz"emits_warning_on..decorater)r#rr,rr2remits_warning_onbs r3cstfdd}|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 s0tddi|||W5QRSQRXdSr&)r0r(r+rrr,sz!uses_deprecated..decoraterr-rr+ruses_deprecatedvsr4TFcCst|||||ddS)zSQLAlchemy internal use only _expect_warnings(). Alembic is using _expect_warnings() directly, and should be updated to use this new interface. T)regex search_msgr'raise_on_any_unexpected)_expect_warnings)exc_clsrr5r6r'rrrrs rc#ssrdd|Dnt|tdk rftdk s4ttdk s@tttt|f7adVntaa|farddntj fdd}t d|Hz dVW5daaa|rrtdd fd d DXW5QRXdS) NcSs g|]}t|tjtjBqSr)recompileIS).0msgrrr sz$_expect_warnings..c_std|dS)NzGot unexpected warning: %rAssertionError)r?argrrrr real_warnsz#_expect_warnings..real_warncst|trt|}t|}n|r*|d}nd}|r.our_warnz warnings.warnzWarnings were not seen: %sz, c3s |]}dr|jn|VqdS)z%rN)pattern)r>s)r5rr sz#_expect_warnings..) list_FILTERS_SEENrBrEextendupdatesetwarningswarnr patchjoin)r9rr5r6r'r7rPrQrrMrr8s2          r8cCs 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`cCstjdSr/)r Ztesting_reaperZassert_all_closedrrrrr_sr_cCs6t||}t|||ks2td||d|fdS)Nz:Given int value %s is not within %d%% of expected value %sd)intabsrB)expectedZreceivedZvarianceZdeviancerrrint_within_variance s recCs$t||s t|pd||fdS)Nz%r !~ %r)r:rIrBabr?rrreq_regexsricCs ||kst|pd||fdS)z.Assert a == b, with repr messaging on failure.%r != %rNrArfrrreq_srkcCs ||kst|pd||fdS)z.Assert a != b, with repr messaging on failure.z%r == %rNrArfrrrne_srlcCs ||kst|pd||fdS)z.Assert a <= b, with repr messaging on failure.rjNrArfrrrle_#srmcCs"t||st|pd||fdS)Nz%r is not an instance of %r)r rBrfrrris_instance_of(srncCst|d|ddSNr?)is_rgr?rrris_none,srscCst|d|ddSro)is_notrrrrr is_not_none0srucCstt|d|ddS)NTrprqboolrrrrris_true4srxcCstt|d|ddS)NFrprvrrrrris_false8srycCs ||kst|pd||fdS)z.Assert a is b, with repr messaging on failure.z %r is not %rNrArfrrrrq<srqcCs ||k st|pd||fdS)z2Assert a is not b, with repr messaging on failure.z%r is %rNrArfrrrrtAsrtcCs ||kst|pd||fdS)z.Assert a in b, with repr messaging on failure.z %r not in %rNrArfrrrin_JsrzcCs ||kst|pd||fdS)z2Assert a in not b, with repr messaging on failure.z %r is in %rNrArfrrrnot_inOsr{cCs"||st|pd||fdS)z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN) startswithrB)rgfragmentr?rrr startswith_Xsr~cCsttdd|}tdd|}tdd|}tdd|}tdd|}tdd|}||kspt|pld||fdS)Nz^\s+?|\nz {2,} z\trj)r:subrBrfrrreq_ignore_whitespace`srcCs,|j|jk r(|js(ds(td||jfdS)aHassert that any exception we're catching does not have a __context__ without a __cause__, and that __suppress_context__ is never set. Python 3 will report nested as exceptions as "during the handling of error X, error Y occurred". That's not what we want to do. we want these exceptions in a cause chain. FzZException %r was correctly raised but did not set a cause, within context %r as its cause.N) __context__ __cause____suppress_context__rB)rKrrr _assert_proper_exception_contextks rcOst||||ddS)NT check_context_assert_raises except_cls callable_r*rrrr assert_raisessrcOst||||Sr/rrrrrassert_raises_context_oksrcOst|||||ddS)NTr?rrrr?rr*kwargsrrrassert_raises_messagesrc Os.t|dg|||W5QRSQRXdS)alegacy adapter function for functions that were previously using assert_raises with SAWarning or similar. has some workarounds to accommodate the fact that the callable completes with this approach rather than stopping at the exception raise. z.*Nr)rrr*rrrr assert_warnss rc Os4t||gddd|||W5QRSQRXdS)aqlegacy adapter function for functions that were previously using assert_raises with SAWarning or similar. has some workarounds to accommodate the fact that the callable completes with this approach rather than stopping at the exception raise. Also uses regex.search() to match the given message to the error string rather than regex.match(). TF)r6r5Nrrrrrassert_warns_messages rcOst|||||dSrorrrrr assert_raises_message_context_oksrc Cs(t|||}|||W5QRX|jSr/)_expect_raiseserror)rrr*rr?recrrrrsrc@seZdZdZdS)_ErrorContainerN)__name__ __module__ __qualname__rrrrrrsrc cst|trt|tst|tr&tdt}|r.DontAccesscSs tddS)Nz=compiler accessed .statement; use compiler.current_executable)NotImplementedError)selfkeyrrr__getattribute__OszFAssertsCompiledSQL.assert_compile..DontAccess.__getattribute__N)rrrrrrrr DontAccessNsrcsDeZdZddZeddZddZddZd d Zfd d Z d S)z>AssertsCompiledSQL.assert_compile..CheckCompilerAccesscSsb||_i|_t|dd|_|jr^|j|_t|dr:|j|_t|drL|j|_t|dr^|j|_dS)Nsupports_executionF _returning_inline_return_defaults) test_statementZ _annotationsgetattrrZ_execution_optionshasattrrrr)rrrrr__init__Vs   zGAssertsCompiledSQL.assert_compile..CheckCompilerAccess.__init__cSs|jjSr/)r_variant_mappingrrrrrgszOAssertsCompiledSQL.assert_compile..CheckCompilerAccess._variant_mappingcSs |jSr/)r_default_dialectrrrrrkszOAssertsCompiledSQL.assert_compile..CheckCompilerAccess._default_dialectc[s|jjj|fd|i|S)Ndialect)rr;__func__rrrrrrr;nszFAssertsCompiledSQL.assert_compile..CheckCompilerAccess.compilec[s|jjj||f|Sr/)r _compilerrrrrrrss zHAssertsCompiledSQL.assert_compile..CheckCompilerAccess._compilerc sVt|drBtj|d|jj|f|W5QRSQRXn|jj|f|SdS)NZ statement)rr r]objectr_compiler_dispatch)rcompilerrrrrrxs  zQAssertsCompiledSQL.assert_compile..CheckCompilerAccess._compiler_dispatchN) rrrrpropertyrrr;rrrrrrCheckCompilerAccessUs rrparamsrasciiignorez SQL String: z[\n\t]rz%r != %r on dialect %r)Z escape_namescsg|]}j|qSr) parametersr>x)expanded_staterrr@sz5AssertsCompiledSQL.assert_compile..csg|] }|qSrrr)prrr@scsi|]}j||jqSr bind_namesZeffective_valuer>rhcrr sz5AssertsCompiledSQL.assert_compile..csi|]}j||jqSrrrrrrrscst}||_||_|Sr/)r paramstyle positional)rrcp)rrr get_dialectsz6AssertsCompiledSQL.assert_compile..get_dialectZpyformatcsg|]}|kr|qSrr)r>vnamerrr@sz%%(%s)sr z#Expected to find bindparam %r in %rc3s"|]}|d|kVqdS)rNr)r>i) positionsrrrTsz4AssertsCompiledSQL.assert_compile..rcSsg|] \}}|qSrr)r>_rrrrr@szQOrder of parameters %s does not match the order in the statement %s. Statement %r)frrr sz5ComparesTables.assert_tables_equal..cSsh|] }|jjqSrrrrrrr s)rrrBrrkrZ primary_keyZnullabler rFassert_types_baserStringlengthZ foreign_keysZserver_defaultrZ FetchedValuecolumns)rtableZreflected_table strict_typesZstrict_constraintsrZ 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')rFZ_compare_type_affinityrBr)rc1c2rrrr (sz ComparesTables.assert_types_baseN)FT)rrrrr rrrrrs +rc@sreZdZddZddZddZddZdd d Zd d ZddZ ddZ e j ddZ ddZe j ddZd S)AssertsExecutionResultscGs&t|}tt|||||dSr/)rUrr assert_list)rrclass_objectsrrr assert_result3s z%AssertsExecutionResults.assert_resultcCsL|t|t|kd|jtdt|D]}||||||q,dS)Nz9result list is not the same size as test list, for class r)r'rrr assert_row)rrrlist_rrrrr8sz#AssertsExecutionResults.assert_listc Cs||j|kdt||D]\}}t|tr~t|dtr`|t|||d|dq| |dt|||dq"|t|||kd|t|||fq"dS)Nzitem class is not rrz'attribute %s value %s does not match %s) r' __class__ritemsr rrUrrr)rrZrowobjdescrvaluerrrrBs   z"AssertsExecutionResults.assert_rowc sGdddtt|}fdd|D}tfdd|D]}tdt|jjfq>t|t|krtdt|t|ftfd d }|D]<}|D]}|||r| |qqtd jt |fqd 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|Sr/)idrrrr__hash__[szOAssertsExecutionResults.assert_unordered_result..immutabledict.__hash__N)rrrr rrrr immutabledictZsr!csh|] }|qSrr)r>e)r!rrr _szBAssertsExecutionResults.assert_unordered_result..cs t|Sr/)r )o)clsrrazAAssertsExecutionResults.assert_unordered_result..z#Unexpected type "%s", expected "%s"z+Unexpected object count "%s", expected "%s"c sv|D]h\}}t|trZz$jt|||df|dWqptk rVYdSXqt|||krdSqdS)NrrFT)rr rassert_unordered_resultrrB)objr$rr)NOVALUErrr _compare_itemos   zFAssertsExecutionResults.assert_unordered_result.._compare_itemz2Expected %s instance with attributes %s not found.T) dictrZ IdentitySetrr rFrrrremover) rrr$rdfoundZwrongr*Z expected_itemZ found_itemr)r)r$r!rrr'Ss8    z/AssertsExecutionResults.assert_unordered_resultNcCs|dkrddlm}t|S)Nr)r#)rr#rZ assert_engine)rr#rrrsql_execution_asserters z.AssertsExecutionResults.sql_execution_asserterc Gs*|| }|}W5QRX|j||Sr/r.r')rr#rrulesasserterrrrrassert_sql_executions  z,AssertsExecutionResults.assert_sql_executioncCsXg}|D]<}t|tr0tjdd|D}n tj|}||q|j||f|S)NcSsg|]\}}t||qSr)r CompiledSQL)r>krrrrr@sz6AssertsExecutionResults.assert_sql..)r r+rZAllOfrr3rr2)rr#rr0ZnewrulesruleZnewrulerrr assert_sqls   z"AssertsExecutionResults.assert_sqlcCs|||t|Sr/)r2rCountStatements)rr#rcountrrrassert_sql_counts z(AssertsExecutionResults.assert_sql_countc gs*|| }dVW5QRX|j|dSr/r/)rr#r0r1rrrassert_executions z(AssertsExecutionResults.assert_executioncCs||t|Sr/)r:rr7)rr#r8rrrassert_statement_countsz.AssertsExecutionResults.assert_statement_countc #sfddt||D}g}|D]\}}}||q z dVW5t||D],\}\}}}|ddd|t|qPXdS)Ncs g|]\}}|||fqSr)r.)r>r#r8rrrr@szKAssertsExecutionResults.assert_statement_count_multi_db..)rr __enter____exit__r'rr7) rZdbscountsZrecsZ assertersctxr#r8r1rrrassert_statement_count_multi_dbs  z7AssertsExecutionResults.assert_statement_count_multi_db)N)rrrrrrr'r.r2r6r9 contextlibcontextmanagerr:r;r@rrrrr2s 6   rc@seZdZddddddZdS)ComparesIndexesz schema.TablerUr!)rrd dialect_namec Cstt|jt|dd|jD}|D]}||d}t|j|ddd|dD}tt|jt||D]}t||jkqr|d} | rtt|jt| t|j| |dD]\} } } | dkrt| j | q|d r(|d |d kr(t|j |d |d |d q(dS) NcSsi|] }|j|qSrr)r>idxrrrrszEComparesIndexes.compare_table_index_with_expected..runiquecSsg|]}|dk r|qSr/r)r>rrrrr@szEComparesIndexes.compare_table_index_with_expected..Z column_names expressionsdialect_optionsZ_includeinclude) rkrZindexesrFrrxrrGrtextrH) rrrdrDZidx_dictexprEcolsrZexprsZidx_expexprcolrrr!compare_table_index_with_expecteds6   z1ComparesIndexes.compare_table_index_with_expectedN)rrrrOrrrrrCsrC)TFT)TFTFF)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NF)NF)T)T)Q __future__r collectionsrrAr itertoolsrr:rr[rrrr r Z exclusionsr rr rrrrrrZenginerrZsql.selectablerrrrBr%r.r0r1r3r4rVrWrErr8r`r_rerirkrlrmrnrsrurxryrqrtZis_not_rzr{Znot_in_r~rrrrrrrrrrrrrrrrrCrrrr s                      P                   *   6