
5dVJ                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d d l m Z m Z m Z d d l m	 Z	 m
 Z
 Gd d   d e  Z d d	   Z e d
 k r e j   n  d S)    N)b   )unittestConnectingTestCaseskip_before_postgres)skip_if_no_namedtupleskip_if_no_getrefcountc               @   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 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" d> S)?CursorTestsc             C   s7   |  j  j   } | j   | j   |  j | j  d  S)N)conncursorclose
assertTrueclosed)selfcur r   >/tmp/pip-build-0jahl3lb/psycopg2/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   buggygen3   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_exceptions/   s    		z1CursorTests.test_executemany_propagate_exceptionsc             C   s  |  j  } | j   } | j d  |  j d | j   d  |  j d | j d   | j d  d } | j d |  |  j | j d  t | j   d   |  j d | j d  | j d |  j	 d	 d
   | j d | f  |  j | j d  t | j   d   |  j d | j d  | j d | f  j	 d	 d
   | j d | f  |  j | j d  t | j   d   |  j d | j d  | j d | f  j	 d	 d
   d  S)NzSELECT 'foo';foor   s   SELECT 'foo';UTF8u   ☃zSELECT '%s';utf8s   E'   'z
SELECT %s;zutf-8)
r
   r   r   assertEqualfetchonemogrifyZset_client_encodingencoder   replace)r   r
   r   Zsnowmanr   r   r   test_mogrify_unicode9   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_explodesY   s    			z)CursorTests.test_mogrify_decimal_explodesc             C   s   |  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>j   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   ir    r0   Znref1Znref2r   r   r   'test_mogrify_leak_on_multiple_referencee   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_placeholderr   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   r4   i:  z
2011-01-02z	who am i?i  )
r
   r   r$   castZassertAlmostEqualr+   r*   ImportErrordatetimer9   )r   cursr*   r9   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)Nr4   r   )vcr   r   r   r.      s    z3CursorTests.test_cast_specificity.<locals>.<lambda>ZfoofooZTREBLINGc             S   s   |  d S)N   r   )r@   rA   r   r   r   r.      s    Z	foofoofoo)r?   )r?   )r
   r   r$   r:   r   
extensionsZnew_typeZregister_type)r   r=   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)weakrefrG   r
   r   gcZcollectr   )r   rG   r=   wrI   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   r=   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);r/   r8      zinsert into invname values (%s)z1-2-3 \ "test"z&select data from invname order by data)r/   r8   rN   )r/   )r8   )rN   )r
   r   r   r   r$   fetchall)r   r=   r2   r   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)r/   r8   rN   z insert into withhold values (%s))r/   r8   rN   )r
   r   r   r   r   rollbackr   )r   r=   r2   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 datar/   r8   rN   zdrop table withhold)r/   )r8   )rN   )r/   )r8   )rN   )r   r   r   r
   r   rR   r$   rS   r   commitrO   r   )r   r=   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)	NrJ   rS   Tz'select data from withhold order by datar/   r8   )r/   )r8   )rR   r
   r   r   r$   r%   statusr   rC   ZSTATUS_BEGINget_transaction_statusZTRANSACTION_STATUS_INTRANSrU   STATUS_READYTRANSACTION_STATUS_IDLEr   )r   r=   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)NTrJ   rS   z'select data from withhold order by datar/   )r/   )rR   r
   rU   Z
autocommitr   r   r$   r%   rW   r   rC   rY   rX   rZ   r   )r   r=   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   ).0r2   r   r   r   
<listcomp>  s   	 z/CursorTests.test_scrollable.<locals>.<listcomp>d   r4   Sr/   z
                select x.data
                from scrollable x
                join scrollable y on x.data = y.data
                order by y.datar   c   r   rc   rc   rc   rc   )r   r   r   r
   r   r   r   ranger   r$   r]   itersize	enumeratescrollr%   )r   r=   tr2   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)Nr]   Fz"create table scrollable (data int)z"insert into scrollable values (%s)c             S   s   g  |  ] } | f  q Sr   r   )r^   r2   r   r   r   r_   3  s   	 z3CursorTests.test_not_scrollable.<locals>.<listcomp>r`   ra   zselect * from scrollabler4   r   z$can't evaluate non-scrollable cursorrc   rc   )r   r   r   r
   r   r   r   rd   r   r$   r]   rg   ZOperationalErrorZskipTest)r   r=   r   r   r   test_not_scrollable,  s*    	

zCursorTests.test_not_scrollable   r4   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   r=   r2   t1t2r   r   r    test_iter_named_cursor_efficientF  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)Nrm   zselect generate_series(1,50)c                s#   g  |  ] } | d    j  f  q S)r   )	rownumber)r^   r)r=   r   r   r_   X  s   	 zGCursorTests.test_iter_named_cursor_default_itersize.<locals>.<listcomp>c             S   s   g  |  ] } | | f  q Sr   r   )r^   r2   r   r   r   r_   Z  s   	 r   3   )r
   r   r   r$   rd   )r   rvr   )r=   r   'test_iter_named_cursor_default_itersizeT  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)	Nrm   rN   zselect generate_series(1,50)c                s#   g  |  ] } | d    j  f  q S)r   )rv   )r^   rw   )r=   r   r   r_   a  s   	 z?CursorTests.test_iter_named_cursor_itersize.<locals>.<listcomp>c             S   s(   g  |  ] } | | d  d d  f  q S)r   rN   r   )r^   r2   r   r   r   r_   c  s   	 r   rx   )r
   r   re   r   r$   rd   )r   ry   r   )r=   r   test_iter_named_cursor_itersize\  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)Nrm   r8   zselect generate_series(1,10)r   )r
   r   re   r   rf   r$   rv   )r   r=   r2   Zrecr   r   r    test_iter_named_cursor_rownumbere  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;
            rB      rL   	type_codedisplay_sizeinternal_size	precisionscalenull_okr   pir/   r4   r   hinow)znamez	type_coder   zinternal_sizez	precisionzscaler   )r
   r   r   r$   lendescriptionr   hasattrrL   r~   r   rC   ZDECIMALvaluesr   r   r   STRINGZDATE)r   r=   rA   ar   r   r   test_namedtuple_descriptionp  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   r=   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   r4   rB            r}   )r   )r4   )rB   )r   )r   )r   )r}   )r
   r   r   r$   r%   Z	fetchmanyrO   )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)r4   r   moderelativerl   r   r}   absoluter   	   r/   )r4   )r   )rl   rc   )rl   )r}   )r4   )r   rc   rc   )r   )
r
   r   r   rg   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)Nrm   r]   Tzselect generate_series(0,9)r4   r   r   r   rl   r   r   )r4   )r   )rl   )r   )r
   r   r   rg   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   rC   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lowerr7   )r
   r   r   	TypeErrorZcallproc)r   r   r   r   r   test_callproc_badparam  s    z"CursorTests.test_callproc_badparamN)#r   r   r   r   r   r   r)   r,   r   r3   r6   r>   rF   rK   rM   rP   rR   rV   r[   r\   rj   rk   r   ru   rz   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r	   !   s:   
 '	$
"r	   c               C   s   t  j   j t  S)N)r   Z
TestLoaderZloadTestsFromNamer   r   r   r   r   
test_suite  s    r   __main__)rp   r   r   Zpsycopg2.extensionsr   Z	testutilsr   r   r   r   r   r	   r   r   mainr   r   r   r   <module>   s    