U kf8@s@ddlmZddlmZddlZddlZddlZddlmZddl Z ddl Z ddl m Z ddl Z ddl Z ddlmZddlmZdd lmZd d lmZd d lmZd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dl m!Z!d dl m"Z"d dl m#Z#d dl m$Z$e#sLddZ%e%Z&Z'nej(Z&ddZ'ddZ)dPdd Z*d!d"Z+Gd#d$d$e,Z-d%d&Z.d'd(Z/d)d*Z0d+d,Z1d-d.Z2d/d0Z3e!d1d2Z4d3d4Z5d5d6Z6d7d8Z7dQd:d;Z8dd?d?e:Z;d@dAZdGdHZ?dIdJZ@ejAdSdKdLdMdNdOZBdS)T) annotations)dequeN)chain) getsizeof)Any)config)mock)inspect) Connection)Column)DropConstraint) DropTable)ForeignKeyConstraint)MetaData)Tableschema)Integer decorator) defaultdict)has_refcount_gc)inspect_getfullargspeccGsttdSN)gccollect)argsrG/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/testing/util.pynon_refcount_gc_collect,sr!cCsdSrrrrrr lazy_gc5sr"c#sRt}ddl}|||D]0td|jdD]jfddfVq0qdS)Nrrcs |Sr)dumpsdZpickle_protocolrr Bzpicklers..)setpickleaddrangeHIGHEST_PROTOCOLloads)picklersr,rr'r r19s  r1cCstj||dS)N)k)randomchoices) populationr2rrr random_choicesEsr6cCs<t|trt||S|tdd|tjtd|S)N10 ) isinstancefloatrounddecimalDecimal to_integral ROUND_FLOORpow)valueprecrrr round_decimalIs  rDc@s<eZdZddZddZddZddZd d Zd d Zd S) RandomSetcCs tt|}t|t|Sr)listr+__iter__r3shuffleiter)selflrrr rGTs zRandomSet.__iter__cCs4tdt|d}tt||}|||S)Nrr)r3randintlenrFr+rGremove)rJindexitemrrr popYs z RandomSet.popcCstt||Sr)rEr+unionrJotherrrr rR_szRandomSet.unioncCstt||Sr)rEr+ differencerSrrr rUbszRandomSet.differencecCstt||Sr)rEr+ intersectionrSrrr rVeszRandomSet.intersectioncCst|Sr)rErJrrr copyhszRandomSet.copyN) __name__ __module__ __qualname__rGrQrRrUrVrXrrrr rESs rEcCs`tt}|D]\}}|||q t|D].\}}||dD]}|||kr@dSq@q,dS)zATrue if the given sorting conforms to the given partial ordering.NFT)rr+r- enumerate)tuplesZsorted_elementsdepsparentchildinodenrrr conforms_partial_orderingls   rdcs>tt|D]\}}||q fddt|S)Nc3s^t|dkrt|VnB|D]<}t||g}||s|D]}|g|VqFqdS)Nr)rMrFr+rUrV)elementselemZsubsetZ sub_ordering_all_orderingsedgesrr rhs   z-all_partial_orderings.._all_orderings)rr+r-rI)r]rer_r`rrgr all_partial_orderingszs   rjc Cs>z ||_Wn.tk r8t|j|j||j|j}YnX|S)acReturn a function with a given __name__. Will assign to __name__ and return the original function if possible on the Python implementation, otherwise a new function will be constructed. This function should be phased out as much as possible in favor of @decorator. Tests that "generate" many named tests should be modernized. )rY TypeErrortypes FunctionType__code__ __globals__ __defaults__ __closure__)fnnamerrr function_nameds   rtcOs`|}z$||f||}|ddd|WSt}|j|}|sNn|YSYnXdS)aRun the given function under the given contextmanager, simulating the behavior of 'with' to support older Python versions. This is not necessary anymore as we have placed 2.6 as minimum Python version, however some tests are still using this structure. N) __enter____exit__sysexc_info)ctxrrargkwobjresultrxZraise_rrr run_as_contextmanagers  r~cCsdd|DS)zConverts the results of sql execution into a plain set of column tuples. Useful for asserting the results of an unordered query. cSsh|] }t|qSrtuple).0rowrrr szrowset..r)resultsrrr rowsetsrcCsds t|dS)NF)AssertionError)msgrrr failsrc Osddlm}t}|d}t|dd}||_z|||WS||jj}|rjt ||| n t |t j ||_XdS)ztProvide bound MetaData for a single test, dropping afterwards. Legacy; use the "metadata" pytest fixture. r)fixturesrmetadataN)rrrgetattrrZclose_all_sessionsbaseZ_connection_fixture_connectiondrop_all_tables_from_metadataZget_transactioncommitrdb)rrrr{rrrJZ prev_metacfcrrr provide_metadatas     rcsTt|D]}|q ttjfdd|DddtddS)a A facade around @testing.combinations() oriented towards boolean keyword-based arguments. Basically generates a nice looking identifier based on the keywords and also sets up the argument names. E.g.:: @testing.flag_combinations( dict(lazy=False, passive=False), dict(lazy=True, passive=False), dict(lazy=False, passive=True), dict(lazy=False, passive=True, raiseload=True), ) would result in:: @testing.combinations( ('', False, False, False), ('lazy', True, False, False), ('lazy_passive', True, True, False), ('lazy_passive', True, True, True), id_='iaaa', argnames='lazy,passive,raiseload' ) cs<g|]4dfddDftfddDqS)_c3s|]}|dr|VqdSFNgetrr2r%rr s z/flag_combinations...c3s|]}|dVqdSrrrr%rr rs)joinr)rkeysr%r sz%flag_combinations..raa,)Zid_argnames)r+updatesortedr combinationsrMr)rr&rrr flag_combinationss  rc sLt}dd|dD}fddtjfddtt|D|S)NcSsg|] }tqSr)r ZMockrrzrrr r%sz'lambda_combinations..rcsfdd}d|_|S)Ncsf|Srrr{)lambda_arg_setsposrr fixture(sz.create_fixture..fixturez fixture_%3.3d)rY)rr)r)rr create_fixture's z+lambda_combinations..create_fixturecsg|]}|fqSrr)rra)rrr r/s)rrrr.rM)rr{rZarg_setsr)rrr lambda_combinations"s rc sJt|d}fdd|D}t|j}|t|j|}|f|S)zGiven a no-arg lambda and a namespace, return a new lambda that has all the values filled in. This is used so that we can have module-level fixtures that refer to instance-level variables using lambdas. rcsi|]}||qSr)rQrrrr =sz"resolve_lambda..)rdictrorrlrmrn)Z__fnr{Zpos_argsZ pass_pos_argsZglbZnew_fnrrr resolve_lambda3s   rfunctioncsfdd}|S)z&Provide MetaData for a pytest fixture.csfdd}tjd|S)Nc 3sBt}|_z ||}|tj|VW5|tjXdSr)rrrdrop_allrrZ create_all)rJrr}rrrr run_ddlHs    z3metadata_fixture..decorate..run_ddl)scope)rr)rrrddlrr decorateGs z"metadata_fixture..decorater)rrrrr metadata_fixtureDs rcstfdd}|S)ziForce the given table names to be dropped after test complete, isolating for foreign key cycles c s,z|||WSttjttjdXdS)N) include_names)drop_all_tablesrrr )rrrr{namesrr go^szforce_drop_names..gor)rrrrr force_drop_namesXsrc@s$eZdZdZddZddZeZdS)adictz,Dict keys available as attributes. Shadows.cCs0z ||WStk r*t||YSXdSr)KeyErrorr__getattribute__)rJkeyrrr rks zadict.__getattribute__cstfdd|DS)Ncsg|] }|qSrr)rrrWrr rrsz"adict.__call__..r)rJrrrWr __call__qszadict.__call__N)rYrZr[__doc__rrget_allrrrr rhsrc sNddlmfdd}t|tsB|}||W5QRXn||dS)Nr)enginesc sTj||jjsFddlm}|jddd|W5QRXn |dS)Nr) assertionszCan't sort tablesF)Zassert_)Ztesting_reaperZprepare_for_drop_tablesdialectsupports_alterrrZexpect_warningsr) connectionrrrrr rzs  z)drop_all_tables_from_metadata..go)rrr:r begin)rZengine_or_connectionrrrrr rws    rrc Cs|dk rt|}|dk r.|dks(td|f}|}t|j|dD]\}}|r|dk rj|d|krjqH|tt|dt|ddqH|rH|j j sqH|D]j\}} |dk r|d|krqt|dtt dt t dt |dd} |t t| jjg| jjg| d qqHW5QRXdS) Nrz2consider_schemas and schema are mutually exclusive)consider_schemasrrrxy)rs)r+rrreversedZ%sort_tables_on_foreign_key_dependencyexecuterrrrrr rrrcrr) engineZ inspectorrrrconnZ table_keyZfkcsZt_keyZfkctbrrr rsZ      rcstfdd}|S)Ncsz|||WSXdSr)_clear)rrrzr{ event_clsrr rsz!teardown_events..decorater)rrrrr teardown_eventssrc sLdd}ttttttt|ttttittdfdd|S)zReturns the approximate memory footprint an object and all of its contents. source: https://code.activestate.com/recipes/577504/ cSst|Sr)r from_iterableitemsr%rrr dict_handlersz total_size..dict_handlerrcsbt|krdSt|t|}D],\}}t||r0|tt||7}q^q0|S)Nr)idr-rrr:summap)ostyphandlerZ all_handlersZ default_sizeseensizeofrr rs   ztotal_size..sizeof)rrIrFrrr+ frozensetr)rrrrr total_sizes$  rcCsV|g}t}d}|rR|d}||kr0|d7}qt|tr|rt||g|}q|S)zgiven a cache key tuple, counts how many instances of actual tuples are found. used to alert large jumps in cache key complexity. rr)objectrQr:rrF)tupstacksentinelZ num_elementsrfrrr count_cache_key_tupless   rr;r)secondscleanupc csNt}dVt|}||krJz |W5td|dd|dXdS)Nztest took too long (z.4fz seconds > ))timerZ skip_test)rrnowsecrrr skip_if_timeout s  r)r)r)NrN)N)C __future__r collectionsr contextlibr=r itertoolsrr3rwrrrltypingrrrr r rr rr rrrrrsqlZ sql.sqltypesrutilrrrrr!Z gc_collectr"rr1r6rDr+rErdrjrtr~rrrrrrrrrrrrrrrcontextmanagerrrrrr  sx                          +0  8 &