o
    [hI_                     @   s  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 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 G dd dejZG dd dejZG dd dejZG d d! d!ejZG d"d# d#ejZ G d$d% d%ejZ!G d&d' d'ejZ"G d(d) d)ejZ#G d*d+ d+ejZ$d,S )-   )config)fixtures)eq_)in_)Column)Table   )	bindparam)case)Computed)exists)false)func)Integer)literal)literal_column)null)select)String)testing)text)true)tuple_)union)utilc                   @   s@   e Zd ZdZedd Zedd Zdd Zej	j
dd	 Zd
S )CollateTestTc                 C   &   t d|tdtddtdtd d S )N
some_tableidTZprimary_keydatad   r   r   r   r   clsmetadata r&   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/testing/suite/test_select.pydefine_tables      zCollateTest.define_tablesc                 C   s(   | | jj ddddddg d S )N   collate data1r   r    r   collate data2executetablesr   insertr$   
connectionr&   r&   r'   insert_data'   s   
zCollateTest.insert_datac                 C      t tj| | d S Nr   r   dbr/   fetchallselfr   resultr&   r&   r'   _assert_result1      zCollateTest._assert_resultc                 C   sD   t jt j}| t| jjg| jjj	j
| ddg d S )N)r*   r+   )r   r-   )r   requiresZget_order_by_collationr   r=   r   r0   r   order_bycr    Zcollateasc)r;   Z	collationr&   r&   r'   test_collate_order_by4   s   z!CollateTest.test_collate_order_byN)__name__
__module____qualname____backend__classmethodr(   r4   r=   r   r?   Zorder_by_collationrC   r&   r&   r&   r'   r      s    

	r   c                   @   sl   e Zd ZdZdZedd Zedd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zejjdd ZdS )OrderByLabelTestzTest the dialect sends appropriate ORDER BY expressions when
    labels are used.

    This essentially exercises the "supports_simple_order_by_label"
    setting.

    Tc                 C   sB   t d|tdtddtdttdttdtdtd	td d S )
Nr   r   Tr   xyq2   pr"   r#   r&   r&   r'   r(   K   s   zOrderByLabelTest.define_tablesc                 C   sB   | | jj ddddddddddddddd	d
ddg d S )Nr*   r   q1Zp3)r   rJ   rK   rL   rN   r   q2Zp2   Zq3p1r.   r2   r&   r&   r'   r4   W   s   
zOrderByLabelTest.insert_datac                 C   r5   r6   r7   r:   r&   r&   r'   r=   b   r>   zOrderByLabelTest._assert_resultc                 C   s6   | j j}|jjd}| t|g|g d d S )Nlx)r*   r   r   )r0   r   rA   rJ   labelr=   r   r@   r;   tablerS   r&   r&   r'   
test_plaine   s    zOrderByLabelTest.test_plainc                 C   s>   | j j}|jj|jj d}| t|g|g d d S )NrS   )rV         )	r0   r   rA   rJ   rK   rW   r=   r   r@   rX   r&   r&   r'   test_composed_intj   s    z"OrderByLabelTest.test_composed_intc              	   C   s   | j j}|jj|jj d}t|jj|jj	 d}| 
t||g|| dtdfdtdfdtdfg d S )	NrS   lyr   Zq1p3r\   Zq2p2r^   Zq3p1)r0   r   rA   rJ   rK   rW   r   lowerrL   rN   r=   r   r@   descr   u)r;   rY   rS   r`   r&   r&   r'   test_composed_multipleo   s   &z'OrderByLabelTest.test_composed_multiplec                 C   s:   | j j}|jjd}| t|g| g d d S )NrS   )rV   rU   rT   )	r0   r   rA   rJ   rW   r=   r   r@   rb   rX   r&   r&   r'   test_plain_descx   s
   z OrderByLabelTest.test_plain_descc                 C   sB   | j j}|jj|jj d}| t|g|	 g d d S )NrS   )r]   r[   rV   )
r0   r   rA   rJ   rK   rW   r=   r   r@   rb   rX   r&   r&   r'   test_composed_int_desc   s
   z'OrderByLabelTest.test_composed_int_descc                 C   sT   | j j}|jj|jj d}tt|jj	|g
||}| |g d d S )NrS   ))r*   r   )r*   r\   )r*   r^   )r0   r   rA   rJ   rK   rW   r   r   countr   Zgroup_byr@   r=   )r;   rY   exprstmtr&   r&   r'   test_group_by_composed   s   z'OrderByLabelTest.test_group_by_composedN)rD   rE   rF   __doc__rG   rH   r(   r4   r=   rZ   r_   rd   re   rf   r   r?   Zgroup_by_complex_expressionrj   r&   r&   r&   r'   rI   @   s    


	rI   c                   @   s   e Zd ZdZedd Zedd ZdddZd	d
 Ze	j
jdd Ze	j
jdd Ze	j
jdd Ze	j
jdd Ze	j
jdd Ze	j
jdd ZdS )LimitOffsetTestTc                 C   *   t d|tdtddtdttdt d S Nr   r   Tr   rJ   rK   r   r   r   r#   r&   r&   r'   r(         zLimitOffsetTest.define_tablesc              
   C   @   | | jj ddddddddddddddddg d S Nr*   r   )r   rJ   rK   r   rQ   r\   r.   r2   r&   r&   r'   r4         




zLimitOffsetTest.insert_datar&   c                 C      t tj|| | d S r6   r7   r;   r   r<   paramsr&   r&   r'   r=         zLimitOffsetTest._assert_resultc                 C   2   | j j}| t|g|jjdddg d S )Nr   r*   r*   r   r   r   r   )r0   r   r=   r   r@   rA   r   limitr;   rY   r&   r&   r'   test_simple_limit   s
   z!LimitOffsetTest.test_simple_limitc                 C   rx   )Nr   r   r   rQ   rQ   rQ   r\   )r0   r   r=   r   r@   rA   r   offsetr|   r&   r&   r'   test_simple_offset   s
   z"LimitOffsetTest.test_simple_offsetc                 C   s8   | j j}| t|g|jjddddg d S )Nr   r*   rz   r~   )	r0   r   r=   r   r@   rA   r   r{   r   r|   r&   r&   r'   test_simple_limit_offset   s
   z(LimitOffsetTest.test_simple_limit_offsetc                 C   sZ   | j j}t|g|jjdd}|jt	j
jddid}t|}| |ddg dS )	z7test that 'literal binds' mode works - no bound params.r   r*   Zliteral_bindsT)dialectcompile_kwargsrz   r~   N)r0   r   r   r@   rA   r   r{   r   compiler   r8   r   strr=   )r;   rY   ri   sqlr&   r&   r'   test_limit_offset_nobinds   s    z)LimitOffsetTest.test_limit_offset_nobindsc                 C   >   | j j}| jt|g|jjtdddgddid d S )Nlry   rz   r   rv   )	r0   r   r=   r   r@   rA   r   r{   r	   r|   r&   r&   r'   test_bound_limit      
z LimitOffsetTest.test_bound_limitc                 C   r   )Nor~   r   r   r   )	r0   r   r=   r   r@   rA   r   r   r	   r|   r&   r&   r'   test_bound_offset   r   z!LimitOffsetTest.test_bound_offsetc                 C   sJ   | j j}| jt|g|jjtd	tdddgdddd d S )	Nr   r   rz   r~   r   r*   )r   r   r   )
r0   r   r=   r   r@   rA   r   r{   r	   r   r|   r&   r&   r'   test_bound_limit_offset   s   



z'LimitOffsetTest.test_bound_limit_offsetNr&   )rD   rE   rF   rG   rH   r(   r4   r=   r}   r   r?   r   r   r   r   Zbound_limit_offsetr   r   r   r&   r&   r&   r'   rl      s(    
	






rl   c                   @   s   e Zd ZdZedd Zedd ZdddZd	d
 Zdd Z	e
jje
jjdd Ze
jjdd Zdd Ze
jjdd Zdd ZdS )CompoundSelectTestTc                 C   rm   rn   ro   r#   r&   r&   r'   r(      rp   z CompoundSelectTest.define_tablesc              
   C   rq   rr   r.   r2   r&   r&   r'   r4      rs   zCompoundSelectTest.insert_datar&   c                 C   rt   r6   r7   ru   r&   r&   r'   r=   
  rw   z!CompoundSelectTest._assert_resultc                 C   s`   | j j}t|g|jjdk}t|g|jjdk}t||}| ||jjddg d S Nr   r   rz   r~   )	r0   r   r   whererA   r   r   r=   r@   r;   rY   s1s2u1r&   r&   r'   test_plain_union  s
   
z#CompoundSelectTest.test_plain_unionc                 C   sh   | j j}t|g|jjdk}t|g|jjdk}t||  }| |	|jjddg d S r   )
r0   r   r   r   rA   r   r   aliasr=   r@   r   r&   r&   r'   test_select_from_plain_union  s
   z/CompoundSelectTest.test_select_from_plain_unionc                 C   s   | j j}t|g|jjdkd|jj}t|g|jjdkd|jj}t||d}| 	||jjddg d S Nr   r*   r   rz   r~   )
r0   r   r   r   rA   r   r{   r@   r   r=   r   r&   r&   r'   &test_limit_offset_selectable_in_unions  s   

z9CompoundSelectTest.test_limit_offset_selectable_in_unionsc                 C   sz   | j j}t|g|jjdk|jj}t|g|jjdk|jj}t||d}| 	||jjddg d S r   )
r0   r   r   r   rA   r   r@   r   r{   r=   r   r&   r&   r'   "test_order_by_selectable_in_unions1  s
   ""z5CompoundSelectTest.test_order_by_selectable_in_unionsc                 C   sn   | j j}t|g|jjdk }t|g|jjdk }t||d}| 	|
|jjddg d S r   )r0   r   r   r   rA   r   Zdistinctr   r{   r=   r@   r   r&   r&   r'   "test_distinct_selectable_in_unions:  s
   z5CompoundSelectTest.test_distinct_selectable_in_unionsc                 C   s   | j j}t|g|jjdkd|jj}t|g|jjdkd|jj}t||	 }| 
| d|jjddg d S r   )r0   r   r   r   rA   r   r{   r@   r   r   r=   r   r&   r&   r'   &test_limit_offset_in_unions_from_aliasB  s   

z9CompoundSelectTest.test_limit_offset_in_unions_from_aliasc                 C   s   | j j}t|g|jjdkd|jj  }t|g|jjdkd|jj  }t	||d}| 
||jjddg d S r   )r0   r   r   r   rA   r   r{   r@   r   r   r=   r   r&   r&   r'   .test_limit_offset_aliased_selectable_in_unionsX  s"   
	
	zACompoundSelectTest.test_limit_offset_aliased_selectable_in_unionsNr   )rD   rE   rF   rG   rH   r(   r4   r=   r   r   r   r?   Zorder_by_col_from_unionZ/parens_in_union_contained_select_w_limit_offsetr   Z0parens_in_union_contained_select_wo_limit_offsetr   r   r   r   r&   r&   r&   r'   r      s$    
	



r   c                   @   s   e Zd ZdZedd Zedd Zd ddZd	d
 Ze	j
jdd Ze	j
jdd Zdd Ze	j
jdd Ze	j
jdd Zdd Zdd Zdd Zdd Zdd ZdS )!ExpandingBoundInTestTc              
   C   s6   t d|tdtddtdttdttdtd d S )	Nr   r   Tr   rJ   rK   zrM   r"   r#   r&   r&   r'   r(   r  s   z"ExpandingBoundInTest.define_tablesc                 C   sH   | | jj dddddddddddddddddd	d
dg d S )Nr*   r   Zz1)r   rJ   rK   r   r   z2rQ   z3r\   z4r.   r2   r&   r&   r'   r4   }  s   
z ExpandingBoundInTest.insert_datar&   c                 C   rt   r6   r7   ru   r&   r&   r'   r=     rw   z#ExpandingBoundInTest._assert_resultc                 C   sj   | j j}t|jjg|jjtddd|jj	tddd
|jj}| j|g g g dd d S )NrL   T	expandingrN   )rL   rN   r   )r0   r   r   rA   r   r   rJ   r   r	   rK   r@   r=   r;   rY   ri   r&   r&   r'   test_multiple_empty_sets  s   
z-ExpandingBoundInTest.test_multiple_empty_setsc                 C   Z   | j j}t|jjgt|jj|jj	t
ddd|jj}| j|g dg id d S NrL   Tr   r   r0   r   r   rA   r   r   r   rJ   r   r   r	   r@   r=   r   r&   r&   r'   test_empty_heterogeneous_tuples     


z4ExpandingBoundInTest.test_empty_heterogeneous_tuplesc                 C   r   r   r0   r   r   rA   r   r   r   rJ   rK   r   r	   r@   r=   r   r&   r&   r'   test_empty_homogeneous_tuples  r   z2ExpandingBoundInTest.test_empty_homogeneous_tuplesc                 C   sX   | j j}t|jjg|jjtddd	|jj}| j
|g ddg did d S )NrL   Tr   rU   rV   rQ   )r   r   rQ   r   r0   r   r   rA   r   r   rJ   r   r	   r@   r=   r   r&   r&   r'   test_bound_in_scalar  s   
 z)ExpandingBoundInTest.test_bound_in_scalarc                 C   b   | j j}t|jjgt|jj|jj	t
ddd|jj}| j|g ddg did d S )NrL   Tr   r   ))r   r   )r   rQ   )rQ   r\   r   r   r   r&   r&   r'   test_bound_in_two_tuple  s   



z,ExpandingBoundInTest.test_bound_in_two_tuplec                 C   r   )NrL   Tr   r   ))r   r   )r   r   )rQ   r   r   r   r   r&   r&   r'   %test_bound_in_heterogeneous_two_tuple  s   




z:ExpandingBoundInTest.test_bound_in_heterogeneous_two_tuplec                 C   P   | j j}t|jjg|jjtddd	|jj}| j
|g dg id d S r   r   r   r&   r&   r'   test_empty_set_against_integer     
z3ExpandingBoundInTest.test_empty_set_against_integerc                 C   T   | j j}t|jjg|jjtddd	|jj}| j
|g ddg id d S NrL   Tr   )rT   rU   rV   r   r   )r0   r   r   rA   r   r   rJ   notin_r	   r@   r=   r   r&   r&   r'   'test_empty_set_against_integer_negation     
z<ExpandingBoundInTest.test_empty_set_against_integer_negationc                 C   r   r   )r0   r   r   rA   r   r   r   r   r	   r@   r=   r   r&   r&   r'   test_empty_set_against_string  r   z2ExpandingBoundInTest.test_empty_set_against_stringc                 C   r   r   )r0   r   r   rA   r   r   r   r   r	   r@   r=   r   r&   r&   r'   &test_empty_set_against_string_negation  r   z;ExpandingBoundInTest.test_empty_set_against_string_negationc              	   C   sL   t tt tddddt fgt dg}ttj	|
 d d d S )NZfoor&   T)valuer   )Zelse_    )Fr   )r   r
   r   r   r	   r   r   r   r8   r/   Zfetchone)r;   ri   r&   r&   r'   test_null_in_empty_set_is_false  s   z4ExpandingBoundInTest.test_null_in_empty_set_is_falseNr   )rD   rE   rF   rG   rH   r(   r4   r=   r   r   r?   Ztuple_in_w_emptyr   r   r   Ztuple_inr   r   r   r   r   r   r   r&   r&   r&   r'   r   o  s,    







r   c                   @   s   e Zd ZdZdZdZedd Zedd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdS )&LikeFunctionsTestTonceNc                 C   r   )Nr   r   Tr   r    rM   r"   r#   r&   r&   r'   r(   0  r)   zLikeFunctionsTest.define_tablesc                 C   sh   | | jj ddddddddddd	dd
dddddddddddddddddg
 d S )Nr*   Zabcdefgr,   r   zab/cdefgr   zab%cdefgrQ   Zab_cdefgr\   zabcde/fg   zabcde%fgr^   zab#cdefg   Zab9cdefg	   zabcde#fg
   Zabcd9fgr.   r2   r&   r&   r'   r4   9  s   
zLikeFunctionsTest.insert_datac                 C   sd   | j j}tj }dd |t|jjg	|D }W d    n1 s&w   Y  t
|| d S )Nc                 S   s   h | ]\}|qS r&   r&   ).0r   r&   r&   r'   	<setcomp>O  s    z*LikeFunctionsTest._test.<locals>.<setcomp>)r0   r   r   r8   connectr/   r   rA   r   r   r   )r;   rh   expectedr   connrowsr&   r&   r'   _testK  s   zLikeFunctionsTest._testc                 C   &   | j jjj}| |dh d d S )Nab%c
   r*   r   r   rQ   r\   r   r^   r   r   r   r0   r   rA   r    r   
startswithr;   colr&   r&   r'   test_startswith_unescapedX     z+LikeFunctionsTest.test_startswith_unescapedc                 C   (   | j jjj}| |jddddh d S )Nr   T
autoescaper   r   r   r&   r&   r'   test_startswith_autoescape\     z,LikeFunctionsTest.test_startswith_autoescapec                 C   *   | j jjj}| |tdh d d S )Nz'ab%c'r   )r0   r   rA   r    r   r   r   r   r&   r&   r'   test_startswith_sqlexpr`  s
   z)LikeFunctionsTest.test_startswith_sqlexprc                 C   r   )Nzab##c#escaper^   r   r   r&   r&   r'   test_startswith_escapeg  r   z(LikeFunctionsTest.test_startswith_escapec                 C   D   | j jjj}| |jdddddh | |jdddddh d S )Nr   Tr   r   r   r   zab#cr^   r   r   r&   r&   r'   !test_startswith_autoescape_escapek     z3LikeFunctionsTest.test_startswith_autoescape_escapec                 C   r   )Ne%fg	   r*   r   r   rQ   r\   r   r^   r   r   r0   r   rA   r    r   endswithr   r&   r&   r'   test_endswith_unescapedp  r   z)LikeFunctionsTest.test_endswith_unescapedc                 C   r   )Nz'e%fg'r   )r0   r   rA   r    r   r   r   r   r&   r&   r'   test_endswith_sqlexprt  s   z'LikeFunctionsTest.test_endswith_sqlexprc                 C   r   )Nr   Tr   r   r   r   r&   r&   r'   test_endswith_autoescapez  r   z*LikeFunctionsTest.test_endswith_autoescapec                 C   r   )Nze##fgr   r   r   r   r   r&   r&   r'   test_endswith_escape~  r   z&LikeFunctionsTest.test_endswith_escapec                 C   r   )Nr   Tr   r   r   ze#fgr   r   r   r&   r&   r'   test_endswith_autoescape_escape  r   z1LikeFunctionsTest.test_endswith_autoescape_escapec                 C   r   )Nb%cder   r0   r   rA   r    r   containsr   r&   r&   r'   test_contains_unescaped  r   z)LikeFunctionsTest.test_contains_unescapedc                 C   r   )Nr   Tr   r   r   r   r&   r&   r'   test_contains_autoescape  r   z*LikeFunctionsTest.test_contains_autoescapec                 C   r   )Nzb##cder   r   r^   r   r   r&   r&   r'   test_contains_escape  r   z&LikeFunctionsTest.test_contains_escapec                 C   r   )Nzb%cdTr   r   r   zb#cdr^   r   r   r&   r&   r'   test_contains_autoescape_escape  r   z1LikeFunctionsTest.test_contains_autoescape_escape)rD   rE   rF   rG   Zrun_insertsZrun_deletesrH   r(   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r   *  s.    

r   c                   @   s<   e Zd ZdZdZedd Zedd Zdd Zd	d
 Z	dS )ComputedColumnTestT)Zcomputed_columnsc                 C   s>   t d|tdtddtdttdttdtdttd	 d S )
Nsquarer   Tr   sideareazside * side	perimeterz4 * side)r   r   r   r   r#   r&   r&   r'   r(     s   z ComputedColumnTest.define_tablesc                 C   s(   | | jj ddddddg d S )Nr*   r   )r   r   *   )r/   r0   r   r1   r2   r&   r&   r'   r4     s   
zComputedColumnTest.insert_datac                 C   sn   t j (}|ttdg| jj	| jjj
j }t|ddg W d    d S 1 s0w   Y  d S )N*)r*   r   r!   (   )r   r        )r   r8   r   r/   r   r   select_fromr0   r   r@   rA   r   r9   r   r;   r   resr&   r&   r'   test_select_all  s   
"z"ComputedColumnTest.test_select_allc                 C   s|   t j /}|t| jjjj| jjjj	g
| jj| jjjj }t|ddg W d    d S 1 s7w   Y  d S )N)r!   r  )r  r  )r   r8   r   r/   r   r0   r   rA   r   r   r  r@   r   r9   r   r  r&   r&   r'   test_select_columns  s   
"z&ComputedColumnTest.test_select_columnsN)
rD   rE   rF   rG   __requires__rH   r(   r4   r  r	  r&   r&   r&   r'   r     s    


	r   c                   @   s8   e Zd ZdZedd Zedd Zdd Zdd	 Zd
S )
ExistsTestTc                 C   r   )Nstuffr   Tr   r    rM   r"   r#   r&   r&   r'   r(     r)   zExistsTest.define_tablesc              	   C   s8   | | jj ddddddddddddg d S )Nr*   	some datar,   r   r   rQ   zsome other data)r/   r0   r  r1   r2   r&   r&   r'   r4     s   
zExistsTest.insert_datac              	   C   sB   | j j}t|ttdgt |jj	dk
 dg d S )Nr*   r  rT   r0   r  r   r/   r   r   r   r   rA   r    r9   r;   r3   r  r&   r&   r'   test_select_exists  s   zExistsTest.test_select_existsc              	   C   s@   | j j}t|ttdgt |jj	dk
 g  d S )Nr*   zno datar  r  r&   r&   r'   test_select_exists_false  s   z#ExistsTest.test_select_exists_falseN)	rD   rE   rF   rG   rH   r(   r4   r  r  r&   r&   r&   r'   r    s    

r  c                	   @   s@   e Zd ZdZdZedd Zejddddd	d
dddd Z	dS )IsOrIsNotDistinctFromTestT)Zsupports_is_distinct_fromc              
   C   s2   t d|tdtddtdtddtdtdd d S )Nis_distinct_testr   Tr   col_a)Znullablecol_bro   r#   r&   r&   r'   r(     s   z'IsOrIsNotDistinctFromTest.define_tables)Zboth_int_differentr   r*   r*   )Zboth_int_samer*   r*   r   )Zone_null_firstNr*   r*   )Zone_null_secondr   Nr*   )Z	both_nullNNr   Ziaaaz3col_a_value, col_b_value, expected_row_count_for_is)Zid_argnamesc                 C   s   | j j}|| d||dg |||jj|jj	 }t
t|| |dkr/dnd}|||jj|jj	 }t
t|| d S )Nr*   )r   r  r  r   )r0   r  r/   r1   r   rA   r  Zis_distinct_fromr  r9   r   lenZisnot_distinct_from)r;   Zcol_a_valueZcol_b_valueZexpected_row_count_for_isr3   Ztblr<   Zexpected_row_count_for_isnotr&   r&   r'   test_is_or_isnot_distinct_from  s2   z8IsOrIsNotDistinctFromTest.test_is_or_isnot_distinct_fromN)
rD   rE   rF   rG   r
  rH   r(   r   combinationsr  r&   r&   r&   r'   r    s    
		r  N)% r   r   Z
assertionsr   r   Zschemar   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z
TablesTestr   rI   rl   r   r   r   r   r  r  r&   r&   r&   r'   <module>   sD    %R_~ <o+/