
5dV.                 @   sb  d  d l  Z  d  d l Z d d l m Z m Z m Z d d l m Z m Z d  d l m	 Z	 d  d l
 m Z d  d l m Z m Z d  d l Z d  d l Z d d l m Z m Z d d	 l m Z e  j d  d
 k  r e Z n d  d l m Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z e e e  d d   Z e d k r^e j   n  d S)    N   )unittestConnectingTestCasedecorate_all_tests)skip_if_no_iobaseskip_before_postgres)StringIO)cycle)PopenPIPE)skip_copy_if_greenscript_to_py3)dsn   )
TextIOBasec               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	MinimalReadz;A file wrapper exposing the minimal interface to copy from.c             C   s   | |  _  d  S)N)f)selfr    r   </tmp/pip-build-0jahl3lb/psycopg2/psycopg2/tests/test_copy.py__init__.   s    zMinimalRead.__init__c             C   s   |  j  j |  S)N)r   read)r   sizer   r   r   r   1   s    zMinimalRead.readc             C   s   |  j  j   S)N)r   readline)r   r   r   r   r   4   s    zMinimalRead.readlineN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   ,   s   r   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)MinimalWritez9A file wrapper exposing the minimal interface to copy to.c             C   s   | |  _  d  S)N)r   )r   r   r   r   r   r   9   s    zMinimalWrite.__init__c             C   s   |  j  j |  S)N)r   write)r   datar   r   r   r   <   s    zMinimalWrite.writeN)r   r   r   r   r   r   r   r   r   r   r   7   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	 e
 d d    Z e
 d d    Z e
 d d    Z d d   Z d d   Z d d   Z d d   Z e d d  d d     Z d! d"   Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ S),	CopyTestsc             C   s   t  j |   |  j   d  S)N)r   setUp_create_temp_table)r   r   r   r   r"   B   s    zCopyTests.setUpc             C   s    |  j  j   } | j d  d  S)Nzv
            CREATE TEMPORARY TABLE tcopy (
              id serial PRIMARY KEY,
              data text
            ))conncursorexecute)r   cursr   r   r   r#   F   s    zCopyTests._create_temp_tablec             C   sD   |  j  j   } z# |  j | d d d d d i  Wd  | j   Xd  S)Nnrecsi   srec
   copykwi (  )r$   r%   
_copy_fromclose)r   r'   r   r   r   test_copy_fromN   s    #zCopyTests.test_copy_fromc             C   sK   |  j  j   } z* |  j | d d d d	 d i d d 6Wd  | j   Xd  S)Nr(   r*   i   r)   r+      r   i (  i (  i P  i  @)r$   r%   r,   r-   )r   r'   r   r   r   test_copy_from_insane_sizeU   s
    z$CopyTests.test_copy_from_insane_sizec             C   s   |  j  j   } t   } x( t d  D] } | j d | f  q% W| j d  | j t |  d d d g | j d  |  j	 d d	   t d  D | j
    d  S)
Nr*   z%s
r   tcopycolumnsidzselect * from tcopy order by idc             S   s   g  |  ] } | d  f  q S)Nr   ).0ir   r   r   
<listcomp>h   s   	 z1CopyTests.test_copy_from_cols.<locals>.<listcomp>)r$   r%   r   ranger   seek	copy_fromr   r&   assertEqualZfetchall)r   r'   r   r5   r   r   r   test_copy_from_cols^   s    	zCopyTests.test_copy_from_colsc             C   s   |  j  j   } t   } x( t d  D] } | j d | f  q% W| j d  d d   } |  j t | j t	 |  d d |   d  S)Nr*   z%s
r   c               s   s   t     d Vd  S)Nr3   )ZeroDivisionErrorr   r   r   r   colsq   s    	z/CopyTests.test_copy_from_cols_err.<locals>.colsr1   r2   )
r$   r%   r   r7   r   r8   assertRaisesr<   r9   r   )r   r'   r   r5   r=   r   r   r   test_copy_from_cols_errj   s    		z!CopyTests.test_copy_from_cols_errc             C   sW   |  j  j   } z6 |  j | d d d d d i  |  j | d d Wd  | j   Xd  S)Nr(   i   r)   r*   r+   i (  i (  )r$   r%   r,   _copy_tor-   )r   r'   r   r   r   test_copy_tox   s
    zCopyTests.test_copy_toc             C   sN  |  j  j d  |  j   t j d d k  r d j t t t t	 d d   t t	 d d     } | j
 d  j d	 d
  } nI t t t	 d d   t t	 d d    j
 d  } | j d	 d
  } |  j  j   } | j d d | f  d d  l } | j   } | j | d d d | j d  |  j | j   j   |  d  S)Nlatin1r   r                 \z\\z!insert into tcopy values (%s, %s)*   r1   r2   r    )zdata)r$   set_client_encodingr#   sysversion_infojoinmapchrlistr7   decodereplacebytesr%   r&   ior   copy_tor8   r:   r   rstrip)r   abinaboutr'   rT   r   r   r   r   test_copy_text   s    
:7	zCopyTests.test_copy_textc             C   sN  |  j  j d  |  j   t j d d k  r| d j t t t t	 d d   t t	 d d     } | j
 d	 d
  } nR t t t	 d d   t t	 d d    j d  } | j
 d	 d
  j d  } |  j  j   } | j d d | f  d d  l } | j   } | j | d d d | j d  |  j | j   j   |  d  S)NrB   r   r   rC   rD   rE   rF      rH   z\\z!insert into tcopy values (%s, %s)rI   r1   r2   r    )zdata)r$   rJ   r#   rK   rL   rM   rN   rO   rP   r7   rR   rS   rQ   encoder%   r&   rT   BytesIOrU   r8   r:   r   rV   )r   rW   rX   r'   rT   r   r   r   r   test_copy_bytes   s    
:7	zCopyTests.test_copy_bytesc                sD   j  j d   j   t j d d k  r d j t t t t	 d d   t t	 d d     } | j
 d  } | j d	 d
  } nI t t t	 d d   t t	 d d    j
 d  } | j d	 d
  } d d  l } | j   } | j |  | j d   j  j   } t j j t j j |  | j d |  | j d   j | j   d |  | j   } | j d |  | j d   j | j   j   |  | j   } | j |  | j d  d } | j   | |    f d d  } | | _ | j d | d | | j d   j | j   d |  d  S)NrB   r   r   rC   rD   rE   rF   rG   rH   z\\zCOPY tcopy (data) FROM STDINzselect data from tcopy;zCOPY tcopy (data) TO STDOUT{   c                s    j  |  |    |   S)N)r:   )_sizer   exp_size)	real_readr   r   r   r      s    z3CopyTests.test_copy_expert_textiobase.<locals>.readr   )r$   rJ   r#   rK   rL   rM   rN   rO   rP   r7   rQ   rR   rS   rT   r   r   r8   r%   psycopg2
extensionsZregister_typeUNICODEcopy_expertr&   r:   fetchoner   rV   r   )r   rW   rX   rT   r   r'   r`   r   r   )ra   r   r   test_copy_expert_textiobase   s@    
:7			z%CopyTests.test_copy_expert_textiobasec       	      C   s  t    } xM t t |  t t j   D]- \ } } | | } | j d | | f  q( W| j d  | j t	 |  d |  | j
 d  |  j | | j   d  | j
 d t t j  f  x8 t |  D]* \ } \ } |  j | t j | |  q Wd  S)Nz%s	%s
r   r1   zselect count(*) from tcopyz0select data from tcopy where id < %s order by id)r   zipr7   r	   stringascii_lettersr   r8   r9   r   r&   r:   rf   len	enumerate)	r   r'   r(   r)   r+   r   r5   clr   r   r   r,      s    	+
	zCopyTests._copy_fromc             C   s   t    } | j t |  d  | j d  d } xi | D]a } | j   \ } } t |  t t j  k  r9 |  j	 | t j t |  |  | d 7} q9 q9 W|  j	 | t t j   d  S)Nr1   r   r   )
r   rU   r   r8   splitintrk   ri   rj   r:   )r   r'   r)   r   Zntestslinensr   r   r   r@      s    	!zCopyTests._copy_toc             C   sK   Gd d   d t   } |   } |  j j   } |  j t | j d |  d  S)Nc               @   s   e  Z d  Z d S)z:CopyTests.test_copy_expert_file_refcount.<locals>.WhateverN)r   r   r   r   r   r   r   Whatever   s   rt   zCOPY tcopy (data) FROM STDIN)objectr$   r%   r>   	TypeErrorre   )r   rt   r   r'   r   r   r   test_copy_expert_file_refcount   s
    		z(CopyTests.test_copy_expert_file_refcountc             C   s   d d   t  d  D } |  j j   } | j d d j d d   | D   | j d  t   } | j | d d	 | | j d
  |  j | j	   j
   d g t |   | j d
  | j | d d	 | | j d  |  j | j   d
 d  d  S)Nc             S   s   g  |  ] } d  |  q S)zc%050dr   )r4   r5   r   r   r   r6     s   	 z7CopyTests.test_copy_no_column_limit.<locals>.<listcomp>   z$CREATE TEMPORARY TABLE manycols (%s)z,
c             S   s   g  |  ] } d  |  q S)z%s intr   )r4   rm   r   r   r   r6   	  s   	 z#INSERT INTO manycols DEFAULT VALUESZmanycolsr2   r   z\Nzselect count(*) from manycols;   )r7   r$   r%   r&   rM   r   rU   r8   r:   r   ro   rk   r9   rf   )r   r=   r'   r   r   r   r   test_copy_no_column_limit  s    	)z#CopyTests.test_copy_no_column_limit   ry   c             C   s   |  j  j   } | j t d  d d d g |  j | j d  | j d t d   |  j | j d  | j t   d  |  j | j d	  | j d
  | j d t    |  j | j d  d  S)Nzaaa
bbb
ccc
r1   r2   r    r   zcopy tcopy (data) from stdinzddd
eee
ry      z'insert into tcopy (data) values ('fff')zcopy tcopy to stdout   )	r$   r%   r9   r   r:   rowcountre   rU   r&   )r   r'   r   r   r   test_copy_rowcount  s    zCopyTests.test_copy_rowcountc             C   sh   |  j  j   } | j d  |  j | j d  |  j t j | j t	 d  d  |  j | j d  d  S)Nz'insert into tcopy (data) values ('fff')r   zaaa
bbb
ccc
r1   )
r$   r%   r&   r:   r~   r>   rb   Z	DataErrorr9   r   )r   r'   r   r   r   test_copy_rowcount_error)  s    z"CopyTests.test_copy_rowcount_errorc             C   sP   d i t  d 6} t t j d t |  g  } | j   |  j d | j  d  S)Nzimport psycopg2
conn = psycopg2.connect(%(dsn)r)
curs = conn.cursor()
curs.execute("create table copy_segf (id int)")
try:
    curs.execute("copy copy_segf from stdin")
except psycopg2.ProgrammingError:
    pass
conn.close()
r   z-cr   )r   r
   rK   
executabler   communicater:   
returncode)r   scriptprocr   r   r   test_copy_from_segfault3  s    
z!CopyTests.test_copy_from_segfaultc             C   sV   d i t  d 6} t t j d t |  g d t } | j   |  j d | j  d  S)Nzimport psycopg2
conn = psycopg2.connect(%(dsn)r)
curs = conn.cursor()
curs.execute("create table copy_segf (id int)")
try:
    curs.execute("copy copy_segf to stdout")
except psycopg2.ProgrammingError:
    pass
conn.close()
r   z-cstdoutr   )	r   r
   rK   r   r   r   r   r:   r   )r   r   r   r   r   r   test_copy_to_segfaultE  s    $
zCopyTests.test_copy_to_segfaultc             C   s   Gd d   d t   } |  j j   } y | j |   d  Wn> t k
 r| } z |  j d t |  k  WYd  d  } ~ Xn Xd  S)Nc               @   s(   e  Z d  Z d d   Z d d   Z d S)z<CopyTests.test_copy_from_propagate_error.<locals>.BrokenReadc             S   s   d d S)Nr   r   r   )r   r   r   r   r   r   Y  s    zACopyTests.test_copy_from_propagate_error.<locals>.BrokenRead.readc             S   s   d d S)Nr   r   r   )r   r   r   r   r   \  s    zECopyTests.test_copy_from_propagate_error.<locals>.BrokenRead.readlineN)r   r   r   r   r   r   r   r   r   
BrokenReadX  s   r   r1   r<   )_baser$   r%   r9   	ExceptionZ
assertTruestr)r   r   r'   er   r   r   test_copy_from_propagate_errorW  s    z(CopyTests.test_copy_from_propagate_errorc             C   sR   Gd d   d t   } |  j j   } | j d  |  j t | j |   d  d  S)Nc               @   s   e  Z d  Z d d   Z d S)z;CopyTests.test_copy_to_propagate_error.<locals>.BrokenWritec             S   s   d d S)Nr   r   r   )r   r    r   r   r   r   j  s    zACopyTests.test_copy_to_propagate_error.<locals>.BrokenWrite.writeN)r   r   r   r   r   r   r   r   BrokenWritei  s   r   z#insert into tcopy values (10, 'hi')r1   )r   r$   r%   r&   r>   r<   rU   )r   r   r'   r   r   r   test_copy_to_propagate_errorh  s
    	z&CopyTests.test_copy_to_propagate_errorN)r   r   r   r"   r#   r.   r0   r;   r?   rA   r   rY   r]   rg   r,   r@   rw   rz   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_suitev  s    r   __main__)rK   ri   Z	testutilsr   r   r   r   r   rT   r   	itertoolsr	   
subprocessr
   r   rb   Zpsycopg2.extensionsr   r   Z
testconfigr   rL   ru   r   r   r   r   r!   r   r   mainr   r   r   r   <module>   s*   		 4