
xR\Y                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z d  d l Z Gd d   d e  Z d d   Z e d k r e j   n  d S)	    N   )unittestConnectingTestCaseskip_before_postgresskip_if_no_namedtupleskip_if_no_getrefcountslowskip_if_no_superuserskip_if_windowsc               @   s\  e  Z d  Z d d   Z d d   Z d d   Z d d   Z 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 e e d% d&  d' d(     Z e d% d)  d* d+    Z e d% d)  d, d-    Z e d% d)  d. d/    Z e d0 d1    Z d2 d3   Z e d% d)  d4 d5    Z e d% d)  d6 d7    Z  e d% d)  d8 d9    Z! d: d;   Z" d< d=   Z# e d> d)  d? d@    Z$ e% e& e d% dA  dB dC      Z' e% e& e d% dA  dD dE      Z( dF dG   Z) dH S)ICursorTestsc             C   s7   |  j  j   } | j   | j   |  j | j  d  S)N)conncursorclose
assertTrueclosed)selfcur r   O/var/www/dbchiro/venv/lib/python3.4/site-packages/psycopg2/tests/test_cursor.pytest_close_idempotent&   s    

z!CursorTests.test_close_idempotentc             C   s^   |  j  j   } |  j t j | j d  |  j t j | j d  |  j t j | j d  d  S)N  ;)r   r   assertRaisespsycopg2ProgrammingErrorexecute)r   r   r   r   r   test_empty_query,   s    zCursorTests.test_empty_queryc             C   sX   |  j  } | j   } | j d  d d   } |  j t | j d |    | j   d  S)Nz&create temp table test_exc (data int);c               s   s   d d Vd  S)Nr   r   r   r   r   r   r   buggygen7   s    zCCursorTests.test_executemany_propagate_exceptions.<locals>.buggygenz insert into test_exc values (%s))r   r   r   r   ZeroDivisionErrorexecutemanyr   )r   r   r   r   r   r   r   %test_executemany_propagate_exceptions2   s    		z1CursorTests.test_executemany_propagate_exceptionsc             C   s  |  j  } | j   } | j d  |  j d | j   d  |  j d | j d   | j d  d } d d   } | j d	 |  |  j | j d
  | | j   d   |  j d	 | j d
  | j d	 |   | j d | f  |  j | j d  | | j   d   |  j d	 | j d
  | j d | f   | j d | f  |  j | j d  | | j   d   |  j d	 | j d
  | j d | f   d  S)NzSELECT 'foo';foor   s   SELECT 'foo';UTF8u   ☃c             S   s$   t  |  t  r |  j d  S|  Sd  S)Nutf8)
isinstancestrencode)sr   r   r   bL   s    z+CursorTests.test_mogrify_unicode.<locals>.bzSELECT '%s';r$   z
SELECT %s;zutf-8)	r   r   r   assertEqualfetchonemogrifyZset_client_encodingr'   ZassertQuotedEqual)r   r   r   Zsnowmanr)   r   r   r   test_mogrify_unicode>   s(    	)))z CursorTests.test_mogrify_unicodec             C   sa   y d d l  m } Wn d  SYn X|  j } | j   } |  j d | j d | d  f   d  S)Nr   )Decimals   SELECT 10.3;z
SELECT %s;z10.3)decimalr.   r   r   r*   r,   )r   r.   r   r   r   r   r   test_mogrify_decimal_explodesd   s    			z)CursorTests.test_mogrify_decimal_explodesc             C   sz   |  j  j   } d d   d  d } d d  l } | j |  } | j d i | d 6 | j |  } |  j | |  d  S)Nc             S   s   |  S)Nr   )xr   r   r   <lambda>u   s    zECursorTests.test_mogrify_leak_on_multiple_reference.<locals>.<lambda>r"   
   r   z select %(foo)s, %(foo)s, %(foo)s)r   r   sysgetrefcountr,   r*   )r   r   r"   r4   Znref1Znref2r   r   r   'test_mogrify_leak_on_multiple_referencep   s    z3CursorTests.test_mogrify_leak_on_multiple_referencec             C   s   |  j  j   } |  j t j | j d i   |  j t j | j d i d d 6 |  j t j | j d i d d 6 |  j t j | j d i d d 6d d 6 d  S)Nzselect %(foor   r"   zselect %(foo, %(bar)   bar)r   r   r   r   r   r,   )r   r   r   r   r   test_bad_placeholder|   s    z CursorTests.test_bad_placeholderc             C   s  |  j  j   } |  j d | j d d   |  j d | j d d   y d d l m } Wn. t k
 r |  j d	 | j d
 d   Yn# X|  j | d  | j d
 d   d d l m	 } |  j | d d d  | j d d   |  j d | j d d   d  S)N*      Z42gQ	@i  z3.14r   )r.   g^@i  z123.45)datei  r   r7   i:  z
2011-01-02z	who am i?i  )
r   r   r*   castassertAlmostEqualr/   r.   ImportErrordatetimer<   )r   cursr.   r<   r   r   r   	test_cast   s    !"(zCursorTests.test_castc             C   s   |  j  j   } |  j d | j d d   t j j d
 d d d    } t j j | |  j   |  j d | j d d   t j j d d d d    } t j j | |  |  j d	 | j d d   |  j  j   } |  j d | j d d   d  S)Nr"     ZDOUBLINGc             S   s   |  d S)Nr7   r   )vcr   r   r   r2      s    z3CursorTests.test_cast_specificity.<locals>.<lambda>ZfoofooZTREBLINGc             S   s   |  d S)N   r   )rD   rE   r   r   r   r2      s    Z	foofoofoo)rC   )rC   )r   r   r*   r=   r   
extensionsZnew_typeZregister_type)r   rA   DTZcurs2r   r   r   test_cast_specificity   s    z!CursorTests.test_cast_specificityc             C   s^   d d l  m } |  j j   } | |  } ~ d d  l } | j   |  j |   d  k  d  S)Nr   )ref)weakrefrK   r   r   gcZcollectr   )r   rK   rA   wrM   r   r   r   test_weakref   s    
zCursorTests.test_weakrefc             C   s)   |  j  j d   } |  j | j d   d  S)N)r   r   r*   name)r   rA   r   r   r   test_null_name   s    zCursorTests.test_null_namec             C   s   |  j  j   } | j d  x! d D] } | j d | f  q# W| j   |  j  j d  } | j d  |  j | j   d	 d
 d g  d  S)Nz%create temp table invname (data int);r3   r;      zinsert into invname values (%s)z1-2-3 \ "test"z&select data from invname order by data)r3   r;   rR   )r3   )r;   )rR   )r   r   r   r   r*   fetchall)r   rA   ir   r   r   test_invalid_name   s    
zCursorTests.test_invalid_namec             C   s   |  j  j   } y | j d  Wn" t j k
 rD |  j  j   Yn X| j d  x! d D] } | j d | f  qY W| j   d  S)Nzdrop table withholdz create table withhold (data int)r3   r;   rR   z insert into withhold values (%s))r3   r;   rR   )r   r   r   r   r   rollbackr   )r   rA   rT   r   r   r   _create_withhold_table   s    z"CursorTests._create_withhold_tablec             C   s>  |  j  t j |  j j d d |  j   |  j j d  } |  j | j d  d | _ |  j | j d  | j d  |  j j	   |  j | j
   d
 d d g  | j   |  j j d d d } |  j | j d  | j d  |  j j	   |  j | j
   d d d g  |  j j   } | j d	  |  j j	   d  S)NwithholdTWFz'select data from withhold order by datar3   r;   rR   zdrop table withhold)r3   )r;   )rR   )r3   )r;   )rR   )r   r   r   r   r   rW   r*   rX   r   commitrS   r   )r   rA   r   r   r   test_withhold   s&    
	
zCursorTests.test_withholdc             C   sb  |  j    |  j j d d d } | j d  |  j | j   d  |  j |  j j t j j	  |  j |  j j
   t j j  |  j j   |  j |  j j t j j  |  j |  j j
   t j j  |  j | j   d  |  j |  j j t j j  |  j |  j j
   t j j  | j   |  j |  j j t j j  |  j |  j j
   t j j  d  S)	NrN   rX   Tz'select data from withhold order by datar3   r;   )r3   )r;   )rW   r   r   r   r*   r+   statusr   rG   ZSTATUS_BEGINget_transaction_statusZTRANSACTION_STATUS_INTRANSrZ   STATUS_READYTRANSACTION_STATUS_IDLEr   )r   rA   r   r   r   test_withhold_no_begin   s&    

z"CursorTests.test_withhold_no_beginc             C   s*  |  j    |  j j   d |  j _ |  j j d d d } | j d  |  j | j   d  |  j |  j j t	 j
 j  |  j |  j j   t	 j
 j  |  j j   |  j |  j j t	 j
 j  |  j |  j j   t	 j
 j  | j   |  j |  j j t	 j
 j  |  j |  j j   t	 j
 j  d  S)NTrN   rX   z'select data from withhold order by datar3   )r3   )rW   r   rZ   Z
autocommitr   r   r*   r+   r\   r   rG   r^   r]   r_   r   )r   rA   r   r   r   test_withhold_autocommit   s"    

z$CursorTests.test_withhold_autocommitc             C   s  |  j  t j |  j j d d |  j j   } | j d  | j d d d   t d  D  | j   x$t d  D]} | s |  j j d	  } |  j	 | j
 d   d | _
 n |  j j d	 d d } |  j	 | j
 d  d
 | _ | j d  x- t |  D] \ } \ } |  j	 | |  q W| j d  xN t d d d  D]: } | j d  |  j	 | | j   d  | j d  qCW| j   qu Wd  S)N
scrollableTz"create table scrollable (data int)z"insert into scrollable values (%s)c             S   s   g  |  ] } | f  q Sr   r   ).0rT   r   r   r   
<listcomp>  s   	 z/CursorTests.test_scrollable.<locals>.<listcomp>d   r7   Sr3   z
                select x.data
                from scrollable x
                join scrollable y on x.data = y.data
                order by y.datar   c   r   rh   rh   rh   rh   )r   r   r   r   r   r   r    ranger   r*   rb   itersize	enumeratescrollr+   )r   rA   trT   nr   r   r   test_scrollable  s2    	
	zCursorTests.test_scrollablec             C   sI  |  j  t j |  j j d d |  j j   } | j d  | j d d d   t d  D  | j   |  j j d  } | j d	  |  j	 | j
 d   | j d
  y | j d  Wn" t j k
 r |  j d  SYn X| j   |  j j d d d } |  j	 | j
 d  | j d	  | j d
  |  j  t j | j d  d  S)Nrb   Fz"create table scrollable (data int)z"insert into scrollable values (%s)c             S   s   g  |  ] } | f  q Sr   r   )rc   rT   r   r   r   rd   >  s   	 z3CursorTests.test_not_scrollable.<locals>.<listcomp>re   rf   zselect * from scrollabler7   r   z$can't evaluate non-scrollable cursorrh   rh   )r   r   r   r   r   r   r    ri   r   r*   rb   rl   OperationalErrorZskipTest)r   rA   r   r   r   test_not_scrollable7  s*    	

zCursorTests.test_not_scrollable   r7   c             C   s   |  j  j d  } | j d  t |  } t |  d } t j d  t |  d } |  j | | j d d k  d | |  d  S)Ntmpz2select clock_timestamp() from generate_series(1,2)r   g?gư>g?z8named cursor records fetched in 2 roundtrips (delta: %s))	r   r   r   iternexttimesleepr   microseconds)r   rA   rT   t1Zt2r   r   r    test_iter_named_cursor_efficientQ  s    z,CursorTests.test_iter_named_cursor_efficientr   c                sb   |  j  j d      j d    f d d     D } |  j | d d   t d d  D  d  S)Nrs   zselect generate_series(1,50)c                s#   g  |  ] } | d    j  f  q S)r   )	rownumber)rc   r)rA   r   r   rd   d  s   	 zGCursorTests.test_iter_named_cursor_default_itersize.<locals>.<listcomp>c             S   s   g  |  ] } | | f  q Sr   r   )rc   rT   r   r   r   rd   f  s   	 r   3   )r   r   r   r*   ri   )r   rvr   )rA   r   'test_iter_named_cursor_default_itersize`  s    z3CursorTests.test_iter_named_cursor_default_itersizec                sk   |  j  j d    d   _   j d    f d d     D } |  j | d d   t d d  D  d  S)	Nrs   rR   zselect generate_series(1,50)c                s#   g  |  ] } | d    j  f  q S)r   )r{   )rc   r|   )rA   r   r   rd   m  s   	 z?CursorTests.test_iter_named_cursor_itersize.<locals>.<listcomp>c             S   s(   g  |  ] } | | d  d d  f  q S)r   rR   r   )rc   rT   r   r   r   rd   o  s   	 r   r}   )r   r   rj   r   r*   ri   )r   r~   r   )rA   r   test_iter_named_cursor_itersizeh  s
    	z+CursorTests.test_iter_named_cursor_itersizec             C   s`   |  j  j d  } d | _ | j d  x1 t |  D]# \ } } |  j | d | j  q5 Wd  S)Nrs   r;   zselect generate_series(1,10)r   )r   r   rj   r   rk   r*   r{   )r   rA   rT   Zrecr   r   r    test_iter_named_cursor_rownumberq  s
    	z,CursorTests.test_iter_named_cursor_rownumberc             C   s  |  j  j   } | j d  |  j t | j  d  xQ | j D]F } |  j t |  d  x' d D] } |  j t | |  |  qb Wq? W| j d } |  j | j d  |  j | j	 t
 j j j k  |  j | j d k  |  j | j d  |  j | j d  | j d } |  j | j d  |  j | j	 t
 j j k  |  j | j d k   |  j | j d   |  j | j d   | j d } |  j | j d  |  j | j	 t
 j j j k  |  j | j d k  |  j | j d   |  j | j d   d  S)Nzselect
            3.14::decimal(10,2) as pi,
            'hello'::text as hi,
            '2010-02-18'::date as now;
            rF      rP   	type_codedisplay_sizeinternal_size	precisionscalenull_okr   pir3   r7   r   hinow)znamez	type_coder   zinternal_sizez	precisionzscaler   )r   r   r   r*   lendescriptionr   hasattrrP   r   r   rG   ZDECIMALvaluesr   r   r   STRINGZDATE)r   rA   rE   ar   r   r   test_namedtuple_description|  s6    
!z'CursorTests.test_namedtuple_descriptionc             C   s]   |  j  j   } | j d  | j } t j | t j  } t j |  } |  j | |  d  S)NzSELECT 1 AS foo)	r   r   r   r   pickledumpsHIGHEST_PROTOCOLloadsr*   )r   rA   r   ZpickledZ	unpickledr   r   r   test_pickle_description  s    	z#CursorTests.test_pickle_descriptionc             C   s   |  j  j   } | j d  |  j  j d  } |  j d
 | j    |  j d d d g | j d   |  j d d d g | j    d  S)NzADECLARE test CURSOR WITHOUT HOLD  FOR SELECT generate_series(1,7)testr   r7   rF            r   )r   )r7   )rF   )r   )r   )r   )r   )r   r   r   r*   r+   Z	fetchmanyrS   )r   Zcur1Zcur2r   r   r   test_named_cursor_stealing  s    "z&CursorTests.test_named_cursor_stealingc             C   s  |  j  j   } | j d  | j d  |  j | j   d  | j d  |  j | j   d  | j d d d |  j | j   d  | j d  |  j | j   d  | j d  |  j | j   d  | j d d d	 |  j | j   d  | j d
 d d	 |  j | j   d  |  j t t j	 f | j d d d	 | j d
 d d	 |  j t t j	 f | j d  | j d d d	 |  j | j   d  |  j t t j	 f | j d d d	 | j d d d	 |  j t t j	 f | j d  d  S)Nzselect generate_series(0,9)r7   r   moderelativerr   r   r   absoluter   	   r3   )r7   )r   )rr   rh   )rr   )r   )r7   )r   rh   rh   )r   )
r   r   r   rl   r*   r+   r   
IndexErrorr   r   )r   r   r   r   r   test_scroll  s8    zCursorTests.test_scrollc             C   s   |  j  j d d d } | j d  | j d  |  j | j   d  | j d  |  j | j   d  | j d d d |  j | j   d  | j d
 d d |  j | j   d  d  S)Nrs   rb   Tzselect generate_series(0,9)r7   r   r   r   rr   r   r   )r7   )r   )rr   )r   )r   r   r   rl   r*   r+   )r   r   r   r   r   test_scroll_named  s    zCursorTests.test_scroll_namedc             C   s^   Gd d   d t  j j  } |   } |  j t  j | j d  |  j t  j | j d g   d  S)Nc               @   s   e  Z d  Z d d   Z d S)z3CursorTests.test_bad_subclass.<locals>.StupidCursorc             _   s   d  S)Nr   )r   argskwargsr   r   r   __init__  s    z<CursorTests.test_bad_subclass.<locals>.StupidCursor.__init__N)__name__
__module____qualname__r   r   r   r   r   StupidCursor  s   r   zselect 1)r   rG   r   r   ZInterfaceErrorr   r    )r   r   r   r   r   r   test_bad_subclass  s
    	zCursorTests.test_bad_subclassc             C   s,   |  j  j   } |  j t | j d d  d  S)Nlowerr:   )r   r   r   	TypeErrorcallproc)r   r   r   r   r   test_callproc_badparam  s    z"CursorTests.test_callproc_badparamr   c             C   s9  d j    } d | j d d  } d } |  j j   } | j d | | f  | j | i d | 6 |  j | j   d d	  i d | 6d
 d 6t j	 f i d | 6t j	 f i d | 6t j	 f i d d 6t j	 f i d d 6t
 f i d |  6t
 f g } x: | D]2 \ } } |  j | | j | |  |  j j   q Wd  S)Nz8
            Robert'); drop table "students" --
        z"%s""z""zpg_temp.randallz
            CREATE FUNCTION %s(%s INT)
            RETURNS INT AS
                'SELECT $1 * $1'
            LANGUAGE SQL
        r7   r   r   r8   r"   2Ztwou   bjÃrnrF   )stripreplacer   r   r   r   r*   r+   r   r   r   r   rV   )r   Z	paramnameZescaped_paramnameZprocnamer   Zfailing_casesZparameter_sequence	exceptionr   r   r   test_callproc_dict  s"    zCursorTests.test_callproc_dictr   c             C   s5   |  j  } |  j } d d   } |  j | | |  d  S)Nc             S   s   d  S)Nr   )r   r   r   r   r2   *  s    z6CursorTests.test_external_close_sync.<locals>.<lambda>)r   connect_test_external_close)r   control_connconnect_func	wait_funcr   r   r   test_external_close_sync  s    			z$CursorTests.test_external_close_syncc                s>     j  }   f d d   } t j j }   j | | |  d  S)Nc                  s     j  d d  S)NZasyncT)r   r   )r   r   r   r2   4  s    z7CursorTests.test_external_close_async.<locals>.<lambda>)r   r   extrasZwait_selectr   )r   r   r   r   r   )r   r   test_external_close_async-  s    	z%CursorTests.test_external_close_asyncc                s   x t  d  D] } |           j   2 } | j d      | j   d d } Wd  QX| j    } | j d | f  Wd  QXt j d     f d d   } |  j t j |  |  j	   j
 d  q Wd  S)	Nr3   zselect pg_backend_pid()r   zselect pg_terminate_backend(%s)gMbP?c           	      s0     j     }  |  j d      Wd  QXd  S)Nzselect 1)r   r   )r   )victim_connr   r   r   fJ  s    z+CursorTests._test_external_close.<locals>.fr7   )ri   r   r   rS   rv   rw   r   r   rp   r*   r   )r   r   r   r   rT   r   Zpid1r   r   )r   r   r   r   8  s    	

z CursorTests._test_external_closeN)*r   r   r   r   r   r!   r-   r0   r   r6   r9   rB   rJ   rO   rQ   rU   rW   r[   r`   ra   ro   rq   r   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   $   sL   &	'	$
"$!!	r   c               C   s   t  j   j t  S)N)r   Z
TestLoaderZloadTestsFromNamer   r   r   r   r   
test_suiteT  s    r   __main__)rv   r   r   Zpsycopg2.extensionsZ	testutilsr   r   r   r   r   r   r	   r
   Zpsycopg2.extrasr   r   r   mainr   r   r   r   <module>   s   :  2