o
    iCM                     @   s.  d dl m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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 Zejdd Zdd Zdd Zdd Zdd Z ej	 dUd!d"Z!d#d$ Z"d a#d%d& Z$dVd'd(Z%dVd)d*Z&dVd+d,Z'dVd-d.Z(dVd/d0Z)dVd1d2Z*dVd3d4Z+dVd5d6Z,dVd7d8Z-dVd9d:Z.dVd;d<Z/dVd=d>Z0dVd?d@Z1dAdB Z2dCdD Z3dEdF Z4dGdH Z5dIdJ Z6dKdL Z7	 dWdMdNZ8G dOdP dPe9Z:G dQdR dRe9Z;G dSdT dTe9Z<dS )X    )absolute_importN   )	assertsql)config)mock)util)db_spec)fail   )exc)pool)schema)types)default)url)compat	decoratorc                  O      t tj| fi |S )a  Context manager which expects one or more warnings.

    With no arguments, squelches all SAWarnings emitted via
    sqlalchemy.util.warn and sqlalchemy.util.warn_limited.   Otherwise
    pass string expressions that will match selected warnings via regex;
    all non-matching warnings are sent through.

    The expect version **asserts** that the warnings were in fact seen.

    Note that the test suite sets SAWarning warnings to raise exceptions.

    )_expect_warningssa_exc	SAWarningmessageskw r   X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/sqlalchemy/testing/assertions.pyexpect_warnings    s   r   c                 o   sd    t | }t| tjr|tjsdV  dS t|i | dV  W d   dS 1 s+w   Y  dS )zContext manager which expects one or more warnings on specific
    dialects.

    The expect version **asserts** that the warnings were in fact seen.

    N)r   
isinstancer   string_typesr   _currentr   )dbr   r   specr   r   r   expect_warnings_on0   s   
"r#   c                        t  fdd}|S )zDecorator form of expect_warnings().

    Note that emits_warning does **not** assert that the warnings
    were in fact seen.

    c                    @   t  ddi | |i |W  d    S 1 sw   Y  d S Nassert_F)r   fnargsr   r   r   r   decorateI      $zemits_warning.<locals>.decorater   r   r,   r   r+   r   emits_warningA   s   r/   c                  O   r   N)r   r   SADeprecationWarningr   r   r   r   expect_deprecatedQ   s   r2   c                    s   t  fdd}|S )aV  Mark a test as emitting a warning on a specific dialect.

    With no arguments, squelches all SAWarning failures.  Or pass one or more
    strings; these will be matched to the root of the warning description by
    warnings.filterwarnings().

    Note that emits_warning_on does **not** assert that the warnings
    were in fact seen.

    c                    sH   t  gR ddi | |i |W  d    S 1 sw   Y  d S r&   )r#   r(   r!   r   r   r   r,   a   s   $z"emits_warning_on.<locals>.decorater   )r!   r   r,   r   r3   r   emits_warning_onU   s   r4   c                     r$   )a+  Mark a test as immune from fatal deprecation warnings.

    With no arguments, squelches all SADeprecationWarning failures.
    Or pass one or more strings; these will be matched to the root
    of the warning description by warnings.filterwarnings().

    As a special case, you may pass a function name prefixed with //
    and it will be re-written as needed to match the standard warning
    verbiage emitted by the sqlalchemy.util.deprecated decorator.

    Note that uses_deprecated does **not** assert that the warnings
    were in fact seen.

    c                    r%   r&   )r2   r(   r+   r   r   r,   y   r-   z!uses_deprecated.<locals>.decorater   r.   r   r+   r   uses_deprecatedi   s   r5   TFc                 #   s    rdd |D n|t tj fdd}td| d V  W d    n1 s2w   Y  |rP|r>tjsTrRJ ddfdd	D  d S d S d S )
Nc                 S   s    g | ]}t |t jt jB qS r   )recompileIS).0msgr   r   r   
<listcomp>   s     z$_expect_warnings.<locals>.<listcomp>c                    s   t |  r| }t|} n	|r|d }nd }|rt| s'| g|R i |S s+d S D ]}r6|| s<sD|| krD|  d S q-| g|R i | d S )Nr   )r   str
issubclassmatchdiscard)r;   argr   	exceptionfilter_exc_clsfilters	real_warnregexseenr   r   our_warn   s&   



z"_expect_warnings.<locals>.our_warnzwarnings.warnzWarnings were not seen: %sz, c                 3   s"    | ]}d  r
|j n| V  qdS )z%rN)pattern)r:   s)rH   r   r   	<genexpr>   s    
z#_expect_warnings.<locals>.<genexpr>)setwarningswarnr   patchr   py3kjoin)rE   r   rH   r'   py2konlyrJ   r   rD   r   r      s"   r   c                   C   s
   t   dS )zCheck things that have to be finalized at the end of a test suite.

    Hardcoded at the moment, a modular system can be built here
    to support things like PG prepared transactions, tables all
    dropped, etc.

    N)!_assert_no_stray_pool_connectionsr   r   r   r   global_cleanup_assertions   s   
rV   c                  C   st   t   tjrtd7 atdttj  t   tjr0ttj} tj  dat	
d|   d S tdkr8J dd S )Nr   z2Encountered a stray connection in test cleanup: %sr   z8Stray connection refused to leave after gc.collect(): %s
   Fz*Encountered more than 10 stray connections)testutillazy_gcr   _refs_STRAY_CONNECTION_FAILURESprintr=   
gc_collectclearrO   rP   )errr   r   r   rU      s&   

rU   c                 C   s$   t || sJ |pd| |f d S )Nz%r !~ %r)r6   r?   abr;   r   r   r   eq_regex   s   $rc   c                 C   s    | |ksJ |pd| |f dS )z.Assert a == b, with repr messaging on failure.%r != %rNr   r`   r   r   r   eq_       re   c                 C   s    | |ksJ |pd| |f dS )z.Assert a != b, with repr messaging on failure.z%r == %rNr   r`   r   r   r   ne_   rf   rg   c                 C   s    | |ksJ |pd| |f dS )z.Assert a <= b, with repr messaging on failure.rd   Nr   r`   r   r   r   le_   rf   rh   c                 C   s"   t | |sJ |pd| |f d S )Nz%r is not an instance of %rr   r`   r   r   r   is_instance_of   s   "rj   c                 C      t | d|d d S )NTr;   is_ra   r;   r   r   r   is_true      rp   c                 C   rk   )NFrl   rm   ro   r   r   r   is_false  rq   rr   c                 C   s    | |u sJ |pd| |f dS )z.Assert a is b, with repr messaging on failure.z%r is not %rNr   r`   r   r   r   rn     rf   rn   c                 C   s    | |usJ |pd| |f dS )z2Assert a is not b, with repr messaging on failure.z%r is %rNr   r`   r   r   r   is_not_  rf   rs   c                 C   s    | |v sJ |pd| |f dS )z.Assert a in b, with repr messaging on failure.z%r not in %rNr   r`   r   r   r   in_  rf   rt   c                 C   s    | |vsJ |pd| |f dS )z2Assert a in not b, with repr messaging on failure.z%r is in %rNr   r`   r   r   r   not_in_  rf   ru   c                 C   s"   |  |sJ |pd| |f dS )z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN)
startswith)ra   fragmentr;   r   r   r   startswith_  s   rx   c                 C   sX   t dd| } t dd| } t dd|}t dd|}| |ks*J |p(d| |f d S )Nz^\s+?|\n z {2,} rd   )r6   subr`   r   r   r   eq_ignore_whitespace"  s
    r|   c                 C   s6   t jsdS | j| jur| jsJ d| | jf dS dS )aH  assert that any exception we're catching does not have a __context__
    without a __cause__, and that __suppress_context__ is never set.

    Python 3 will report nested as exceptions as "during the handling of
    error X, error Y occurred". That's not what we want to do.  we want
    these exceptions in a cause chain.

    NFzZException %r was correctly raised but did not set a cause, within context %r as its cause.)r   rR   __context__	__cause____suppress_context__)rB   r   r   r    _assert_proper_exception_context+  s   
r   c                 O   s   t | |||dd d S NT)check_context_assert_raises
except_cls	callable_r*   r   r   r   r   assert_raisesC  s   r   c                 O   s   t | ||| d S r0   r   r   r   r   r   assert_raises_context_okG  s   r   c                 O   s   t | |||ddS r   r   r   r   r   r   assert_raises_returnM  rq   r   c                 O   s   t | ||||dd d S )NT)r;   r   r   r   r;   r   r*   kwargsr   r   r   assert_raises_messageQ  s   
r   c                 O   s   t | ||||d d S )Nrl   r   r   r   r   r    assert_raises_message_context_okW  s   r   c           
   
   C   s   d }|r
t  d }z||i | d}W n? | yT }	 z3|	}d}|d ur8t|t|	tjs8J d||	f |r@|s@t|	 tt|		d W Y d }	~	nd }	~	ww |s[J d|S )Nr   FTz%r !~ %sutf-8z#Callable did not raise an exception)
sysexc_infor6   searchr   	text_typeUNICODEr   r\   encode)
r   r   r*   r   r;   r   ret_errare_we_already_in_a_tracebacksuccessr_   r   r   r   r   ]  s,   
 r   c                   @   s(   e Zd Z									dddZdS )AssertsCompiledSQLNFc                    sL  |rt  }n8|	rd }n3|d u rt| dd }|d u rtjj}n |dkr(t  }n|dkr1t  }nt|tj	r?t
|  }i }i }|rI||d< |d urSt||d< |
rYd|d< dd	lm} t||jrq| }d|j_|j}n+t||jjrtj|d
}|  |jd d d }W d    n1 sw   Y  |r||d< |jdd|i|}tt|di }tjr|ddd}t dt!| | d nt dt!|d |  t"#ddt!|}t$||d|||f  |d urt$|%|| |d ur|%| t$t& fdd|j'D | |d ur$t$|j(| d S d S )N__dialect__r   default_enhancedschema_translate_mapcolumn_keysTliteral_bindsr   )orm_execute_stmtr   compile_kwargsdialectparamsr   asciiignorez
SQL String:
z[\n\t]ry   z%r != %r on dialect %rc                    s   g | ]} | qS r   r   )r:   xpr   r   r<         z5AssertsCompiledSQL.assert_compile.<locals>.<listcomp>r   ))r   DefaultDialectgetattrr   r!   r   StrCompileDialectr   r   r   r   URLget_dialectlist
sqlalchemyr   Query_compile_context	statement
use_labelspersistenceBulkUDr   rQ   objectexec_
mock_callsr7   reprrR   r   decoder\   r   r6   r{   re   construct_paramstuplepositiontupprefetch)selfclauseresultr   checkparamsr   checkpositionalcheck_prefetchuse_default_dialectallow_dialect_selectr   r   r   r   r   context	stmt_mockc	param_strccr   r   r   assert_compilex  sx   






z!AssertsCompiledSQL.assert_compile)	NNNNNFFFN)__name__
__module____qualname__r   r   r   r   r   r   w  s    r   c                   @   s   e Zd ZdddZdd ZdS )ComparesTablesFc                 C   sB  t |jt |jksJ t|j|jD ]n\}}t|j|j ||j|j u s(J t|j|j t|j|j |rNd}t|jt|jsMJ ||j|jf n| 	|| t|jt
jrdt|jj|jj tdd |jD dd |jD  |jrt|jtjsJ qt |jt |jksJ |jD ]}|jj|j d usJ qd S )Nz)Type '%s' doesn't correspond to type '%s'c                 S      h | ]}|j jqS r   columnnamer:   fr   r   r   	<setcomp>  r   z5ComparesTables.assert_tables_equal.<locals>.<setcomp>c                 S   r   r   r   r   r   r   r   r     r   )lenr   zipre   r   primary_keynullabler   typeassert_types_basesqltypesStringlengthforeign_keysserver_defaultr   FetchedValuecolumns)r   tablereflected_tablestrict_typesr   reflected_cr;   r   r   r   assert_tables_equal  s8   

z"ComparesTables.assert_tables_equalc                 C   s*   |j |j sJ d|j|j |j f d S )Nz7On column %r, type '%s' doesn't correspond to type '%s')r   _compare_type_affinityr   )r   c1c2r   r   r   r     s
   z ComparesTables.assert_types_baseN)F)r   r   r   r   r   r   r   r   r   r     s    
!r   c                   @   sl   e Zd Zdd Zdd Zdd Zdd Zdd
dZdd Zdd Z	dd Z
dd Zejdd Zdd Zd	S )AssertsExecutionResultsc                 G   s&   t |}tt| | ||| d S r0   )r   r\   r   assert_list)r   r   class_objectsr   r   r   assert_result  s   z%AssertsExecutionResults.assert_resultc                 C   sL   |  t|t|kd|j  tdt|D ]}| ||| ||  qd S )Nz9result list is not the same size as test list, for class r   )r'   r   r   range
assert_row)r   r   r   list_ir   r   r   r     s   z#AssertsExecutionResults.assert_listc              	   C   s   |  |j|u dt|  | D ]A\}}t|tr?t|d tr0| t|||d |d  q| 	|d t|||d  q|  t|||kd|t|||f  qd S )Nzitem class is not r   r   z'attribute %s value %s does not match %s)
r'   	__class__r   itemsr   r   r   r   r   r   )r   r   rowobjdesckeyvaluer   r   r   r   	  s   
z"AssertsExecutionResults.assert_rowc           	         s   G dd dt t|}fdd|D }tfdd|D ]}tdt|jjf  q t|t|krCtdt|t|f  t   fd	d
}|D ]}|D ]}|||ra|	|  nqStdjt
|f  qOdS )zAs assert_result, but the order of objects is not considered.

        The algorithm is very expensive but not a big deal for the small
        numbers of rows that the test suite manipulates.
        c                   @   s   e Zd Zdd ZdS )zFAssertsExecutionResults.assert_unordered_result.<locals>.immutabledictc                 S   s   t | S r0   )idr   r   r   r   __hash__"  s   zOAssertsExecutionResults.assert_unordered_result.<locals>.immutabledict.__hash__N)r   r   r   r   r   r   r   r   immutabledict!  s    r   c                    s   h | ]} |qS r   r   )r:   e)r   r   r   r   &  r   zBAssertsExecutionResults.assert_unordered_result.<locals>.<setcomp>c                    s
   t |  S r0   ri   )o)clsr   r   <lambda>)  s   
 zAAssertsExecutionResults.assert_unordered_result.<locals>.<lambda>z#Unexpected type "%s", expected "%s"z+Unexpected object count "%s", expected "%s"c              	      st   |  D ]3\}}t|tr,zjt| ||d g|d R   W q ty+   Y  dS w t| | |kr7 dS qdS )Nr   r   FT)r   r   r   assert_unordered_resultr   AssertionError)objr"   r   r   )NOVALUEr   r   r   _compare_item8  s   
zFAssertsExecutionResults.assert_unordered_result.<locals>._compare_itemz2Expected %s instance with attributes %s not found.T)dictr   IdentitySetitertools_filterfalser	   r   r   r   r   remover   )	r   r   r  expectedfoundwrongr	  expected_item
found_itemr   )r  r  r   r   r   r    s@   


z/AssertsExecutionResults.assert_unordered_resultNc                 C   s   |d u r
ddl m} t|S )Nr   )r!   )ry   r!   r   assert_engine)r   r!   r   r   r   sql_execution_asserterR  s   
z.AssertsExecutionResults.sql_execution_asserterc                 G   s>   |  |}| }W d    n1 sw   Y  |j|  |S r0   r  r'   )r   r!   r   rulesasserterr   r   r   r   assert_sql_executionX  s
   
z,AssertsExecutionResults.assert_sql_executionc                 C   sZ   g }|D ]}t |trtjdd | D  }ntj| }|| q| j||g|R  S )Nc                 S   s   g | ]
\}}t ||qS r   )r   CompiledSQL)r:   kvr   r   r   r<   d  s    z6AssertsExecutionResults.assert_sql.<locals>.<listcomp>)r   r
  r   AllOfr   r  appendr  )r   r!   r   r  newrulesrulenewruler   r   r   
assert_sql^  s   

z"AssertsExecutionResults.assert_sqlc                 C   s   |  ||t| d S r0   )r  r   CountStatements)r   r!   r   countr   r   r   assert_sql_countl  s   z(AssertsExecutionResults.assert_sql_countc           
         s    fddt ||D }g }|D ]\}}}||  qz | W t ||D ]\}	\}}}|d d d  |	t| q&S t ||D ]\}	\}}}|d d d  |	t| qCw )Nc                    s    g | ]\}}  |||fqS r   )r  )r:   r!   r#  r   r   r   r<   r  s    zEAssertsExecutionResults.assert_multiple_sql_count.<locals>.<listcomp>)r   r  	__enter____exit__r'   r   r"  )
r   dbsr   countsrecs	assertersctxr!   r#  r  r   r   r   assert_multiple_sql_countq  s   
z1AssertsExecutionResults.assert_multiple_sql_countc                 g   s@    |  |}d V  W d    n1 sw   Y  |j|  d S r0   r  )r   r!   r  r  r   r   r   assert_execution  s
   z(AssertsExecutionResults.assert_executionc                 C   s   |  |t|S r0   )r-  r   r"  )r   r!   r#  r   r   r   assert_statement_count  rq   z.AssertsExecutionResults.assert_statement_countr0   )r   r   r   r   r   r   r  r  r  r!  r$  r,  
contextlibcontextmanagerr-  r.  r   r   r   r   r     s    

8
r   )TTFr0   )NF)=
__future__r   r/  r6   r   rO   ry   r   r   r   r   rX   
exclusionsr   r	   r   r   r   r   r   r   enginer   r   r   r   r   r0  r#   r/   r2   r4   r5   r   rV   r[   rU   rc   re   rg   rh   rj   rp   rr   rn   rs   rt   ru   rx   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sr   
-
*











	
Y)