
yR\c;                 @   s   d  d l  m Z m Z m Z d d l Z d d l m Z d d l Z d d l Z d  d l  m	 Z	 Gd d   d e
  Z Gd d	   d	 e	  Z d
 d   Z e d k r e j   n  d S)   )unittestskip_before_postgresslow    N)
extensions)ConnectingTestCasec               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	PollableStubz=A 'pollable' wrapper allowing analysis of the `poll()` calls.c             C   s   | |  _  g  |  _ d  S)N)pollablepolls)selfr	    r   N/var/www/dbchiro/venv/lib/python3.4/site-packages/psycopg2/tests/test_async.py__init__'   s    	zPollableStub.__init__c             C   s   |  j  j   S)N)r	   fileno)r   r   r   r   r   +   s    zPollableStub.filenoc             C   s#   |  j  j   } |  j j |  | S)N)r	   pollr
   append)r   rvr   r   r   r   .   s    zPollableStub.pollN)__name__
__module____qualname____doc__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 e e d	 d
  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 d' d(   Z e d) d*    Z d+ d,   Z d- d.   Z d/ d0   Z d1 d2   Z d3 d4   Z d5 d6   Z d7 d8   Z d9 d:   Z  d; S)<
AsyncTestsc             C   sk   t  j |   |  j |  _ |  j d d  |  _ |  j |  j  |  j j   } | j d  |  j |  d  S)Nasync_Tz[
            CREATE TEMPORARY TABLE table1 (
              id int PRIMARY KEY
            ))r   setUpconn	sync_connconnectwaitcursorexecute)r   cursr   r   r   r   6   s    zAsyncTests.setUpc             C   s   |  j  j   } |  j j   } ~ ~ |  j |  j  j  |  j |  j j  |  j |  j  j  |  j |  j  j t j	  |  j |  j  j
  |  j |  j  j d k  |  j |  j  j t j k  d  S)N      )r!   r"   )r   r   r   
assertTruer   Z
autocommitassertEqualZisolation_levelextZISOLATION_LEVEL_DEFAULTserver_versionprotocol_versionencoding	encodings)r   cursync_curr   r   r   test_connection_setupE   s    z AsyncTests.test_connection_setupc             C   s    |  j  t j |  j j d  d  S)Nname)assertRaisespsycopg2ProgrammingErrorr   r   )r   r   r   r   test_async_named_cursorV   s    z"AsyncTests.test_async_named_cursorc             C   s   |  j  j   } |  j |  j  j    | j d  |  j |  j  j    |  j |  |  j |  j  j    |  j | j   d d  d  S)Nz
select 'a'r   a)	r   r   assertFalseisexecutingr   r#   r   r$   fetchone)r   r*   r   r   r   test_async_selectZ   s    zAsyncTests.test_async_select   r!   c             C   sz   |  j  j   } | j d d  |  j |  j  j    |  j |  |  j |  j  j    |  j | j   d d d  d  S)NZpg_sleep皙?r    )r8   )	r   r   callprocr#   r4   r   r3   r$   fetchall)r   r*   r   r   r   test_async_callproce   s    zAsyncTests.test_async_callprocc             C   s   |  j  j   } |  j  j   } ~ | j d  |  j t j | j d  |  j t j | j d  |  j |  | j d  |  j |  |  j | j	   d d d  | j d  |  j |  | j d  |  j |  |  j | j
   d   d  S)Nzinsert into table1 values (1)zselect * from table1versionr   r   zdelete from table1)r   r   r   r.   r/   r0   r:   r   r$   r;   r5   )r   r*   cur2r   r   r   test_async_after_asyncp   s"    z!AsyncTests.test_async_after_asyncc             C   sa   |  j  j   } | j d  |  j t j | j  |  j |  |  j | j   d d d  d  S)Nz
select 'a'r   r2   )	r   r   r   r.   r/   r0   r;   r   r$   )r   r*   r   r   r   test_fetch_after_async   s    
z!AsyncTests.test_fetch_after_asyncc             C   s9   |  j  j   } | j d  |  j t j |  j  j  d  S)Nz
select 'a')r   r   r   r.   r/   r0   Zrollback)r   r*   r   r   r   test_rollback_while_async   s    z$AsyncTests.test_rollback_while_asyncc             C   s  |  j  j   } | j d  |  j |  | j d  |  j t j |  j  j  |  j |  j  j	    |  j |  | j d  |  j |  | j d  |  j |  |  j
 | j   d d d  | j d  |  j |  | j d  |  j |  |  j
 | j   d   d  S)Nbeginzinsert into table1 values (1)commitzselect * from table1r   r   zdelete from table1)r   r   r   r   r.   r/   r0   rC   r#   r4   r$   r;   r5   )r   r*   r   r   r   test_commit_while_async   s"    z"AsyncTests.test_commit_while_asyncc             C   s   |  j  j   } | j d  |  j |  j  j    |  j |  j  j   t j  |  j |  j  j    |  j	 t
 j |  j  j d  |  j	 t
 j |  j  j d  d  S)Nz
select 'c'ZLATIN1r   )r   r   r   r#   r4   r$   Zget_transaction_statusr%   ZTRANSACTION_STATUS_ACTIVEr.   r/   r0   Zset_client_encodingZset_isolation_level)r   r*   r   r   r   test_set_parameters_while_async   s    
z*AsyncTests.test_set_parameters_while_asyncc             C   sO   |  j  j   } | j d  |  j |  j  j    |  j t j |  j  j  d  S)Nz
select 'c')	r   r   r   r#   r4   r.   r/   r0   reset)r   r*   r   r   r   test_reset_while_async   s    z!AsyncTests.test_reset_while_asyncc             C   s   |  j  j   } | j d  |  j |  | j d  |  j |  | j d  |  j t j t |  |  j |  |  j t |  d d d	 g  |  j	 |  j  j
    d  S)
NrB   z
            insert into table1 values (1);
            insert into table1 values (2);
            insert into table1 values (3);
        z!select id from table1 order by idr   r!   r"   )r   )r!   )r"   )r   r   r   r   r.   r/   r0   listr$   r3   r4   )r   r*   r   r   r   test_async_iter   s    zAsyncTests.test_async_iterc             C   sE   |  j  j   } | j d  |  j t j | j t j d  d  d  S)Nz
select 'a'z	1
3
5
\.
Ztable1)	r   r   r   r.   r/   r0   Z	copy_fromioStringIO)r   r*   r   r   r   test_copy_while_async   s
    z AsyncTests.test_copy_while_asyncc             C   s   |  j  t j |  j j  d  S)N)r.   r/   r0   r   Zlobject)r   r   r   r   test_lobject_while_async   s    z#AsyncTests.test_lobject_while_asyncc             C   s8   |  j  j   } |  j t j | j d d d d g  d  S)Nzinsert into table1 values (%s)r   r!   r"   )r   r   r.   r/   r0   Zexecutemany)r   r*   r   r   r   test_async_executemany   s    z!AsyncTests.test_async_executemanyc             C   sh  |  j  j   } | j d  |  j |  | j d  |  j t j | j d  |  j |  j  j	    |  j |  | j d  |  j
 | j   d d g  |  j  j   } | j d  |  j |  |  j  j   } |  j t j | j d  |  j t j | j d  |  j  j   } | j d  |  j |  | j d  | j d	  |  j
 | j   d
 d g  d  S)Nz
            insert into table1 values (1);
            insert into table1 values (2);
            insert into table1 values (3);
        z!select id from table1 order by idr   r!   r"      )r!   )r"   )r!   )r"   )r   r   r   r   r.   r/   r0   scrollr#   r4   r$   r;   )r   r*   r>   r   r   r   test_async_scroll   s,    zAsyncTests.test_async_scrollc             C   sp   |  j  j   } | j d  | j d  | j d  | j d  | j d  |  j | j   d d	 g  d  S)
Nzcreate table table1 (id int)z
            insert into table1 values (1);
            insert into table1 values (2);
            insert into table1 values (3);
        z!select id from table1 order by idr!   r   r"   rP   )r!   )r"   )r   r   r   rQ   r$   r;   )r   r*   r   r   r   test_scroll#  s    zAsyncTests.test_scrollc             C   sK   |  j  j   } | j d  |  j |  |  j | j   d d d  d  S)Nz4select repeat('a', 10000); select repeat('b', 10000)r   bi'  )r   r   r   r   r$   r5   )r   r*   r   r   r   test_async_dont_read_all0  s    z#AsyncTests.test_async_dont_read_allc             C   se   Gd d   d t  j  } |  j d | d d  } |  j t | |   |  j | j  | j   d  S)Nc               @   s   e  Z d  Z d d d  Z d S)z.AsyncTests.test_async_subclass.<locals>.MyConnr   c             S   s   t  j j |  | d | d  S)Nr   )r%   
connectionr   )r   Zdsnr   r   r   r   r   <  s    z7AsyncTests.test_async_subclass.<locals>.MyConn.__init__N)r   r   r   r   r   r   r   r   MyConn;  s   rW   connection_factoryr   T)r%   rV   r   r#   
isinstancer   close)r   rW   r   r   r   r   test_async_subclass:  s
    zAsyncTests.test_async_subclassc             C   s   |  j  j   } x d D] } | d d } t |  j   } | j d d | f  |  j |  |  j | t | j   d	   | j j	 t
 j  d k r d  Sq Wd	 d  l } | j d
  d  S)Nr      
      2   i   z
select %s;xr   z4sending a large query didn't trigger block on write.)r   r\   r]   r^   r_   )r   r   r   r   r   r$   lenr5   r
   countr%   Z
POLL_WRITEwarningswarn)r   r    ZmbsizeZstubrc   r   r   r   test_flush_on_writeD  s     zAsyncTests.test_flush_on_writec             C   sG   |  j  j   } | j d  | j j   |  j | j   d d  d  S)Nzselect 1r   r   )r   r   r   rV   r   r$   r5   )r   r*   r   r   r   test_sync_pollX  s    zAsyncTests.test_sync_pollc             C   s  |  j  j   } |  j j   } | j d  |  j j   | j d  |  j |  |  j |  j j g   |  j  j   } x t	 d  D]t } |  j |  j  |  j j s t
 j d  q n  |  j t |  j j  d  |  j |  j j j   | d f  d  SW|  j d  d  S)Nzlisten test_notifyznotify test_notifyr\   g      ?r   test_notifyzNo NOTIFY in 2.5 seconds)r   r   r   r   rC   r   r$   ZnotifiesZget_backend_pidrangetimesleepra   popfail)r   r*   r+   pid_r   r   r   rh   _  s$    zAsyncTests.test_notifyc             C   s   |  j  j   } |  j  j   } | j d  |  j |  |  j |  j  j    |  j t j | j	  |  j
 | j	   d d  d  S)Nzselect 1r   r   )r   r   r   r   r3   r4   r.   r/   r0   r5   r$   )r   Zcur1r>   r   r   r   test_async_fetch_wrong_cursorv  s    z(AsyncTests.test_async_fetch_wrong_cursorc             C   s  |  j  j   } | j d d  |  j |  | j d d	  |  j t j |  j |  | j d d
  |  j t j |  j |  | j d d  |  j |  | j d d  |  j |  | j d  |  j |  |  j | j   d d d g  | j d  |  j |  d  S)Nzinsert into table1 values (%s)r   z>insert into table1 values (%s); insert into table1 values (%s)r!   r"   z select * from table1 order by idzdelete from table1)r   )r   )r!   r!   )r!   )r"   )r   )r!   )r"   )	r   r   r   r   r.   r/   ZIntegrityErrorr$   r;   )r   r*   r   r   r   
test_error  s"    	zAsyncTests.test_errorc             C   s|   |  j  j   } |  j  j   } | j d  |  j t j |  j |  | j d  |  j |  |  j | j   d d  d  S)Nzselect * from no_such_tablezselect 1r   r   )	r   r   r   r.   r/   r0   r   r$   r5   )r   r*   r>   r   r   r   test_error_two_cursors  s    z!AsyncTests.test_error_two_cursorsc             C   s   |  j  j d  d   =|  j  j   } |  j  j d k rQ | j d  |  j |  n  | j d  |  j |  |  j d | j  |  j |  j  j  d  S)Ni` zset client_min_messages=debug1z1create temp table chatty (id serial primary key);zCREATE TABLE)	r   Znoticesr   r&   r   r   r$   Zstatusmessager#   )r   r*   r   r   r   test_notices  s    zAsyncTests.test_noticesc             C   s   d d  l  } |  j j   } | j d  ~ | j   |  j t j |  j |  j  |  j j   } | j d  |  j |  j  |  j	 | j
   d  d  S)Nr   z
select 42;*   )rt   )gcr   r   r   Zcollectr.   r/   ZInterfaceErrorr   r$   r5   )r   ru   r*   r   r   r   test_async_cursor_gone  s    
z!AsyncTests.test_async_cursor_gonec             C   s{   y& t  j d d d } |  j |  WnA t  j k
 ri } z |  j t |  d d  WYd  d  } ~ Xn X|  j d  d  S)Nzdbname=thisdatabasedoesntexistr   Tzasynchronous connection failedzconnection error reason lostzno exception raised)r/   r   r   ErrorassertNotEqualstrrm   )r   Zcnner   r   r   #test_async_connection_error_message  s    z.AsyncTests.test_async_connection_error_messageN)!r   r   r   r   r,   r1   r6   r   r   r<   r?   r@   rA   rD   rE   rG   rI   rL   rM   rN   rR   rS   rU   r[   rf   rg   rh   rp   rq   rr   rs   rv   r{   r   r   r   r   r   4   s:   
	#

	r   c               C   s   t  j   j t  S)N)r   Z
TestLoaderZloadTestsFromNamer   r   r   r   r   
test_suite  s    r|   __main__)Z	testutilsr   r   r   r/   r   r%   rj   rJ   r   objectr   r   r|   r   mainr   r   r   r   <module>   s    