o
    g8                     @  s>  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
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#sdd Z%e% Z&Z'nej(Z&dd Z'dd Z)dQdd Z*d!d" Z+G d#d$ d$e,Z-d%d& Z.d'd( Z/d)d* Z0d+d, Z1d-d. Z2d/d0 Z3e!d1d2 Z4d3d4 Z5d5d6 Z6d7d8 Z7dRd:d;Z8d<d= Z9G d>d? d?e:Z;d@dA Z<		B	dSdCdDZ=dEdF Z>dGdH Z?dIdJ Z@ejAdBdTdOdPZBdS )U    )annotations)dequeN)chain)	getsizeof)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   Y/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/testing/util.pynon_refcount_gc_collect,   s   r!   c                   C  s   d S r   r   r   r   r   r    lazy_gc5   s   r"   c                  #  sT    t  } dd l}| | | D ] td|jd D ] j fddfV  qqd S )Nr   r   c                   s     | S r   )dumpsdpickle_protocolr   r    <lambda>B   s    zpicklers.<locals>.<lambda>)setpickleaddrangeHIGHEST_PROTOCOLloads)picklersr,   r   r'   r    r1   9   s   
r1   c                 C  s   t j| |dS )N)k)randomchoices)
populationr2   r   r   r    random_choicesE      r6   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_decimalI   s   

rE   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__r3   shuffleiter)selflr   r   r    rH   T   s   
zRandomSet.__iter__c                 C  s4   t dt| d }tt| | }| | |S )Nr   r   )r3   randintlenrG   r+   rH   remove)rK   indexitemr   r   r    popY   s   
zRandomSet.popc                 C     t t| |S r   )rF   r+   unionrK   otherr   r   r    rT   _      zRandomSet.unionc                 C  rS   r   )rF   r+   
differencerU   r   r   r    rX   b   rW   zRandomSet.differencec                 C  rS   r   )rF   r+   intersectionrU   r   r   r    rY   e   rW   zRandomSet.intersectionc                 C  s   t | S r   )rF   rK   r   r   r    copyh      zRandomSet.copyN)	__name__
__module____qualname__rH   rR   rT   rX   rY   r[   r   r   r   r    rF   S   s    rF   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   r+   r-   	enumerate)tuplessorted_elementsdepsparentchildinodenr   r   r    conforms_partial_orderingl   s   ri   c                   s>   t t| D ]\}}| | q fdd t |S )Nc                 3  sb    t | dkrt| V  d S | D ]}t| |g}|| s. |D ]}|g| V  q%qd S )Nr   )rN   rG   r+   rX   rY   )elementselemsubsetsub_ordering_all_orderingsedgesr   r    ro      s   z-all_partial_orderings.<locals>._all_orderings)r   r+   r-   rJ   )ra   rj   rd   re   r   rn   r    all_partial_orderingsz   s
   
rq   c              
   C  s<   z|| _ W | S  ty   t| j| j|| j| j} Y | S 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.

    )r]   	TypeErrortypesFunctionType__code____globals____defaults____closure__)fnnamer   r   r    function_named   s   r{   c                 O  sZ   |   }z||g|R i |}| ddd |W S    t }| j| }|s) | Y 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)ctxry   argkwobjresultr   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>       zrowset.<locals>.<setcomp>r   )resultsr   r   r    rowset   s   r   c                 C  s   J | r   r   )msgr   r   r    fail   r\   r   c              	   O  s   ddl m} t }|d }t|dd}||_z*| |i |W |  |jj}|r8t	|| |
   ||_S t	|tj ||_S |  |jj}|r[t	|| |
   ||_w t	|tj ||_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)ry   r   r   r   r   rK   	prev_meta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 | ] 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   r2   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>rf   a,)id_argnames)r+   updatesortedr   combinationsrN   r   )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	   Mockr   r   r   r   r    r   &  r   z'lambda_combinations.<locals>.<listcomp>r   c                   s    fdd}d  |_ |S )Nc                    s    di |  S )Nr   r   r   )lambda_arg_setsposr   r    fixture)  s   z<lambda_combinations.<locals>.create_fixture.<locals>.fixturezfixture_%3.3d)r]   )r   r   )r   )r   r    create_fixture(  s   
z+lambda_combinations.<locals>.create_fixturec                   s   g | ]} |fqS r   r   )r   rf   )r   r   r    r   0  s    )r   r   r   r.   rN   )r   r   r   arg_setsr   )r   r   r    lambda_combinations#  s   r   c                   sN   t | d } fdd|D }t| j}|  t| 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                   s   i | ]}|  |qS r   )rR   r   r   r   r    
<dictcomp>>  s    z"resolve_lambda.<locals>.<dictcomp>Nr   )r   dictrv   r   rs   rt   ru   )__fnr   pos_argspass_pos_argsglbnew_fnr   r   r    resolve_lambda4  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 d S |tj w r   )r   r   r   
create_allr   r   drop_all)rK   r   r   ry   r   r    run_ddlI  s   
z3metadata_fixture.<locals>.decorate.<locals>.run_ddl)scope)r   r   )ry   r   ddlr   r    decorateH  s   z"metadata_fixture.<locals>.decorater   )r   r   r   r   r    metadata_fixtureE  s   r   c                       t  fdd}|S )ziForce the given table names to be dropped after test complete,
    isolating for foreign key cycles

    c                   s@   z| |i |W t tjttj d S t tjttj d w )N)include_names)drop_all_tablesr   r   r   )ry   r   r   namesr   r    go_  s   0zforce_drop_names.<locals>.gor   )r   r   r   r   r    force_drop_namesY  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 w r   )KeyErrorr   __getattribute__)rK   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   rZ   r   r    r   s  r   z"adict.__call__.<locals>.<listcomp>r   )rK   r   r   rZ   r    __call__r  s   zadict.__call__N)r]   r^   r_   __doc__r   r   get_allr   r   r   r    r   i  s
    r   c                   sf   ddl m   fdd}t|ts-| }|| W d    d S 1 s&w   Y  d S || d S )Nr   )enginesc                   sl    j |  | jjs/ddlm} |jddd |  W d    d S 1 s(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   r   r    r   {  s   "z)drop_all_tables_from_metadata.<locals>.go)r   r   r;   r   begin)r   engine_or_connectionr   r   r   r   r    r   x  s   


"r   r   c                 C  s2  |d urt |}|d ur|dksJ d|f}|  t}t|j|dD ]b\}}|rG|d ur5|d |vr5q$|tt|d t |d d q$|r| jj	sNq$|D ]5\}}	|d ur_|d |vr_qPt|d t t
dtt
dt|d d}
|tt|
jjg|
jjg|	d	 qPq$W d    d S 1 sw   Y  d S )
Nr   z2consider_schemas and schema are mutually exclusive)consider_schemasr   r   r   xy)rz   )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tbr   r   r    r     sX   

"r   c                   r   )Nc                   s$   z| |i |W     S     w r   )_clear)ry   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 )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  r7   z total_size.<locals>.dict_handlerr   c                   sd   t | v rdS t |  t| }  D ]\}}t| |r/|tt|| 7 } |S q|S )Nr   )idr-   r   r   r;   summap)ostyphandlerall_handlersdefault_sizeseensizeofr   r    r    s   

ztotal_size.<locals>.sizeof)r   rJ   rG   r   r   r+   	frozensetr   )r   r   r   r  r    
total_size  s   	r	  c                 C  sX   | g}t  }d}|r*|d}||u r|d7 }nt|tr(|r(t||g | }|s
|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   )objectrR   r;   r   rG   )tupstacksentinelnum_elementsrk   r   r   r    count_cache_key_tuples  s   


r  secondsr<   cleanupr   c                 c  sl    t   }d V  t   | }|| kr4z|  W td|dd|  d d S td|dd|  d w d S )Nztest took too long (z.4fz seconds > ))timer   	skip_test)r  r  nowsecr   r   r    skip_if_timeout  s   r  )r   )r   )Nr   N)r  r<   r  r   )C
__future__r   collectionsr   
contextlibr>   r   	itertoolsr   r3   r~   r   r  rs   typingr   r   r   r	   r   r   r   r   r   r   r   r   r   r   sqlsql.sqltypesr   utilr   r   r   r   r!   
gc_collectr"   r   r1   r6   rE   r+   rF   ri   rq   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  contextmanagerr  r   r   r   r    <module>   sz   	


	
+1

8&