
    +h6                        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 ddlmZ esd Z e xZ!Z"nejF                  Z!d Z"d Z$erd3dZ%nd3dZ%d Z& G d de'      Z(d  Z)d! Z*d" Z+d# Z,d$ Z-d% Z.ed&        Z/d' Z0d( Z1d) Z2d4d*Z3d+ Z4 G d, d-e5      Z6d. Z7d5d/Z8d0 Z9d1 Z:d2 Z;y)6    )dequeN)chain)	getsizeof   )config)mock   )inspect)
Connection)Column)DropConstraint)	DropTable)ForeignKeyConstraint)MetaData)Tableschema)Integer	decorator)defaultdict)has_refcount_gc)inspect_getfullargspec)py2kc                  T    t        j                          t        j                          y N)gccollect)argss    J/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/testing/util.pynon_refcount_gc_collectr!   &   s    





    c                       y r    r$   r"   r    lazy_gcr%   /   s    r"   c               #     K   t               } t        r	 dd l}| j                  |       dd l}| j                  |       | D ]1  t        d|j                        D ]  j                  fdf  3 y # t        $ r Y Ww xY ww)Nr   c                 (    j                  |       S r   )dumps)dpickle_protocols    r    <lambda>zpicklers.<locals>.<lambda>D   s    7==H+E r"   )	setr   cPickleaddImportErrorpicklerangeHIGHEST_PROTOCOLloads)picklersr/   r2   r+   r,   s      @@r    r6   r6   3   s     uH	LL! LL  Fb&"9"9: 	FH--!EEE	FF  		s(   BA7 AB7	B BBBc                 L    t        |       }t        j                  |       |d| S Nr   )listrandomshuffle)
populationkpops      r    random_choicesr?   I   s#    :s1Qxr"   c                 0    t        j                  | |      S )N)r=   )r:   choices)r<   r=   s     r    r?   r?   R   s    ~~jA..r"   c                     t        | t              rt        | |      S | t        j                  dd|z  z         z  j                  t        j                        t        d|      z  S )N10
   )
isinstancefloatrounddecimalDecimalto_integralROUND_FLOORpow)valueprecs     r    round_decimalrP   V   s\    %UD!! GOOC#*$455BBB r"   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)	RandomSetc                 ~    t        t        j                  |             }t        j                  |       t        |      S r   )r9   r.   __iter__r:   r;   iter)selfls     r    rT   zRandomSet.__iter__a   s*    d#$qAwr"   c                     t        j                  dt        |       dz
        }t        t        j                  |             |   }| j                  |       |S )Nr   r   )r:   randintlenr9   r.   rT   remove)rV   indexitems      r    r>   zRandomSet.popf   sC    q#d)a-0CLL&'.Dr"   c                 @    t        t        j                  | |            S r   )rR   r.   unionrV   others     r    r_   zRandomSet.unionl   s    4/00r"   c                 @    t        t        j                  | |            S r   )rR   r.   
differencer`   s     r    rc   zRandomSet.differenceo   s    e455r"   c                 @    t        t        j                  | |            S r   )rR   r.   intersectionr`   s     r    re   zRandomSet.intersectionr   s    ))$677r"   c                     t        |       S r   )rR   )rV   s    r    copyzRandomSet.copyu   s    r"   N)	__name__
__module____qualname__rT   r>   r_   rc   re   rg   r$   r"   r    rR   rR   `   s     
168r"   rR   c                     t        t              }| D ]  \  }}||   j                  |        t        |      D ]  \  }}||d D ]  }|||   v s  y  y)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_orderingru   y   st     sD  V _- 4 $ 	AtAw	
 r"   c                     t        t              | D ]  \  }}|   j                  |        fdt         |            S )Nc              3      K   t        |       dk(  rt        |        y | D ]G  }t        |       j                  |g      }|j	                  |         r3 |      D ]
  }|g|z     I y w)Nr   )rZ   r9   r.   rc   re   )elementselemsubsetsub_ordering_all_orderingsedgess       r    r|   z-all_partial_orderings.<locals>._all_orderings   su     x=Ax.   4X114&9**5;7(6v(> 4#f|3344s   AA-A-)r   r.   r0   rU   )rm   rx   rp   rq   r|   r}   s       @@r    all_partial_orderingsr~      sI    E !e !	4 x())r"   c                     	 || _         | S # t        $ rE t        j                  | j                  | j
                  || j                  | j                        } Y | S w xY w)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.

    )rh   	TypeErrortypesFunctionType__code____globals____defaults____closure__)fnnames     r    function_namedr      sX    

 I	  
KKr
 I	
s    A
AAc                     | j                         }	  ||g|i |}| j                  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      sj     --/C
C$#$$T4&<<>x(Ms	    3 *Ac                 >    | D ch c]  }t        |       c}S c c}w )zConverts the results of sql execution into a plain set of column tuples.

    Useful for asserting the results of an unordered query.
    tuple)resultsrows     r    rowsetr      s     #**3E#J***s   c                     J |        r   r$   )msgs    r    failr      s    #5r"   c                    ddl m} t        j                         }|d   }t	        |dd      }||_        	  | |i ||j                          |j                  rFt        ||j                         |j                  j                         j                          ||_        S t        |t        j                         ||_        S # |j                          |j                  rFt        ||j                         |j                  j                         j                          ||_        w 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_connection_fixture_connectiondrop_all_tables_from_metadataget_transactioncommitr   db)r   r   r   r   r   rV   	prev_metas          r    provide_metadatar      s     H7Dj$/IDM"42 	$$& 22 *(AA 33CCELLN " *(FII>!% 	$$& 22 *(AA 33CCELLN " *(FII>!s   C   BEc            
      J   t               }| D ]  |j                          t        |      }t        j                  | D cg c]2  dj                  fd|D              ft        fd|D              z   4 c}ddt        |      z  z   dj                  |      dS c c}w )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              3   H   K   | ]  }j                  |d       s|  ywFNget.0r=   r*   s     r    	<genexpr>z$flag_combinations.<locals>.<genexpr>(  s     9Aq%a9s   ""c              3   B   K   | ]  }j                  |d         ywr   r   r   s     r    r   z$flag_combinations.<locals>.<genexpr>)  s     2AEE!UO2s   rr   a,)id_argnames)r.   updatesortedr   combinationsjoinr   rZ   )r   keysr*   s     `r    flag_combinationsr     s    < 5D A $<D "

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


 3T?#$ 

s   7B c                      t               }  |d   D cg c]  }t        j                          c} } fd}t        j                  t        t        |            D cg c]  } ||      f c}i |S c c}w c c}w )Nr   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>.fixture7  s    "(R(--r"   zfixture_%3.3d)rh   )r   r   r   s   ` r    create_fixturez+lambda_combinations.<locals>.create_fixture6  s    	. +S0r"   )r   r   Mockr   r   r3   rZ   )r   r   r   r   arg_setsr   rr   s   `      r    lambda_combinationsr   1  sx    !/2DQ @ @AH (-c(m(<	=1>!
	=AC  !A 
>s   A6A;c                    t        |       d   }|D ci c]  }||j                  |       }}t        | j                        }|j	                  |       t        j                  | j                  |      } |di |S c c}w )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   r$   )r   r>   dictr   r   r   r   r   )__fnr   pos_argsr   pass_pos_argsglbnew_fns          r    resolve_lambdar   B  sw     &d+A.H19:#S"&&+%:M:
t
 CJJrNs3F"M""	 ;s   A<c                       fd}|S )z&Provide MetaData for a pytest fixture.c                 H      fd} t        j                        |      S )Nc              3   $  K   t        j                         x}| _        	  | |      }|j                  t        j
                         | |j                  t        j
                         y # |j                  t        j
                         w xY wwr   )r   r   r   
create_allr   r   drop_all)rV   r   r   r   s      r    run_ddlz3metadata_fixture.<locals>.decorate.<locals>.run_ddlW  sh     '-'88Ht}-D(+##FII. !!&)),!!&)),s   B,A,  B,!BB)scope)r   r   )r   r   ddls   ` r    decoratez"metadata_fixture.<locals>.decorateV  s     
	- )v~~C(11r"   r$   )r   r   s   ` r    metadata_fixturer   S  s    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>.gon  sG    	Pt?r?FIIwvyy'9OOFIIwvyy'9Os	   > 5A3r   )r   r   s   ` r    force_drop_namesr   h  s      P P Ir"   c                        e Zd ZdZd Zd ZeZy)adictz,Dict keys available as attributes.  Shadows.c                 X    	 | |   S # t         $ r t        j                  | |      cY S w xY wr   )KeyErrorr   __getattribute__)rV   keys     r    r   zadict.__getattribute__|  s3    	49 	4((s33	4s    ))c                 D    t        |D cg c]  }| |   	 c}      S c c}w r   r   )rV   r   r   s      r    __call__zadict.__call__  s    40Cd3i0110s   N)rh   ri   rj   __doc__r   r   get_allr$   r"   r    r   r   y  s    642 Gr"   r   c                      ddl m  fd}t        |t              s"|j	                         5 } ||       d d d        y  ||       y # 1 sw Y   y xY w)Nr   )enginesc                 
   j                   j                  |        | j                  j                  s4ddlm} |j                  dd      5  j                  |        d d d        y j                  |        y # 1 sw Y   y xY w)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>.go  s{    66zB!!00$++#U ,  . !!*-. .
 j). .s   A99B)r   r   rF   r   begin)r   engine_or_connectionr   r   r   s   `   @r    r   r     sR    * *J7!'') 	ZzN	 	 	 	 	s   	A

Ac                 ~   |t        |      }| j                         5 }t        |j                  |            D ]  \  }}|r6|||vr|j	                  t        t        |t               |                   >|sA| j                  j                  sX|D ]  \  }}|||vrt        |t               t        dt              t        dt              |      }|j	                  t        t        |j                  j                  g|j                  j                   g|                     	 d d d        y # 1 sw Y   y xY w)Nr   xy)r   )r.   r   reversedget_sorted_table_and_fkc_namesexecuter   r   r   r   r   r   r   r   r   cr   r   )	engine	inspectorr   r   conntnamefkcsfkctbs	            r    r   r     s'    M*	 4#44F4C
 	KE4  ,m1KeE8:fEF ~~44"& JE3%1!6  
sG,sG,%B LL&0"$$&&BDDFF8#N	  s   AD3;B.D33D<c                 $     t          fd       }|S )Nc                 `    	  | |i |j                          S # j                          w xY wr   )_clear)r   r   r   	event_clss      r    r   z!teardown_events.<locals>.decorate  s,    	s>b>Is    -r   )r  r   s   ` r    teardown_eventsr    s      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                 H    t        j                  | j                               S r   )r   from_iterableitems)r*   s    r    dict_handlerz total_size.<locals>.dict_handler  s    ""1779--r"   r   c           
          t        |       v ryj                  t        |              t        |       }j                         D ]2  \  }}t	        | |      s|t        t         ||                   z  } |S  |S r8   )idr0   r   r  rF   summap)ostyphandlerall_handlersdefault_sizeseensizeofs       r    r  ztotal_size.<locals>.sizeof  s{    a5D=Aa&(..0 	LC!S!SVWQZ011		 r"   )r   rU   r9   r   r   r.   	frozensetr   )r  r  r  r  r  r  s     @@@@r    
total_sizer    sR    . 	tdtlT4L 5DQ<L
 !9r"   c                     | g}t               }d}|rB|j                  d      }||u r|dz  }n$t        |t              r|rt	        |      |gz   |z   }|rB|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   )objectr>   rF   r   r9   )tupstacksentinelnum_elementsry   s        r    count_cache_key_tuplesr&    sh     EExHL
yy|8ALe$T
hZ/%7  r"   )r   )function)NN)<collectionsr   rI   r   	itertoolsr   r:   r   r   r   r   r   r   r
   r  r   r   r   r   r   r   r   r   sqlsql.sqltypesr   utilr   r   r   r   r   r!   
gc_collectr%   r   r6   r?   rP   r.   rR   ru   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r&  r$   r"   r    <module>r.     s     	   
        #  )    "   " )   32J JF( / 2*&(2+ )" )"X-`"#"*"D !.#L#Lr"   