a
    •˜ëjÀ=  ã                   @  sx  d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlZd dlZd dlmZ d	d
lmZ d	dlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddl#mZ ddl$m%Z% ddl&m'Z' ddl&m(Z( ddl&m)Z) ddl&m*Z* e)�s|dd„ Z+e+ Z,Z-nej.Z,dd„ Z-dd „ Z/d!d"„ Z0dTd#d$„Z1d%d&„ Z2G d'd(„ d(e3ƒZ4d)d*„ Z5d+d,„ Z6d-d.„ Z7d/d0„ Z8d1d2„ Z9d3d4„ Z:e'd5d6„ ƒZ;d7d8„ Z<d9d:„ Z=d;d<„ Z>dUd>d?„Z?d@dA„ Z@G dBdC„ dCeAƒZBdDdE„ ZCdVdGdH„ZDdIdJ„ ZEdKdL„ ZFdMdN„ ZGejHdWdOdPdQœdRdS„ƒZIdS )Xé    )Úannotations)Údeque)Ú
namedtupleN)Úchain)Ú	getsizeof)Úmkstemp)ÚAnyé   )Úconfig)Úmocké   )Úinspect)Ú
Connection)ÚColumn)ÚDropConstraint)Ú	DropTable)ÚForeignKeyConstraint)ÚMetaData)ÚTable©Úschema)ÚInteger©Ú	decorator)Údefaultdict)Úhas_refcount_gc)Úinspect_getfullargspecc                  G  s   t  ¡  t  ¡  d S ©N)ÚgcÚcollect)Úargs© r!   úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/testing/util.pyÚnon_refcount_gc_collect0   s    r#   c                   C  s   d S r   r!   r!   r!   r!   r"   Úlazy_gc9   s    r$   c                  #  s>   t dddgƒ} tdtjd ƒD ]‰ | tj‡ fdd„ƒV  qd S )NÚpicklersÚloadsÚdumpséþÿÿÿr	   c                   s   t  | ˆ ¡S r   )Úpickler'   ©Úd©Úprotocolr!   r"   Ú<lambda>A   ó    zpicklers.<locals>.<lambda>)r   Úranger)   ÚHIGHEST_PROTOCOLr&   )Úntr!   r,   r"   r%   =   s    r%   c              	   C  sð   t dƒ\}}z¬t |d¡�}t | |¡ W d  ƒ n1 s<0    Y  ttjƒ}tj 	d¡rl| 
tjd ¡ tjtjd|| tjd¡gtjtji tj¥dtj |¡i¥d�}W t |¡ nt |¡ 0 |jdkrætd	|jjd
d� ƒ‚|j ¡ S )aÅ  pickle ``obj`` to a file, then unpickle it in a new interpreter.

    ``code`` is Python source run by that interpreter, which receives the
    name of the pickle file as ``sys.argv[1]``.  The new interpreter has
    the current ``sys.path``, so that the SQLAlchemy under test, as well
    as the ``test`` package, are importable.

    Returns the stripped stdout of the subprocess; a non-zero exit status
    fails the test, reporting its stderr.

    ZpklÚwbNÚ
PYTHONPATHz-cú/)ÚstdoutÚstderrÚenvr   zsubprocess failed: %sÚreplace)Úerrors)r   ÚosÚfdopenr)   ÚdumpÚlistÚsysÚpathÚenvironÚgetÚappendÚ
subprocessÚrunÚ
executabler9   ÚsepÚPIPEÚpathsepÚjoinÚunlinkÚ
returncodeÚAssertionErrorr7   Údecoder6   Ústrip)ÚobjÚcodeÚfdÚfilenameÚfile_ÚpartsÚprocr!   r!   r"   Úunpickle_in_subprocessD   s&    *
ü
ÿrW   c                 C  s   t j| |d�S )N)Úk)ÚrandomÚchoices)Ú
populationrX   r!   r!   r"   Úrandom_choicesj   s    r\   c                 C  s<   t | tƒrt| |ƒS | t dd|  ¡  tj¡td|ƒ S )NÚ1Ú0é
   )Ú
isinstanceÚfloatÚroundÚdecimalÚDecimalÚto_integralÚROUND_FLOORÚpow)ÚvalueÚprecr!   r!   r"   Úround_decimaln   s    

ÿþrj   c                   @  s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú	RandomSetc                 C  s    t t | ¡ƒ}t |¡ t|ƒS r   )r>   ÚsetÚ__iter__rY   ÚshuffleÚiter)ÚselfÚlr!   r!   r"   rm   y   s    
zRandomSet.__iter__c                 C  s4   t  dt| ƒd ¡}tt | ¡ƒ| }|  |¡ |S )Nr   r	   )rY   ÚrandintÚlenr>   rl   rm   Úremove)rp   ÚindexÚitemr!   r!   r"   Úpop~   s    
zRandomSet.popc                 C  s   t t | |¡ƒS r   )rk   rl   Úunion©rp   Úotherr!   r!   r"   rx   „   s    zRandomSet.unionc                 C  s   t t | |¡ƒS r   )rk   rl   Ú
differencery   r!   r!   r"   r{   ‡   s    zRandomSet.differencec                 C  s   t t | |¡ƒS r   )rk   rl   Úintersectionry   r!   r!   r"   r|   Š   s    zRandomSet.intersectionc                 C  s   t | ƒS r   )rk   ©rp   r!   r!   r"   Úcopy�   s    zRandomSet.copyN)	Ú__name__Ú
__module__Ú__qualname__rm   rw   rx   r{   r|   r~   r!   r!   r!   r"   rk   x   s   rk   c                 C  s`   t tƒ}| D ]\}}||  |¡ qt|ƒD ].\}}||d… D ]}||| v r@  dS q@q,dS )zATrue if the given sorting conforms to the given partial ordering.NFT)r   rl   ÚaddÚ	enumerate)ÚtuplesZsorted_elementsÚdepsÚparentÚchildÚiÚnodeÚnr!   r!   r"   Úconforms_partial_ordering‘   s    r‹   c                   s>   t tƒ‰| D ]\}}ˆ|  |¡ q‡ ‡fdd„‰ tˆ |ƒƒS )Nc                 3  s^   t | ƒdkrt| ƒV  nB| D ]<}t| ƒ |g¡}| ˆ| ¡sˆ |ƒD ]}|g| V  qFqd S )Nr	   )rs   r>   rl   r{   r|   )ÚelementsÚelemZsubsetZsub_ordering©Ú_all_orderingsÚedgesr!   r"   r�   ¤   s    z-all_partial_orderings.<locals>._all_orderings)r   rl   r‚   ro   )r„   rŒ   r†   r‡   r!   rŽ   r"   Úall_partial_orderingsŸ   s
    
r‘   c              
   C  s<   z
|| _ W n, ty6   t | j| j|| j| j¡} Y n0 | S )ac  Return 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.

    )r   Ú	TypeErrorÚtypesÚFunctionTypeÚ__code__Ú__globals__Ú__defaults__Ú__closure__)ÚfnÚnamer!   r!   r"   Úfunction_named±   s    
ÿ
r›   c                 O  sf   |   ¡ }z*||g|¢R i |¤Ž}|  ddd¡ |W S    t ¡ }| j|Ž }|sT‚ n| Y S Y n0 dS )a  Run 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__r?   Úexc_info)Úctxr™   ÚargÚkwrP   Úresultrž   Zraise_r!   r!   r"   Úrun_as_contextmanagerÅ   s    
r£   c                 C  s   dd„ | D ƒS )zŠConverts the results of sql execution into a plain set of column tuples.

    Useful for asserting the results of an unordered query.
    c                 S  s   h | ]}t |ƒ’qS r!   ©Útuple)Ú.0Úrowr!   r!   r"   Ú	<setcomp>ä   r/   zrowset.<locals>.<setcomp>r!   )Úresultsr!   r!   r"   ÚrowsetÞ   s    rª   c                 C  s   dsJ | ƒ‚d S )NFr!   )Úmsgr!   r!   r"   Úfailç   s    r¬   c              	   O  sÖ   ddl m} t ¡ }|d }t|ddƒ}||_zb| |i |¤ŽW | ¡  |jj}|rjt	||ƒ | 
¡  ¡  nt	|tjƒ ||_S t	|tjƒ ||_n@| ¡  |jj}|r¾t	||ƒ | 
¡  ¡  nt	|tjƒ ||_0 dS )ztProvide bound MetaData for a single test, dropping afterwards.

    Legacy; use the "metadata" pytest fixture.

    r	   )Úfixturesr   ÚmetadataN)Ú r­   r   r   Úgetattrr®   Zclose_all_sessionsÚbaseZ_connection_fixture_connectionÚdrop_all_tables_from_metadataZget_transactionÚcommitr
   Údb)r™   r    r¡   r­   r®   rp   Z	prev_metaZcfcr!   r!   r"   Úprovide_metadataë   s:    	
æ	     ï
rµ   c                    sT   t ƒ ‰ | D ]}ˆ  |¡ q
tˆ ƒ‰ tj‡ fdd„| D ƒddtˆ ƒ  d ˆ ¡dœŽS )aq  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),
        )
        def test_fn(lazy, passive, raiseload): ...

    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",
        )
        def test_fn(lazy, passive, raiseload): ...

    c                   s<   g | ]4‰ d   ‡ fdd„ˆD ƒ¡ft‡ fdd„ˆD ƒƒ ‘qS )Ú_c                 3  s   | ]}ˆ   |d ¡r|V  qdS ©FN©rB   ©r¦   rX   r*   r!   r"   Ú	<genexpr>?  r/   z/flag_combinations.<locals>.<listcomp>.<genexpr>c                 3  s   | ]}ˆ   |d ¡V  qdS r·   r¸   r¹   r*   r!   r"   rº   @  r/   )rJ   r¥   )r¦   ©Úkeysr*   r"   Ú
<listcomp>>  s   þÿz%flag_combinations.<locals>.<listcomp>rˆ   Úaú,)Zid_Úargnames)rl   ÚupdateÚsortedr
   Úcombinationsrs   rJ   )rÃ   r+   r!   r»   r"   Úflag_combinations  s    
ýùrÄ   c                   sP   t ˆƒ}ˆdd„ |d D ƒŽ }‡fdd„‰ tj‡ fdd„tt|ƒƒD ƒi |¤ŽS )Nc                 S  s   g | ]}t  ¡ ‘qS r!   )r   ZMock©r¦   r    r!   r!   r"   r½   K  r/   z'lambda_combinations.<locals>.<listcomp>r   c                   s   ‡‡ fdd„}dˆ  |_ |S )Nc                    s   ˆ f i | ¤Žˆ S r   r!   ©r¡   )Úlambda_arg_setsÚposr!   r"   ÚfixtureN  s    z<lambda_combinations.<locals>.create_fixture.<locals>.fixturezfixture_%3.3d)r   )rÈ   rÉ   )rÇ   )rÈ   r"   Úcreate_fixtureM  s    
z+lambda_combinations.<locals>.create_fixturec                   s   g | ]}ˆ |ƒf‘qS r!   r!   )r¦   rˆ   )rÊ   r!   r"   r½   U  r/   )r   r
   rÃ   r0   rs   )rÇ   r¡   r    Zarg_setsr!   )rÊ   rÇ   r"   Úlambda_combinationsH  s    ÿÿrË   c                   sN   t | ƒd }‡ fdd„|D ƒ}t| jƒ}| ˆ ¡ t | j|¡}|f i |¤ŽS )zÝGiven 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.

    r   c                   s   i | ]}|ˆ   |¡“qS r!   )rw   rÅ   rÆ   r!   r"   Ú
<dictcomp>c  r/   z"resolve_lambda.<locals>.<dictcomp>)r   Údictr–   rÁ   r“   r”   r•   )Z__fnr¡   Zpos_argsZpass_pos_argsZglbZnew_fnr!   rÆ   r"   Úresolve_lambdaY  s    	

rÎ   Úfunctionc                   s   ‡ fdd„}|S )z&Provide MetaData for a pytest fixture.c                   s   ‡ fdd„}t jˆd�|ƒS )Nc              	   3  sN   t  ¡  }| _z,ˆ | |ƒ}| tj¡ |V  W | tj¡ n| tj¡ 0 d S r   )r   r   r®   Z
create_allr
   r´   Údrop_all)rp   r®   r¢   ©r™   r!   r"   Úrun_ddln  s    
z3metadata_fixture.<locals>.decorate.<locals>.run_ddl)Zscope)r
   rÉ   )r™   rÒ   ©ÚddlrÑ   r"   Údecoratem  s    z"metadata_fixture.<locals>.decorater!   )rÔ   rÕ   r!   rÓ   r"   Úmetadata_fixturej  s    rÖ   c                    s   t ‡ fdd„ƒ}|S )ziForce the given table names to be dropped after test complete,
    isolating for foreign key cycles

    c                   sD   z&| |i |¤ŽW t tjttjƒˆ d� S t tjttjƒˆ d� 0 d S )N)Úinclude_names)Údrop_all_tablesr
   r´   r   )r™   r    r¡   ©Únamesr!   r"   Úgo„  s
    þzforce_drop_names.<locals>.gor   )rÚ   rÛ   r!   rÙ   r"   Úforce_drop_names~  s    rÜ   c                   @  s$   e Zd ZdZdd„ Zdd„ ZeZdS )Úadictz,Dict keys available as attributes.  Shadows.c                 C  s.   z
| | W S  t y(   t | |¡ Y S 0 d S r   )ÚKeyErrorrÍ   Ú__getattribute__)rp   Úkeyr!   r!   r"   rß   ‘  s    
zadict.__getattribute__c                   s   t ‡ fdd„|D ƒƒS )Nc                   s   g | ]}ˆ | ‘qS r!   r!   )r¦   rà   r}   r!   r"   r½   ˜  r/   z"adict.__call__.<locals>.<listcomp>r¤   )rp   r¼   r!   r}   r"   Ú__call__—  s    zadict.__call__N)r   r€   r�   Ú__doc__rß   rá   Úget_allr!   r!   r!   r"   rÝ   Ž  s   rÝ   c                   sb   ddl m‰  ‡ ‡fdd„}t|tƒsV| ¡ �}||ƒ W d   ƒ q^1 sJ0    Y  n||ƒ d S )Nr	   )Úenginesc                   sh   ˆ j  | ¡ | jjsZddlm} |jddd�� ˆ | ¡ W d   ƒ qd1 sN0    Y  n
ˆ | ¡ d S )Nr	   )Ú
assertionszCan't sort tablesF)Zassert_)Ztesting_reaperZprepare_for_drop_tablesÚdialectÚsupports_alterr¯   rå   Zexpect_warningsrÐ   )Ú
connectionrå   ©rä   r®   r!   r"   rÛ      s    ÿ*z)drop_all_tables_from_metadata.<locals>.go)r¯   rä   r`   r   Úbegin)r®   Zengine_or_connectionrÛ   rè   r!   ré   r"   r²   �  s    

(r²   r   c                 C  s2  |d urt |ƒ}|d ur.|dks(J dƒ‚|f}|  ¡ �æ}t|j|d�ƒD ]Ä\}}|rŽ|d urj|d |vrjqH| tt|d tƒ |d d�ƒ¡ qH|rH| jj	sœqH|D ]j\}}	|d ur¾|d |vr¾q t|d tƒ t
dtƒt
dtƒ|d d�}
| tt|
jjg|
jjg|	d	�ƒ¡ q qHW d   ƒ n1 �s$0    Y  d S )
Nr   z2consider_schemas and schema are mutually exclusive)Úconsider_schemasr	   r   r   ÚxÚy)rš   )rl   rê   ÚreversedZ%sort_tables_on_foreign_key_dependencyÚexecuter   r   r   ræ   rç   r   r   r   r   Úcrì   rí   )ÚengineZ	inspectorr   rë   r×   ÚconnZ	table_keyZfkcsZt_keyZfkcÚtbr!   r!   r"   rØ   ´  sZ    
þ
ÿÿÿ
þÿÿÿ
þûÿÿrØ   c                   s   t ‡ fdd„ƒ}|S )Nc                   s(   z| |i |¤ŽW ˆ   ¡  S ˆ   ¡  0 d S r   )Ú_clear)r™   r    r¡   ©Ú	event_clsr!   r"   rÕ   í  s
    þz!teardown_events.<locals>.decorater   )rö   rÕ   r!   rõ   r"   Úteardown_eventsì  s    r÷   c                   sL   dd„ }t tttttt|tttti‰ tƒ ‰tdƒ‰‡ ‡‡‡fdd„‰ˆ| ƒS )z�Returns the approximate memory footprint an object and all of its
    contents.

    source: https://code.activestate.com/recipes/577504/


    c                 S  s   t  |  ¡ ¡S r   )r   Úfrom_iterableÚitemsr*   r!   r!   r"   Údict_handler   s    z total_size.<locals>.dict_handlerr   c                   sb   t | ƒˆv rdS ˆ t | ƒ¡ t| ˆƒ}ˆ  ¡ D ],\}}t| |ƒr0|ttˆ|| ƒƒƒ7 } q^q0|S )Nr   )Úidr‚   r   rù   r`   ÚsumÚmap)ÚoÚsÚtypÚhandler©Zall_handlersZdefault_sizeÚseenÚsizeofr!   r"   r    s    

ztotal_size.<locals>.sizeof)r¥   ro   r>   r   rÍ   rl   Ú	frozensetr   )rþ   rú   r!   r  r"   Ú
total_size÷  s    	úr  c                 C  sV   | g}t ƒ }d}|rR| d¡}||u r0|d7 }qt|tƒr|rt|ƒ|g | }q|S )z�given a cache key tuple, counts how many instances of actual
    tuples are found.

    used to alert large jumps in cache key complexity.

    r   r	   )Úobjectrw   r`   r¥   r>   )ÚtupÚstackÚsentinelZnum_elementsr�   r!   r!   r"   Úcount_cache_key_tuples  s    


r  ra   r   )ÚsecondsÚcleanupc                 c  sh   t   ¡ }d V  t   ¡ | }|| krdz$|ƒ  W t d|d›d| › d�¡ nt d|d›d| › d�¡ 0 d S )Nztest took too long (z.4fz seconds > ú))Útimer
   Z	skip_test)r  r  ÚnowÚsecr!   r!   r"   Úskip_if_timeout3  s    ÿ
ÿr  )r	   )rÏ   )Nr   N)N)JÚ
__future__r   Úcollectionsr   r   Ú
contextlibrc   r   Ú	itertoolsr   r;   r)   rY   rD   r?   r   Útempfiler   r  r“   Útypingr   r¯   r
   r   r   rñ   r   r   r   r   r   r   r   r   ZsqlZsql.sqltypesr   Úutilr   r   r   r   r#   Z
gc_collectr$   r   r%   rW   r\   rj   rl   rk   r‹   r‘   r›   r£   rª   r¬   rµ   rÄ   rË   rÎ   rÖ   rÜ   rÍ   rÝ   r²   rØ   r÷   r  r  Úcontextmanagerr  r!   r!   r!   r"   Ú<module>
   s„   
&

	
+1
   û
8&