
    e 7                       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sd Z!e!xZ"Z#n
ej$        Z"d Z#d Z%d4dZ&d Z' G d de(          Z)d Z*d  Z+d! Z,d" Z-d# Z.d$ Z/ed%             Z0d& Z1d' Z2d( Z3d5d*Z4d+ Z5 G d, d-e6          Z7d. Z8	 	 	 d6d0Z9d1 Z:d2 Z;d3 Z<dS )7    )annotations)dequeN)chain)	getsizeof   )config)mock   )inspect)
Connection)Column)DropConstraint)	DropTable)ForeignKeyConstraint)MetaData)Tableschema)Integer	decorator)defaultdict)has_refcount_gc)inspect_getfullargspecc                 R    t          j                     t          j                     d S N)gccollect)argss    7lib/python3.11/site-packages/sqlalchemy/testing/util.pynon_refcount_gc_collectr!   )   s    



    c                     d S r    r$   r"   r    lazy_gcr%   2   s    r"   c               #     K   t                      } dd l}|                     |           | D ],t          d|j        dz             D ]j        fdfV  -d S )Nr   r   c                0                         |           S r   )dumps)dpickle_protocols    r    <lambda>zpicklers.<locals>.<lambda>?   s    7==H+E+E r"   )setpickleaddrangeHIGHEST_PROTOCOLloads)picklersr/   r+   r,   s     @@r    r4   r4   6   s      uuHMMMLL  F Fb&"9A"=>> 	F 	FH-!E!E!E!E!EEEEEE	FF Fr"   c                .    t          j        | |          S )N)k)randomchoices)
populationr6   s     r    random_choicesr:   B   s    >*****r"   c                    t          | t                    rt          | |          S | t          j        dd|z  z             z                      t          j                  t          d|          z  S )N10
   )
isinstancefloatrounddecimalDecimalto_integralROUND_FLOORpow)valueprecs     r    round_decimalrI   F   sl    % "UD!!! GOC#*$4555BB B r"   c                  2    e Zd Zd Zd Zd Zd Zd Zd ZdS )	RandomSetc                    t          t                              |                     }t          j        |           t          |          S r   )listr.   __iter__r7   shuffleiter)selfls     r    rN   zRandomSet.__iter__Q   s6    d##$$qAwwr"   c                    t          j        dt          |           dz
            }t          t                              |                     |         }|                     |           |S )Nr   r   )r7   randintlenrM   r.   rN   remove)rQ   indexitems      r    popzRandomSet.popV   sR    q#d))a-00CLL&&''.Dr"   c                R    t          t                              | |                    S r   )rK   r.   unionrQ   others     r    r[   zRandomSet.union\   s    4//000r"   c                R    t          t                              | |                    S r   )rK   r.   
differencer\   s     r    r_   zRandomSet.difference_   s    e44555r"   c                R    t          t                              | |                    S r   )rK   r.   intersectionr\   s     r    ra   zRandomSet.intersectionb   s     ))$66777r"   c                     t          |           S r   )rK   )rQ   s    r    copyzRandomSet.copye   s    r"   N)	__name__
__module____qualname__rN   rY   r[   r_   ra   rc   r$   r"   r    rK   rK   P   sn          
  1 1 16 6 68 8 8    r"   rK   c                    t          t                    }| D ] \  }}||                             |           !t          |          D ] \  }}||d         D ]}|||         v r  dS !dS )zATrue if the given sorting conforms to the given partial ordering.NFT)r   r.   r0   	enumerate)tuplessorted_elementsdepsparentchildinodens           r    conforms_partial_orderingrq   i   s     sD    V_--  4 $ 	 	AtAwuuu 	 tr"   c                    t          t                    | D ] \  }}|                             |           !fdt           |                    S )Nc              3  
  K   t          |           dk    rt          |           V  d S | D ]V}t          |                               |g          }|                    |                   s |          D ]
}|g|z   V  Wd S )Nr   )rU   rM   r.   r_   ra   )elementselemsubsetsub_ordering_all_orderingsedgess       r    rx   z-all_partial_orderings.<locals>._all_orderings|   s      x==Ax..       4 4X114&99**5;77 4(6v(>(> 4 4#f|33333	4 4r"   )r   r.   r0   rP   )ri   rt   rl   rm   rx   ry   s       @@r    all_partial_orderingsrz   w   sw    E ! !e    4 4 4 4 4 4 x(()))r"   c                    	 || _         n<# t          $ r/ t          j        | j        | j        || j        | j                  } Y nw xY w| 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.

    )rd   	TypeErrortypesFunctionType__code____globals____defaults____closure__)fnnames     r    function_namedr      s[    
 
 
 
Kr
 

 Is   
 6AAc                    |                                  }	  ||g|R i |}|                     ddd           |S #  t          j                    } | j        | }|s |cY S xY w)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)ctxr   argkwobjresultr   raise_s           r    run_as_contextmanagerr      s     --//C
C$#$$$$$T4&&&<>>x( 	MMMs	   $; $A!c                    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                ,    h | ]}t          |          S r$   tuple).0rows     r    	<setcomp>zrowset.<locals>.<setcomp>   s    ***3E#JJ***r"   r$   )resultss    r    rowsetr      s     +*'****r"   c                    J |             r   r$   )msgs    r    failr      s    #r"   c                x   ddl m} t          j                    }|d         }t	          |dd          }||_        	  | |i ||                                 |j        j        }|r7t          ||           |
                                                                 nt          |t          j                   ||_        S # |                                 |j        j        }|r7t          ||           |
                                                                 nt          |t          j                   ||_        w xY w)ztProvide bound MetaData for a single test, dropping afterwards.

    Legacy; use the "metadata" pytest fixture.

    r   )fixturesr   metadataN) r   r   r   getattrr   close_all_sessionsbase_connection_fixture_connectiondrop_all_tables_from_metadataget_transactioncommitr   db)r   r   r   r   r   rQ   	prev_metacfcs           r    provide_metadatar      sS      H7Dj$//IDM"r42 	##%%% m: 	? *(C888
 !!((****)(FI>>>!# 	##%%% m: 	? *(C888
 !!((****)(FI>>>!!!!!s   B= =A<D9c                     t                      | D ]}                    |           t                    t          j        fd| D             ddt                    z  z   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                    g | ]@d                      fdD                       ft          fdD                       z   AS )_c              3  H   K   | ]}                     |d           |V  dS FNgetr   r6   r*   s     r    	<genexpr>z/flag_combinations.<locals>.<listcomp>.<genexpr>  s4      99Aq%9a999999r"   c              3  D   K   | ]}                     |d           V  dS r   r   r   s     r    r   z/flag_combinations.<locals>.<listcomp>.<genexpr>  s/      22AEE!UOO222222r"   )joinr   )r   r*   keyss    @r    
<listcomp>z%flag_combinations.<locals>.<listcomp>  su     

 

 

  XX999999999;2222T222223

 

 

r"   rn   a,)id_argnames)r.   updatesortedr   combinationsrU   r   )r   r*   r   s     @r    flag_combinationsr      s    < 55D  A$<<D

 

 

 

 "

 

 


 3T?#$   r"   c                     t                     }  d |d         D              } fdt          j        fdt          t	          |                    D             i |S )Nc                4    g | ]}t          j                    S r$   )r	   Mock)r   r   s     r    r   z'lambda_combinations.<locals>.<listcomp>"  s     @ @ @ @ @ @r"   r   c                *      fd}d z  |_         |S )Nc                       di |          S )Nr$   r$   )r   lambda_arg_setsposs    r    fixturez<lambda_combinations.<locals>.create_fixture.<locals>.fixture%  s    "?((R((--r"   zfixture_%3.3d)rd   )r   r   r   s   ` r    create_fixturez+lambda_combinations.<locals>.create_fixture$  s6    	. 	. 	. 	. 	. 	. +S0r"   c                (    g | ]} |          fS r$   r$   )r   rn   r   s     r    r   z'lambda_combinations.<locals>.<listcomp>,  s&    	=	=	=1>>!
	=	=	=r"   )r   r   r   r1   rU   )r   r   r   arg_setsr   s   `   @r    lambda_combinationsr     s    !/22D @ @Q @ @ @AH     	=	=	=	=c(mm(<(<	=	=	=AC  r"   c                    t          |           d         }fd|D             }t          | j                  }|                               t	          j        | j        |          } |di |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                <    i | ]}|                     |          S r$   )rY   )r   r   r   s     r    
<dictcomp>z"resolve_lambda.<locals>.<dictcomp>:  s%    :::#S"&&++:::r"   r$   )r   dictr   r   r}   r~   r   )__fnr   pos_argspass_pos_argsglbnew_fns    `    r    resolve_lambdar   0  sy     &d++A.H:::::::M
t
 
 CJJrNNNs33F6""M"""r"   functionc                      fd}|S )z&Provide MetaData for a pytest fixture.c                L      fd} t          j                  |          S )Nc              3  &  K   t          j                    x}| _        	  | |          }|                    t          j                   |V  |                    t          j                   d S # |                    t          j                   w xY wr   )r   r   r   
create_allr   r   drop_all)rQ   r   r   r   s      r    run_ddlz3metadata_fixture.<locals>.decorate.<locals>.run_ddlE  s      '-'8'88Ht}-D(++##FI... !!&),,,,,!!&),,,,s   /A/ /!B)scope)r   r   )r   r   ddls   ` r    decoratez"metadata_fixture.<locals>.decorateD  s<    		- 		- 		- 		- 		- )v~C(((111r"   r$   )r   r   s   ` r    metadata_fixturer   A  s#    2 2 2 2 2 Or"   c                 ,     t            fd            }|S )ziForce the given table names to be dropped after test complete,
    isolating for foreign key cycles

    c                    	  | |i |t          t          j        t          t          j                             S # t          t          j        t          t          j                             w xY w)N)include_names)drop_all_tablesr   r   r   )r   r   r   namess      r    gozforce_drop_names.<locals>.go[  sc    	P2t?r??FIwvy'9'9OOOOOOFIwvy'9'9OOOOOs	   > 5A3r   )r   r   s   ` r    force_drop_namesr   U  s4     P P P P YP Ir"   c                  "    e Zd ZdZd Zd ZeZdS )adictz,Dict keys available as attributes.  Shadows.c                j    	 | |         S # t           $ r t                              | |          cY S w xY wr   )KeyErrorr   __getattribute__)rQ   keys     r    r   zadict.__getattribute__h  sH    	49 	4 	4 	4((s33333	4s   
 %22c                :     t           fd|D                       S )Nc                     g | ]
}|         S r$   r$   )r   r   rQ   s     r    r   z"adict.__call__.<locals>.<listcomp>o  s    000Cd3i000r"   r   )rQ   r   s   ` r    __call__zadict.__call__n  s&    00004000111r"   N)rd   re   rf   __doc__r   r   get_allr$   r"   r    r   r   e  s9        664 4 42 2 2 GGGr"   r   c                     ddl m  fd}t          |t                    s:|                                5 } ||           d d d            d S # 1 swxY w Y   d S  ||           d S )Nr   )enginesc                   j                             |            | j        j        sMddlm} |                    dd          5                      |            d d d            d S # 1 swxY w Y   d S                     |            d S )Nr   )
assertionszCan't sort tablesF)assert_)testing_reaperprepare_for_drop_tablesdialectsupports_alterr   r   expect_warningsr   )
connectionr   r   r   s     r    r   z)drop_all_tables_from_metadata.<locals>.gow  s    66zBBB!0 	*$$$$$$++#U ,   . . !!*---. . . . . . . . . . . . . . . . . .
 j)))))s   A((A,/A,)r   r   r?   r   begin)r   engine_or_connectionr   r   r   s   `   @r    r   r   t  s    * * * * * * *J77 !!'')) 	ZBzNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     s   AAAr   c                4   |t          |          }||dk    s
J d            |f}|                                 5 }t          |                    |                    D ]\  }}|rX||d         |vr|                    t          t          |d         t                      |d                                        `|r| j        j	        so|D ]\  }}	||d         |vrt          |d         t                      t          dt                    t          dt                    |d                   }
|                    t          t          |
j        j        g|
j        j        g|		                                	 d d d            d S # 1 swxY w Y   d S )
Nr   z2consider_schemas and schema are mutually exclusive)consider_schemasr   r   r   xy)r   )r.   r   reversed%sort_tables_on_foreign_key_dependencyexecuter   r   r   r   r   r   r   r   r   cr  r  )engine	inspectorr   r  r   conn	table_keyfkcst_keyfkctbs              r    r   r     s6     M** $
 
 
 
?
 
 
 #9	 %4';;!1 <   
  
 $	 $	OIt
  !-!!M99ilHJJy|LLL    
  ~4 "&  JE3%1!!HM99 a 

sG,,sG,,$Qx  B LL&0"$&BDF8#NNN    A$	% % % % % % % % % % % % % % % % % %s   EFFFc                ,     t            fd            }|S )Nc                p    	  | |i |                                  S #                                   w xY wr   )_clear)r   r   r   	event_clss      r    r   z!teardown_events.<locals>.decorate  sD    	2s>b>>Is    5r   )r  r   s   ` r    teardown_eventsr    s-        Y Or"   c                    d }t           t          t          t          t          t          t          |t
          t          t          t          it                      t          d          fd |           S )zReturns the approximate memory footprint an object and all of its
    contents.

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


    c                N    t          j        |                                           S r   )r   from_iterableitems)r*   s    r    dict_handlerz total_size.<locals>.dict_handler  s    "17799---r"   r   c           
     <   t          |           v rdS                     t          |                      t          |           }                                D ]@\  }}t	          | |          r+|t          t           ||                               z  } nA|S )Nr   )idr0   r   r  r?   summap)ostyphandlerall_handlersdefault_sizeseensizeofs       r    r'  ztotal_size.<locals>.sizeof  s    a55D==1Aa&&(..00 	 	LC!S!! SVWWQZZ00111 r"   )r   rP   rM   r   r   r.   	frozensetr   )r   r  r$  r%  r&  r'  s     @@@@r    
total_sizer)    s    . . . 	tdtlT4L 55DQ<<L
 
 
 
 
 
 
 
 6!99r"   c                    | g}t                      }d}|rN|                    d          }||u r|dz  }n-t          |t                    r|rt	          |          |gz   |z   }|N|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   )objectrY   r?   r   rM   )tupstacksentinelnum_elementsru   s        r    count_cache_key_tuplesr0    s     EExxHL
 8yy||8ALLe$$ 	8 8T

hZ/%7  8 r"   )r   )r   )Nr   N)=
__future__r   collectionsr   rB   r   	itertoolsr   r7   r   r   r}   r   r   r	   r   r
  r   r   r   r   r   r   r   r   sqlsql.sqltypesr   utilr   r   r   r   r!   
gc_collectr%   r   r4   r:   rI   r.   rK   rq   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r)  r0  r$   r"   r    <module>r8     s   # " " " " "        				        



                                      # # # # # #       ) ) ) ) ) )                   " " " " " "             " " " " " " ) ) ) ) ) )     32J J  	F 	F 	F+ + + +         2  * * *$  (  2+ + +   (" (" ("V- - -`  "# # #"   (       D   ! ! !4 5 5 5 5p  # # #L    r"   