U a2@sddlZddlZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddlm Z ddlmZddlmZddlmZddlmZddlmZddlmZes ddZeZZnejZddZddZer4dCddZ n dDddZ ddZ!Gd d!d!e"Z#d"d#Z$d$d%Z%d&d'Z&d(d)Z'd*d+Z(d,d-Z)ed.d/Z*d0d1Z+d2d3Z,d4d5Z-dEd7d8Z.d9d:Z/Gd;d<dZ2dFd?d@Z3dAdBZ4dS)GN)config)mock)inspect) Connection)Column)DropConstraint) DropTable)ForeignKeyConstraint)MetaData)Tableschema)Integer decorator) defaultdict)has_refcount_gc)inspect_getfullargspec)py2kcGsttdSN)gccollect)argsrXC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\testing\util.pynon_refcount_gc_collect#srcCsdSrrrrrrlazy_gc,src#s~t}tr6zddl}||Wntk r4YnXddl}|||D],td|jD]jfddfVq\qLdS)Nrcs |Sr)dumpsdZpickle_protocolrrAzpicklers..) setrcPickleadd ImportErrorpicklerangeHIGHEST_PROTOCOLloads)picklersr(r+rr#rr/0s r/cCst|}t||d|S)Nr)listrandomshuffle) populationkpoprrrrandom_choicesFs r6cCstj||dS)N)r4)r1choices)r3r4rrrr6OscCs<t|trt||S|tdd|tjtd|S)N10 ) isinstancefloatrounddecimalDecimal to_integral ROUND_FLOORpow)valueprecrrr round_decimalSs  rEc@s<eZdZddZddZddZddZd d Zd d Zd S) RandomSetcCs tt|}t|t|Sr)r0r'__iter__r1r2iter)selflrrrrG^s zRandomSet.__iter__cCs4tdt|d}tt||}|||S)Nrr)r1randintlenr0r'rGremove)rIindexitemrrrr5cs z RandomSet.popcCstt||Sr)rFr'unionrIotherrrrrPiszRandomSet.unioncCstt||Sr)rFr' differencerQrrrrSlszRandomSet.differencecCstt||Sr)rFr' intersectionrQrrrrToszRandomSet.intersectioncCst|Sr)rFrIrrrcopyrszRandomSet.copyN) __name__ __module__ __qualname__rGr5rPrSrTrVrrrrrF]s rFcCs`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_elementsdepsparentchildinodenrrrconforms_partial_orderingvs   rbcs>tt|D]\}}||q fddt|S)Nc3s^t|dkrt|VnB|D]<}t||g}||s|D]}|g|VqFqdS)Nr)rLr0r'rSrT)elementselemZsubsetZ sub_ordering_all_orderingsedgesrrrfs   z-all_partial_orderings.._all_orderings)rr'r)rH)r[rcr]r^rrerall_partial_orderingss   rhc 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. )rW TypeErrortypes FunctionType__code__ __globals__ __defaults__ __closure__)fnnamerrrfunction_nameds   rrcOs`|}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)ctxrpargkwobjresultrvZraise_rrrrun_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)resultsrrrrowsetsrcCsds t|dS)NF)AssertionError)msgrrrfailsrc Osddlm}t}|d}t|dd}||_z|||WS||jrht||j|j n t|t j ||_XdS)ztProvide bound MetaData for a single test, dropping afterwards. Legacy; use the "metadata" pytest fixture. r)fixturesrmetadataN) rrr getattrrZ_close_all_sessionsZ_connection_fixture_connectiondrop_all_tables_from_metadataZget_transactioncommitrdb)rprryrrrIZ prev_metarrrprovide_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|VqdSFNgetrr4r!rr %s z/flag_combinations...c3s|]}|dVqdSrrrr!rrr&s)joinr~)rkeysr!r $sz%flag_combinations..r_a,)Zid_argnames)r'updatesortedr combinationsrLr)rr"rrrflag_combinationss  rc sLt}dd|dD}fddtjfddtt|D|S)NcSsg|] }tqSr)rZMockrrxrrrr1sz'lambda_combinations..rcsfdd}d|_|S)Ncsf|Srrry)lambda_arg_setsposrrfixture4sz.create_fixture..fixturez fixture_%3.3d)rW)rr)r)rrcreate_fixture3s z+lambda_combinations..create_fixturecsg|]}|fqSrr)rr_)rrrr;s)rrrr,rL)rryrZarg_setsr)rrrlambda_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)r5rrrr Isz"resolve_lambda..)rdictrmrrjrkrl)Z__fnryZpos_argsZ pass_pos_argsZglbZnew_fnrrrresolve_lambda?s   rfunctioncsfdd}|S)z&Provide MetaData for a pytest fixture.csfdd}tjd|S)Nc 3sBt}|_z ||}|tj|VW5|tjXdSr)rr rdrop_allrrZ create_all)rIrr{rprrrun_ddlTs    z3metadata_fixture..decorate..run_ddl)Zscope)rr)rprddlrrdecorateSs z"metadata_fixture..decorater)rrrrrmetadata_fixturePs 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)rprrynamesrrgokszforce_drop_names..gor)rrrrrforce_drop_namesesrc@s$eZdZdZddZddZeZdS)adictz,Dict keys available as attributes. Shadows.cCs0z ||WStk r*t||YSXdSr)KeyErrorr__getattribute__)rIkeyrrrrys zadict.__getattribute__cstfdd|DS)Ncsg|] }|qSrr)rrrUrrrsz"adict.__call__..r})rIrrrUr__call__szadict.__call__N)rWrXrY__doc__rrget_allrrrrrvsrc 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) connectionrrrrrrs  z)drop_all_tables_from_metadata..go)rrr;rbegin)rZengine_or_connectionrrrrrrs    rc Cs|dk rt|}|}t|j|dD]\}}|rd|dk rH||krHq*|tt|t|dq*|r*|jj srq*|D]^\}}|dk r||krqvt|tt dt t dt |d}|t t |jjg|jjg|dqvq*W5QRXdS)Nrxy)rq)r'rreversedZget_sorted_table_and_fkc_namesexecuter r r rrrrr r crr) engineZ inspectorrrconnZtnameZfkcsZfkctbrrrrsB   rcstfdd}|S)Ncsz|||WSXdSr)_clear)rprxry event_clsrrrsz!teardown_events..decorater)rrrrrteardown_eventssr)r)r)r)NN)5r>rr1rurjrrrrrrrrr r r r r ZsqlZ sql.sqltypesrutilrrrrrrZ gc_collectrrr/r6rEr'rFrbrhrrr|rrrrrrrrrrrrrrrrrsb                      ,0  &