U
    cc                     @   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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-G d+d, d,e	j&Z.G d-d. d.e	j/Z0G d/d0 d0e	j&Z1G d1d2 d2e	j&Z2d3Z3dS )4    N   )config)engines)eq_)expect_warnings)fixtures)is_)get_temp_table_name)temp_table_keyword_argsColumn)Table   )event)
ForeignKey)func)Identity)inspect)Integer)MetaData)String)testing)types)DDL)Index)quoted_name)BLANK_SCHEMA)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   sX   t d|tdtddtdtd tjjjrTt d|tdtddtdtdtj	d 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 r0   L/tmp/pip-unpacked-wheel-8u86ls_i/sqlalchemy/testing/suite/test_reflection.pydefine_tables'   s    
zHasTableTest.define_tablesc              	   C   sV   t j B}tt jj|d tt jj|d tt jj|d W 5 Q R X d S )Nr    r&   nonexistent_table)r   dbbeginr   dialect	has_tabler   selfconnr0   r0   r1   test_has_table8   s    zHasTableTest.test_has_tablec              	   C   sh   t j T}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 5 Q R X d S )Nr    r'   r&   r3   )r   r4   r5   r   r6   r7   r,   r   r8   r0   r0   r1   test_has_table_schema>   s,          z"HasTableTest.test_has_table_schemaN)__name__
__module____qualname____backend__classmethodr2   r;   r   r)   r*   r<   r0   r0   r0   r1   r   $   s   
r   c                   @   s4   e Zd ZdZedd Zdd Zejj	dd Z
dS )	HasIndexTestTc                 C   st   t d|tdtddtdtd}td|jj tjj	j
rpt d|tdtddtdtdtjd}td	|jj d S )
Nr    r!   Tr"   r$   r%   my_idxr'   my_idx_s)r   r   r   r   r   cr$   r   r)   r*   r+   r   r,   )r.   r/   ttr0   r0   r1   r2   U   s     
zHasIndexTest.define_tablesc              	   C   sr   t j ^}t jj|dds"tt jj|ddr8tt jj|ddrNtt jj|ddrdtW 5 Q R X d S )Nr    rC   rD   r3   Znonexistent_idx)r   r4   r5   r6   	has_indexAssertionErrorr8   r0   r0   r1   test_has_indexi   s"          zHasIndexTest.test_has_indexc              	   C   s   t j v}t jjj|ddt jds(tt jjj|ddt jdrDtt jjj|ddt jdr`tt jjj|ddt jdr|tW 5 Q R X d S )Nr    rD   r'   rC   r3   Znonexistent_idx_s)r   r4   r5   r6   rG   r,   rH   r8   r0   r0   r1   test_has_index_schemav   s2       
   

z"HasIndexTest.test_has_index_schemaN)r=   r>   r?   r@   rA   r2   rI   r   r)   r*   rJ   r0   r0   r0   r1   rB   R   s   
rB   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rt 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r>ddg}ndg}|D ]f}dtjjjd| tjjj|f }t|dt| t|dtd tjjjd|   qHd S )!Nquote ' oner!   namer%   r$   Z
related_idzpk quote ' onerN   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"   rQ   "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_reflectionr   r4   r6   Zidentifier_preparerquoter   listenr   )r.   r/   namesrN   queryr0   r0   r1   r2      s    
  

  

z$QuotedNameArgumentTest.define_tablesc                 C   s   t ddt jjf| S )N)rM   rR   )r   combinationsr)   r_   )fnr0   r0   r1   quote_fixtures   s    
z%QuotedNameArgumentTest.quote_fixturesc                 C   s   t tj}|| d S N)r   r   r4   Zget_table_optionsr9   rN   inspr0   r0   r1   test_get_table_options   s    
z-QuotedNameArgumentTest.test_get_table_optionsc                 C   s    t tj}|d| std S )NrV   )r   r   r4   get_view_definitionrH   ri   r0   r0   r1   test_get_view_definition   s    
z/QuotedNameArgumentTest.test_get_view_definitionc                 C   s   t tj}||std S rh   )r   r   r4   get_columnsrH   ri   r0   r0   r1   test_get_columns   s    
z'QuotedNameArgumentTest.test_get_columnsc                 C   s   t tj}||std S rh   )r   r   r4   get_pk_constraintrH   ri   r0   r0   r1   test_get_pk_constraint   s    
z-QuotedNameArgumentTest.test_get_pk_constraintc                 C   s   t tj}||std S rh   )r   r   r4   get_foreign_keysrH   ri   r0   r0   r1   test_get_foreign_keys  s    
z,QuotedNameArgumentTest.test_get_foreign_keysc                 C   s   t tj}||std S rh   )r   r   r4   get_indexesrH   ri   r0   r0   r1   test_get_indexes  s    
z'QuotedNameArgumentTest.test_get_indexesc                 C   s   t tj}||std S rh   )r   r   r4   get_unique_constraintsrH   ri   r0   r0   r1   test_get_unique_constraints  s    
z2QuotedNameArgumentTest.test_get_unique_constraintsc                 C   s   t tj}||std S rh   )r   r   r4   get_table_commentrH   ri   r0   r0   r1   test_get_table_comment  s    
z-QuotedNameArgumentTest.test_get_table_commentc                 C   s   t tj}||std S rh   )r   r   r4   get_check_constraintsrH   ri   r0   r0   r1   test_get_check_constraints  s    
z1QuotedNameArgumentTest.test_get_check_constraintsN)r=   r>   r?   Zrun_create_tablesr@   rA   r2   rg   rk   r   r)   r`   rm   ro   rq   rs   ru   unique_constraint_reflectionrw   comment_reflectionry   check_constraint_reflectionr{   r0   r0   r0   r1   rK      s6   
S




rK   c                   @   s6  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jdd Zejjejdddddejjfddddddejjfdddddejjfddd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dTd(d)Z ejd*ddejjfddejjfddejjejj fd+dd,d- Z!ejj"d.d/ Z#ejj"ejj$ejjd0d1 Z%ejd2dejjfd3dejj&d4d5 Z'ejd2dejjfd3dejjd6d7 Z(ejj)ejjd8d9 Z*d:d; Z+ejd2dejjfd3dd<d= Z,ejd>d?d@dejj-ejj.dAdB Z/ejj"ejj0dCdD Z1ejj2dEdF Z3ejdejjfd2d3dejj0dGdH Z4ejjejd2dejjfd3ddIdJ Z5ejdKdLdejjfdMde6dNdOdPdQ Z7ejj8dRdS Z9dS )UComponentReflectionTestNTc                 C   s6   t jjjr,ddlm} tjt|j	dddS t j
S d S )Nr   )poolclass)Z	poolclassZscope)options)r   requirementsZindependent_connectionsr+   
sqlalchemyr   r   testing_enginedictZ
StaticPoolr4   )r.   r   r0   r0   r1   
setup_bind#  s    
z"ComponentReflectionTest.setup_bindc                 C   s*   |  |d  tjjjr&|  |tjj d S rh   )define_reflected_tablesr   r)   r*   r+   r   r,   r-   r0   r0   r1   r2   .  s    
z%ComponentReflectionTest.define_tablesc                 C   s$  |r|d }nd}t jjjrzt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}nBt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rX|d k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) nnt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r| || |st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rtd2|jj  td3|jj  t jjjr| || |s t jjjr | | d S )4N. usersuser_idTr"   test1   Fnullabletest2parent_user_idz%susers.user_id
user_id_fkrO   r(   rQ   
dingalingsdingaling_id
address_idz%semail_addresses.address_idr$      email_addressesremote_user_idZemail_address   email_ad_pkcomment_testr!   
id comment)r#   rP   data % comment)rP   d2)Comment types type speedily ' " \ '' Fun!the test % ' " \ table comment)r(   rP   local_table	remote_idz%s.remote_table_2.id)rQ   r(   remote_tablelocal_idz%s.local_table.idremote_table_2noncol_idx_test_nopkqrT   noncol_idx_test_pknoncol_idx_nopknoncol_idx_pk) r   r)   self_referential_foreign_keysr+   r   r   rZ   INTZCHARZFloatr   r   r   rE   r   r[   cross_schema_fk_reflectionr   r,   r4   r6   default_schema_nameindex_reflectiondefine_indexindexes_with_ascdescr   r   descr`   define_viewstemp_table_reflectiondefine_temp_tables)r.   r/   r(   Zschema_prefixr   r   r   r0   r0   r1   r   4  s
   

 

  

	z/ComponentReflectionTest.define_reflected_tablesc                 C   s   t ttj}tttjdtj }t||tdtjddtdt	dtdtjtj
ddtj d	td
df|}tjjjrtjjjrt|dtdtj  t|dtd d S )Nuser_tmp_%sr!   Tr"   rN   r%   foouser_tmp_uq_%srO   user_tmp_ixrW   z=create temporary view user_tmp_v as select * from user_tmp_%srX   zdrop view user_tmp_v)r
   r   r4   r	   identr   r   rZ   r   VARCHARr\   r   r   r)   view_reflectionr+   temporary_viewsr   rb   r   )r.   r/   kw
table_nameuser_tmpr0   r0   r1   r     s>      

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   rE   r   r   r   )r.   r/   r   r0   r0   r1   r     s    z$ComponentReflectionTest.define_indexc                 C   s^   dD ]T}|}|rd||f }|d }d||f }t |dt| t |dtd|  qd S )N)r   r   z%s.%sZ_vrU   rW   rX   rY   )r   rb   r   )r.   r/   r(   r   fullnameZ	view_namerd   r0   r0   r1   r     s      
z$ComponentReflectionTest.define_viewsc                 C   s$   t | j}| tjj| k d S rh   )r   bindassert_r   r   r,   get_schema_namesr9   rj   r0   r0   r1   test_get_schema_names  s    
z-ComponentReflectionTest.test_get_schema_namesc                 C   s:   |j ddttjjid}t|}| tjj| k dS )z
test #7300r   bar)Zschema_translate_mapN)Zexecution_optionsr   r   r   r,   r   r   r   )r9   
connectionrj   r0   r0   r1   %test_get_schema_names_w_translate_map  s      z=ComponentReflectionTest.test_get_schema_names_w_translate_mapc                 C   s$   t  }t| t|jds td S )Nr   )r   r   r   hasattrr6   rH   )r9   enginer0   r0   r1   test_dialect_initialize  s    z/ComponentReflectionTest.test_dialect_initializec                 C   s    t | j}t|j| jjj d S rh   )r   r   r   r   r6   r   r0   r0   r1   test_get_default_schema_name  s    
z4ComponentReflectionTest.test_get_default_schema_name)NTFFF)foreign_keyTFF)NFTF)NTTFz/order_by,include_plain,include_views,use_schemaargnamesc                    s   |rt j}nd }ddddddg t|}|rT||}|  ddg}	tt||	 |r|rrd	d
 ||D }
n
||}
 fdd
|
D }|dkrdddg}	t||	 ndddg}	tt||	 d S )Nr   r   r   r   r   r   email_addresses_vusers_vc                 S   s   g | ]}|d  r|d  qS )r   r0   .0Zrecr0   r0   r1   
<listcomp>D  s   z@ComponentReflectionTest.test_get_table_names.<locals>.<listcomp>c                    s   g | ]}| kr|qS r0   r0   r   tZ_ignore_tablesr0   r1   r   K  s      r   r   r   r   )	r   r,   r   Zget_view_namessortr   sortedZget_sorted_table_and_fkc_namesget_table_names)r9   r   Zorder_byZinclude_plainZinclude_views
use_schemar(   rj   table_namesZanswertablesr0   r   r1   test_get_table_names  s8    	



z,ComponentReflectionTest.test_get_table_namesc                 C   s,   t | j}| }tt|dtj g d S )Nr   )r   r   Zget_temp_table_namesr   r   r   r   r9   rj   temp_table_namesr0   r0   r1   test_get_temp_table_namesT  s    
z1ComponentReflectionTest.test_get_temp_table_namesc                 C   s&   t | j}| }tt|dg d S )N
user_tmp_v)r   r   Zget_temp_view_namesr   r   r   r0   r0   r1   test_get_temp_view_namesZ  s    
z0ComponentReflectionTest.test_get_temp_view_namesc                 C   s   |    d S rh   )_test_get_commentsr9   r0   r0   r1   test_get_commentsb  s    z)ComponentReflectionTest.test_get_commentsc                 C   s   |  tjj d S rh   )r   r   r   r,   r   r0   r0   r1   test_get_comments_with_schemaf  s    z5ComponentReflectionTest.test_get_comments_with_schemac                 C   st   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 )rN   rP   )rN   rP   r0   r   r0   r0   r1   r   v  s   z>ComponentReflectionTest._test_get_comments.<locals>.<listcomp>r   r!   )rP   rN   r   r$   r   r   )r   r   r   rx   rn   )r9   r(   rj   r0   r0   r1   r   k  s"    
z*ComponentReflectionTest._test_get_comments)FFzuse_views,use_schemac                 C   sn  |rt j}nd }| jj| jj }}|r0ddg}nddg}t|}t|||fD ]\}	}
|}|j|	|d}| t	|dkt	| t
|
jD ]\}}t|j|| d  || d j}|j}t|tjjr|j}td	r|tjtjfkrt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 kst"qqNd S )Nr   r   r   r   r'   r   rN   typeoraclez%s(%s), %s(%s)default)#r   r,   r   r   r   r   ziprn   r   len	enumeratecolumnsr   rN   	__class__r   
isinstancerZ   r   Z
TypeEnginer   Zagainst	sql_typesDateDateTimeset__mro__intersectionr   NumericZTimer   Z_Binaryr#   rH   )r9   r   Z	use_viewsr   r(   r   	addressesr   rj   r   tableZschema_namecolsicolctypeZ	ctype_defr0   r0   r1   ro     s\    

z(ComponentReflectionTest.test_get_columnsc                 C   sv   t t| jdtj }| j| }t| j}||}| t|dkt| t	|j
D ]\}}t|j|| d  qTd S )Nr   r   rN   )r	   r   r   r   r   r   rn   r   r   r   r   r   rN   )r9   r   r   rj   r   r  r  r0   r0   r1   test_get_temp_table_columns  s      


z3ComponentReflectionTest.test_get_temp_table_columnsc                 C   s2   t | j}|d}tdd |D dddg d S )Nr   c                 S   s   g | ]}|d  qS rO   r0   r   r  r0   r0   r1   r     s     zFComponentReflectionTest.test_get_temp_view_columns.<locals>.<listcomp>r!   rN   r   )r   r   rn   r   )r9   rj   r   r0   r0   r1   test_get_temp_view_columns  s    

z2ComponentReflectionTest.test_get_temp_view_columnsFr   c              	   C   s   |rt jj}nd }| jj| jj }}t|}|j|j|d}|d }t	|dg |j|j|d}	|	d }
t	|
dg t j
j  t	|	d d W 5 Q R X d S )Nr'   constrained_columnsr   r   rN   r   )r   r   r,   r   r   r   r   rp   rN   r   r)   Zreflects_pk_namesfail_if)r9   r   r   r(   r   r   rj   Z
users_consZusers_pkeysZ	addr_consZ
addr_pkeysr0   r0   r1   rq     s    
z.ComponentReflectionTest.test_get_pk_constraintc              	   C   sB  |rt j}nd }| jj| jj }}t|}|}tjjj	r|j
|j|d}|d }	tjj  t|	d d W 5 Q R X t|	d | t|	d |j t|	d dg tjjj	rt|	d	 d
g |j
|j|d}
|
d }	tjj  | |	d d k	 W 5 Q R X t|	d | t|	d |j t|	d dg t|	d	 dg d S )Nr'   r   rN   r   referred_schemareferred_tablereferred_columnsr   r  r   r   )r   r,   r   r   r   r   r   r)   r   r+   rr   rN   Znamed_constraintsr	  r   Zimplicitly_named_constraintsr   )r9   r   r   r(   r   r   rj   Zexpected_schemaZusers_fkeysfkey1Z
addr_fkeysr0   r0   r1   rs     s0    

z-ComponentReflectionTest.test_get_foreign_keysc           	      C   s  |  d| jjj dtjj dtjj \}}}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 | jjjfkst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   r
  r  r  r!   r  r   r'   r   )r   r   r6   r   r   r   r,   r   rr   rN   r   r   rH   )	r9   r   r   r   rj   Zlocal_fkeysr  Zremote_fkeysZfkey2r0   r0   r1   "test_get_inter_schema_foreign_keys%  s4    



 
z:ComponentReflectionTest.test_get_inter_schema_foreign_keysc                 C   sZ   dd |D }|D ]B}|d |ks&t |||d  }|D ]}t|| ||  q<qd S )Nc                 S   s   g | ]}|d  qS rO   r0   )r   dr0   r0   r1   r   I  s     z@ComponentReflectionTest._assert_insp_indexes.<locals>.<listcomp>rN   )rH   indexr   )r9   indexesexpected_indexesZindex_namesZe_indexr  keyr0   r0   r1   _assert_insp_indexesH  s    z,ComponentReflectionTest._assert_insp_indexesc                 C   sZ   |rt j}nd }t| j}|jd|d}dddgddddddgd	dg}| || d S )
Nr   r'   Fr   r   r   uniquecolumn_namesrN   r   r   )r   r,   r   r   rt   r  )r9   r   r   r(   rj   r  r  r0   r0   r1   ru   P  s    
z(ComponentReflectionTest.test_get_indexes)r   r   )r   r   ztname,ixnamec                 C   sz   t |}||}d|dg}| || t|t |d}tt|jd tt	|jd j
| tt	|jd j| d S )NF)r  rN   autoload_withr  r   )r   rt   r  r   r   r   r   r  r   listr   rN   )r9   r   tnameZixnamerj   r  r  r   r0   r0   r1   test_get_noncol_indexl  s    
z-ComponentReflectionTest.test_get_noncol_indexc                 C   sN   t | j}|dtj }|D ]}|dd  qt|dgdtj dg d S )Nr   duplicates_indexrN   r   )r  rN   )r   r   rv   r   r   popr   )r9   rj   	reflectedreflr0   r0   r1   &test_get_temp_table_unique_constraints  s    
z>ComponentReflectionTest.test_get_temp_table_unique_constraintsc                 C   s|   t | j}tttjdtj }||}|D ]}|dd  q,ddgddg}tj	j
jrdg |d d< td	d
 |D | d S )Nr   dialect_optionsFr   r   r  r   include_columnsc                 S   s   g | ]}|d  dkr|qS )rN   r   r0   r   idxr0   r0   r1   r     s      zGComponentReflectionTest.test_get_temp_table_indexes.<locals>.<listcomp>)r   r   r	   r   r4   r   rt   r  r   r)   index_reflects_included_columnsr+   r   )r9   rj   r   r  indexpectedr0   r0   r1   test_get_temp_table_indexes  s"    
  

z3ComponentReflectionTest.test_get_temp_table_indexesc                 C   s  |rt j}nd }tddgdddddgdddddgddd	d
gdddgdddgdgtdd}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 q|| t|}t|jd|dtdd}	t }
t||	D ]0\}}|dd }|rB|
| t|| qt }td|||d}	tdd |	jD }tdd |	jD dg}||rt|
rt|
| t|t  d S )NZunique_aa)rN   r  Zunique_a_b_cbrE   Zunique_c_a_bZunique_asc_keyZascr  zi.have.dotszi have spacesrN   r  Ztesttblr   r   r'   r  r  )r  r(   c                 S   s   g | ]
}|j qS r0   rO   r%  r0   r0   r1   r     s     zGComponentReflectionTest.test_get_unique_constraints.<locals>.<listcomp>c                 S   s   g | ]}t |tjr|jqS r0   )r   rZ   r\   rN   )r   Zuqr0   r0   r1   r     s   )r   r,   r   operator
itemgetterr   r   rZ   r   r   Zappend_constraintr\   creater   rv   r   r   r  addr   r   r  constraints
differencer   rH   )r9   r/   r   r   r(   Zuniquesr   Zuc	inspectorr   Znames_that_duplicate_indexorigr!  ZdupeZreflected_metadataZ	idx_namesZuq_namesr0   r0   r1   rw     st    






z3ComponentReflectionTest.test_get_unique_constraintsc           	      C   sT   |rt j}nd }d}d}t|}|j||d}| | |j||d}| | d S )Nr   r   r'   )r   r,   r   rl   r   )	r9   r   r   r(   Z
view_name1Z
view_name2rj   Zv1Zv2r0   r0   r1   rm     s    
z0ComponentReflectionTest.test_get_view_definition)r   Fr   ztable_name,use_schemaZ
postgresqlzPG specific featurec                 C   s8   |rt j}nd }t|}|||}| t|t d S rh   )r   r,   r   Zget_table_oidr   r   int)r9   r   r   r   r(   rj   oidr0   r0   r1   test_get_table_oid  s    z*ComponentReflectionTest.test_get_table_oidc                 C   sH   t | j}dD ]4\}}||}dd |D | }|ddst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   s   i | ]}|d  |qS rO   r0   r   rE   r0   r0   r1   
<dictcomp>5  s      zBComponentReflectionTest.test_autoincrement_col.<locals>.<dictcomp>autoincrementTN)r   r   rn   getrH   )r9   rj   r  cnamer   Zid_r0   r0   r1   test_autoincrement_col  s
    

z.ComponentReflectionTest.test_autoincrement_col)N):r=   r>   r?   run_insertsrun_deletesr@   rA   r   r2   r   r   r   r   r   r)   Zschema_reflectionr   r   r   r   !foreign_key_constraint_reflectionre   r*   r   r   r   r   r   r   r}   r   r   r   ro   r   r  r`   r  !primary_key_constraint_reflectionrq   rs   r   r  r  ru   r   r   r  r|   r"  Ztemp_table_reflect_indexesr*  rw   rm   Zonly_onr8  table_reflectionr>  r0   r0   r0   r1   r     s   



 
#






,



?
 
  
 $! 
 


  R 
 
	r   c                   @   s   e Zd ZdZdZejdd Ze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ej	j
dd Zej	jej	j
dd ZdS )TableNoColumnsTest)reflect_tables_no_columnsTc                 C   s   t d| || d S Nempty)r   
create_allr9   r   r/   r0   r0   r1   table_no_columns=  s    
z#TableNoColumnsTest.table_no_columnsc                 C   sP   t d| || t d| t|dtd t|dtd || d S )NrG  rW   z*CREATE VIEW empty_v AS SELECT * FROM emptyrX   zDROP VIEW IF EXISTS empty_v)r   rH  r   rb   r   rI  r0   r0   r1   view_no_columnsB  s    


  z"TableNoColumnsTest.view_no_columnsc                 C   s$   t dt |d}tt|jg  d S )NrG  r  r   r   r   r  rE   )r9   r   rJ  t2r0   r0   r1   test_reflect_table_no_columnsU  s    z0TableNoColumnsTest.test_reflect_table_no_columnsc                 C   s   t t|dg  d S rF  r   r   rn   )r9   r   rJ  r0   r0   r1   !test_get_columns_table_no_columnsZ  s    z4TableNoColumnsTest.test_get_columns_table_no_columnsc                 C   s*   t  }|| t|jdgs&td S rF  )r   reflectr   r   r   rH   )r9   r   rJ  mr0   r0   r1   "test_reflect_incl_table_no_columns^  s    
z5TableNoColumnsTest.test_reflect_incl_table_no_columnsc                 C   s$   t dt |d}tt|jg  d S )Nempty_vr  rL  )r9   r   rK  rM  r0   r0   r1   test_reflect_view_no_columnsd  s    z/TableNoColumnsTest.test_reflect_view_no_columnsc                 C   s   t t|dg  d S )NrT  rO  )r9   r   rK  r0   r0   r1    test_get_columns_view_no_columnsj  s    z3TableNoColumnsTest.test_get_columns_view_no_columnsN)r=   r>   r?   __requires__r@   r   ZfixturerJ  rK  r)   rE  rN  rP  rS  ZviewsrU  rV  r0   r0   r0   r1   rD  9  s$   




rD  c                
   @   s   e Zd ZdZejdejjfdddejj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ejjdd Zejdddejjfdddejjfi ddejjfi ddejjfdddejjfdddejjfdddd ZdS )ComponentReflectionTestExtraTr  r   r   c              	      s   |rt j}nd }td|tdt tjdddtjddd|d || t|}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cc1rO   za = 1 OR (a > 2 AND a < 5)Z
UsesCasingr'   rN   r-  c                 S   s   d td|  tjS )N zand|\d|=|a|or|<|>)joinrefindalllowerI)sqltextr0   r0   r1   	normalize  s    zJComponentReflectionTestExtra.test_get_check_constraints.<locals>.normalizec                    s"   g | ]}|d   |d dqS )rN   r_  rN   r_  r0   )r   itemr`  r0   r1   r     s   zKComponentReflectionTestExtra.test_get_check_constraints.<locals>.<listcomp>za = 1 or a > 2 and a < 5ra  za > 1 and a < 5)r   r,   r   r   r   rZ   r^   rH  r   r   rz   r.  r/  r   )r9   r/   r   r   r(   r4  r   r0   rc  r1   r{   t  s<    
 

z7ComponentReflectionTestExtra.test_get_check_constraintsc              	   C   s   t d|tdtdtdtd}tdt|jjt|jj td|jj |	| t
|}ddgddg}tjjjrg |d	 d
< d|jj g i|d	 d< td t|d| W 5 Q R X d S )Nr   xr   yt_idxZt_idx_2F)rN   r  r  r   r$  
%s_includer#  z>Skipped unsupported reflection of expression-based index t_idx)r   r   r   r   r   r]  rE   rd  re  rH  r   r   r)   r'  r+   r   rN   r   r   rt   )r9   r/   r   r   rj   r)  r0   r0   r1   %test_reflect_expression_based_indexes  s0     


 zBComponentReflectionTestExtra.test_reflect_expression_based_indexesc                 C   s   t d|tdtdtdtd}td|jj}dg|j|jj d< |	| t
|}t|dddgdgdd|jj dgid	g t dt |d
}tt|jd j|jj d dg d S )Nr   rd  r   re  rf  includeFrg  )rN   r  r$  r  r#  r  r   )r   r   r   r   rE   rd  r#  r   rN   rH  r   r   rt   r   r  r  )r9   r/   r   r   r&  rj   rM  r0   r0   r1   test_reflect_covering_index  s:    

 z8ComponentReflectionTestExtra.test_reflect_covering_indexc                 G   s@   t d|fdd t|D  }|| dd t|dD S )Nr   c                 S   s   g | ]\}}t d | |qS )zt%dr   )r   r  type_r0   r0   r1   r     s     zAComponentReflectionTestExtra._type_round_trip.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r0   r9  r0   r0   r1   r     s     )r   r   r0  r   rn   )r9   r   r/   r   r   r0   r0   r1   _type_round_trip  s    
z-ComponentReflectionTestExtra._type_round_tripc                 C   sH   |  ||tddD ],}t|tjs*tt|jd t|jd qd S )N   r   )rl  r   r   r   rH   r   Z	precisionZscaler9   r   r/   typr0   r0   r1   test_numeric_reflection  s      
z4ComponentReflectionTestExtra.test_numeric_reflectionc                 C   s8   |  ||tdd }t|tjs(tt|jd d S )N4   r   )rl  r   r   r   rH   r   lengthrn  r0   r0   r1   test_varchar_reflection   s      z4ComponentReflectionTestExtra.test_varchar_reflectionc              	   C   sX   t d|tdtddtdtdd}|| ttdd t|dD ddd	 d S )
Nr   r+  Tr   r,  Fc                 s   s   | ]}|d  |d fV  qdS )rN   r   Nr0   r  r0   r0   r1   	<genexpr>  s   zHComponentReflectionTestExtra.test_nullable_reflection.<locals>.<genexpr>)r+  r,  )r   r   r   r0  r   r   r   rn   )r9   r   r/   r   r0   r0   r1   test_nullable_reflection  s    
z5ComponentReflectionTestExtra.test_nullable_reflectionNZCASCADEzSET NULLz	NO ACTIONZRESTRICTzexpected,ondelete,onupdatec                    s(  i }|r||d< |r||d< |d kr(|}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|}|	dd d  t
t fdd D i  |	dd d  t
 | d S )Nondeleteonupdaterd  r!   Tr"   rT   r   Zx_idzx.idxidrO   test
   userrN   r%   Fr   tidztable.idZmyfkr   r   c                 3   s"   | ]} | r| | fV  qd S rh   r0   )r   koptsr0   r1   rt  p  s      zLComponentReflectionTestExtra.test_get_foreign_key_options.<locals>.<genexpr>)r   r   r   rZ   r   r   r]   rH  r   rr   r   r   )r9   r   r/   r)  rv  rw  r   rj   r0   r~  r1   test_get_foreign_key_options  sV    *	 
z9ComponentReflectionTestExtra.test_get_foreign_key_options)r=   r>   r?   r@   r   re   r)   r*   r~   r{   Zindexes_with_expressionsrh  r'  rj  rl  rC  rp  rs  ru  Z1foreign_key_constraint_option_reflection_ondeleteZ1foreign_key_constraint_option_reflection_onupdateZ1fk_constraint_option_reflection_ondelete_noactionZ1fk_constraint_option_reflection_onupdate_restrictZ1fk_constraint_option_reflection_ondelete_restrictr  r0   r0   r0   r1   rX  p  sj   
  -
!
%



'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ra   r!   r"   rM  t1idzt1.id)r   r   r   r   r   r-   r0   r0   r1   r2   {  s    

z NormalizedNameTest.define_tablesc                 C   s~   t  }ttddd|tjd}|jd }|jj|jj	s>t
t  }|jtjdd d |jd jj|jd jj	szt
d S )	NrM  Tr  r  r  c                 S   s   |   dkS )Nr  rM  r]  )rN   rR  r0   r0   r1   <lambda>      zINormalizedNameTest.test_reflect_lowercase_forced_tables.<locals>.<lambda>)only)r   r   r   r   r4   r   rE   r  Z
referencesr!   rH   rQ  )r9   m2Zt2_refZt1_refZm3r0   r0   r1   $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 kr|qS )r  r  r   r0   r0   r1   r     s   z;NormalizedNameTest.test_get_table_names.<locals>.<listcomp>r   r  )r   r   r4   r   r   upperr]  )r9   Z
tablenamesr0   r0   r1   r     s
    z'NormalizedNameTest.test_get_table_namesN)	r=   r>   r?   rW  r@   rA   r2   r  r   r0   r0   r0   r1   r  w  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   s^   t tj}|d}dd |D }td|d d k t|d d d  t|d d d  d S )	Ncomputed_default_tablec                 S   s   i | ]}|d  |qS rO   r0   r9  r0   r0   r1   r:    s      zLComputedReflectionTest.test_computed_col_default_not_set.<locals>.<dictcomp>Z42with_defaultr   normalcomputed_col)r   r   r4   rn   r   r   )r9   rj   r   Zcol_datar0   r0   r1   !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|| k q&|d }td|k td|d k t| |d d d td	|d ktjj	j
 tjj	j
rt|d d	 tjjj
 d S )
Nr  c                 S   s   i | ]}|d  |qS rO   r0   r9  r0   r0   r1   r:    s      zKComputedReflectionTest.test_get_column_returns_computed.<locals>.<dictcomp>)r!   r  r  computedr  r_  	normal+42	persisted)r   r   r4   rn   r   r   r`  r   r)   "computed_columns_reflect_persistedr+   "computed_columns_default_persisted)r9   rj   r   r$   r  compDatar0   r0   r1    test_get_column_returns_computed  s$    




z7ComputedReflectionTest.test_get_column_returns_computedc                 C   sX   t d|| k || d }t| |d | tjjjrTt d|k t|d | d S )Nr  r_  r  )r   r   r`  r   r)   r  r+   r   )r9   r$   columnr_  r  r  r0   r0   r1   check_column  s    
z#ComputedReflectionTest.check_columnc                 C   sp   t tj}|d}dd |D }| |ddtjjj tjj	jrR| |ddd tjj
jrl| |d	d
d d S )Ncomputed_column_tablec                 S   s   i | ]}|d  |qS rO   r0   r9  r0   r0   r1   r:    s      zLComputedReflectionTest.test_get_column_returns_persisted.<locals>.<dictcomp>computed_no_flagr  computed_virtualznormal+2Fcomputed_storedz	normal-42T)r   r   r4   rn   r  r   r)   r  r+   computed_columns_virtualcomputed_columns_storedr9   rj   r   r$   r0   r0   r1   !test_get_column_returns_persisted  s.    



z8ComputedReflectionTest.test_get_column_returns_persistedc                 C   sv   t tj}|jdtjd}dd |D }| |ddtjjj	 tjj
j	rX| |ddd	 tjjj	rr| |d
dd d S )Nr  r'   c                 S   s   i | ]}|d  |qS rO   r0   r9  r0   r0   r1   r:    s      zXComputedReflectionTest.test_get_column_returns_persisted_with_schema.<locals>.<dictcomp>r  z	normal/42r  znormal/2Fr  z	normal*42T)r   r   r4   rn   r,   r  r   r)   r  r+   r  r  r  r0   r0   r1   -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  r0   r0   r0   r1   r    s   	r  c                   @   sH   e Zd Zd ZZdZdZedd Zdd Z	dd	 Z
ejjd
d ZdS )IdentityReflectionTestNT)Zidentity_columnsrC  c                 C   s|   t d|tdttdtt  t d|tdttdddd	d
ddd tjjjrxt d|tdttdttdddtj	d d S )Nr  r  id1rM  id2Tr   r   *      alwaysstart	incrementminvaluemaxvaluecyclecacher   )r  r  r'   )
r   r   r   r   r   r)   r*   r+   r   r,   r-   r0   r0   r1   r2   
  s>    
z$IdentityReflectionTest.define_tablesc                 C   s   t jjjrd}t|D ]}||kr|| q|rtt|t| |D ]T}|dkrjt|| || k qH|dkrt|| || k qHt|| || | qHqt|| n$t|d |d  t|d |d  d S )Nr  r  >   r  r  r  r  )	r   r)   Zidentity_columns_standardr+   r  r  r   r   r   )r9   valueexpapproxZcommon_keysr}  r0   r0   r1   check,  s     
	zIdentityReflectionTest.checkc                 C   s   t tj}|d|d }|D ]}|d dkr@td|k q"|d dkrt|d dk t|d	 d  td|k | j|d td
ddddd
dddd q"|d dkr"t|d dk t|d	 d  td|k | j|d tddddddddd
d q"d S )Nr  rM  rN   r  identityr  r;  Tautor   Fr  r  Tr  r  r   r   r  r  r  )	r   r   r4   rn   r   r   r   r  r   r9   rj   r   r  r0   r0   r1   test_reflect_identityI  sN    
		z,IdentityReflectionTest.test_reflect_identityc                 C   s   t tj}|jdtjd}|D ]z}|d dkr<td|k q|d dkrt|d dk t|d	 d  td|k | j|d t	d
dddddddd
d qd S )Nr  r'   rN   r  r  r  r;  r  r   Tr   r  r  Fr  r  )
r   r   r4   rn   r,   r   r   r   r  r   r  r0   r0   r1   test_reflect_identity_schemas  s,    
	z3IdentityReflectionTest.test_reflect_identity_schema)r=   r>   r?   r?  r@  r@   rW  rA   r2   r  r  r   r)   r*   r  r0   r0   r0   r1   r    s   
!*r  c                   @   s<   e Zd ZdZedd Zejjdd Z	ej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dddg|jj	|jj
|jjgddd dd		 d S )Ntb1r!   attrrN   r   Zpk_tb1rO   Tr   tb2r"   pidpattrpnameZfk_tb1_name_id_attr)r   r   r   r   r   rZ   r[   r]   rE   rN   r!   r  )r.   r/   r  r0   r0   r1   r2     s2    
z(CompositeKeyReflectionTest.define_tablesc                 C   s4   t | j}|| jjj}t|ddddg d S )Nr  rN   r!   r  )r   r   rp   r   r  rN   r   r<  )r9   rj   r#   r0   r0   r1   test_pk_column_order  s    
z/CompositeKeyReflectionTest.test_pk_column_orderc                 C   s`   t | j}|| jjj}tt|d |d }t|ddddg t|ddd	d
g d S )Nr  r   r  rN   r!   r  r  r  r  r  )	r   r   rr   r   r  rN   r   r   r<  )r9   rj   Zforeign_keysr  r0   r0   r1   test_fk_column_order  s    
z/CompositeKeyReflectionTest.test_fk_column_orderN)r=   r>   r?   r@   rA   r2   r   r)   rB  r  rA  r  r0   r0   r0   r1   r    s   

r  )
r   rX  rD  rK   r   rB   r  r  r  r  )4r.  r[  r   rZ   r   r   r   r   r   r   r   Z	provisionr	   r
   r(   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsql.elementsr   Z
sql.schemar   r   r   r/   r   Z
TablesTestr   rB   rK   r   ZTestBaserD  rX  r  ZComputedReflectionFixtureTestr  r  r  __all__r0   r0   r0   r1   <module>   sd   .;       !7  	,a 1