U a_@sddlmZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddl m Z dd l mZd d lmZd d lmZd d lmZd dlmZd dlm Z d dlmZd dlmZd dlmZd dl mZd dl mZddZejddZddZddZ ddZ!dd Z"d!d"Z#ejdcd%d&Z$d'd(Z%d)d*Z&ddd+d,Z'ded-d.Z(dfd/d0Z)dgd1d2Z*dhd3d4Z+did5d6Z,djd7d8Z-dkd9d:Z.dld;d<Z/dmd=d>Z0dnd?d@Z1e1Z2dodAdBZ3dpdCdDZ4e4Z5dqdEdFZ6drdGdHZ7dIdJZ8dKdLZ9dMdNZ:dOdPZ;dQdRZZ?ejdtdWdXZ@dudYdZZAdvd[d\ZBGd]d^d^e>ZCGd_d`d`e>ZDGdadbdbe>ZEdS)w)absolute_importN) assertsql)config)engines)mock)db_spec)fail)exc)schema)sql)types)util)default)url)LABEL_STYLE_TABLENAME_PLUS_COL)compat decoratorcOsttjtjf|f|S)aContext manager which expects one or more warnings. With no arguments, squelches all SAWarning and RemovedIn20Warning 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_excRemovedIn20WarningZ SAWarningmessageskwr^C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\testing\assertions.pyexpect_warnings!s  rc osFt|}t|tjr&|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 isinstancer string_typesr_currentr)dbrrspecrrrexpect_warnings_on3s  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)rfnargsrrrrdecorateLszemits_warning..decoraterrr+rr*r emits_warningDsr-cOsttj|f|SN)rrZSADeprecationWarningrrrrexpect_deprecatedTsr/cOsttj|f|Sr.)rrrrrrrexpect_deprecated_20Xsr0cstfdd}|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+hsz"emits_warning_on..decorater)r"rr+rr1remits_warning_on\s r2cstfdd}|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%)r/r'r*rrr+sz!uses_deprecated..decoraterr,rr*ruses_deprecatedpsr3TFc #srdd|Dn|tr.ddntjfdd}td|Ttdd <td d $td d  dVW5QRXW5QRXW5QRXW5QRX|r|rtjsrtd dfddDdS)NcSs g|]}t|tjtjBqSr)recompileIS).0msgrrr sz$_expect_warnings..c_std|dS)NzGot unexpected warning: %rAssertionError)r9argrrrr real_warnsz#_expect_warnings..real_warncst|rt|}t|}n|r*|d}nd}|rregexseenrrour_warns&    z"_expect_warnings..our_warnz warnings.warnz"sqlalchemy.util.SQLALCHEMY_WARN_20Tz/sqlalchemy.util.deprecations.SQLALCHEMY_WARN_20z2sqlalchemy.engine.row.LegacyRow._default_key_styler zWarnings were not seen: %sz, c3s |]}dr|jn|VqdS)z%rN)pattern)r8s)rIrr sz#_expect_warnings..) setwarningswarnrpatchrpy3kr<join)rFrrIr&Zpy2konlyrHrKrrErrs6  .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_assertionssrVcCstjdSr.)rZtesting_reaperZassert_all_closedrrrrrUsrUcCs$t||s t|pd||fdS)Nz%r !~ %r)r4rBr<abr9rrreq_regexsrZcCs ||kst|pd||fdS)z.Assert a == b, with repr messaging on failure.%r != %rNr;rWrrreq_sr\cCs ||kst|pd||fdS)z.Assert a != b, with repr messaging on failure.z%r == %rNr;rWrrrne_sr]cCs ||kst|pd||fdS)z.Assert a <= b, with repr messaging on failure.r[Nr;rWrrrle_sr^cCs"t||st|pd||fdS)Nz%r is not an instance of %r)rr<rWrrris_instance_ofsr_cCst|d|ddSNr9)is_rXr9rrris_nonesrdcCst|d|ddSr`)is_notrcrrr is_not_nonesrfcCstt|d|ddS)NTrarbboolrcrrris_truesricCstt|d|ddS)NFrargrcrrris_falsesrjcCs ||kst|pd||fdS)z.Assert a is b, with repr messaging on failure.z %r is not %rNr;rWrrrrbsrbcCs ||k st|pd||fdS)z2Assert a is not b, with repr messaging on failure.z%r is %rNr;rWrrrresrecCs ||kst|pd||fdS)z.Assert a in b, with repr messaging on failure.z %r not in %rNr;rWrrrin_ srkcCs ||kst|pd||fdS)z2Assert a in not b, with repr messaging on failure.z %r is in %rNr;rWrrrnot_insrlcCs"||st|pd||fdS)z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN) startswithr<)rXfragmentr9rrr startswith_srocCsXtdd|}tdd|}tdd|}tdd|}||ksTt|pPd||fdS)Nz^\s+?|\nz {2,} r[)r4subr<rWrrreq_ignore_whitespace"s rscCs6tjs dS|j|jk r2|js2ds2td||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. NFzZException %r was correctly raised but did not set a cause, within context %r as its cause.)rrS __context__ __cause____suppress_context__r<)rDrrr _assert_proper_exception_context+s  rwcOst||||ddS)NT check_context_assert_raises except_cls callable_r)rrrr assert_raisesCsrcOst||||Sr.rzr|rrrassert_raises_context_okGsrcOst|||||ddS)NTr9ryrzr}r9r~r)kwargsrrrassert_raises_messageKsrcOst|||||dSr`rzrrrr assert_raises_message_context_okQsrc Cs(t|||}|||W5QRX|jSr.)_expect_raiseserror)r}r~r)rr9ryecrrrr{Wsr{c@seZdZdZdS)_ErrorContainerN)__name__ __module__ __qualname__rrrrrr`src cst}|rtd}z|Vd}Wn~|k r}z`||_d}|dk rnt|t|tjsnt d||f|r~|s~t |t t| dW5d}~XYnX~|st ddS)NrFTz%r !~ %sutf-8z#Callable did not raise an exception) rsysexc_inforr4searchr text_typeUNICODEr<rwprintencode)r}r9ryrZare_we_already_in_a_tracebacksuccesserrrrrrds,  &rcCs t||dS)Nrxr)r}ryrrr expect_raisessrcCst|||dS)Nrr)r}r9ryrrrexpect_raises_messagesrc@seZdZdddZdS)AssertsCompiledSQLNFTcs| rt}| |_||_n~| r$d}nt|dkr8t|dd}|dkrJtjj}nN|dkrht}| |_||_n0|dkrzt}nt |t j rt j |}|r||_i}i}|r||d<|dk rt||d<|rd|d<|rd|d<|rd|d <|rd|d <|st|d d rtj|d <ddlm}t ||jrD|}t|_|}|rR||d<GdddtGfdddt}||jfd|i|ttdi}t jr|ddd}t dt !|dnt dt !d|t"#ddt !}t$||d|||f|dk r6t$%||| dk rh%|t$t&fddj'D| | dk r~t$j(| |dk rt$fd d!j)D||dk rt$fd"d!j*D|dS)#NZ __dialect__rZdefault_enhancedschema_translate_mapZ column_keysT literal_bindsrender_postcompilefor_executemanyrender_schema_translateZassert_from_lintingFZlintingr)ormcompile_kwargsc@seZdZddZdS)z5AssertsCompiledSQL.assert_compile..DontAccesscSs tddS)Nz=compiler accessed .statement; use compiler.current_executable)NotImplementedError)selfkeyrrr__getattribute__szFAssertsCompiledSQL.assert_compile..DontAccess.__getattribute__N)rrrrrrrr DontAccesssrcs8eZdZddZddZddZddZfd d Zd 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__s   zGAssertsCompiledSQL.assert_compile..CheckCompilerAccess.__init__cSs |jSr.)r_default_dialectrrrrrszOAssertsCompiledSQL.assert_compile..CheckCompilerAccess._default_dialectc[s|jjj|fd|i|S)Ndialect)rr5__func__rrrrrrr5szFAssertsCompiledSQL.assert_compile..CheckCompilerAccess.compilec[s|jjj||f|Sr.)r _compilerrrrrrrs zHAssertsCompiledSQL.assert_compile..CheckCompilerAccess._compilerc sVt|drBtj|d|jj|f|W5QRSQRXn|jj|f|SdS)NZ statement)rrrRobjectr_compiler_dispatch)rcompilerrrrrrs  zQAssertsCompiledSQL.assert_compile..CheckCompilerAccess._compiler_dispatchN)rrrrrr5rrrrrrCheckCompilerAccesss rrparamsrasciiignorez SQL String: z[\n\t]rpz%r != %r on dialect %rcsg|] }|qSrr)r8x)prrr:+sz5AssertsCompiledSQL.assert_compile..csi|]}j||jqSrZ bind_namesZeffective_valuer8rYcrr 0sz5AssertsCompiledSQL.assert_compile..csi|]}j||jqSrrrrrrr8s)+rZDefaultDialectsupports_default_valuessupports_default_metavaluerrr"rZStrCompileDialectrrr rURLcreate get_dialectdefault_schema_namelistr Z FROM_LINTINGZ sqlalchemyrZQueryZ _statement_20rZ _label_stylerr5reprrSrdecoderrr4rrr\Zconstruct_paramstupleZ positiontupZprefetchZliteral_execute_paramsZpost_compile_params)rZclauseresultrZ checkparamsrZcheck_literal_executeZcheck_post_paramrZcheckpositionalZcheck_prefetchZuse_default_dialectZallow_dialect_selectrrrrrrrZ from_lintingrrrZstmtrZ param_strccr)rrrrassert_compiles       /         z!AssertsCompiledSQL.assert_compile)NNFNNNNNFFTTFFNFNF)rrrrrrrrrs&rc@seZdZdddZddZdS)ComparesTablesFcCsHt|jt|jkstt|j|jD]\}}t|j|j||j|jksPtt|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&t|jt|jkst|jD]}|jj|jdk s$tq$dS)Nz)Type '%s' doesn't correspond to type '%s'cSsh|] }|jjqSrcolumnnamer8frrr Vsz5ComparesTables.assert_tables_equal..cSsh|] }|jjqSrrrrrrrWs)lenrr<zipr\rZ primary_keyZnullablerr?assert_types_basesqltypesStringlengthZ foreign_keysZserver_defaultr Z FetchedValuecolumns)rtableZreflected_table strict_typesrZ reflected_cr9rrrassert_tables_equalAs6    z"ComparesTables.assert_tables_equalcCs*|j|js&td|j|j|jfdS)Nz7On column %r, type '%s' doesn't correspond to type '%s')r?Z_compare_type_affinityr<r)rZc1c2rrrrbsz ComparesTables.assert_types_baseN)F)rrrrrrrrrr@s !rc@sleZdZddZddZddZddZdd d Zd d ZddZ ddZ ddZ e j ddZddZd S)AssertsExecutionResultscGs&t|}tt|||||dSr.)rrr assert_list)rrclass_Zobjectsrrr assert_resultms 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&rrrange assert_row)rrrlist_irrrrrsz#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__ritemsrrrrrr)rrZrowobjdescrvaluerrrr|s   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)rrrrrrrr immutabledictsrcsh|] }|qSrr)r8e)rrrrszBAssertsExecutionResults.assert_unordered_result..cs t|Sr.)r)o)clsrrzAAssertsExecutionResults.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)rrrassert_unordered_resultrr<)objr#rr)NOVALUErrr _compare_items   zFAssertsExecutionResults.assert_unordered_result.._compare_itemz2Expected %s instance with attributes %s not found.T) dictrZ IdentitySetZitertools_filterfalser r?rrrremover) rrrexpectedfoundZwrongrZ expected_itemZ found_itemr)rrrrrrs>     z/AssertsExecutionResults.assert_unordered_resultNcCs|dkrddlm}t|S)Nr)r")rpr"rZ assert_engine)rr"rrrsql_execution_asserters z.AssertsExecutionResults.sql_execution_asserterc Gs*|| }|}W5QRX|j||Sr.rr&)rr"r~rulesasserterrrrrassert_sql_executions  z,AssertsExecutionResults.assert_sql_executioncCsXg}|D]<}t|tr0tjdd|D}n tj|}||q|j||f|S)NcSsg|]\}}t||qSr)r CompiledSQL)r8kvrrrr:sz6AssertsExecutionResults.assert_sql..)rrrZAllOfrrappendr)rr"r~rZnewrulesZruleZnewrulerrr assert_sqls   z"AssertsExecutionResults.assert_sqlcCs|||t|dSr.)rrCountStatements)rr"r~countrrrassert_sql_counts z(AssertsExecutionResults.assert_sql_countc sfddt||D}g}|D]\}}}||q z |WSt||D],\} \}}}|ddd| t|qPXdS)Ncs g|]\}}|||fqSr)r)r8r"r rrrr:szEAssertsExecutionResults.assert_multiple_sql_count..)rr __enter____exit__r&rr ) rZdbsr~countsZrecsZ assertersctxr"r rrrrassert_multiple_sql_counts  z1AssertsExecutionResults.assert_multiple_sql_countc gs*|| }dVW5QRX|j|dSr.r)rr"rrrrrassert_executions z(AssertsExecutionResults.assert_executioncCs||t|Sr.)rrr )rr"r rrrassert_statement_countsz.AssertsExecutionResults.assert_statement_count)N)rrrrrrrrrrr r contextlibcontextmanagerrrrrrrrls 8  r)TTFF)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NF)NF)T)T)F __future__rrr4rrPrprrrrZ exclusionsrrr r rr r rrZenginerrZsql.selectablerrrrrr$r-r/r0r2r3rrVrUrZr\r]r^r_rdrfrirjrbreZis_not_rkrlZnot_in_rorsrwrrrrr{rrrrrrrrrrrrs                   ?                    7,