o znhG@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 ej !dLd"d#Z!d$d%Z"da#d&d'Z$dMd(d)Z%dMd*d+Z&dMd,d-Z'dMd.d/Z(dMd0d1Z)dMd2d3Z*dMd4d5Z+dMd6d7Z,dMd8d9Z-dMd:d;Z.dMdd?Z0d@dAZ1dBdCZ2dDdEZ3GdFdGdGe4Z5GdHdIdIe4Z6GdJdKdKe4Z7dS)N)absolute_importN) assertsql)config)mock)util)db_spec)fail)exc)orm)pool)schema)types)default)url)compat decoratorcOttj|fi|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_exc SAWarningmessageskwrH/usr/local/lib/python3.10/dist-packages/sqlalchemy/testing/assertions.pyexpect_warnings s rcosdt|}t|tjr|tjsdVdSt|i| dVWddS1s+wYdS)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_on0s "r$ctfdd}|S)zDecorator form of expect_warnings(). Note that emits_warning does **not** assert that the warnings were in fact seen. c@tddi||i|WdS1swYdSNassert_F)rfnargsrrrrdecorateI $zemits_warning..decoraterrr-rr,r emits_warningAsr0cOrN)rrSADeprecationWarningrrrrexpect_deprecatedQsr3cstfdd}|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. csHtgRddi||i|WdS1swYdSr')r$r)r"rrrr-as $z"emits_warning_on..decorater)r"rr-rr4remits_warning_onUs r5cr%)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. cr&r')r3r)r,rrr-yr.z!uses_deprecated..decoraterr/rr,ruses_deprecatedisr6TFc#sr dd|Dn|ttjfdd}td| dVWdn1s2wY|rP|r>tjsTrRJddfdd DdSdSdS) NcSs g|] }t|tjtjBqSr)recompileIS).0msgrrr s z$_expect_warnings..cst|r |}t|}n |r|d}nd}|rt|s'|g|Ri|Ss+dSD]}r6||s<sD||krD|dSq-|g|Ri|dS)Nr)rstr issubclassmatchdiscard)r<argr exceptionfilter_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)rIrr s z#_expect_warnings..)setwarningswarnrpatchrpy3kjoin)rFrrIr(py2konlyrKrrErrs" 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_assertionss rWcCstttjrtd7atdttjttjr0ttj}tjdat d|dStdkr8JddS)Nrz2Encountered a stray connection in test cleanup: %srz8Stray connection refused to leave after gc.collect(): %s Fz*Encountered more than 10 stray connections) testutillazy_gcr _refs_STRAY_CONNECTION_FAILURESprintr> gc_collectclearrPrQ)errrrrrVs&  rVcCs$t||sJ|pd||fdS)Nz%r !~ %r)r7r@abr<rrreq_regexs$rdcCs ||ksJ|p d||fdS)z.Assert a == b, with repr messaging on failure.%r != %rNrrarrreq_ rfcCs ||ksJ|p d||fdS)z.Assert a != b, with repr messaging on failure.z%r == %rNrrarrrne_rgrhcCs ||ksJ|p d||fdS)z.Assert a <= b, with repr messaging on failure.reNrrarrrle_rgricCt|d|ddS)NTr<is_rbr<rrris_truerocCrj)NFrkrlrnrrris_falserprqcCs ||usJ|p d||fdS)z.Assert a is b, with repr messaging on failure.z %r is not %rNrrarrrrmrgrmcCs ||usJ|p d||fdS)z2Assert a is not b, with repr messaging on failure.z%r is %rNrrarrris_not_rgrrcCs ||vsJ|p d||fdS)z.Assert a in b, with repr messaging on failure.z %r not in %rNrrarrrin_ rgrscCs ||vsJ|p d||fdS)z2Assert a in not b, with repr messaging on failure.z %r is in %rNrrarrrnot_in_rgrtcCs"||sJ|p d||fdS)z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN) startswith)rbfragmentr<rrr startswith_s rwcCsXtdd|}tdd|}tdd|}tdd|}||ks*J|p(d||fdS)Nz^\s+?|\nz {2,} re)r7subrarrreq_ignore_whitespaces  r{cOs>z ||i|d}Wn |yd}Ynw|sJddSNFT#Callable did not raise an exceptionr) except_cls callable_r+rsuccessrrr assert_raises's rc OsXd}z ||i|d}Wn|y#}z d}|}WYd}~nd}~ww|s*Jd|Sr|r)r~rr+rret_errrr`rrrassert_raises_return2s rc Osxz ||i|Jd|y;}z$t|t|tjs&Jd||ftt|dWYd}~dSd}~ww)NFr}z%r !~ %sutf-8)r7searchr text_typeUNICODEr]encode)r~r<rr+kwargserrrassert_raises_message@s"rc@s(eZdZ         dddZdS)AssertsCompiledSQLNFc s@|rt}n8| r d}n3|durt|dd}|durtjj}n |dkr(t}n|dkr1t}nt|tj r?t | }i} i} | rI| | d<|durSt || d<| rYd| d<t|tjrk|}d|j_|j}n+t|tjjrtj|d}||jd d d }Wdn1swY| r| | d <|jdd |i| }tt|d i}tjr|ddd}tdt ||dntdt |d|t!"ddt |}t#||d|||f|durt#|$|||dur|$|t#t%fdd|j&D||durt#|j'|dSdS)N __dialect__rdefault_enhancedschema_translate_map column_keysT literal_binds _execute_stmtrrcompile_kwargsdialectparamsrasciiignorez SQL String: z[\n\t]rxz%r != %r on dialect %rcsg|]}|qSrr)r;xprrr=z5AssertsCompiledSQL.assert_compile..r)(rDefaultDialectgetattrrr"rStrCompileDialectrrr rURL get_dialectlistr Query_compile_context statement use_labels persistenceBulkUDrrRobjectexec_ mock_callsr8reprrSrdecoder]rr7rzrfconstruct_paramstuple positiontupprefetch)selfclauseresultr checkparamsrcheckpositionalcheck_prefetchuse_default_dialectallow_dialect_selectrrrrcontext stmt_mockc param_strccrrrassert_compileMsv           z!AssertsCompiledSQL.assert_compile) NNNNNFFFN)__name__ __module__ __qualname__rrrrrrLsrc@seZdZdddZddZdS)ComparesTablesFcCsBt|jt|jks Jt|j|jD]n\}}t|j|j||j|jus(Jt|j|jt|j|j|rNd}t|jt|jsMJ||j|jfn| ||t|jt j rdt|jj |jj tdd|j Ddd|j D|jrt|jtjsJqt|jt|jksJ|jD] }|jj|jdusJqdS)Nz)Type '%s' doesn't correspond to type '%s'cSh|]}|jjqSrcolumnnamer;frrr rz5ComparesTables.assert_tables_equal..cSrrrrrrrrr)lenrziprfr primary_keynullablertypeassert_types_basesqltypesStringlength foreign_keysserver_defaultr FetchedValuecolumns)rtablereflected_table strict_typesr reflected_cr<rrrassert_tables_equals8  z"ComparesTables.assert_tables_equalcCs*|j|jsJd|j|j|jfdS)Nz7On column %r, type '%s' doesn't correspond to type '%s')r_compare_type_affinityr)rc1c2rrrrs z ComparesTables.assert_types_baseN)F)rrrrrrrrrrs  !rc@sleZdZddZddZddZddZdd d Zd d ZddZ ddZ ddZ e j ddZddZd S)AssertsExecutionResultscGs&t|}tt|||||dSr1)rr]r assert_list)rrclass_objectsrrr assert_results z%AssertsExecutionResults.assert_resultcCsL|t|t|kd|jtdt|D] }||||||qdS)Nz9result list is not the same size as test list, for class r)r(rrrange assert_row)rrrlist_irrrrsz#AssertsExecutionResults.assert_listc Cs||j|udt||D]A\}}t|tr?t|dtr0|t|||d|dq| |dt|||dq|t|||kd|t|||fqdS)Nzitem class is not rrz'attribute %s value %s does not match %s) r( __class__ritemsrrrrrr)rrrowobjdesckeyvaluerrrrs  z"AssertsExecutionResults.assert_rowc sGdddtt|}fdd|D}tfdd|D]}tdt|jjfq t|t|krCtdt|t|ftfd d }|D]}|D]}|||ra| |n qStd jt |fqOd 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|Sr1)idrrrr__hash__szOAssertsExecutionResults.assert_unordered_result..immutabledict.__hash__N)rrrrrrrr immutabledicts rcsh|]}|qSrr)r;r)rrrrrzBAssertsExecutionResults.assert_unordered_result..cs t|Sr1)r)o)clsrrs zAAssertsExecutionResults.assert_unordered_result..z#Unexpected type "%s", expected "%s"z+Unexpected object count "%s", expected "%s"c st|D]3\}}t|tr,zjt|||dg|dRWqty+YdSwt|||kr7dSqdS)NrrFT)rrrassert_unordered_resultrAssertionError)objr#rr)NOVALUErrr _compare_item s  zFAssertsExecutionResults.assert_unordered_result.._compare_itemz2Expected %s instance with attributes %s not found.T) dictr IdentitySetitertools_filterfalser rrrrremover) rrrexpectedfoundwrongr expected_item found_itemr)rrrrrrs@     z/AssertsExecutionResults.assert_unordered_resultNcCs|dur ddlm}t|S)Nr)r")rxr"r assert_engine)rr"rrrsql_execution_asserter%s  z.AssertsExecutionResults.sql_execution_assertercGs>|| }|}Wdn1swY|j||Sr1rr()rr"rrulesasserterrrrrassert_sql_execution+s  z,AssertsExecutionResults.assert_sql_executioncCsZg}|D]}t|trtjdd|D}ntj|}||q|j||g|RS)NcSsg|] \}}t||qSr)r CompiledSQL)r;kvrrrr=7sz6AssertsExecutionResults.assert_sql..)rrrAllOfrr appendr)rr"rrnewrulesrulenewrulerrr assert_sql1s   z"AssertsExecutionResults.assert_sqlcCs|||t|dSr1)rrCountStatements)rr"rcountrrrassert_sql_count?s z(AssertsExecutionResults.assert_sql_countc sfddt||D}g}|D] \}}}||qz |Wt||D]\} \}}}|ddd| t|q&St||D]\} \}}}|ddd| t|qCw)Ncs g|] \}}|||fqSr)r)r;r"rrrrr=EszEAssertsExecutionResults.assert_multiple_sql_count..)rr  __enter____exit__r(rr) rdbsrcountsrecs assertersctxr"rrrrrassert_multiple_sql_countDs z1AssertsExecutionResults.assert_multiple_sql_countcgs@|| }dVWdn1swY|j|dSr1r)rr"rrrrrassert_executionSs  z(AssertsExecutionResults.assert_executioncCs||t|Sr1)rrr)rr"rrrrassert_statement_countYrpz.AssertsExecutionResults.assert_statement_countr1)rrrrrrrrrrrr contextlibcontextmanagerrrrrrrrs  8  r)TTFr1)8 __future__rrr7rPrxrrrrrY exclusionsrr r rr r rrrenginerrrrrr r$r0r3r5r6rrWr\rVrdrfrhrirorqrmrrrsrtrwr{rrrrrrrrrrrsf                   -  *              W)