o
    [h                     @   s  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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\Z$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*Z.dS )+    N   )assert_raises_message)config)engines)eq_)expect_warnings)fixtures)is_)temp_table_keyword_argsColumn)Table   )event)exc)
ForeignKey)inspect)Integer)MetaData)String)testing)types)	Inspector)DDL)Index)quoted_name)is_false)is_true)NNc                   @   s4   e Zd ZdZedd Zdd Zejj	dd Z
dS )	HasTableTestTc                 C   s\   t d|tdtddtdtd tjjjr,t d|tdtddtdtdtj	d d S d S )	N
test_tableidTprimary_keydata2   test_table_sschema)
r   r   r   r   r   requiresschemasenabledr   test_schemaclsmetadata r/   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/testing/suite/test_reflection.pydefine_tables&   s   

zHasTableTest.define_tablesc                 C   sl   t j '}tt jj|d tt jj|d tt jj|d W d    d S 1 s/w   Y  d S )Nr   r%   nonexistent_table)r   dbbeginr   dialect	has_tabler   selfconnr/   r/   r0   test_has_table7   s
   "zHasTableTest.test_has_tablec                 C   s~   t j 0}tt jjj|dt jd tt jjj|dt jd tt jjj|dt jd W d    d S 1 s8w   Y  d S )Nr   r&   r%   r2   )r   r3   r4   r   r5   r6   r+   r   r7   r/   r/   r0   test_has_table_schema=   s"   "z"HasTableTest.test_has_table_schemaN)__name__
__module____qualname____backend__classmethodr1   r:   r   r(   r)   r;   r/   r/   r/   r0   r   #   s    
r   c                   @   s   e Zd ZdZdZedd Zdd Zedd Zee	j
jd	d
 Zedd Zedd Zedd Zedd Zee	j
jdd Zee	j
jdd Zee	j
jdd ZdS )QuotedNameArgumentTestonceTc                 C   s  t d|tdttdtdtdtdtdttjdddtd	dtjdd
dtjdgdgddtj	dddddd t
jjjr}t d|tdttdtdtdtdtdttjdddtddtjdddtjdgdgddtj	dddddd t d|tdtddtdtdd t
jjjrt
jjjrddg}ndg}|D ]4}dt
jjjd| t
jjj|f }t|dt| t|dtd t
jjjd|   qd S d S )!Nquote ' oner    namer$   r#   Z
related_idzpk quote ' onerD   zix quote ' onezuq quote' onez
related.idzfk quote ' onezname != 'foo'zck quote ' onezquote ' one commentT)commenttest_needs_fkquote " twozpk quote " twozix quote " twozuq quote" twozfk quote " twozck quote " two zquote " two commentrelatedr!   rG   "CREATE VIEW %s AS SELECT * FROM %sview %safter_createbefore_dropDROP VIEW %s)r   r   r   r   saPrimaryKeyConstraintr   UniqueConstraintForeignKeyConstraintCheckConstraintr   r(   symbol_names_w_double_quoter*   view_column_reflectionr3   r5   Zidentifier_preparerquoter   listenr   )r-   r.   namesrD   queryr/   r/   r0   r1   U   s   








z$QuotedNameArgumentTest.define_tablesc                 C   s   t ddt jjf| S )N)rC   rH   )r   combinationsr(   rU   )fnr/   r/   r0   quote_fixtures   s   
z%QuotedNameArgumentTest.quote_fixturesc                 C   s   t tj}|| d S N)r   r   r3   Zget_table_optionsr8   rD   inspr/   r/   r0   test_get_table_options   s   
z-QuotedNameArgumentTest.test_get_table_optionsc                 C   s    t tj}|d| sJ d S )NrL   )r   r   r3   get_view_definitionr_   r/   r/   r0   test_get_view_definition   s   
z/QuotedNameArgumentTest.test_get_view_definitionc                 C      t tj}||sJ d S r^   )r   r   r3   get_columnsr_   r/   r/   r0   test_get_columns      
z'QuotedNameArgumentTest.test_get_columnsc                 C   rd   r^   )r   r   r3   get_pk_constraintr_   r/   r/   r0   test_get_pk_constraint   rg   z-QuotedNameArgumentTest.test_get_pk_constraintc                 C   rd   r^   )r   r   r3   get_foreign_keysr_   r/   r/   r0   test_get_foreign_keys   rg   z,QuotedNameArgumentTest.test_get_foreign_keysc                 C   rd   r^   )r   r   r3   get_indexesr_   r/   r/   r0   test_get_indexes   rg   z'QuotedNameArgumentTest.test_get_indexesc                 C   rd   r^   )r   r   r3   get_unique_constraintsr_   r/   r/   r0   test_get_unique_constraints      
z2QuotedNameArgumentTest.test_get_unique_constraintsc                 C   rd   r^   )r   r   r3   get_table_commentr_   r/   r/   r0   test_get_table_comment   rp   z-QuotedNameArgumentTest.test_get_table_commentc                 C   rd   r^   )r   r   r3   get_check_constraintsr_   r/   r/   r0   test_get_check_constraints   rp   z1QuotedNameArgumentTest.test_get_check_constraintsN)r<   r=   r>   Zrun_create_tablesr?   r@   r1   r]   ra   r   r(   rV   rc   rf   ri   rk   rm   unique_constraint_reflectionro   comment_reflectionrr   check_constraint_reflectionrt   r/   r/   r/   r0   rA   Q   s8    
S




rA   c                   @   s  e Zd Zd ZZdZedd Zedd Zedd Z	ed	d
 Z
edd Zedd Zejjdd Zejjdd Zejjdd Zej	dddZejjdd Zejjejjejjdd Zejjdd Zejjejjdd Zejjd d! Zejjejj d"d# Z!dd$d%Z"ejjejj d&d' Z#ejj$d(d) Z%ejj$ejj d*d+ Z&ejjejj$d,d- Z'dd.d/Z(ejjd0d1 Z)ejd2d3 Z*ejjd4d5 Z+ejjd6d7 Z,ejjejd8d9 Z-ejjejj d:d; Z.ejj/d<d= Z0ejj/ejj$ejjd>d? Z1ejj$d@dA Z2ejj$ejj dBdC Z3ejddDdEZ4ejj5dFdG Z6ejjejj5ejj dHdI Z7ejjejdJdK Z8ejddLdMZ9ejjdNdO Z:ejjejj dPdQ Z;ejj<ejj dRdS Z=ejj>dTdU Z?ejj@dVdW ZAejdXdY ZBdZd[ ZCejdd\d]ZDejjEd^d_ ZFejjEejj d`da ZGejdbdc ZHejjEejjIddde ZJejjEejjIdfdg ZKejjLejdhdi ZMejjNdjdk ZOejj/ejjNdldm ZPejj/dndo ZQejjNejj dpdq ZRejddrdsZSejjTdtdu ZUejjTejj dvdw ZVejddxdyZWejddzd{ZXejjd|d} ZYejjejj d~d ZZe[ddejdddZ\dd Z]ejj dd Z^ejjejdd Z_dS )ComponentReflectionTestNTc                 C   s0   t jjjrddlm} tjt|j	ddS t j
S )Nr   )pool)Z	poolclass)options)r   requirementsZindependent_connectionsr*   
sqlalchemyry   r   testing_enginedictZ
StaticPoolr3   )r-   ry   r/   r/   r0   
setup_bind   s   

z"ComponentReflectionTest.setup_bindc                 C   s.   |  |d  tjjjr|  |tjj d S d S r^   )define_reflected_tablesr   r(   r)   r*   r   r+   r,   r/   r/   r0   r1      s   
z%ComponentReflectionTest.define_tablesc                 C   s*  |r|d }nd}t jjjr=td|tdtjddtdtdd	d
tdt	dd	d
tdtj
tjd| dd|dd}n!td|tdtjddtdtdd	d
tdt	dd	d
|dd}td|tdtj
ddtdtj
td| tdtd|dd td|tdtj
tdtj
t|jjtdtdtjddd|dd td|tdtj
dddtdtdd d!td"tdd#d!|d$d% t jjjr+|d u rtd&|tdtj
ddtdtdtd'td(t jj dtjjjd) n7td*|tdtj
ddtd+td,tjjj tdtd|dd td-|tdtj
ddtdtd|dd t jjjru| || |sutd.|td/tddd0}td1|tdtj
ddtd/tddd0}t jjjrutd2|jj  td3|jj  t jjjr| || |st jjjr| | d S d S d S )4N. usersuser_idTr!   test1   Fnullabletest2parent_user_idz%susers.user_id
user_id_fkrE   r'   rG   
dingalingsdingaling_id
address_idz%semail_addresses.address_idr#      email_addressesremote_user_idZemail_address   email_ad_pkcomment_testr    
id comment)r"   rF   data % comment)rF   d2)Comment types type speedily ' " \ '' Fun!the test % ' " \ table comment)r'   rF   local_table	remote_idz%s.remote_table_2.id)rG   r'   remote_tablelocal_idz%s.local_table.idremote_table_2noncol_idx_test_nopkqrJ   noncol_idx_test_pknoncol_idx_nopknoncol_idx_pk) r   r(   self_referential_foreign_keysr*   r   r   rP   INTZCHARFloatr   r   r   cr   rQ   cross_schema_fk_reflectionr   r+   r3   r5   default_schema_nameindex_reflectiondefine_indexindexes_with_ascdescr   r   ZdescrV   define_viewstemp_table_reflectiondefine_temp_tables)r-   r.   r'   Zschema_prefixr   r   r   r/   r/   r0   r      s  




	z/ComponentReflectionTest.define_reflected_tablesc                 C   s   t ttj}td|tdtjddtdtdtdtjtjddd	t	d
dfi |}t
jjjrKt
jjjrMt|dtd t|dtd d S d S d S )Nuser_tmpr    Tr!   rD   r$   foouser_tmp_uqrE   user_tmp_ixrM   z:create temporary view user_tmp_v as select * from user_tmprN   zdrop view user_tmp_v)r
   r   r3   r   r   rP   r   VARCHARrR   r   r   r(   view_reflectionr*   temporary_viewsr   rX   r   )r-   r.   kwr   r/   r/   r0   r     s2   

z*ComponentReflectionTest.define_temp_tablesc                 C   s2   t d|jj|jj t d|jj|jj|jj d S )Nusers_t_idxusers_all_idx)r   r   r   r   r   )r-   r.   r   r/   r/   r0   r     s   z$ComponentReflectionTest.define_indexc                 C   s^   dD ]*}|}|rd||f }|d }d||f }t |dt| t |dtd|  qd S )N)r   r   z%s.%s_vrK   rM   rN   rO   )r   rX   r   )r-   r.   r'   
table_namefullnameZ	view_namerZ   r/   r/   r0   r     s   z$ComponentReflectionTest.define_viewsc                 C   s$   t tj}| tjj| v  d S r^   )r   r   r3   assert_r   r+   Zget_schema_namesr8   r`   r/   r/   r0   test_get_schema_names  s   
z-ComponentReflectionTest.test_get_schema_namesc                 C   s4   t  }t|jdrJ t| t|jdsJ d S )Nr   )r   r}   hasattrr5   r   )r8   Zenginer/   r/   r0   test_dialect_initialize  s   z/ComponentReflectionTest.test_dialect_initializec                 C   s    t tj}t|jtjjj d S r^   )r   r   r3   r   r   r5   r   r/   r/   r0   test_get_default_schema_name  s   
z4ComponentReflectionTest.test_get_default_schema_nametablec           	         s   g d | j }t|j}|dkr&||}|  ddg}tt|| d S |r3dd ||D }n||} fdd|D }|dkrPg d	}t|| d S g d
}tt|| d S )N)r   r   r   r   r   r   viewemail_addresses_vusers_vc                 S   s   g | ]
}|d  r|d  qS )r   r/   .0Zrecr/   r/   r0   
<listcomp>  s    zAComponentReflectionTest._test_get_table_names.<locals>.<listcomp>c                    s   g | ]}| vr|qS r/   r/   r   tZ_ignore_tablesr/   r0   r     s    foreign_key)r   r   r   )r   r   r   )	r.   r   bindZget_view_namessortr   sortedZget_sorted_table_and_fkc_namesget_table_names)	r8   r'   
table_typeorder_bymetar`   table_namesZanswertablesr/   r   r0   _test_get_table_names  s&   


z-ComponentReflectionTest._test_get_table_namesc                 C   &   t | j}| }tt|dg d S )Nr   )r   r   Zget_temp_table_namesr   r   r8   r`   temp_table_namesr/   r/   r0   test_get_temp_table_names  s   
z1ComponentReflectionTest.test_get_temp_table_namesc                 C   r   )N
user_tmp_v)r   r   Zget_temp_view_namesr   r   r   r/   r/   r0   test_get_temp_view_names  s   
z0ComponentReflectionTest.test_get_temp_view_namesc                 C      |    d S r^   r   r8   r/   r/   r0   test_get_table_names      z,ComponentReflectionTest.test_get_table_namesc                 C      | j dd d S )Nr   )r   r   r   r/   r/   r0   test_get_table_names_fks     z0ComponentReflectionTest.test_get_table_names_fksc                 C   r   r^   )_test_get_commentsr   r/   r/   r0   test_get_comments	  r   z)ComponentReflectionTest.test_get_commentsc                 C      |  tjj d S r^   )r   r   r   r+   r   r/   r/   r0   test_get_comments_with_schema     z5ComponentReflectionTest.test_get_comments_with_schemac                 C   st   t tj}t|jd|dddi t|jd|ddd i tdd |jd|dD dd	d
ddd
ddd
g d S )Nr   r&   textr   r   c                 S   s   g | ]}|d  |d dqS )rD   rF   )rD   rF   r/   r   r/   r/   r0   r     s    z>ComponentReflectionTest._test_get_comments.<locals>.<listcomp>r   r    )rF   rD   r   r#   r   r   )r   r   r3   r   rq   re   )r8   r'   r`   r/   r/   r0   r     s"   
z*ComponentReflectionTest._test_get_commentsc                 C   r   r^   r   r   r   r+   r   r/   r/   r0    test_get_table_names_with_schema-  r   z8ComponentReflectionTest.test_get_table_names_with_schemac                 C   r   Nr   )r   r   r   r/   r/   r0   test_get_view_names2     z+ComponentReflectionTest.test_get_view_namesc                 C      | j tjjdd d S r   r   r   r/   r/   r0   test_get_view_names_with_schema6     
z7ComponentReflectionTest.test_get_view_names_with_schemac                 C   s   |    | j dd d S r   r   r   r/   r/   r0   test_get_tables_and_views=  s   z1ComponentReflectionTest.test_get_tables_and_viewsc                 C   sj  t tj}| jj| jj}}ddg}|dkrddg}t|j}t|||fD ]\}}	|}
|j	||
d}| 
t|dkt| t|	jD ]m\}}t|j|| d  || d	 j}|j}t|tjjrf|j}td
rv|tjtjfv rvtj}| 
tt|j|jtjtjtjtjtjtj tj!gdkd|j|j|| d |f  |j"s|| d d u sJ qDq&d S )Nr   r   r   r   r   r&   r   rD   typeoraclez%s(%s), %s(%s)default)#r   r   r3   r   r   r   r   r   zipre   r   len	enumeratecolumnsr   rD   	__class__r   
isinstancerP   r   Z
TypeEngineZagainst	sql_typesDateDateTimeset__mro__intersectionr   NumericZTimer   Z_Binaryr"   )r8   r'   r   r   r   	addressesr   r`   r   r   Zschema_namecolsicolctypeZ	ctype_defr/   r/   r0   _test_get_columnsC  s^   

z)ComponentReflectionTest._test_get_columnsc                 C   r   r^   r  r   r/   r/   r0   rf   {  r   z(ComponentReflectionTest.test_get_columnsc                 G   sF   t d| jgdd t|D R  }|  dd t| jjdD S )Nr   c                 S   s   g | ]\}}t d | |qS )zt%dr   )r   r  type_r/   r/   r0   r     s    z<ComponentReflectionTest._type_round_trip.<locals>.<listcomp>c                 S      g | ]}|d  qS )r   r/   r   r   r/   r/   r0   r     s    )r   r.   r   creater   r   re   )r8   r   r   r/   r/   r0   _type_round_trip  s   z(ComponentReflectionTest._type_round_tripc                 C   sD   |  tddD ]}t|tjsJ t|jd t|jd q	d S )N   r   )r  r   r  r   r   	precisionscaler8   typr/   r/   r0   test_numeric_reflection  s
   z/ComponentReflectionTest.test_numeric_reflectionc                 C   s4   |  tdd }t|tjsJ t|jd d S )N4   r   )r  r   r   r   r   lengthr  r/   r/   r0   test_varchar_reflection  s   z/ComponentReflectionTest.test_varchar_reflectionc              	   C   s\   t d| jtdtddtdtdd}|  ttdd t| jj	dD ddd	 d S )
Nr   aTr   bFc                 s   s     | ]}|d  |d fV  qdS )rD   r   Nr/   r   r	  r/   r/   r0   	<genexpr>  s
    
zCComponentReflectionTest.test_nullable_reflection.<locals>.<genexpr>)r  r  )
r   r.   r   r   r  r   r~   r   r   re   )r8   r   r/   r/   r0   test_nullable_reflection  s   z0ComponentReflectionTest.test_nullable_reflectionc                 C      | j tjjd d S Nr&   r  r   r   r+   r   r/   r/   r0   test_get_columns_with_schema     z4ComponentReflectionTest.test_get_columns_with_schemac                 C   sj   t | j}| jj}t|j}|d}| t|dkt| t|j	D ]\}}t
|j|| d  q$d S )Nr   r   rD   )r   r   r   r   r   re   r   r   r   r   r   rD   )r8   r   r   r`   r  r  r	  r/   r/   r0   test_get_temp_table_columns  s   


z3ComponentReflectionTest.test_get_temp_table_columnsc                 C   s0   t | j}|d}tdd |D g d d S )Nr   c                 S   r  rE   r/   r  r/   r/   r0   r         zFComponentReflectionTest.test_get_temp_view_columns.<locals>.<listcomp>)r    rD   r   )r   r   re   r   )r8   r`   r  r/   r/   r0   test_get_temp_view_columns  s   

z2ComponentReflectionTest.test_get_temp_view_columnsc                 C   r   r   r  r   r/   r/   r0   test_get_view_columns  r   z-ComponentReflectionTest.test_get_view_columnsc                 C   r   )Nr   )r'   r   r"  r   r/   r/   r0   !test_get_view_columns_with_schema  r   z9ComponentReflectionTest.test_get_view_columns_with_schemac           
      C   s   | j }| jj| jj}}t|j}|j|j|d}|d }t|dg |j|j|d}|d }	t|	dg t	j
j  t|d d W d    d S 1 sNw   Y  d S )Nr&   constrained_columnsr   r   rD   r   )r.   r   r   r   r   r   rh   rD   r   r   r(   Zreflects_pk_namesfail_if)
r8   r'   r   r   r  r`   Z
users_consZusers_pkeysZ	addr_consZ
addr_pkeysr/   r/   r0   _test_get_pk_constraint  s   
"z/ComponentReflectionTest._test_get_pk_constraintc                 C   r   r^   )r,  r   r/   r/   r0   ri     r   z.ComponentReflectionTest.test_get_pk_constraintc                 C   r   r!  )r,  r   r   r+   r   r/   r/   r0   "test_get_pk_constraint_with_schema  s   z:ComponentReflectionTest.test_get_pk_constraint_with_schemac                 C   s0   | j }| jj}t|j}ttjd|j|j	 d S )Nz+.*get_primary_keys\(\) method is deprecated)
r.   r   r   r   r   r   sa_excZSADeprecationWarningZget_primary_keysrD   )r8   r   r   r`   r/   r/   r0    test_deprecated_get_primary_keys  s   
z8ComponentReflectionTest.test_deprecated_get_primary_keysc           
      C   sb  | j }| jj| jj}}t|j}|}tjjj	re|j
|j|d}|d }tjj  t|d d W d    n1 s<w   Y  t|d | t|d |j t|d dg tjjj	ret|d	 d
g |j
|j|d}	|	d }tjj  | |d d u W d    n1 sw   Y  t|d | t|d |j t|d dg t|d	 dg d S )Nr&   r   rD   r   referred_schemareferred_tablereferred_columnsr   r*  r   r   )r.   r   r   r   r   r   r   r(   r   r*   rj   rD   Znamed_constraintsr+  r   Zimplicitly_named_constraintsr   )
r8   r'   r   r   r  r`   Zexpected_schemaZusers_fkeysfkey1Z
addr_fkeysr/   r/   r0   _test_get_foreign_keys  s0   


z.ComponentReflectionTest._test_get_foreign_keysc                 C   r   r^   )r4  r   r/   r/   r0   rk     r   z-ComponentReflectionTest.test_get_foreign_keysc                 C   r   r!  )r4  r   r   r+   r   r/   r/   r0   !test_get_foreign_keys_with_schema  r$  z9ComponentReflectionTest.test_get_foreign_keys_with_schemac           	      C   s  |  dtjjj dtjj dtjj \}}}ttj}||j	}t
t|d |d }t
|d tjj t
|d |j	 t
|d d	g t
|d
 dg |j|j	tjjd}t
t|d |d }|d d tjjjfv spJ t
|d |j	 t
|d d	g t
|d
 dg d S )Nz%s.local_tablez%s.remote_tablez%s.remote_table_2   r   r0  r1  r2  r    r*  r   r&   r   )r   r   r3   r5   r   r   r+   r   rj   rD   r   r   )	r8   r   r   r   r`   Zlocal_fkeysr3  Zremote_fkeysZfkey2r/   r/   r0   "test_get_inter_schema_foreign_keys   s2   





z:ComponentReflectionTest.test_get_inter_schema_foreign_keysc                 C   r   )NZCASCADE)Zondelete_test_get_foreign_key_optionsr   r/   r/   r0   %test_get_foreign_key_options_ondeleteC  r   z=ComponentReflectionTest.test_get_foreign_key_options_ondeletec                 C   r   )NzSET NULL)Zonupdater8  r   r/   r/   r0   %test_get_foreign_key_options_onupdateG  r   z=ComponentReflectionTest.test_get_foreign_key_options_onupdatec                    s  | j }td|tdtdddd td|tdtddtdttjdd	d
tdtddd td|tdtddtdtdddtdttjdgdgfddi|dd |  t	|j
}|dd d  tt fdd D i  |dd d  tt fdd D | d S )Nxr    Tr!   rJ   r   Zx_idzx.idxidrE   test
   userrD   r$   Fr   tidztable.idZmyfkr   rz   c                 3   $    | ]} | r| | fV  qd S r^   r/   r   koptsr/   r0   r  s     " zHComponentReflectionTest._test_get_foreign_key_options.<locals>.<genexpr>c                 3   rB  r^   r/   rC  rE  r/   r0   r  v  rG  )r.   r   r   r   rP   r   r   rS   
create_allr   r   rj   r   r~   )r8   rz   r   r`   r/   rE  r0   r9  K  sH   	
 z5ComponentReflectionTest._test_get_foreign_key_optionsc                 C   sZ   dd |D }|D ]!}|d |v sJ || |d  }|D ]}t|| ||  qq	d S )Nc                 S   r  rE   r/   )r   dr/   r/   r0   r   y  r&  z@ComponentReflectionTest._assert_insp_indexes.<locals>.<listcomp>rD   )indexr   )r8   indexesexpected_indexesZindex_namesZe_indexrJ  keyr/   r/   r0   _assert_insp_indexesx  s   z,ComponentReflectionTest._assert_insp_indexesc                 C   sN   | j }t|j}|jd|d}dddgdddg dd	dg}| || d S )
Nr   r&   Fr   r   r   uniquecolumn_namesrD   )r   r   r   r   )r.   r   r   rl   rN  )r8   r'   r   r`   rK  rL  r/   r/   r0   _test_get_indexes  s   
z)ComponentReflectionTest._test_get_indexesc                 C   r   r^   )rR  r   r/   r/   r0   rm     r   z(ComponentReflectionTest.test_get_indexesc                 C   r   r!  )rR  r   r   r+   r   r/   r/   r0   test_get_indexes_with_schema  r$  z4ComponentReflectionTest.test_get_indexes_with_schemac                 C   s   | j }t|j}||}d|dg}| || t|||jd}tt|jd t	t
|jd j| tt
|jd j| d S )NF)rP  rD   )autoload_withr6  r   )r.   r   r   rl   rN  r   r   r   rK  r	   listr   rD   )r8   tnameZixnamer   r`   rK  rL  r   r/   r/   r0   _test_get_noncol_index  s   

z.ComponentReflectionTest._test_get_noncol_indexc                 C      |  dd d S )Nr   r   rW  r   r/   r/   r0   test_get_noncol_index_no_pk  r   z3ComponentReflectionTest.test_get_noncol_index_no_pkc                 C   rX  )Nr   r   rY  r   r/   r/   r0   test_get_noncol_index_pk  r   z0ComponentReflectionTest.test_get_noncol_index_pkc                 C   s   t d| jtdtdtdtd t| jdtd t| jdtd | j  t| jj	}t
d t|dd	dgd
dg W d    d S 1 sOw   Y  d S )Nr   r<  r   yrM   z+CREATE INDEX t_idx ON t(lower(x), lower(y))zCREATE INDEX t_idx_2 ON t(x)z>Skipped unsupported reflection of expression-based index t_idxZt_idx_2r   )rD   rQ  rP  )r   r.   r   r   r   rX   r   rH  r   r   r   r   rl   r   r/   r/   r0   %test_reflect_expression_based_indexes  s0   
"z=ComponentReflectionTest.test_reflect_expression_based_indexesc                 C   r   r^   )_test_get_unique_constraintsr   r/   r/   r0   ro     r   z3ComponentReflectionTest.test_get_unique_constraintsc                 C   sB   t | j}|d}|D ]}|dd  qt|dgddg d S )Nr   duplicates_indexrD   r   )rQ  rD   )r   r   rn   popr   )r8   r`   	reflectedreflr/   r/   r0   &test_get_temp_table_unique_constraints  s
   

z>ComponentReflectionTest.test_get_temp_table_unique_constraintsc                 C   sN   t | j}|d}|D ]}|dd  qtdd |D ddgddg d S )	Nr   Zdialect_optionsc                 S   s   g | ]
}|d  dkr|qS )rD   r   r/   r   idxr/   r/   r0   r     s    zGComponentReflectionTest.test_get_temp_table_indexes.<locals>.<listcomp>Fr   r   rO  )r   r   rl   r`  r   )r8   r`   rK  indr/   r/   r0   test_get_temp_table_indexes  s   

z3ComponentReflectionTest.test_get_temp_table_indexesc                 C   r   r!  )r^  r   r   r+   r   r/   r/   r0   'test_get_unique_constraints_with_schema  r$  z?ComponentReflectionTest.test_get_unique_constraints_with_schemac                 C   s  t ddgddg dddg dddd	d
gdddgdddgdgtdd}| j}td|tdtdtdtdtdtjtd	tdtd
td|d}|D ]}|	tj
|d d|d i qW|  t|j}t |jd|dtdd}t }t||D ]\}	}
|
dd }|r|| t|	|
 qt }td||j|d}tdd |jD }tdd |jD dg}||rJ |rt|| t|t  d S d S )NZunique_ar  )rD   rQ  Zunique_a_b_c)r  r  r   Zunique_c_a_b)r   r  r  Zunique_asc_keyascrM  zi.have.dotsr  zi have spacesr   rD   rM  Ztesttblr   r   r&   rQ  r_  )rT  r'   c                 S   s   g | ]}|j qS r/   rE   rd  r/   r/   r0   r   ?  s    zHComponentReflectionTest._test_get_unique_constraints.<locals>.<listcomp>c                 S   s   g | ]}t |tjr|jqS r/   )r   rP   rR   rD   )r   Zuqr/   r/   r0   r   A  s    
)r   operator
itemgetterr.   r   r   rP   r   r   Zappend_constraintrR   rH  r   r   rn   r  r   r`  addr   r   rK  constraints
differencer  )r8   r'   Zuniques	orig_metar   Zuc	inspectorra  Znames_that_duplicate_indexorigrb  ZdupeZreflected_metadataZ	idx_namesZuq_namesr/   r/   r0   r^    sr   






z4ComponentReflectionTest._test_get_unique_constraintsc                 C   r   r^   )_test_get_check_constraintsr   r/   r/   r0   rt   M  r   z2ComponentReflectionTest.test_get_check_constraintsc                 C   r   r!  )rs  r   r   r+   r   r/   r/   r0   !test_get_check_constraints_schemaQ  r$  z9ComponentReflectionTest.test_get_check_constraints_schemac              	      s   | j }td|tdt tjdddtjddd|d |  t|j}t	|j
d|dtd	d
}dd   fdd|D }t|ddddddg d S )NZsa_ccr  za > 1 AND a < 5Zcc1rE   za = 1 OR (a > 2 AND a < 5)Zcc2r&   rD   rj  c                 S   s   d td|  tjS )N zand|\d|=|a|or|<|>)joinrefindalllowerI)sqltextr/   r/   r0   	normalizem  s   zFComponentReflectionTest._test_get_check_constraints.<locals>.normalizec                    s"   g | ]}|d   |d dqS )rD   r{  rD   r{  r/   )r   itemr|  r/   r0   r   r  s    zGComponentReflectionTest._test_get_check_constraints.<locals>.<listcomp>za > 1 and a < 5r}  za = 1 or a > 2 and a < 5)r.   r   r   r   rP   rT   rH  r   r   r   rs   rk  rl  r   )r8   r'   rp  rq  ra  r/   r  r0   rs  V  s2   
	

z3ComponentReflectionTest._test_get_check_constraintsc                 C   sL   | j }d}d}t|j}|j||d}| | |j||d}| | d S )Nr   r   r&   )r.   r   r   rb   r   )r8   r'   r   Z
view_name1Z
view_name2r`   Zv1Zv2r/   r/   r0   _test_get_view_definition~  s   

z1ComponentReflectionTest._test_get_view_definitionc                 C   r   r^   )r  r   r/   r/   r0   rc     r   z0ComponentReflectionTest.test_get_view_definitionc                 C   r   r!  )r  r   r   r+   r   r/   r/   r0   $test_get_view_definition_with_schema  r$  z<ComponentReflectionTest.test_get_view_definition_with_schema
postgresqlzPG specific featurec                 C   s0   | j }t|j}|||}| t|t d S r^   )r.   r   r   Zget_table_oidr   r   int)r8   r   r'   r   r`   oidr/   r/   r0   _test_get_table_oid  s   
z+ComponentReflectionTest._test_get_table_oidc                 C   s   |  d d S )Nr   )r  r   r/   r/   r0   test_get_table_oid  s   z*ComponentReflectionTest.test_get_table_oidc                 C   s   | j dtjjd d S )Nr   r&   )r  r   r   r+   r   r/   r/   r0   test_get_table_oid_with_schema  s   z6ComponentReflectionTest.test_get_table_oid_with_schemac                 C   sN   | j }t|j}dD ]\}}||}dd |D | }|dds$J q
dS )a  test that 'autoincrement' is reflected according to sqla's policy.

        Don't mark this test as unsupported for any backend !

        (technically it fails with MySQL InnoDB since "id" comes before "id2")

        A backend is better off not returning "autoincrement" at all,
        instead of potentially returning "False" for an auto-incrementing
        primary key column.

        ))r   r   )r   r   )r   r   c                 S      i | ]}|d  |qS rE   r/   r  r/   r/   r0   
<dictcomp>      zBComponentReflectionTest.test_autoincrement_col.<locals>.<dictcomp>autoincrementTN)r.   r   r   re   get)r8   r   r`   rV  cnamer  Zid_r/   r/   r0   test_autoincrement_col  s   

z.ComponentReflectionTest.test_autoincrement_col)Nr   Nr^   )Nr   )`r<   r=   r>   Zrun_insertsZrun_deletesr?   r@   r   r1   r   r   r   r   r   r(   Zschema_reflectionr   r   r   provide_metadatar   r   r   r   r   r   Ztable_reflectionr   !foreign_key_constraint_reflectionr   rv   r   r)   r   r   r   rV   r   r   r   r  rf   r  r  r  r  r#  r   r%  r'  r(  r)  r,  !primary_key_constraint_reflectionri   r-  r/  r4  rk   r5  r   r7  Z1foreign_key_constraint_option_reflection_ondeleter:  Z1foreign_key_constraint_option_reflection_onupdater;  r9  rN  rR  r   rm   rS  rW  r   rZ  r[  Zindexes_with_expressionsr]  ru   ro   rc  rg  rh  r^  rw   rt   rt  rs  r  rc   r  Zonly_onr  r  r  r  r/   r/   r/   r0   rx      s@   



 





&





8







 
!


,


	
N
'



rx   c                   @   s0   e Zd ZdZdZedd Zdd Zdd Zd	S )
NormalizedNameTest)Zdenormalized_namesTc                 C   sL   t tddd|tdtdd t tddd|tdtddtdtd d S )	Nt1TrW   r    r!   t2t1idzt1.id)r   r   r   r   r   r,   r/   r/   r0   r1     s   

z NormalizedNameTest.define_tablesc                 C   s   t tj}ttddd|dd}|jd }|jj|jj	s J t tj}|j
dd d |jd jj|jd jj	s>J d S )	Nr  Tr  )Zautoloadr  c                 S   s   |   dv S )Nr  r  ry  )rD   mr/   r/   r0   <lambda>  s    zINormalizedNameTest.test_reflect_lowercase_forced_tables.<locals>.<lambda>)only)r   r   r3   r   r   r   r   r  Z
referencesr    Zreflect)r8   m2Zt2_refZt1_refZm3r/   r/   r0   $test_reflect_lowercase_forced_tables  s   


&z7NormalizedNameTest.test_reflect_lowercase_forced_tablesc                 C   sP   dd t tj D }t|d  |d   t|d  |d   d S )Nc                 S   s   g | ]
}|  d v r|qS )r  r  r   r/   r/   r0   r     s
    z;NormalizedNameTest.test_get_table_names.<locals>.<listcomp>r   r6  )r   r   r3   r   r   upperry  )r8   Z
tablenamesr/   r/   r0   r     s
   z'NormalizedNameTest.test_get_table_namesN)	r<   r=   r>   __requires__r?   r@   r1   r  r   r/   r/   r/   r0   r    s    
r  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zejj	d	d
 Z
dS )ComputedReflectionTestc                 C   sV   t tj}|d}|D ]}|d dkrtd|d v  q|d s(t|d d  qd S )Ncomputed_column_tablerD   with_defaultZ42r   r  )r   r   r3   re   r   r	   )r8   r`   r  r	  r/   r/   r0   !test_computed_col_default_not_set  s   

z8ComputedReflectionTest.test_computed_col_default_not_setc                 C   s   t tj}|d}dd |D }dD ]
}td|| v q|d }td|v  td|d v  t| |d d d td	|d v tjj	j
 tjj	j
r[t|d d	 tjjj
 d S d S )
NZcomputed_default_tablec                 S   r  rE   r/   r  r/   r/   r0   r    r  zKComputedReflectionTest.test_get_column_returns_computed.<locals>.<dictcomp>)r    normalr  computedZcomputed_colr{  	normal+42	persisted)r   r   r3   re   r   r   r|  r   r(   "computed_columns_reflect_persistedr*   "computed_columns_default_persisted)r8   r`   r  r#   rM  compDatar/   r/   r0    test_get_column_returns_computed  s&   




z7ComputedReflectionTest.test_get_column_returns_computedc                 C   s\   t d|| v  || d }t| |d | tjjjr,t d|v  t|d | d S d S )Nr  r{  r  )r   r   r|  r   r(   r  r*   r	   )r8   r#   columnr{  r  r  r/   r/   r0   check_column  s   
z#ComputedReflectionTest.check_columnc                 C   st   t tj}|d}dd |D }| |ddtjjj tjj	jr)| |ddd tjj
jr8| |d	d
d d S d S )Nr  c                 S   r  rE   r/   r  r/   r/   r0   r    r  zLComputedReflectionTest.test_get_column_returns_persisted.<locals>.<dictcomp>computed_no_flagr  computed_virtualznormal+2Fcomputed_storedz	normal-42T)r   r   r3   re   r  r   r(   r  r*   computed_columns_virtualcomputed_columns_storedr8   r`   r  r#   r/   r/   r0   !test_get_column_returns_persisted  s0   



z8ComputedReflectionTest.test_get_column_returns_persistedc                 C   sz   t tj}|jdtjd}dd |D }| |ddtjjj	 tjj
j	r,| |ddd	 tjjj	r;| |d
dd d S d S )Nr  r&   c                 S   r  rE   r/   r  r/   r/   r0   r  /  r  zXComputedReflectionTest.test_get_column_returns_persisted_with_schema.<locals>.<dictcomp>r  z	normal/42r  znormal/2Fr  z	normal*42T)r   r   r3   re   r+   r  r   r(   r  r*   r  r  r  r/   r/   r0   -test_get_column_returns_persisted_with_schema(  s4   


zDComputedReflectionTest.test_get_column_returns_persisted_with_schemaN)r<   r=   r>   r  r  r  r  r   r(   r)   r  r/   r/   r/   r0   r    s    
r  c                   @   sH   e Zd ZdZedd Zejjej	dd Z
ejjej	dd ZdS )	CompositeKeyReflectionTestTc                 C   s   t d|tdttdttdtdtjdddddd dd	}t d
|tdtddtdttdttdtdtjg d|jj	|jj
|jjgddd dd		 d S )Ntb1r    attrrD   r   Zpk_tb1rE   Tr   tb2r!   pidpattrpnamer  r  r  Zfk_tb1_name_id_attr)r   r   r   r   r   rP   rQ   rS   r   rD   r    r  )r-   r.   r  r/   r/   r0   r1   J  s2   

z(CompositeKeyReflectionTest.define_tablesc                 C   s8   | j }t|j}|| jjj}t|dg d d S )Nr*  rD   r    r  )	r.   r   r   rh   r   r  rD   r   r  )r8   r   r`   r"   r/   r/   r0   test_pk_column_orderf  s   
z/CompositeKeyReflectionTest.test_pk_column_orderc                 C   sb   | j }t|j}|| jjj}tt|d |d }t|	dg d t|	dg d d S )Nr6  r   r2  r  r*  r  )
r.   r   r   rj   r   r  rD   r   r   r  )r8   r   r`   Zforeign_keysr3  r/   r/   r0   test_fk_column_ordero  s   
z/CompositeKeyReflectionTest.test_fk_column_orderN)r<   r=   r>   r?   r@   r1   r   r(   r  r  r  r  r  r/   r/   r/   r0   r  G  s    
r  )rx   rA   r   r  r  r  )/rk  rw  r|   rP   r   r   r   r   r   r   r   r	   Z	provisionr
   r'   r   r   r   r   r.  r   r   r   r   r   r   r   r   Zengine.reflectionr   r   r   Zsql.elementsr   r   r   r.   r   Z
TablesTestr   rA   rx   r  ZComputedReflectionFixtureTestr  r  __all__r/   r/   r/   r0   <module>   sX    .        b(b5