
|R\                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l	 Z	 d  d l
 Z	 d  d l	 m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z Gd d   d e  Z Gd	 d
   d
 e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z  e e  e  Gd d   d e  Z! Gd d   d e  Z" Gd d   d e  Z# Gd d   d e  Z$ d d   Z% e& d k re j'   n  d S)    N)
attrgetter)
extensions   )script_to_py3unittestdecorate_all_testsskip_if_no_superuserskip_before_postgresskip_after_postgresskip_before_libpqConnectingTestCaseskip_if_tpc_disabledskip_if_windowsslow)dsndbnamec               @   sZ  e  Z d  Z d d   Z d d   Z d d   Z e d d  e e d	 d
      Z	 d d   Z
 d d   Z d d   Z e d d    Z e d d    Z d d   Z d d   Z d d   Z d d   Z e e d d  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 d0 S)1ConnectionTestsc             C   s=   |  j  } |  j | j d  | j   |  j | j d  d  S)NFT)connassertEqualclosedclose)selfr    r   S/var/www/dbchiro/venv/lib/python3.4/site-packages/psycopg2/tests/test_connection.pytest_closed_attribute.   s    	
z%ConnectionTests.test_closed_attributec             C   s1   |  j  } | j   | j   |  j | j  d  S)N)r   r   
assertTruer   )r   r   r   r   r   test_close_idempotent4   s    	

z%ConnectionTests.test_close_idempotentc             C   sr   |  j  } | j   } |  j | j d  | j   |  j | j d  | j   } | j   |  j | j d  d  S)NFT)r   cursorr   r   r   )r   r   cursr   r   r   test_cursor_closed_attribute:   s    	

z,ConnectionTests.test_cursor_closed_attribute      c             C   sb   |  j  } | j   } |  j t j | j d  |  j | j d  | j   |  j | j d  d  S)Nz-select pg_terminate_backend(pg_backend_pid())   r   )	r   r   assertRaisespsycopg2ZOperationalErrorexecuter   r   r   )r   r   curr   r   r   test_cleanup_on_badconn_closeF   s    	
z-ConnectionTests.test_cleanup_on_badconn_closec             C   s  |  j  } d | _ d | _ d | _ |  j  j d k rB d | _ n  |  j | j  |  j | j t j	  |  j | j d k  |  j  j d k r |  j | j d k  n  | j
   |  j | j  |  j | j t j  |  j | j d  k  |  j  j d k r|  j | j d  k  n  d  S)NTserializablei_ F)r   
autocommitisolation_levelreadonlyserver_version
deferrabler   r   extISOLATION_LEVEL_SERIALIZABLEresetISOLATION_LEVEL_DEFAULT)r   r   r   r   r   
test_resetT   s"    				
zConnectionTests.test_resetc             C   sk   |  j  } | j   } |  j  j d k r7 | j d  n  | j d  |  j d | j  |  j | j  d  S)Ni` zset client_min_messages=debug1z1create temp table chatty (id serial primary key);zCREATE TABLE)r   r   r,   r%   r   Zstatusmessager   notices)r   r   r&   r   r   r   test_noticesk   s    	zConnectionTests.test_noticesc             C   s   |  j  } | j   } |  j  j d k r7 | j d  n  | j d  | j d  |  j d t | j   |  j d | j d k  |  j d | j d	 k  |  j d
 | j d k  |  j d | j d k  d  S)Ni` zset client_min_messages=debug1zq
            create temp table table1 (id serial);
            create temp table table2 (id serial);
            zq
            create temp table table3 (id serial);
            create temp table table4 (id serial);
            r!   table1r   table2r   table3r"   table4   )r   r   r,   r%   r   lenr3   r   )r   r   r&   r   r   r   test_notices_consistent_ordert   s    	z-ConnectionTests.test_notices_consistent_orderc             C   s   |  j  } | j   } |  j  j d k r7 | j d  n  xP t d d d  D]< } d j d d   t | | d  D  } | j |  qJ W|  j d	 t | j   |  j	 d
 | j d k | j d  d  S)Ni` zset client_min_messages=debug1r   d   
    c             S   s   g  |  ] } d  |  q S)z&create temp table table%d (id serial);r   ).0jr   r   r   
<listcomp>   s   	z8ConnectionTests.test_notices_limited.<locals>.<listcomp>2   Ztable99r   rC   )
r   r   r,   r%   rangejoinr   r:   r3   r   )r   r   r&   isqlr   r   r   test_notices_limited   s    	z$ConnectionTests.test_notices_limitedc             C   s  d d l  m } |  j } |   |  j _ | j   } |  j j d k rV | j d  n  | j d  | j d  |  j t | j  d  |  j	 d | j j
   k  |  j	 d	 | j j
   k  |  j	 d
 | j j
   k  |  j	 d | j j
   k  |  j t | j  d  xP t d d d  D]< } d j d d   t | | d  D  } | j |  q%W|  j t d d   | j D  d  d  S)Nr   )dequei` zset client_min_messages=debug1zq
            create temp table table1 (id serial);
            create temp table table2 (id serial);
            zd
            create temp table table3 (id serial);
            create temp table table4 (id serial);r!   r5   r6   r7   r8   r<   r=   r>   c             S   s   g  |  ] } d  |  q S)z(create temp table table2_%d (id serial);r   )r?   r@   r   r   r   rA      s   	z6ConnectionTests.test_notices_deque.<locals>.<listcomp>c             S   s"   g  |  ] } d  | k r |  q S)zCREATE TABLEr   )r?   nr   r   r   rA      s   	 )collectionsrI   r   r3   r   r,   r%   r   r:   r   popleftrD   rE   )r   rI   r   r&   rF   rG   r   r   r   test_notices_deque   s,    	z"ConnectionTests.test_notices_dequec             C   sj   |  j  } d  |  j  _ | j   } |  j  j d k rC | j d  n  | j d  |  j |  j  j d   d  S)Ni` zset client_min_messages=debug1z%create temp table table1 (id serial);)r   r3   r   r,   r%   r   )r   r   r&   r   r   r   test_notices_noappend   s    	z%ConnectionTests.test_notices_noappendc             C   s   |  j  |  j j  d  S)N)r   r   r,   )r   r   r   r   test_server_version   s    z#ConnectionTests.test_server_versionc             C   s&   |  j  |  j j d k |  j j  d  S)Nr"   r9   )r"   r9   )r   r   protocol_version)r   r   r   r   test_protocol_version   s    z%ConnectionTests.test_protocol_versionc             C   sH   |  j  } | j d k r% |  j d  S|  j t j | j d d d  d  S)Ni8 ztpc is supported*   foobar)r   r,   ZskipTestr#   r$   ZNotSupportedErrorxid)r   cnnr   r   r   test_tpc_unsupported   s
    	z$ConnectionTests.test_tpc_unsupportedr"   c                s     f d d   } t  j d |  } t  j d |  } t j   } | j   | j   | j   | j     j t j   | d k  d  d  S)Nc                 s=     j    }  |  j   } | j d  | j   |  j   d  S)Nzselect pg_sleep(4))connectr   r%   r   )rV   r&   )r   r   r   slave   s
    
z8ConnectionTests.test_concurrent_execution.<locals>.slavetarget   zsomething broken in concurrency)	threadingThreadtimestartrE   r   )r   rY   t1Zt2t0r   )r   r   test_concurrent_execution   s    



z)ConnectionTests.test_concurrent_executionc             C   s]   |  j  j d  |  j  j   } t j t j |  | j d  |  j | j   d d  d  S)NZEUC_JPzselect 'foo'::text;r   rS   )	r   set_client_encodingr   r.   Zregister_typeUNICODEr%   r   fetchone)r   r&   r   r   r   test_encoding_name   s
    z"ConnectionTests.test_encoding_namec             C   sm   |  j  j   t j j d  } d t j d <z |  j   |  _  Wd  | d  k	 r^ | t j d <n
 t j d =Xd  S)NZPGCLIENTENCODINGzutf-8)r   r   osenvirongetrX   )r   Zoldencr   r   r   test_connect_nonnormal_envvar   s    z-ConnectionTests.test_connect_nonnormal_envvarc             C   sh   d d l  m } d d  l } t j t  } | |  } | j   ~ | j   |  j |   d  k  d  S)Nr   )ref)	weakrefrk   gcr$   rX   r   r   Zcollectr   )r   rk   rm   r   wr   r   r   test_weakref   s    

zConnectionTests.test_weakrefc                s   |  j    g   g       f d d   }   j   } t j d |  } | j   d } x] t d  D]O } | j d | f    j   x)   j r  j	 d   j j
   f  q Wqh W j	 d  |  j  d t    d  S)	Nc                  sG   x@  sB   j    x)   j r>  j d   j j   f  q Wq Wd  S)Nr"   )commitr3   appendpopr   )r   r3   stopr   r   	committer  s    	
z:ConnectionTests.test_commit_concurrency.<locals>.committerrZ   r   i  z
select %s;Tz%d notices raised)r   r   r\   r]   r_   rD   r%   rp   r3   rq   rr   r   r:   )r   rt   r&   r`   rF   r   )r   r3   rs   r   test_commit_concurrency  s    	

$z'ConnectionTests.test_commit_concurrencyc             C   s[   d d  l  } |  j d | j j  } | j   } | j d  |  j | j   d d  d  S)Nr   cursor_factoryzselect 1 as aar   )psycopg2.extrasrX   extras
DictCursorr   r%   r   re   )r   r$   r   r&   r   r   r   test_connect_cursor_factory!  s
    z+ConnectionTests.test_connect_cursor_factoryc             C   s  |  j  |  j j d   |  j j   } | j d  |  j t d d   | j    t j	 j
 |  j _ |  j  |  j j t j	 j
  |  j j   } | j d  |  j  | j   d d  d  |  j _ |  j  |  j j d   |  j j   } | j d  |  j t d d   | j    d  S)Nzselect 1 as ac             S   s   |  d S)Nrw   r   )rr   r   r   <lambda>,  s    z5ConnectionTests.test_cursor_factory.<locals>.<lambda>rw   r   c             S   s   |  d S)Nrw   r   )r|   r   r   r   r}   8  s    )r   r   rv   r   r%   r#   	TypeErrorre   r$   ry   rz   )r   r&   r   r   r   test_cursor_factory(  s    z#ConnectionTests.test_cursor_factoryc             C   s   |  j    } | j d d   } |  j t |  t j  |  j  d t j j  } | j d d   } |  j t |  t j j  d  S)Nrv   )rX   r   r   typer.   r$   ry   rz   )r   r   r&   r   r   r   test_cursor_factory_none:  s    z(ConnectionTests.test_cursor_factory_nonec                s[   G  f d d   d t  j      d  } |  j | j d  |  j d | j k d  d  S)Nc                   s%   e  Z d  Z    f d d   Z   S)z>ConnectionTests.test_failed_init_status.<locals>.SubConnectionc                s3   y t    |   j |  Wn t k
 r. Yn Xd  S)N)super__init__	Exception)r   r   )SubConnection	__class__r   r   r   F  s    zGConnectionTests.test_failed_init_status.<locals>.SubConnection.__init__)__name__
__module____qualname__r   r   )r   )r   r   r   E  s   r   z0dbname=thereisnosuchdatabasemate password=foobarz&connection failed so it must be closedZfoobarzpassword was not obscured)r.   
connectionr   r   r   )r   cr   )r   r   test_failed_init_statusD  s    z'ConnectionTests.test_failed_init_statusN)r   r   r   r   r   r   r	   r   r   r'   r2   r4   r;   r   rH   rM   rN   rO   rQ   rW   rb   rf   rj   ro   ru   r{   r   r   r   r   r   r   r   r   -   s2   	!

r   c               @   s[   e  Z d  Z d d   Z e d d  d d    Z d d   Z d	 d
   Z d d   Z d S)ParseDsnTestCasec             C   s.  d d l  m } |  j t j d  t d d d d d d	  d
  |  j | t j d  |  j t j d  t d d d d d d  d  d } y t j d  Wnx | k
 r } z0 d } |  j t |  j	 d  d k  d  WYd  d  } ~ Xn) | k
 r|  j
 d t |   Yn X|  j | d  d  S)Nr   )ProgrammingErrorz'dbname=test user=tester password=secretusertesterpasswordsecretr   testzsimple DSN parsedz)dbname=test 2 user=tester password=secretz+dbname='test 2' user=tester password=secretztest 2zDSN with quoting parsedFz*dbname='test 2 user=tester password=secretTz$DSN was not exposed in error messagezunexpected error condition: z*ProgrammingError raised due to invalid DSN)r$   r   r   r.   	parse_dsndictr#   r   strfindfailrepr)r   r   raiseder   r   r   test_parse_dsnR  s*    zParseDsnTestCase.test_parse_dsn	   r"   c             C   s   |  j  t j d  t d d d d d d  d  d	 } y t j d
 d  Wn{ t j k
 r } z0 d } |  j t |  j d  d k  d  WYd  d  } ~ Xn) | k
 r |  j	 d t
 |   Yn X|  j | d  d  S)Nz postgresql://tester:secret@/testr   r   r   r   r   r   zvalid URI dsn parsedFr   z,postgresql://tester:secret@/test?port=1111=xTr   z$URI was not exposed in error messagezunexpected error condition: z*ProgrammingError raised due to invalid URI)r   r.   r   r   r$   r   r   r   r   r   r   )r   r   r   r   r   r   test_parse_dsn_uriq  s    z#ParseDsnTestCase.test_parse_dsn_uric             C   sd   d } t  j d |  } t j d d k  rL |  j | d | j d   n |  j | d |  d  S)Nu   ☃zdbname=r   r9   r   utf8)r.   r   sysversion_infor   encode)r   snowmandr   r   r   test_unicode_value  s
     z#ParseDsnTestCase.test_unicode_valuec             C   s+   d } |  j  t j t j | d |  d  S)Nu   ☃=)r#   r$   r   r.   r   )r   r   r   r   r   test_unicode_key  s    z!ParseDsnTestCase.test_unicode_keyc             C   s0   |  j  t t j d   |  j  t t j d  d  S)NrR   )r#   r~   r.   r   )r   r   r   r   test_bad_param  s    zParseDsnTestCase.test_bad_paramN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   Q  s
   r   c               @   s   e  Z 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 d S)MakeDsnTestCasec             C   s   |  j  t j   d  d  S)N )r   r.   make_dsn)r   r   r   r   test_empty_arguments  s    z$MakeDsnTestCase.test_empty_argumentsc             C   s#   t  j d  } |  j | d  d  S)Nr   )r.   r   r   )r   r   r   r   r   test_empty_string  s    z!MakeDsnTestCase.test_empty_stringc             C   sX   |  j  t j t j d  |  j  t j t j d d |  j  t j t j d d d d  S)Nzdbnamo=aZdbnamorw   zdbname=aZnosuchparamb)r#   r$   r   r.   r   )r   r   r   r   test_params_validation  s    z&MakeDsnTestCase.test_params_validationc             C   s,   t  j d d d d  } |  j | d  d  S)Nr   Zsonyr   r   zdbname=sony password='')r.   r   assertDsnEqual)r   r   r   r   r   test_empty_param  s    z MakeDsnTestCase.test_empty_paramc             C   s   t  j d d  } |  j | d  t  j d d  } |  j | d  t  j d d  } |  j | d  t  j d d  } |  j | d	  t  j d d
  } |  j | d  d  S)Nr   zhello worldzdbname='hello world'z
back\slashzdbname=back\\slashzquo'tezdbname=quo\'tezwith	tabzdbname='with	tab'z\every thing'zdbname='\\every thing\'')r.   r   r   )r   r   r   r   r   test_escape  s    zMakeDsnTestCase.test_escapec             C   s    |  j  t j d d  d  d  S)NdatabaseZsighzdbname=sigh)r   r.   r   )r   r   r   r   test_database_is_a_keyword  s    z*MakeDsnTestCase.test_database_is_a_keywordc             C   sN   t  j d d d } |  j | d  t  j d d d } |  j | d  d  S)	Nzdbname=foo host=barhostZbazzdbname=foo host=bazz
dbname=foor   Zpostgreszdbname=foo user=postgres)r.   r   r   )r   r   r   r   r   test_params_merging  s    z#MakeDsnTestCase.test_params_mergingc             C   s)   d } t  j |  } |  j | |  d  S)Nz!dbname=a host=b user=c password=d)r.   r   r   )r   Zdsninr   r   r   r   test_no_dsn_munging  s    z#MakeDsnTestCase.test_no_dsn_mungingc             C   s/   t  j d d d d d  } |  j | d  d  S)Nz
dbname=foor   rT   r   zdbname=foo user=bar)r.   r   r   )r   r   r   r   r   test_null_args  s    zMakeDsnTestCase.test_null_argsr   r"   c             C   s   d } t  j |  } |  j | |  t  j | d d } |  j | d  |  j t j t  j d  |  j t j t  j | d d d  S)Nz5postgresql://tester:secret@/test?application_name=watZapplication_nameZwootz=dbname=test user=tester password=secret application_name=wootz-postgresql://tester:secret@/test?nosuch=paramZnosuchparam)r.   r   r   r   r#   r$   r   )r   urlr   r   r   r   test_url_is_cool  s    	z MakeDsnTestCase.test_url_is_coolr9   c             C   sF   |  j    } | j   } |  j | d t  |  j d | k |  d  S)Nr   r   )rX   Zget_dsn_parametersr   r   r   )r   r   r   r   r   r   test_get_dsn_parameters  s    z'MakeDsnTestCase.test_get_dsn_parametersN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c               @   s   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d S)IsolationLevelsTestCasec             C   s}   t  j |   |  j   } | j   } y | j d  Wn t j k
 rW | j   Yn X| j d  | j   | j	   d  S)Nzdrop table isolevel;z#create table isolevel (id integer);)
r   setUprX   r   r%   r$   r   rollbackrp   r   )r   r   r&   r   r   r   r     s    
zIsolationLevelsTestCase.setUpc             C   s&   |  j    } |  j | j t j  d  S)N)rX   r   r*   r.   r1   )r   r   r   r   r   test_isolation_level  s    z,IsolationLevelsTestCase.test_isolation_levelc             C   s)   |  j    } |  j | j t j k  d  S)N)rX   r   encodingr.   	encodings)r   r   r   r   r   test_encoding  s    z%IsolationLevelsTestCase.test_encodingc             C   s<  |  j    } | j   } d t j f d t j f d t j f d t j f g } x | D] \ } } | j |  | j d k  r | t j t j f k r | | j	 | | f  d \ } } q n  |  j
 | j |  | j d  | j   d } |  j
 | |  | j   qU W|  j t | j d
  |  j t | j d	  d  S)Nzread uncommittedzread committedzrepeatable readr(   i8 r   zshow transaction_isolation;r      rC   )rX   r   r.    ISOLATION_LEVEL_READ_UNCOMMITTEDISOLATION_LEVEL_READ_COMMITTEDISOLATION_LEVEL_REPEATABLE_READr/   set_isolation_levelr,   indexr   r*   r%   re   rp   r#   
ValueError)r   r   r   ZlevelsnamelevelZgot_namer   r   r   test_set_isolation_level  s*    	)z0IsolationLevelsTestCase.test_set_isolation_levelc             C   s   |  j    } | j   } | j t j  |  j | j t j  |  j | j	  d | _ |  j | j t j
  |  j | j	  | j d  |  j | j   d d  d  S)Nr(   zshow transaction_isolation;r   )rX   r   r   r.   ISOLATION_LEVEL_AUTOCOMMITr   r*   r1   r   r)   r/   r%   re   )r   r   r   r   r   r   #test_set_isolation_level_autocommit  s    	z;IsolationLevelsTestCase.test_set_isolation_level_autocommitc             C   s   |  j    } | j   } d | _ | j d  d | _ | j t j  |  j | j t j  | j d  |  j | j	   d d  | j
   | j t j  | j d  |  j | j	   d d  d  S)NTz5set default_transaction_isolation to 'read committed'Fzshow transaction_isolationr   r(   zread committed)rX   r   r)   r%   r   r.   r/   r   r*   re   r   r1   )r   r   r   r   r   r    test_set_isolation_level_default+  s    		

z8IsolationLevelsTestCase.test_set_isolation_level_defaultc             C   s  |  j    } | j   } |  j t j | j    | j d  |  j t j | j    | j t	 j
 j  |  j t	 j
 j | j    | j d  |  j d | j   d  | j d  |  j t	 j
 j | j    | j t	 j
 j  |  j t	 j
 j | j    | j d  |  j d | j   d  | j d  |  j t	 j
 j | j    | j t	 j
 j  |  j t	 j
 j | j    | j d  |  j d | j   d  |  j | j t	 j
 j  d  S)Nz!insert into isolevel values (10);zselect count(*) from isolevel;r   r   )rX   r   r   r.   TRANSACTION_STATUS_IDLEget_transaction_statusr%   TRANSACTION_STATUS_INTRANSr   r$   r   r/   re   r   r   r*   )r   r   r&   r   r   r   test_set_isolation_level_abort>  sB    z6IsolationLevelsTestCase.test_set_isolation_level_abortc             C   s   |  j    } |  j    } | j t j  | j   } | j d  |  j d | j   d  | j   | j   } | j d  | j d  |  j d | j   d  d  S)Nzselect count(*) from isolevel;r   z!insert into isolevel values (10);r   )	rX   r   r.   r   r   r%   r   re   rp   )r   cnn1cnn2cur1cur2r   r   r   test_isolation_level_autocommite  s    
z7IsolationLevelsTestCase.test_isolation_level_autocommitc             C   s?  |  j    } |  j    } | j t j  | j   } | j d  |  j d | j   d  | j   | j   } | j d  | j d  | j d  |  j d | j   d  | j   | j d  |  j d | j   d  | j d  |  j d | j   d  | j   | j d  |  j d | j   d  d  S)Nzselect count(*) from isolevel;r   z!insert into isolevel values (10);z!insert into isolevel values (20);r   r"   )	rX   r   r.   r   r   r%   r   re   rp   )r   r   r   r   r   r   r   r   #test_isolation_level_read_committedu  s(    


z;IsolationLevelsTestCase.test_isolation_level_read_committedc             C   sf  |  j    } |  j    } | j t j  | j   } | j d  |  j d | j   d  | j   | j   } | j d  | j d  | j d  |  j d | j   d  | j   | j d  |  j d | j   d  | j d  |  j d | j   d  | j   | j d  |  j d | j   d  | j d  |  j d | j   d  d  S)Nzselect count(*) from isolevel;r   z!insert into isolevel values (10);z!insert into isolevel values (20);r   r"   )	rX   r   r.   r/   r   r%   r   re   rp   )r   r   r   r   r   r   r   r   !test_isolation_level_serializable  s,    


z9IsolationLevelsTestCase.test_isolation_level_serializablec             C   sL   |  j    } | j   |  j t j | j d  |  j t j | j d  d  S)Nr   r   )rX   r   r#   r$   InterfaceErrorr   )r   rV   r   r   r   test_isolation_level_closed  s    
z3IsolationLevelsTestCase.test_isolation_level_closedc             C   s  |  j  j   } t j |  j  _ |  j |  j  j t j  | j d  |  j | j   d d  |  j  j   t j	 |  j  _ | j d  |  j  j
 d k r |  j |  j  j t j	  |  j | j   d d  n3 |  j |  j  j t j  |  j | j   d d  |  j  j   t j |  j  _ |  j |  j  j t j  | j d  |  j | j   d d  |  j  j   t j |  j  _ | j d  |  j  j
 d k r|  j |  j  j t j  |  j | j   d d  n3 |  j |  j  j t j  |  j | j   d d  |  j  j   |  j t j d   t j |  j  _ |  j |  j  j d   | j d  | j   d } | j d  |  j | j   d |  d  S)	NzSHOW transaction_isolation;r   r(   i8 zrepeatable readzread committedzread uncommittedz#SHOW default_transaction_isolation;)r   r   r.   r/   r*   r   r%   re   r   r   r,   r   r   r1   )r   r&   isolr   r   r    test_setattr_isolation_level_int  sN    




z8IsolationLevelsTestCase.test_setattr_isolation_level_intc             C   sm  |  j  j   } d |  j  _ |  j |  j  j t j  | j d  |  j | j   d d  |  j  j   d |  j  _ | j d  |  j  j	 d k r |  j |  j  j t j
  |  j | j   d d  n3 |  j |  j  j t j  |  j | j   d d  |  j  j   d |  j  _ |  j |  j  j t j  | j d  |  j | j   d d  |  j  j   d |  j  _ | j d  |  j  j	 d k r|  j |  j  j t j  |  j | j   d d  n3 |  j |  j  j t j  |  j | j   d d  |  j  j   d |  j  _ |  j |  j  j d   | j d  | j   d } | j d	  |  j | j   d |  d  S)
Nr(   zSHOW transaction_isolation;r   zrepeatable readi8 zread committedzread uncommitteddefaultz#SHOW default_transaction_isolation;)r   r   r*   r   r.   r/   r%   re   r   r,   r   r   r   )r   r&   r   r   r   r    test_setattr_isolation_level_str  sL    




z8IsolationLevelsTestCase.test_setattr_isolation_level_strc             C   st   |  j  t t |  j d d  |  j  t t |  j d d  |  j  t t |  j d d  |  j  t t |  j d d  d  S)Nr*   r   r   r   ZbahrC   )r#   r   setattrr   )r   r   r   r   $test_setattr_isolation_level_invalid  s    z<IsolationLevelsTestCase.test_setattr_isolation_level_invalidN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    '0/r   c               @   s<  e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z e d d     Z e d! d"    Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d- d.   Z d/ d0   Z d1 S)2ConnectionTwoPhaseTestsc             C   s%   t  j |   |  j   |  j   d  S)N)r   r   make_test_tableclear_test_xacts)r   r   r   r   r     s    
zConnectionTwoPhaseTests.setUpc             C   s   |  j    t j |   d  S)N)r   r   tearDown)r   r   r   r   r   !  s    
z ConnectionTwoPhaseTests.tearDownc             C   s   |  j    } | j d  | j   } y | j d t f  Wn- t j k
 rk | j   | j   d SYn Xd d   | D } x! | D] } | j d | f  q W| j   d S)z8Rollback all the prepared transaction in the testing db.r   z5select gid from pg_prepared_xacts where database = %sNc             S   s   g  |  ] } | d   q S)r   r   )r?   r|   r   r   r   rA   3  s   	 z<ConnectionTwoPhaseTests.clear_test_xacts.<locals>.<listcomp>zrollback prepared %s;)	rX   r   r   r%   r   r$   r   r   r   )r   rV   r&   Zgidsgidr   r   r   r   %  s    

	z(ConnectionTwoPhaseTests.clear_test_xactsc             C   sp   |  j    } | j   } y | j d  Wn t j k
 rJ | j   Yn X| j d  | j   | j   d  S)NzDROP TABLE test_tpc;z"CREATE TABLE test_tpc (data text);)rX   r   r%   r$   r   r   rp   r   )r   rV   r&   r   r   r   r   8  s    
z'ConnectionTwoPhaseTests.make_test_tablec             C   sI   |  j    } | j   } | j d t f  | j   d } | j   | S)z=Return the number of prepared xacts currently in the test db.zT
            select count(*) from pg_prepared_xacts
            where database = %s;r   )rX   r   r%   r   re   r   )r   rV   r&   rvr   r   r   count_xactsC  s    

z#ConnectionTwoPhaseTests.count_xactsc             C   sC   |  j    } | j   } | j d  | j   d } | j   | S)z/Return the number of records in the test table.zselect count(*) from test_tpc;r   )rX   r   r%   re   r   )r   rV   r&   r   r   r   r   count_test_recordsO  s    
z*ConnectionTwoPhaseTests.count_test_recordsc             C   s;  |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   gtridbqualz0insert into test_tpc values ('test_tpc_commit');r   )rX   rU   r   statusr.   STATUS_READY	tpc_beginSTATUS_BEGINr   r%   r   r   tpc_prepareSTATUS_PREPARED
tpc_commit)r   rV   rU   r&   r   r   r   test_tpc_commitX  s"    

z'ConnectionTwoPhaseTests.test_tpc_commitc             C   s   |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   r   r   z3insert into test_tpc values ('test_tpc_commit_1p');r   )rX   rU   r   r   r.   r   r   r   r   r%   r   r   r   )r   rV   rU   r&   r   r   r   test_tpc_commit_one_phaseo  s    
z1ConnectionTwoPhaseTests.test_tpc_commit_one_phasec             C   sS  |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   | j   |  j d |  j
    |  j d |  j    |  j    } | j d d d  } | j |  |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   r   r   z4insert into test_tpc values ('test_tpc_commit_rec');r   )rX   rU   r   r   r.   r   r   r   r   r%   r   r   r   r   r   )r   rV   rU   r&   r   r   r   test_tpc_commit_recovered  s&    

z1ConnectionTwoPhaseTests.test_tpc_commit_recoveredc             C   s;  |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   r   r   z2insert into test_tpc values ('test_tpc_rollback');r   )rX   rU   r   r   r.   r   r   r   r   r%   r   r   r   r   tpc_rollback)r   rV   rU   r&   r   r   r   test_tpc_rollback  s"    

z)ConnectionTwoPhaseTests.test_tpc_rollbackc             C   s   |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   r   r   z5insert into test_tpc values ('test_tpc_rollback_1p');r   )rX   rU   r   r   r.   r   r   r   r   r%   r   r   r   )r   rV   rU   r&   r   r   r   test_tpc_rollback_one_phase  s    
z3ConnectionTwoPhaseTests.test_tpc_rollback_one_phasec             C   sS  |  j    } | j d d d  } |  j | j t j  | j |  |  j | j t j  | j   } | j	 d  |  j d |  j
    |  j d |  j    | j   | j   |  j d |  j
    |  j d |  j    |  j    } | j d d d  } | j |  |  j | j t j  |  j d |  j
    |  j d |  j    d  S)Nr   r   r   z4insert into test_tpc values ('test_tpc_commit_rec');r   )rX   rU   r   r   r.   r   r   r   r   r%   r   r   r   r   r   )r   rV   rU   r&   r   r   r   test_tpc_rollback_recovered  s&    

z3ConnectionTwoPhaseTests.test_tpc_rollback_recoveredc             C   s   |  j    } |  j t j | j  | j   |  j t j | j  | j   } | j d  |  j t j | j  | j   |  j t j | j  d  S)Nzselect 1)	rX   r   r.   r   r   tpc_recoverr   r%   r   )r   rV   r&   r   r   r   test_status_after_recover  s    

z1ConnectionTwoPhaseTests.test_status_after_recoverc       
      C   sA  |  j    } | j d  | j   } | j d  | j d  | j d t f  | j   } | j   |  j    } | j   } d d   | D } | j d t d   |  j	 t
 |  t
 |   xu t | |  D]d \ } \ } } } }	 |  j	 | j |  |  j	 | j |  |  j	 | j |  |  j	 | j |	  q Wd  S)	Nr   z#begin; prepare transaction '1-foo';z#begin; prepare transaction '2-bar';zv
            select gid, prepared, owner, database
            from pg_prepared_xacts
            where database = %s;c             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   rU   r   r   r   rA     s   	 z?ConnectionTwoPhaseTests.test_recovered_xids.<locals>.<listcomp>keyr   )rX   r   r   r%   r   Zfetchallsortr   r   r   r:   zipr   preparedownerr   )
r   rV   r&   ZokvalsxidsrU   r   r  r  r   r   r   r   test_recovered_xids  s(    

(z+ConnectionTwoPhaseTests.test_recovered_xidsc             C   s   |  j    } | j d d d  } | j |  | j   |  j    } | j   } | j d t f  |  j d | j   d  d  S)NrR   r   r   z6select gid from pg_prepared_xacts where database = %s;z42_Z3RyaWQ=_YnF1YWw=r   )	rX   rU   r   r   r   r%   r   r   re   )r   rV   rU   r&   r   r   r   test_xid_encoding  s    
	
z)ConnectionTwoPhaseTests.test_xid_encodingc             C   s  x d d d d d d	 d f g D] \ } } } |  j    } | j | | |  } | j |  | j   | j   |  j    } d
 d   | j   D } |  j d t |   | d } |  j | j |  |  j | j	 |  |  j | j
 |  | j |  q! Wd  S)Nr   r   rR   r   r   ix@   yc             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   r  r   r   r   rA   "  s   	 z>ConnectionTwoPhaseTests.test_xid_roundtrip.<locals>.<listcomp>r   )r   r   r   )rR   zgtridzbqual)rX   rU   r   r   r   r   r   r:   	format_idr   r   r   )r   Zfidr   r   rV   rU   r  r   r   r   test_xid_roundtrip  s"    '


z*ConnectionTwoPhaseTests.test_xid_roundtripc             C   s   x d d d d g D] } |  j    } | j |  | j   | j   |  j    } d d   | j   D } |  j d t |   | d } |  j | j d   |  j | j |  |  j | j	 d   | j
 |  q Wd  S)	Nr   zhello, world!r     c             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   r  r   r   r   rA   8  s   	 zCConnectionTwoPhaseTests.test_unparsed_roundtrip.<locals>.<listcomp>r   r   )rX   r   r   r   r   r   r:   r	  r   r   r   )r   tidrV   r  rU   r   r   r   test_unparsed_roundtrip+  s     


z/ConnectionTwoPhaseTests.test_unparsed_roundtripc             C   s_   d d l  m } | d d d  } |  j d | j  |  j d | j  |  j d | j  d  S)Nr   )XidJ   rS   rT   )psycopg2.extensionsr  r   r	  r   r   )r   r  x1r   r   r   test_xid_constructionA  s
    z-ConnectionTwoPhaseTests.test_xid_constructionc             C   s   d d l  m } | j d  } |  j d | j  |  j d | j  |  j d | j  | j d  } |  j d  | j  |  j d | j  |  j d  | j  d  S)Nr   )r  z42_Z3RyaWQ=_YnF1YWw=rR   r   r   
99_xxx_yyy)r  r  from_stringr   r	  r   r   )r   r  x2Zx3r   r   r   test_xid_from_stringI  s    z,ConnectionTwoPhaseTests.test_xid_from_stringc             C   s^   d d l  m } | j d  } |  j t |  d  | j d  } |  j t |  d  d  S)Nr   )r  z42_Z3RyaWQ=_YnF1YWw=r  )r  r  r  r   r   )r   r  r  r  r   r   r   test_xid_to_stringV  s
    z*ConnectionTwoPhaseTests.test_xid_to_stringc             C   s   |  j    } | j d d d  } | j |  | j   | j   d d   | j   D d } |  j d | j  |  j d | j  |  j d | j	  d  S)Nr=   Zunicodec             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   r  r   r   r   rA   e  s   	 z<ConnectionTwoPhaseTests.test_xid_unicode.<locals>.<listcomp>r   )
rX   rU   r   r   r0   r   r   r	  r   r   )r   rV   r  rU   r   r   r   test_xid_unicode_  s    

z(ConnectionTwoPhaseTests.test_xid_unicodec             C   s   |  j    } | j d  | j d  | j   | j   d d   | j   D d } |  j d  | j  |  j d | j  |  j d  | j	  d  S)Nr   ztransaction-idc             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   r  r   r   r   rA   t  s   	 zEConnectionTwoPhaseTests.test_xid_unicode_unparsed.<locals>.<listcomp>r   )
rX   rc   r   r   r0   r   r   r	  r   r   )r   rV   rU   r   r   r   test_xid_unicode_unparsedj  s    

z1ConnectionTwoPhaseTests.test_xid_unicode_unparsedc             C   s=   |  j    } | j d  | j   |  j t j | j  d  S)Ncancel)rX   r   r   r#   r$   r   r  )r   rV   r   r   r   test_cancel_fails_preparedy  s    
z2ConnectionTwoPhaseTests.test_cancel_fails_preparedc             C   s   d d l  m } |  j d |  } | j d  | j   | j   | j   } d d   | D d } |  j d  | j  |  j d | j	  |  j d  | j
  d  S)Nr   )RealDictConnectionconnection_factoryzdict-connectionc             S   s%   g  |  ] } | j  t k r |  q Sr   )r   r   )r?   r  r   r   r   rA     s   	 zQConnectionTwoPhaseTests.test_tpc_recover_non_dbapi_connection.<locals>.<listcomp>)rx   r  rX   r   r   r0   r   r   r	  r   r   )r   r  rV   r  rU   r   r   r   %test_tpc_recover_non_dbapi_connection  s    

z=ConnectionTwoPhaseTests.test_tpc_recover_non_dbapi_connectionN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r
  r  r  r  r  r  r  r  r  r   r   r   r   r     s0   		r   c               @   s   e  Z 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 d d   Z d S)TransactionControlTestsc             C   s0   |  j  j   |  j t j |  j  j t j  d  S)N)r   r   r#   r$   r   set_sessionr.   r/   )r   r   r   r   test_closed  s    	z#TransactionControlTests.test_closedc             C   s?   |  j  j   } | j d  |  j t j |  j  j t j  d  S)Nzselect 1)	r   r   r%   r#   r$   r   r!  r.   r/   )r   r&   r   r   r   test_not_in_transaction  s
    	z/TransactionControlTests.test_not_in_transactionc             C   s  |  j  j   } |  j  j t j  | j d  |  j | j   d d  |  j  j   |  j  j t j	  | j d  |  j  j
 d k r |  j | j   d d  n |  j | j   d d  |  j  j   |  j  j d t j  | j d  |  j | j   d d  |  j  j   |  j  j d t j  | j d  |  j  j
 d k rh|  j | j   d d  n |  j | j   d d  |  j  j   d  S)	NzSHOW transaction_isolation;r   r(   i8 zrepeatable readr*   zread committedzread uncommitted)r   r   r!  r.   r/   r%   r   re   r   r   r,   r   r   )r   r&   r   r   r   r     s2    	
	


z0TransactionControlTests.test_set_isolation_levelc             C   s  |  j  j   } |  j  j d  | j d  |  j | j   d d  |  j  j   |  j  j d  | j d  |  j  j d k r |  j | j   d d  n |  j | j   d d  |  j  j   |  j  j d  | j d  |  j | j   d d  |  j  j   |  j  j d  | j d  |  j  j d k rV|  j | j   d d  n |  j | j   d d  |  j  j   d  S)Nr(   zSHOW transaction_isolation;r   zrepeatable readi8 zread committedzread uncommitted)r   r   r!  r%   r   re   r   r,   )r   r&   r   r   r   test_set_isolation_level_str  s*    z4TransactionControlTests.test_set_isolation_level_strc             C   sO   |  j  t |  j j d  |  j  t |  j j d  |  j  t |  j j d  d  S)Nr   r   Zwhatever)r#   r   r   r!  )r   r   r   r   test_bad_isolation_level  s    z0TransactionControlTests.test_bad_isolation_levelc             C   s   |  j  |  j j d  k  |  j j   } |  j j d d  |  j  |  j j d k  | j d  |  j | j   d d  |  j j   | j d  |  j | j   d d  |  j j   |  j j d d  |  j  |  j j d k  | j d  |  j | j   d d  |  j j   d  S)Nr+   TzSHOW transaction_read_only;r   onFoff)	r   r   r+   r   r!  r%   r   re   r   )r   r&   r   r   r   test_set_read_only  s    z*TransactionControlTests.test_set_read_onlyc             C   s  |  j  j   } d |  j  _ |  j |  j  j d k  | j d  |  j | j   d d  |  j |  j  j t	 |  j  d d  |  j |  j  j d k  |  j  j
   | j d  |  j | j   d d  |  j  j
   |  j  j   } d  |  j  _ |  j |  j  j d  k  | j d  |  j | j   d d  |  j  j
   d |  j  _ |  j |  j  j d k  | j d  |  j | j   d d  |  j  j
   d  S)NTzSHOW transaction_read_only;r   r&  r+   Fr'  )r   r   r+   r   r%   r   re   r#   r   r   r   )r   r&   r   r   r   test_setattr_read_only  s.    z.TransactionControlTests.test_setattr_read_onlyc             C   s   |  j  j   } | j d  | j   d } | j d  | j   d } |  j  j   |  j  j d d d d  |  j  j d d d d  | j d  |  j | j   d |  | j d  |  j | j   d |  d  S)	NzSHOW transaction_isolation;r   zSHOW transaction_read_only;r*   r(   r+   Tr   )r   r   r%   re   r   r!  r   )r   r&   Zisolevelr+   r   r   r   test_set_default  s    z(TransactionControlTests.test_set_defaultr   r   c             C   st  |  j  |  j j d  k  |  j j   } |  j j d d d d  |  j  |  j j d k  | j d  |  j | j   d d  | j d  |  j | j   d d  |  j j   | j d  |  j | j   d d  |  j j   |  j j d d  |  j  |  j j d k  | j d  |  j | j   d d  | j d  |  j | j   d d	  |  j j   d  S)
Nr+   Tr-   zSHOW transaction_read_only;r   r&  zSHOW transaction_deferrable;Fr'  )	r   r   r-   r   r!  r%   r   re   r   )r   r&   r   r   r   test_set_deferrable  s&    z+TransactionControlTests.test_set_deferrablec             C   sH   |  j  t j |  j j d d d d |  j  t j t |  j d d  d  S)Nr+   Tr-   )r#   r$   r   r   r!  r   )r   r   r   r   test_set_deferrable_error4  s    z1TransactionControlTests.test_set_deferrable_errorc             C   s  |  j  j   } d |  j  _ |  j |  j  j d k  | j d  |  j | j   d d  |  j |  j  j t	 |  j  d d  |  j |  j  j d k  |  j  j
   | j d  |  j | j   d d  |  j  j
   |  j  j   } d  |  j  _ |  j |  j  j d  k  | j d  |  j | j   d d  |  j  j
   d |  j  _ |  j |  j  j d k  | j d  |  j | j   d d  |  j  j
   d  S)NTzSHOW transaction_deferrable;r   r&  r-   Fr'  )r   r   r-   r   r%   r   re   r#   r   r   r   )r   r&   r   r   r   test_setattr_deferrable;  s.    z/TransactionControlTests.test_setattr_deferrablec             C   s   |  j  j   } d |  j  _ d |  j  _ | j d  |  j | j   d d  | j d  |  j | j   d d  d |  j  _ | j d  |  j | j   d d  | j d  |  j | j   d d  d  S)NTzSHOW transaction_read_only;r   r&  z#SHOW default_transaction_read_only;Fr'  )r   r   r)   r+   r%   r   re   )r   r&   r   r   r   test_mixing_session_attribsW  s    z3TransactionControlTests.test_mixing_session_attribsN)r   r   r   r"  r#  r   r$  r%  r(  r)  r*  r	   r+  r
   r,  r-  r.  r   r   r   r   r     s    r   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)AutocommitTestsc             C   se   |  j  j   |  j t j t |  j  d d  y |  j |  j  j d k  Wn t j k
 r` Yn Xd  S)Nr)   TF)TF)r   r   r#   r$   r   r   r   r)   )r   r   r   r   r"  k  s    zAutocommitTests.test_closedc             C   s   |  j  |  j j  |  j |  j j t j  |  j |  j j   t j  |  j j	   } | j
 d  |  j |  j j t j  |  j |  j j   t j  |  j j   |  j |  j j t j  |  j |  j j   t j  d  S)Nz	select 1;)r   r   r)   r   r   r.   r   r   r   r   r%   r   r   r   )r   r&   r   r   r   test_default_no_autocommitx  s    

z*AutocommitTests.test_default_no_autocommitc             C   s@  d |  j  _ |  j |  j  j  |  j |  j  j t j  |  j |  j  j   t j  |  j  j	   } | j
 d  |  j |  j  j t j  |  j |  j  j   t j  d |  j  _ |  j |  j  j  |  j |  j  j t j  |  j |  j  j   t j  | j
 d  |  j |  j  j t j  |  j |  j  j   t j  d  S)NTz	select 1;F)r   r)   r   r   r   r.   r   r   r   r   r%   r   r   )r   r&   r   r   r   test_set_autocommit  s&    


z#AutocommitTests.test_set_autocommitc             C   s?   |  j  j   } | j d  |  j t j t |  j  d d  d  S)Nz	select 1;r)   T)r   r   r%   r#   r$   r   r   )r   r&   r   r   r   test_set_intrans_error  s    z&AutocommitTests.test_set_intrans_errorc             C   s  |  j  j d d  |  j |  j  j  |  j |  j  j t j  |  j |  j  j   t j	  |  j  j
   } | j d  |  j |  j  j t j  |  j |  j  j   t j	  |  j  j d d  |  j |  j  j  |  j |  j  j t j  |  j |  j  j   t j	  | j d  |  j |  j  j t j  |  j |  j  j   t j  |  j  j   |  j  j d d d d d |  j |  j  j  | j d  |  j |  j  j t j  |  j |  j  j   t j	  | j d  |  j | j   d d  | j d	  |  j | j   d d
  d  S)Nr)   Tz	select 1;Fr(   r+   zSHOW transaction_isolation;r   zSHOW transaction_read_only;r&  )r   r!  r   r)   r   r   r.   r   r   r   r   r%   r   r   r   re   )r   r&   r   r   r   test_set_session_autocommit  s<    




z+AutocommitTests.test_set_session_autocommitN)r   r   r   r"  r0  r1  r2  r3  r   r   r   r   r/  j  s
   r/  c                   sb   e  Z d  Z   f d d   Z Gd d   d e j  Z d d   Z e d d  d	 d
    Z	   S)PasswordLeakTestCasec                s    t  t |   j   d  t _ d  S)N)r   r4  r   r   )r   )r   r   r   r     s    zPasswordLeakTestCase.setUpc                   s(   e  Z d  Z d Z   f d d   Z   S)z'PasswordLeakTestCase.GrassingConnectionzA connection snitching the dsn away.

        This connection passes the dsn to the test case class even if init
        fails (e.g. connection error). Test that we mangle the dsn ok anyway.
        c                s4   z  t  t j |   j | |   Wd  |  j t _ Xd  S)N)r   r4  GrassingConnectionr   r   )r   argskwargs)r   r   r   r     s    z0PasswordLeakTestCase.GrassingConnection.__init__)r   r   r   __doc__r   r   r   )r   r   r5    s   r5  c             C   s0   |  j  t j |  j d  |  j |  j d  d  S)Nzdbname=nosuch password=whatevazdbname=nosuch password=xxx)r#   r$   DatabaseErrorr5  r   r   )r   r   r   r   	test_leak  s    zPasswordLeakTestCase.test_leakr   r"   c             C   s0   |  j  t j |  j d  |  j |  j d  d  S)Nz+postgres://someone:whateva@localhost/nosuchz6user=someone password=xxx host=localhost dbname=nosuch)r#   r$   r9  r5  r   r   )r   r   r   r   test_url_leak  s
    z"PasswordLeakTestCase.test_url_leak)
r   r   r   r   r.   r   r5  r:  r   r;  r   r   )r   r   r4    s   r4  c               @   sO   e  Z d  Z e e d d  d d     Z e d d    Z d d   Z d	 S)
SignalTestCaser    r"   c             C   s   |  j  d d  d  S)NqueryzL
            INSERT INTO test551 (num) VALUES (%s) RETURNING id
            )_test_bug_551)r   r   r   r   test_bug_551_returning  s    	z%SignalTestCase.test_bug_551_returningc             C   s   |  j  d d  d  S)Nr=  z?
            INSERT INTO test551 (num) VALUES (%s)
            )r>  )r   r   r   r   test_bug_551_no_returning  s    	z(SignalTestCase.test_bug_551_no_returningc             C   s   d i t  d 6| d 6} t j t j d t |  g d t j d t j } | j   \ } } |  j | j	 d  t
 j d d	 |  j   } |  j | |  d  S)
Na  import os
import sys
import time
import signal
import threading

import psycopg2

def handle_sigabort(sig, frame):
    sys.exit(1)

def killer():
    time.sleep(0.5)
    os.kill(os.getpid(), signal.SIGABRT)

signal.signal(signal.SIGABRT, handle_sigabort)

conn = psycopg2.connect(%(dsn)r)

cur = conn.cursor()

cur.execute("create table test551 (id serial, num varchar(50))")

t = threading.Thread(target=killer)
t.daemon = True
t.start()

while True:
    cur.execute(%(query)r, ("Hello, world!",))
r   r=  z-cstdoutstderrr   s
   \[[^\]]+\]    )r   spPopenr   
executabler   PIPEcommunicateassertNotEqual
returncoderesubstripr   )r   r=  scriptprocouterrr   r   r   r>    s    zSignalTestCase._test_bug_551N)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    rR  __main__)(rK  rg   r   r^   r\   
subprocessrD  operatorr   r$   Zpsycopg2.errorcodesr   r.   Z	testutilsr   r   r   r   r	   r
   r   r   r   r   r   Z
testconfigr   r   r   r   r   r   r   r   r/  r4  r<  rR  r   mainr   r   r   r   <module>   s6   L %DN 8 tb%: