o
    [hV                     @   sB  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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	!dVd"d#Z"d$d% Z#d a$d&d' Z%dWd(d)Z&dWd*d+Z'dWd,d-Z(dWd.d/Z)dWd0d1Z*dWd2d3Z+dWd4d5Z,dWd6d7Z-dWd8d9Z.e.Z/dWd:d;Z0dWd<d=Z1e1Z2dWd>d?Z3dWd@dAZ4dBdC Z5dDdE Z6dFdG Z7dHdI Z8dJdK Z9dLdM Z:	!dXdNdOZ;G dPdQ dQe<Z=G dRdS dSe<Z>G dTdU dUe<Z?dS )Y    )absolute_importN   )	assertsql)config)mock)util)db_spec)fail   )exc)pool)schema)sql)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_excZ	SAWarningmessageskw r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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_on1   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   decorateJ      $zemits_warning.<locals>.decorater   r   r,   r   r+   r   emits_warningB   s   r/   c                  O   r   N)r   r   ZSADeprecationWarningr   r   r   r   expect_deprecatedR   s   r1   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,   b   s   $z"emits_warning_on.<locals>.decorater   )r!   r   r,   r   r2   r   emits_warning_onV   s   r3   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&   )r1   r(   r+   r   r   r,   z   r-   z!uses_deprecated.<locals>.decorater   r.   r   r+   r   uses_deprecatedj   s   r4   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filtersZ	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)r9   s)rF   r   r   	<genexpr>   s    
z#_expect_warnings.<locals>.<genexpr>)setwarningswarnr   patchr   py3kjoin)rD   r   rF   r'   Zpy2konlyrH   r   rC   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   
rS   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)testutilZlazy_gcr   Z_refs_STRAY_CONNECTION_FAILURESprintr<   Z
gc_collectclearrM   rN   )errr   r   r   rR      s&   

rR   c                 C   s$   t || sJ |pd| |f d S )Nz%r !~ %r)r5   r>   abr:   r   r   r   eq_regex   s   $r]   c                 C   s    | |ksJ |pd| |f dS )z.Assert a == b, with repr messaging on failure.%r != %rNr   rZ   r   r   r   eq_       r_   c                 C   s    | |ksJ |pd| |f dS )z.Assert a != b, with repr messaging on failure.z%r == %rNr   rZ   r   r   r   ne_   r`   ra   c                 C   s    | |ksJ |pd| |f dS )z.Assert a <= b, with repr messaging on failure.r^   Nr   rZ   r   r   r   le_   r`   rb   c                 C   s"   t | |sJ |pd| |f d S )Nz%r is not an instance of %rr   rZ   r   r   r   is_instance_of   s   "rd   c                 C      t | d|d d S )NTr:   is_r[   r:   r   r   r   is_true      rj   c                 C   re   )NFrf   rg   ri   r   r   r   is_false  rk   rl   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   rZ   r   r   r   rh     r`   rh   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   rZ   r   r   r   is_not  r`   rm   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   rZ   r   r   r   in_  r`   rn   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   rZ   r   r   r   not_in  r`   ro   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)r[   fragmentr:   r   r   r   startswith_#  s   rr   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,} r^   )r5   subrZ   r   r   r   eq_ignore_whitespace+  s
    rv   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   rP   __context__	__cause____suppress_context__)rA   r   r   r    _assert_proper_exception_context4  s   
rz   c                 O   s   t | |||dd d S NT)check_context_assert_raises
except_cls	callable_r*   r   r   r   r   assert_raisesL  s   r   c                 O   s   t | ||| d S r0   r}   r   r   r   r   assert_raises_context_okP  s   r   c                 O   s   t | |||ddS r{   r}   r   r   r   r   assert_raises_returnY  rk   r   c                 O   s   t | ||||dd d S )NT)r:   r|   r}   r   r:   r   r*   kwargsr   r   r   assert_raises_message]  s   
r   c                 O   s   t | ||||d d S )Nrf   r}   r   r   r   r    assert_raises_message_context_okc  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_infor5   searchr   	text_typeUNICODErz   rW   encode)
r   r   r*   r   r:   r|   Zret_errZare_we_already_in_a_tracebacksuccessrY   r   r   r   r~   i  s2    r~   c                   @   s(   e Zd Z									dddZdS )AssertsCompiledSQLNFc                    sv  |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< G dd dt G  fdddt}||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rt$|%|| |d ur,|%|t$t&fdd|j'D | |d ur9t$|j(| d S d S )NZ__dialect__r   Zdefault_enhancedschema_translate_mapZcolumn_keysTliteral_bindsr   )ormZ_execute_stmtr   compile_kwargsc                   @      e Zd Zdd ZdS )z5AssertsCompiledSQL.assert_compile.<locals>.DontAccessc                 S   s   t d)Nz=compiler accessed .statement; use compiler.current_executable)NotImplementedError)selfkeyr   r   r   __getattribute__  s   zFAssertsCompiledSQL.assert_compile.<locals>.DontAccess.__getattribute__N)__name__
__module____qualname__r   r   r   r   r   
DontAccess      r   c                       s8   e Zd Zdd Zdd Zdd Zdd Z fd	d
ZdS )z>AssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccessc                 S   sn   || _ t|dd| _| jr3|j| _t|tjtjtjfr |j	| _	t|tjtjfr5|j
| _
|j| _d S d S d S )Nsupports_executionF)test_statementgetattrr   Z_execution_optionsr   r   ZInsertZUpdateDeleteZ
_returninginlineZ_return_defaults)r   r   r   r   r   __init__  s   zGAssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccess.__init__c                 S   s
   | j  S r0   )r   _default_dialectr   r   r   r   r     s   
zOAssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccess._default_dialectc                 [   s   | j jj| fd|i|S )Ndialect)r   r6   __func__r   r   r   r   r   r   r6     s   zFAssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccess.compilec                 [   s   | j jj| |fi |S r0   )r   	_compilerr   r   r   r   r   r     s
   zHAssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccess._compilerc                    sh   t |dr*tj|d   | jj|fi |W  d    S 1 s#w   Y  d S | jj|fi |S )N	statement)hasattrr   rO   objectr   _compiler_dispatch)r   compilerr   r   r   r   r     s   
$zQAssertsCompiledSQL.assert_compile.<locals>.CheckCompilerAccess._compiler_dispatchN)r   r   r   r   r   r6   r   r   r   r   r   r   CheckCompilerAccess  s    r   r   paramsr   asciiignorez
SQL String:
z[\n\t]rs   z%r != %r on dialect %rc                    s   g | ]} | qS r   r   )r9   x)pr   r   r;         z5AssertsCompiledSQL.assert_compile.<locals>.<listcomp>r   ))r   ZDefaultDialectr   r   r!   r   ZStrCompileDialectr   r   r   r   URLget_dialectlistZ
sqlalchemyr   ZQueryZ_compile_contextr   Z
use_labelsZpersistenceZBulkUDr   rO   r   exec_Z
mock_callsr6   reprrP   r   decoderW   r   r5   ru   r_   Zconstruct_paramstupleZpositiontupZprefetch)r   Zclauseresultr   Zcheckparamsr   ZcheckpositionalZcheck_prefetchZuse_default_dialectZallow_dialect_selectr   r   r   r   r   contextZ	stmt_mockr   cZ	param_strccr   )r   r   r   assert_compile  s|   



.



z!AssertsCompiledSQL.assert_compile)	NNNNNFFFN)r   r   r   r   r   r   r   r   r     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r9   fr   r   r   	<setcomp>*  r   z5ComparesTables.assert_tables_equal.<locals>.<setcomp>c                 S   r   r   r   r   r   r   r   r   +  r   )lenr   zipr_   r   Zprimary_keyZnullabler   typeassert_types_basesqltypesStringlengthZforeign_keysZserver_defaultr   ZFetchedValuecolumns)r   tableZreflected_tablestrict_typesr   Z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   Z_compare_type_affinityr   )r   c1c2r   r   r   r   6  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   rW   r   assert_list)r   r   class_objectsr   r   r   assert_resultA  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   F  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   ZrowobjZdescr   valuer   r   r   r   P  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                   @   r   )zFAssertsExecutionResults.assert_unordered_result.<locals>.immutabledictc                 S   s   t | S r0   )idr   r   r   r   __hash__i  s   zOAssertsExecutionResults.assert_unordered_result.<locals>.immutabledict.__hash__N)r   r   r   r   r   r   r   r   immutabledicth  r   r   c                    s   h | ]} |qS r   r   )r9   e)r   r   r   r   m  r   zBAssertsExecutionResults.assert_unordered_result.<locals>.<setcomp>c                    s
   t |  S r0   rc   )o)clsr   r   <lambda>p  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_item  s   
zFAssertsExecutionResults.assert_unordered_result.<locals>._compare_itemz2Expected %s instance with attributes %s not found.T)dictr   ZIdentitySetZitertools_filterfalser	   r   r   r   r   remover   )	r   r   r   expectedfoundZwrongr   Zexpected_itemZ
found_itemr   )r   r   r   r   r   r   a  s@   


z/AssertsExecutionResults.assert_unordered_resultNc                 C   s   |d u r
ddl m} t|S )Nr   )r!   )rs   r!   r   Zassert_engine)r   r!   r   r   r   sql_execution_asserter  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_execution  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)r9   kvr   r   r   r;     s    z6AssertsExecutionResults.assert_sql.<locals>.<listcomp>)r   r   r   ZAllOfr   r   appendr   )r   r!   r   r   ZnewrulesruleZ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_count  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   )r9   r!   r  r   r   r   r;     s    zEAssertsExecutionResults.assert_multiple_sql_count.<locals>.<listcomp>)r   r   	__enter____exit__r'   r   r  )
r   Zdbsr   countsZrecsZ	assertersctxr!   r  r   r   r   r   assert_multiple_sql_count  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  rk   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  r5   r   rM   rs   r   r   r   r   rU   Z
exclusionsr   r	   r   r   r   r   r   r   r   Zenginer   r   r   r   r   r  r#   r/   r1   r3   r4   r   rS   rV   rR   r]   r_   ra   rb   rd   rj   rl   rh   rm   Zis_not_rn   ro   Znot_in_rr   rv   rz   r   r   r   r   r   r~   r   r   r   r   r   r   r   r   <module>   sz   
-
*











		
 ,