U
    x^G                     @   s  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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dLd"d#Z!d$d% Z"d a#d&d' Z$dMd(d)Z%dNd*d+Z&dOd,d-Z'dPd.d/Z(dQd0d1Z)dRd2d3Z*dSd4d5Z+dTd6d7Z,dUd8d9Z-dVd:d;Z.dWd<d=Z/dXd>d?Z0d@dA Z1dBdC Z2dDdE Z3G dFdG dGe4Z5G dHdI dIe4Z6G dJdK dKe4Z7dS )Y    )absolute_importN   )	assertsql)config)mock)util)db_spec)fail   )exc)orm)pool)schema)types)default)url)compat	decoratorc                  O   s   t tj| f|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   E/tmp/pip-install-dq5v43_d/SQLAlchemy/sqlalchemy/testing/assertions.pyexpect_warnings    s    r   c              	   o   sF   t | }t| tjr&|tjs&dV  nt|| dV  W 5 Q R X 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                     s   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              
      s0   t  ddi | ||W  5 Q R  S Q R X d S Nassert_F)r   fnargsr   r   r   r   decorateI   s    zemits_warning.<locals>.decorater   r   r)   r   r(   r   emits_warningA   s    r+   c                  O   s   t tj| f|S N)r   r   ZSADeprecationWarningr   r   r   r   expect_deprecatedQ   s    r-   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              
      s6   t  fddi | ||W  5 Q R  S Q R X d S r#   )r"   r%   r    r   r   r   r)   a   s    z"emits_warning_on.<locals>.decorater   )r    r   r)   r   r.   r   emits_warning_onU   s    r/   c                     s   t  fdd}|S )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              
      s0   t  ddi | ||W  5 Q R  S Q R X d S r#   )r-   r%   r(   r   r   r)   y   s    z!uses_deprecated.<locals>.decorater   r*   r   r(   r   uses_deprecatedi   s    r0   TFc              	   #   s   rdd |D n|t tj fdd}td| d V  W 5 Q R X |r|rftjsrtddfdd	D  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 }|r8t| sH| f||S sPd S D ],}rf|| srsT|| krT|  qqT| f|| d S )Nr   )r   str
issubclassmatchdiscard)r6   argr   	exceptionZ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)r5   s)rA   r   r   	<genexpr>   s    z#_expect_warnings.<locals>.<genexpr>)	setwarningswarnr   patchr   py3kAssertionErrorjoin)r?   r   rA   r$   Zpy2konlyrC   r   r>   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    rO   c                  C   sz   t   tjr0td7 atdttj  t   tjr^ttj} tj  dat	
d|   ntdkrvdsrtdda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printr8   Z
gc_collectclearrH   rI   rL   )errr   r   r   rN      s&    

rN   c                 C   s$   t || s t|pd| |f d S )Nz%r !~ %r)r1   r:   rL   abr6   r   r   r   eq_regex   s    rY   c                 C   s    | |kst |pd| |f dS )z.Assert a == b, with repr messaging on failure.%r != %rNrL   rV   r   r   r   eq_   s    r\   c                 C   s    | |kst |pd| |f dS )z.Assert a != b, with repr messaging on failure.z%r == %rNr[   rV   r   r   r   ne_   s    r]   c                 C   s    | |kst |pd| |f dS )z.Assert a <= b, with repr messaging on failure.rZ   Nr[   rV   r   r   r   le_   s    r^   c                 C   s   t | d|d d S )NTr6   is_rW   r6   r   r   r   is_true   s    rc   c                 C   s   t | d|d d S )NFr_   r`   rb   r   r   r   is_false   s    rd   c                 C   s    | |kst |pd| |f dS )z.Assert a is b, with repr messaging on failure.z%r is not %rNr[   rV   r   r   r   ra     s    ra   c                 C   s    | |k	st |pd| |f dS )z2Assert a is not b, with repr messaging on failure.z%r is %rNr[   rV   r   r   r   is_not_  s    re   c                 C   s    | |kst |pd| |f dS )z.Assert a in b, with repr messaging on failure.z%r not in %rNr[   rV   r   r   r   in_  s    rf   c                 C   s    | |kst |pd| |f dS )z2Assert a in not b, with repr messaging on failure.z%r is in %rNr[   rV   r   r   r   not_in_  s    rg   c                 C   s"   |  |st|pd| |f dS )z>Assert a.startswith(fragment), with repr messaging on failure.z%r does not start with %rN)
startswithrL   )rW   fragmentr6   r   r   r   startswith_  s    rj   c                 C   sX   t dd| } t dd| } t dd|}t dd|}| |ksTt|pPd| |f d S )Nz^\s+?|\n z {2,} rZ   )r1   subrL   rV   r   r   r   eq_ignore_whitespace  s
    rn   c                 O   s<   z||| d}W n | k
r*   d}Y nX |s8t dd S NFT#Callable did not raise an exceptionr[   )
except_cls	callable_r'   r   successr   r   r   assert_raises'  s    

rt   c              
   O   sR   d }z||| d}W n* | k
r@ } zd}|}W 5 d }~X Y nX |sNt d|S ro   r[   )rq   rr   r'   r   Zret_errrs   rU   r   r   r   assert_raises_return2  s    
ru   c              
   O   s|   z||| dst dW n\ | k
rv } z>t|t|tjsRt d||f tt|d W 5 d }~X Y nX d S )NFrp   z%r !~ %sutf-8)rL   r1   searchr   	text_typeUNICODErS   encode)rq   r6   rr   r'   kwargser   r   r   assert_raises_message@  s    
r}   c                	   @   s   e Zd ZdddZdS )AssertsCompiledSQLNFc              	      s0  |rt  }np|	rd }nf|d kr,t| dd }|d kr>tjj}n@|dkrPt  }n.|dkrbt  }nt|tj	r~t
|  }i }i }|r||d< |d k	rt||d< |
rd|d< t|tjr| }d|j_|j}nDt|tjjrtj|d }|  |jd	 d
 d	 }W 5 Q R X |r(||d< |jf 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 k	rt#|$|| |d k	r|$| t#t% fdd|j&D | |d k	r,t#|j'| d S )NZ__dialect__r   Zdefault_enhancedschema_translate_mapZcolumn_keysTliteral_bindsZ_execute_stmtr   r   compile_kwargsdialectparamsrv   asciiignorez
SQL String:
z[\n\t]rk   z%r != %r on dialect %rc                    s   g | ]} | qS r   r   )r5   xpr   r   r7     s     z5AssertsCompiledSQL.assert_compile.<locals>.<listcomp>)(r   ZDefaultDialectgetattrr   r    r   ZStrCompileDialectr   r   r   r   URLget_dialectlistr   ZQueryZ_compile_contextZ	statementZ
use_labelsZpersistenceZBulkUDr   rJ   objectexec_Z
mock_callsr2   reprrK   rz   decoderS   rx   r1   rm   r\   Zconstruct_paramstupleZpositiontupprefetch)selfZclauseresultr   Zcheckparamsr   ZcheckpositionalZcheck_prefetchZuse_default_dialectZallow_dialect_selectr   r   r   r   contextZ	stmt_mockcZ	param_strccr   r   r   assert_compileM  sr    







z!AssertsCompiledSQL.assert_compile)	NNNNNFFFN)__name__
__module____qualname__r   r   r   r   r   r~   L  s            r~   c                   @   s   e Zd ZdddZdd ZdS )ComparesTablesFc                 C   sH  t |jt |jkstt|j|jD ]\}}t|j|j ||j|j ksPtt|j|j t|j|j |rd}t|j	t	|j	st||j	|j	f n| 
|| t|j	tjrt|j	j|j	j tdd |jD dd |jD  |jr&t|jtjs&tq&t |jt |jkst|jD ]}|jj|j d k	s$tq$d S )Nz)Type '%s' doesn't correspond to type '%s'c                 S   s   h | ]}|j jqS r   columnnamer5   fr   r   r   	<setcomp>  s     z5ComparesTables.assert_tables_equal.<locals>.<setcomp>c                 S   s   h | ]}|j jqS r   r   r   r   r   r   r     s     )lenr   rL   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_cr6   r   r   r   assert_tables_equal  s6    
 

z"ComparesTables.assert_tables_equalc                 C   s*   |j |j s&td|j|j |j f d S )Nz7On column %r, type '%s' doesn't correspond to type '%s')r   Z_compare_type_affinityrL   r   )r   Z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 r,   )r   rS   r   assert_list)r   r   class_Z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|kdt|  | D ]\}}t|tr~t|d tr`| 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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rtdt|t|f  t   fd	d
}|D ]<}|D ]}|||r|	|  qqtdjt
|f  qd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 r,   )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   )r5   r|   )r   r   r   r     s     zBAssertsExecutionResults.assert_unordered_result.<locals>.<setcomp>c                    s
   t |  S r,   )r   )o)clsr   r   <lambda>      zAAssertsExecutionResults.assert_unordered_result.<locals>.<lambda>z#Unexpected type "%s", expected "%s"z+Unexpected object count "%s", expected "%s"c              	      sv   |  D ]h\}}t|trZz$jt| ||d f|d   W qp tk
rV   Y  dS X qt| | |kr dS qdS )Nr   r   FT)r   r   r   assert_unordered_resultr   rL   )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     s>    

 

z/AssertsExecutionResults.assert_unordered_resultNc                 C   s   |d krddl m} t|S )Nr   )r    )rk   r    r   Zassert_engine)r   r    r   r   r   sql_execution_asserter%  s    z.AssertsExecutionResults.sql_execution_asserterc              	   G   s*   |  |}| }W 5 Q R X |j|  |S r,   r   r$   )r   r    rr   rulesasserterr   r   r   r   assert_sql_execution+  s    
z,AssertsExecutionResults.assert_sql_executionc                 C   sX   g }|D ]<}t |tr0tjdd | D  }n
tj| }|| q| j||f| S )Nc                 S   s   g | ]\}}t ||qS r   )r   CompiledSQL)r5   kvr   r   r   r7   7  s     z6AssertsExecutionResults.assert_sql.<locals>.<listcomp>)r   r   r   ZAllOfr   r   appendr   )r   r    rr   r   ZnewrulesZruleZnewruler   r   r   
assert_sql1  s    

z"AssertsExecutionResults.assert_sqlc                 C   s   |  ||t| d S r,   )r   r   CountStatements)r   r    rr   countr   r   r   assert_sql_count?  s
      z(AssertsExecutionResults.assert_sql_countc           
         s    fddt ||D }g }|D ]\}}}||  q z
| W S t ||D ],\}	\}}}|d d d  |	t| qPX d S )Nc                    s    g | ]\}}  |||fqS r   )r   )r5   r    r   r   r   r   r7   E  s   zEAssertsExecutionResults.assert_multiple_sql_count.<locals>.<listcomp>)r   r   	__enter____exit__r$   r   r   )
r   Zdbsrr   countsZrecsZ	assertersctxr    r   r   r   r   r   assert_multiple_sql_countD  s    

z1AssertsExecutionResults.assert_multiple_sql_countc              	   g   s*   |  |}d V  W 5 Q R X |j|  d S r,   r   )r   r    r   r   r   r   r   assert_executionS  s    z(AssertsExecutionResults.assert_executionc                 C   s   |  |t|S r,   )r   r   r   )r   r    r   r   r   r   assert_statement_countY  s    z.AssertsExecutionResults.assert_statement_count)N)r   r   r   r   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r     s   
8

r   )TTF)N)N)N)N)N)N)N)N)N)N)N)N)8
__future__r   r   r1   rH   rk   r   r   r   r   rQ   Z
exclusionsr   r	   r   r   r   r   r   r   r   Zenginer   r   r   r   r   r   r"   r+   r-   r/   r0   r   rO   rR   rN   rY   r\   r]   r^   rc   rd   ra   re   rf   rg   rj   rn   rt   ru   r}   r   r~   r   r   r   r   r   r   <module>   sh   
     -*











	W)