o znh'@sVddlZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddlm Z e rGd d Z e ZZne rRd d ZeZZnejZd dZddZddZGdddeZddZddZddZddZddZdd Zed!d"Zd#d$Zd%d&Zd2d(d)Zd*d+Z Gd,d-d-e!Z"d3d.d/Z#d0d1Z$dS)4N decorator) defaultdict)inspect_getfullargspec)jython)py2k)pypycGs&ttdttdS)z aggressive gc.collect for tests.g?r)gccollecttimesleepargsrB/usr/local/lib/python3.10/dist-packages/sqlalchemy/testing/util.pyjython_gc_collects  rcGsttdSN)r r rrrrpypy_gc_collect%s rcCsdSrrrrrrlazy_gc.src#s~t}trz ddl}||Wn tyYnwddl}|||D]td|jD] jfddfVq.q&dS)Nrcs |Sr)dumpsdpickle_protocolrrCs zpicklers..) setrcPickleadd ImportErrorpicklerangeHIGHEST_PROTOCOLloads)picklersrr"rrrr&2s   r&cCs<t|tr t||S|tdd|tjtd|S)N10 ) isinstancefloatrounddecimalDecimal to_integral ROUND_FLOORpow)valueprecrrr round_decimalFs  r4c@s<eZdZddZddZddZddZd d Zd d Zd S) RandomSetcCs tt|}t|t|Sr)listr__iter__randomshuffleiter)selflrrrr7Qs zRandomSet.__iter__cCs4tdt|d}tt||}|||S)Nr)r8randintlenr6rr7remove)r;indexitemrrrpopVs z RandomSet.popcCtt||Sr)r5runionr;otherrrrrE\zRandomSet.unioncCrDr)r5r differencerFrrrrI_rHzRandomSet.differencecCrDr)r5r intersectionrFrrrrJbrHzRandomSet.intersectioncCst|Sr)r5r;rrrcopyezRandomSet.copyN) __name__ __module__ __qualname__r7rCrErIrJrLrrrrr5Ps r5cCs`tt}|D] \}}|||qt|D]\}}||dD] }|||vr,dSq qdS)zATrue if the given sorting conforms to the given partial ordering.NFT)rrr enumerate)tuplessorted_elementsdepsparentchildinodenrrrconforms_partial_orderingis  rZcs>tt|D] \}}||qfddt|S)Nc3sbt|dkrt|VdS|D]}t||g}||s.|D]}|g|Vq%qdS)Nr=)r?r6rrIrJ)elementselemsubset sub_ordering_all_orderingsedgesrrr`|s  z-all_partial_orderings.._all_orderings)rrr r:)rRr[rUrVrr_rall_partial_orderingsws   rbc Cs<z||_W|Styt|j|j||j|j}Y|Sw)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. )rN TypeErrortypes FunctionType__code__ __globals__ __defaults__ __closure__)fnnamerrrfunction_nameds  rlcOsZ|}z||g|Ri|}|ddd|WSt}|j|}|s)|YS)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)ctxrjargkwobjresultrpraise_rrrrun_as_contextmanagers  rwcCsdd|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 zrowset..r)resultsrrrrowsetsrcCsJ|rr)msgrrrfailrMrc Osddlm}ddlm}ddlm}||j}|d}t|dd}||_z||i|W| ||j||_S| ||j||_w)z>Provide bound MetaData for a single test, dropping afterwards.r=config)enginesrschemametadataN) rr sqlalchemyrMetaDatadbgetattrrdrop_all_tables) rjrrsrrrrr; prev_metarrrprovide_metadatas     rcs`ddlm}t|D]}|q t|jfdd|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' ) r=rcs<g|]dfddDftfddDqS)_c3s |] }|dr|VqdSFNgetrzkrrr sz/flag_combinations...c3s|] }|dVqdSrrrrrrrs)joinry)rzkeysrr s z%flag_combinations..rWa,)id_argnames)rrrupdatesorted combinationsr?r)rrrrrrflag_combinationss   rc sNt|d}fdd|D}t|j}|t|j|}|di|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)rC)rzrrrsrr sz"resolve_lambda..Nr)rdictrgrrdrerf)__fnrspos_args pass_pos_argsglbnew_fnrrrresolve_lambda s   rfunctioncsddlmfdd}|S)z&Provide MetaData for a pytest fixture.r=rcsfdd}jd|S)Nc 3sZddlm}|}|_z||}|j|VW|jdS|jw)Nrr)rrrr create_allrdrop_all)r;rrru)rrjrrrun_ddl s   z3metadata_fixture..decorate..run_ddl)scope)fixture)rjrrddl)rjrdecorates z"metadata_fixture..decorate)rr)rrrrrmetadata_fixtures rcs0ddlmddlmtfdd}|S)ziForce the given table names to be dropped after test complete, isolating for foreign key cycles r=rr)inspectc s@z||i|WtjjdStjjdw)N) include_names)rr)rjrrsrrnamesrrgo:s0zforce_drop_names..go)rrrrr)rrrrrforce_drop_names2s  rc@s$eZdZdZddZddZeZdS)adictz,Dict keys available as attributes. Shadows.cCs*z||WStyt||YSwr)KeyErrorr__getattribute__)r;keyrrrrHs   zadict.__getattribute__cstfdd|DS)Ncsg|]}|qSrr)rzrrKrrrOr}z"adict.__call__..rx)r;rrrKr__call__Nszadict.__call__N)rNrOrP__doc__rrget_allrrrrrEs rc Cs(ddlm}m}m}m}m}ddlm} m} |durt |}| h} t |j |dD]V\} } | rH|dur:| |vr:q+| | || ||dq+| r|jjsOq+| D]/\} }|dur^| |vr^qQ|| ||d||d||d}| | ||jjg|jjg|dqQq+WddS1swYdS)Nr)ColumnTableIntegerrForeignKeyConstraint) DropTableDropConstraintrxy)rk)rrrrrrsqlalchemy.schemarrrconnectreversedget_sorted_table_and_fkc_namesexecutedialectsupports_altercrr)engine inspectorrrrrrrrrrconntnamefkcsfkctbrrrrTsH    "rcstfdd}|S)Ncs$z ||i|WSwr)_clear)rjrrrs event_clsrrrsz!teardown_events..decorater)rrrrrteardown_eventssr)r)NN)%r-r r8ror rdutilrrrrrr r gc_collectrrr r&r4rr5rZrbrlrwrrrrrrrrrrrrrrrsL           2   .