U
    dT7                     @  s  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Zd dl	Z	d dl	m
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  es0dd Z!e! Z"Z#nej$Z"dd Z#dd Z%dJddZ&d d! Z'G d"d# d#e(Z)d$d% Z*d&d' Z+d(d) Z,d*d+ Z-d,d- Z.d.d/ Z/ed0d1 Z0d2d3 Z1d4d5 Z2d6d7 Z3dKd9d:Z4d;d< Z5G d=d> d>e6Z7d?d@ Z8dLdBdCZ9dDdE Z:dFdG Z;dHdI Z<dS )M    )annotations)dequeN)chain)	getsizeof   )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   ;/tmp/pip-unpacked-wheel-wbr0uges/sqlalchemy/testing/util.pynon_refcount_gc_collect)   s    r    c                   C  s   d S r   r   r   r   r   r   lazy_gc2   s    r!   c                  #  sR   t  } dd l}| | | D ]0 td|jd D ] j fddfV  q0qd S )Nr   r   c                   s     | S r   )dumpsdZpickle_protocolr   r   <lambda>?       zpicklers.<locals>.<lambda>)setpickleaddrangeHIGHEST_PROTOCOLloads)picklersr+   r   r&   r   r0   6   s    
r0   c                 C  s   t j| |dS )N)k)randomchoices)
populationr1   r   r   r   random_choicesB   s    r5   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_decimalF   s    

rC   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   )listr*   __iter__r2   shuffleiter)selflr   r   r   rF   Q   s    
zRandomSet.__iter__c                 C  s4   t dt| d }tt| | }| | |S )Nr   r   )r2   randintlenrE   r*   rF   remove)rI   indexitemr   r   r   popV   s    
zRandomSet.popc                 C  s   t t| |S r   )rD   r*   unionrI   otherr   r   r   rQ   \   s    zRandomSet.unionc                 C  s   t t| |S r   )rD   r*   
differencerR   r   r   r   rT   _   s    zRandomSet.differencec                 C  s   t t| |S r   )rD   r*   intersectionrR   r   r   r   rU   b   s    zRandomSet.intersectionc                 C  s   t | S r   )rD   rI   r   r   r   copye   s    zRandomSet.copyN)	__name__
__module____qualname__rF   rP   rQ   rT   rU   rW   r   r   r   r   rD   P   s   rD   c                 C  s`   t t}| D ]\}}|| | qt|D ].\}}||d D ]}||| kr@  dS q@q,dS )zATrue if the given sorting conforms to the given partial ordering.NFT)r   r*   r,   	enumerate)tuplesZsorted_elementsdepsparentchildinodenr   r   r   conforms_partial_orderingi   s    rc   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   )rL   rE   r*   rT   rU   )elementselemZsubsetZsub_ordering_all_orderingsedgesr   r   rg   |   s    z-all_partial_orderings.<locals>._all_orderings)r   r*   r,   rH   )r\   rd   r^   r_   r   rf   r   all_partial_orderingsw   s
    ri   c              
   C  s>   z
|| _ W n. tk
r8   t| j| j|| j| j} Y nX | 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.

    )rX   	TypeErrortypesFunctionType__code____globals____defaults____closure__)fnnamer   r   r   function_named   s    
    
rs   c                 O  s`   |   }z$||f||}| ddd |W S    t }| j| }|sN n| Y S Y nX 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__sysexc_info)ctxrq   argkwobjresultrw   Zraise_r   r   r   run_as_contextmanager   s    
r}   c                 C  s   dd | D S )zConverts 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>   s     zrowset.<locals>.<setcomp>r   )resultsr   r   r   rowset   s    r   c                 C  s   dst | d S )NF)AssertionError)msgr   r   r   fail   s    r   c              	   O  s   ddl m} t }|d }t|dd}||_z| ||W S |  |jrht||j |j	 
  nt|tj ||_X 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_sessionsZ_connection_fixture_connectiondrop_all_tables_from_metadataZget_transactioncommitr   db)rq   r   rz   r   r   rI   Z	prev_metar   r   r   provide_metadata   s     	 r   c                    sT   t   | D ]} | q
t  tj fdd| D ddt   d dS )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'
        )

    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getr   r1   r$   r   r   	<genexpr>  s      z/flag_combinations.<locals>.<listcomp>.<genexpr>c                 3  s   | ]}  |d V  qdS r   r   r   r$   r   r   r     s     )joinr   )r   keysr$   r   
<listcomp>  s   z%flag_combinations.<locals>.<listcomp>r`   a,)Zid_argnames)r*   updatesortedr   combinationsrL   r   )r   r%   r   r   r   flag_combinations   s    
r   c                   sL   t }dd |d D  }fdd tj fddtt|D |S )Nc                 S  s   g | ]}t  qS r   )r   ZMockr   ry   r   r   r   r   $  s     z'lambda_combinations.<locals>.<listcomp>r   c                   s    fdd}d  |_ |S )Nc                    s    f |  S r   r   rz   )lambda_arg_setsposr   r   fixture'  s    z<lambda_combinations.<locals>.create_fixture.<locals>.fixturezfixture_%3.3d)rX   )r   r   )r   )r   r   create_fixture&  s    
z+lambda_combinations.<locals>.create_fixturec                   s   g | ]} |fqS r   r   )r   r`   )r   r   r   r   .  s     )r   r   r   r-   rL   )r   rz   r   Zarg_setsr   )r   r   r   lambda_combinations!  s     r   c                   sJ   t | d } fdd|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.

    r   c                   s   i | ]}|  |qS r   )rP   r   r   r   r   
<dictcomp><  s      z"resolve_lambda.<locals>.<dictcomp>)r   dictrn   r   rk   rl   rm   )Z__fnrz   Zpos_argsZpass_pos_argsZglbZnew_fnr   r   r   resolve_lambda2  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  sB   t   }| _z  | |}|tj |V  W 5 |tj X d S r   )r   r   r   drop_allr   r   Z
create_all)rI   r   r|   rq   r   r   run_ddlG  s    

z3metadata_fixture.<locals>.decorate.<locals>.run_ddl)Zscope)r   r   )rq   r   ddlr   r   decorateF  s    z"metadata_fixture.<locals>.decorater   )r   r   r   r   r   metadata_fixtureC  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                   s,   z| ||W S t tjttj d X d S )N)include_names)drop_all_tablesr   r   r
   )rq   r   rz   namesr   r   go^  s    zforce_drop_names.<locals>.gor   )r   r   r   r   r   force_drop_namesX  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  s0   z
| | W S  t k
r*   t| | Y S X d S r   )KeyErrorr   __getattribute__)rI   keyr   r   r   r   l  s    
zadict.__getattribute__c                   s   t  fdd|D S )Nc                   s   g | ]} | qS r   r   )r   r   rV   r   r   r   s  s     z"adict.__call__.<locals>.<listcomp>r~   )rI   r   r   rV   r   __call__r  s    zadict.__call__N)rX   rY   rZ   __doc__r   r   get_allr   r   r   r   r   i  s   r   c              	     sN   ddl m   fdd}t|tsB| }|| W 5 Q R X n|| d S )Nr   )enginesc              	     sT    j |  | jjsFddlm} |jddd |  W 5 Q R X 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   r9   r   begin)r   Zengine_or_connectionr   r   r   r   r   r   x  s    

r   r   c                 C  s  |d k	rt |}|d k	r.|dks(td|f}|  }t|j|dD ]\}}|r|d k	rj|d |krjqH|tt|d t |d d qH|rH| j	j
sqH|D ]j\}}	|d k	r|d |krqt|d t tdttdt|d d}
|tt|
jjg|
jjg|	d	 qqHW 5 Q R X d S )
Nr   z2consider_schemas and schema are mutually exclusive)consider_schemasr   r   r   xy)rr   )r*   r   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| ||W S     X d S r   )_clear)rq   ry   rz   	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 )zReturns 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 | krdS t |  t| }  D ],\}}t| |r0|tt|| 7 } q^q0|S )Nr   )idr,   r   r   r9   summap)ostyphandlerZall_handlersZdefault_sizeseensizeofr   r   r     s    

ztotal_size.<locals>.sizeof)r   rH   rE   r   r   r*   	frozensetr   )r   r   r   r   r   
total_size  s$    	      r   c                 C  sV   | g}t  }d}|rR|d}||kr0|d7 }qt|tr|rt||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.

    r   r   )objectrP   r9   r   rE   )tupstacksentinelZnum_elementsre   r   r   r   count_cache_key_tuples  s    


r   )r   )r   )Nr   N)=
__future__r   collectionsr   r<   r   	itertoolsr   r2   rv   r   rk   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   r0   r5   rC   r*   rD   rc   ri   rs   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>
   sn   


	
,0
   
9&