
wR\8                 @   sv  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 m	 Z	 m
 Z
 e e d  r e j Z e j Z n< d  d l Z d d   Z d d   Z d	 d
   Z e e j _ e e j d  s e j j e j j k	 rDe j j e j _ e j j e j _ e j j e j _ e j j e j _ n  d d d  Z e e j _ Gd d   d e j  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,  Z# d- d.   Z$ d/ d0   Z% d1 d2   Z& Gd3 d4   d4 e'  Z( d5 d6   Z) d S)7    N)wraps   )dsnrepl_dsnskipIfc                s      f d d   } | S)Nc                s(   t         f d d    } | S)Nc          
      sH     r: t  j   " t  j d t  t  j   Wd  QXd  S |   Sd  S)Nalways)warningscatch_warningssimplefilterUserWarningwarn)self)condfmsg M/var/www/dbchiro/venv/lib/python3.4/site-packages/psycopg2/tests/testutils.pyskipIf__,   s    z)skipIf.<locals>.skipIf_.<locals>.skipIf__)r   )r   r   )r   r   )r   r   skipIf_+   s    $	zskipIf.<locals>.skipIf_r   )r   r   r   r   )r   r   r   r   *   s    c             C   s   t  d |   S)NT)r   )r   r   r   r   skip8   s    r   c          
   C   s4   t  j   " t  j d t  t  j |  Wd  QXd  S)Nr   )r   r	   r
   r   r   )r   r   r   r   r   skipTest;   s    r   assert_c             C   s/   |  j  t | j    t | j    |  d S)z4Check that two conninfo string have the same contentN)assertEqualsetsplit)r   Zdsn1Zdsn2r   r   r   r   assertDsnEqualO   s    r   c               @   s   e  Z d  Z d Z d d   Z d d   Z d d d  Z d	 d
   Z d d   Z d d   Z	 d d   Z
 e e	 e
  Z d d   Z d S)ConnectingTestCasea5  A test case providing connections for tests.

    A connection for the test is always available as `self.conn`. Others can be
    created with `self.connect()`. All are closed on tearDown.

    Subclasses needing to customize setUp and tearDown should remember to call
    the base class implementations.
    c             C   s   g  |  _  d  S)N)_conns)r   r   r   r   setUp_   s    zConnectingTestCase.setUpc             C   s.   x' |  j  D] } | j s
 | j   q
 q
 Wd  S)N)r   closedclose)r   connr   r   r   tearDownb   s    	zConnectingTestCase.tearDownNc                s1     f d d   } |  j  |    | |  |  S)z;Compare two quoted strings disregarding eventual E'' quotesc                sL   t  |  t  r" t j d d |   St    t  rD t j d d |   S|  Sd  S)Nz\bE''s   \bE'   ')
isinstancestrresubbytes)s)firstr   r   r   j   s
    z/ConnectingTestCase.assertQuotedEqual.<locals>.f)r   )r   r+   secondr   r   r   )r+   r   assertQuotedEqualh   s    z$ConnectingTestCase.assertQuotedEqualc             K   s   y |  j  Wn5 t k
 rB } z t d |   WYd  d  } ~ Xn Xd | k ra | j d  } n t } d d  l } | j | |  } |  j  j |  | S)Nz7%s (did you forget to call ConnectingTestCase.setUp()?)r   r   )r   AttributeErrorpopr   psycopg2connectappend)r   kwargseZconninfor0   r!   r   r   r   r1   t   s    zConnectingTestCase.connectc             K   s   t  d k r |  j d  Sd | k r2 t  | d <n  d d l } y2 |  j |   } | j d k ro |  j |  n  WnK | j k
 r } z( | j d k r |  j d |  S  WYd d } ~ Xn X| S)zReturn a connection set up for replication

        The connection is on "PSYCOPG2_TEST_REPL_DSN" unless overridden by
        a *dsn* kwarg.

        Should raise a skip test if not available, but guard for None on
        old Python versions.
        Nz%replication tests disabled by defaultr   r   r   z!replication db not configured: %s)r   r   r0   r1   Zasync_waitZOperationalErrorpgcode)r   r3   r0   r!   r4   r   r   r   repl_connect   s    	zConnectingTestCase.repl_connectc             C   s(   t  |  d  s! |  j   |  _ n  |  j S)N	_the_conn)hasattrr1   r8   )r   r   r   r   	_get_conn   s    zConnectingTestCase._get_connc             C   s   | |  _  d  S)N)r8   )r   r!   r   r   r   	_set_conn   s    zConnectingTestCase._set_connc             C   s   d d  l  } | } t | d  s- | j } n  x | j   } | | j j k rR Pq0 | | j j k r t j | g g  g  d  q0 | | j j k r t j g  | g g  d  q0 t	 d |   q0 Wd  S)Nr   poll
   zUnexpected result from poll: %r)
Zpsycopg2.extensionsr9   
connectionr<   
extensionsZPOLL_OKZ	POLL_READselectZ
POLL_WRITE	Exception)r   Zcur_or_connr0   Zpollablestater   r   r   r5      s    zConnectingTestCase.wait)__name__
__module____qualname____doc__r   r"   r-   r1   r7   r:   r;   propertyr!   r5   r   r   r   r   r   V   s   r   c          	   G   s]   xV t  |   D]H } | j d  r x0 | D]% } t |  | | t |  |    q) Wq q Wd S)zT
    Apply all the *decorators* to all the tests defined in the TestCase *cls*.
    testN)dir
startswithsetattrgetattr)cls
decoratorsndr   r   r   decorate_all_tests   s    rQ   c                s"   t       f d d    } | S)z;Decorator to skip a test if uuid is not supported by Py/PG.c                s   y d d  l  } Wn t k
 r1 |  j d  SYn Xz, |  j j   } | j d  | j   } Wd  |  j j   X| r   |   S|  j d  Sd  S)Nr   z)uuid not available in this Python versionz2select typname from pg_type where typname = 'uuid'z%uuid type not available on the server)uuidImportErrorr   r!   cursorexecutefetchoneZrollback)r   rR   curZhas)r   r   r   skip_if_no_uuid_   s    
z)skip_if_no_uuid.<locals>.skip_if_no_uuid_)r   )r   rX   r   )r   r   skip_if_no_uuid   s    rY   c                s"   t       f d d    } | S)z3Skip a test if the server has tpc support disabled.c                s   d d l  m } |  j   } | j   } y | j d  Wn | k
 rZ |  j d  SYn Xt | j   d  } | j   | s |  j d  S  |   S)Nr   )ProgrammingErrorzSHOW max_prepared_transactions;z5server too old: two phase transactions not supported.zfserver not configured for two phase transactions. set max_prepared_transactions to > 0 to run the test)	r0   rZ   r1   rT   rU   r   intrV   r    )r   rZ   ZcnnrW   Zmtp)r   r   r   skip_if_tpc_disabled_   s    
z3skip_if_tpc_disabled.<locals>.skip_if_tpc_disabled_)r   )r   r\   r   )r   r   skip_if_tpc_disabled   s    r]   c                s"   t       f d d    } | S)Nc                sD   y d d l  m } Wn t k
 r5 |  j d  SYn X  |   Sd  S)Nr   )
namedtuplez$collections.namedtuple not available)collectionsr^   rS   r   )r   r^   )r   r   r   skip_if_no_namedtuple_   s
    z5skip_if_no_namedtuple.<locals>.skip_if_no_namedtuple_)r   )r   r`   r   )r   r   skip_if_no_namedtuple   s    	ra   c                s"   t       f d d    } | S)z.Skip a test if io.TextIOBase is not available.c                sD   y d d l  m } Wn t k
 r5 |  j d  SYn X  |   Sd  S)Nr   )
TextIOBasezio.TextIOBase not found.)iorb   rS   r   )r   rb   )r   r   r   skip_if_no_iobase_
  s
    z-skip_if_no_iobase.<locals>.skip_if_no_iobase_)r   )r   rd   r   )r   r   skip_if_no_iobase  s    	re   c                 s.     d d t         f d d   } | S)z3Skip a test on PostgreSQL before a certain version.r      c                s%   t        f d d    } | S)Nc                sA   |  j  j t d   k  r3 |  j d |  j  j  S  |   Sd  S)Nz
%d%02d%02dzskipped because PostgreSQL %s)r!   server_versionr[   r   )r   )r   verr   r   skip_before_postgres__  s    	zSskip_before_postgres.<locals>.skip_before_postgres_.<locals>.skip_before_postgres__)r   )r   ri   )rh   )r   r   skip_before_postgres_  s    !z3skip_before_postgres.<locals>.skip_before_postgres_)r   )len)rh   rj   r   )rh   r   skip_before_postgres  s    
rl   c                 s.     d d t         f d d   } | S)z>Skip a test on PostgreSQL after (including) a certain version.r   rf   c                s%   t        f d d    } | S)Nc                sA   |  j  j t d   k r3 |  j d |  j  j  S  |   Sd  S)Nz
%d%02d%02dzskipped because PostgreSQL %s)r!   rg   r[   r   )r   )r   rh   r   r   skip_after_postgres__,  s    	zPskip_after_postgres.<locals>.skip_after_postgres_.<locals>.skip_after_postgres__)r   )r   rm   )rh   )r   r   skip_after_postgres_+  s    !z1skip_after_postgres.<locals>.skip_after_postgres_)r   )rk   )rh   rn   r   )rh   r   skip_after_postgres'  s    
ro   c              C   s@   d d  l  }  |  j } | d k r< t | |  j j    } n  | S)Nr   i_ )r0   Z__libpq_version__minr?   libpq_version)r0   vr   r   r   rq   8  s
    	rq   c                 s.     d d t         f d d   } | S)zESkip a test if libpq we're linked to is older than a certain version.r   rf   c                s%   t        f d d    } | S)Nc                s>   t    } | t d   k  r0 |  j d |  S  |   Sd  S)Nz
%d%02d%02dzskipped because libpq %d)rq   r[   r   )r   rr   )r   rh   r   r   skip_before_libpq__E  s    	zJskip_before_libpq.<locals>.skip_before_libpq_.<locals>.skip_before_libpq__)r   )r   rs   )rh   )r   r   skip_before_libpq_D  s    !z-skip_before_libpq.<locals>.skip_before_libpq_)r   )rk   )rh   rt   r   )rh   r   skip_before_libpq@  s    
ru   c                 s.     d d t         f d d   } | S)zESkip a test if libpq we're linked to is newer than a certain version.r   rf   c                s%   t        f d d    } | S)Nc                s>   t    } | t d   k r0 |  j d |  S  |   Sd  S)Nz
%d%02d%02dzskipped because libpq %s)rq   r[   r   )r   rr   )r   rh   r   r   skip_after_libpq__V  s    	zGskip_after_libpq.<locals>.skip_after_libpq_.<locals>.skip_after_libpq__)r   )r   rv   )rh   )r   r   skip_after_libpq_U  s    !z+skip_after_libpq.<locals>.skip_after_libpq_)r   )rk   )rh   rw   r   )rh   r   skip_after_libpqQ  s    
rx   c                 s     f d d   } | S)z/Skip a test on Python before a certain version.c                s%   t        f d d    } | S)Nc          	      sc   t  j d  t     k  rU |  j d d j t t t  j d  t       S  |   Sd  S)Nzskipped because Python %s.)sysversion_infork   r   joinmapr&   )r   )r   rh   r   r   skip_before_python__e  s    	-zMskip_before_python.<locals>.skip_before_python_.<locals>.skip_before_python__)r   )r   r~   )rh   )r   r   skip_before_python_d  s    !z/skip_before_python.<locals>.skip_before_python_r   )rh   r   r   )rh   r   skip_before_pythonb  s    
r   c                 s     f d d   } | S)z:Skip a test on Python after (including) a certain version.c                s%   t        f d d    } | S)Nc          	      sc   t  j d  t     k rU |  j d d j t t t  j d  t       S  |   Sd  S)Nzskipped because Python %sry   )rz   r{   rk   r   r|   r}   r&   )r   )r   rh   r   r   skip_from_python__t  s    	-zGskip_from_python.<locals>.skip_from_python_.<locals>.skip_from_python__)r   )r   r   )rh   )r   r   skip_from_python_s  s    !z+skip_from_python.<locals>.skip_from_python_r   )rh   r   r   )rh   r   skip_from_pythonq  s    
r   c                s"   t       f d d    } | S)zDSkip a test if the database user running the test is not a superuserc                s~   d d l  m } y   |   SWnY | k
 ry } z9 d d  l } | j | j j k rd |  j d  n   WYd  d  } ~ Xn Xd  S)Nr   )rZ   zskipped because not superuser)r0   rZ   Zpsycopg2.errorcodesr6   Z
errorcodesZINSUFFICIENT_PRIVILEGEr   )r   rZ   r4   r0   )r   r   r   skip_if_no_superuser_  s    z3skip_if_no_superuser.<locals>.skip_if_no_superuser_)r   )r   r   r   )r   r   skip_if_no_superuser  s    r   c                s     f d d   } | S)Nc                s%   t        f d d    } | S)Nc                s1   d d l  m } | r# |  j   S  |   Sd  S)Nr   )green)
testconfigr   r   )r   r   )r   reasonr   r   skip_if_green__  s    z>skip_if_green.<locals>.skip_if_green_.<locals>.skip_if_green__)r   )r   r   )r   )r   r   skip_if_green_  s    !z%skip_if_green.<locals>.skip_if_green_r   )r   r   r   )r   r   skip_if_green  s    
r   z*copy in async mode currently not supportedc                s"   t       f d d    } | S)Nc                s*   t  t d  s |  j d  S  |   Sd  S)Ngetrefcountzskipped, no sys.getrefcount())r9   rz   r   )r   )r   r   r   skip_if_no_getrefcount_  s    z7skip_if_no_getrefcount.<locals>.skip_if_no_getrefcount_)r   )r   r   r   )r   r   skip_if_no_getrefcount  s    r   c                s"   t       f d d    } | S)zSkip a test if run on windowsc                s-   t  j   d k r |  j d  S  |   Sd  S)NWindowszNot supported on Windows)platformsystemr   )r   )r   r   r   skip_if_windows_  s    z)skip_if_windows.<locals>.skip_if_windows_)r   )r   r   r   )r   r   skip_if_windows  s    r   c             C   s   t  j d d k  r |  Sd d l } | j d d d d  } | j |  j    | j   | j } | j   d d l	 } | j
 d t j  d d	 l m } | d
 d d d | g  r t d   n  t |  } z | j   SWd | j   t j |  Xd S)z/Convert a script to Python3 syntax if required.r   rf   Nsuffixz.pydeleteFfilename)mainzlib2to3.fixesz
--no-diffsz-wz-nzpy3 conversion failed)rz   r{   tempfileNamedTemporaryFilewriteencodeflushnamer    loggingbasicConfigosdevnullZlib2to3.mainr   rA   openreadremove)scriptr   r   r   r   r   f2r   r   r   script_to_py3  s$    
	

r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)py3_raises_typeerrorc             C   s   d  S)Nr   )r   r   r   r   	__enter__  s    zpy3_raises_typeerror.__enter__c             C   s-   t  j d d k r) | t k s% t  d Sd  S)Nr   rf   T)rz   r{   	TypeErrorAssertionError)r   typeexctbr   r   r   __exit__  s    zpy3_raises_typeerror.__exit__N)rC   rD   rE   r   r   r   r   r   r   r     s   r   c                s"   t       f d d    } | S)zDecorator to mark slow tests we may want to skip

    Note: in order to find slow tests you can run:

    make check 2>&1 | ts -i "%.s" | sort -n
    c                s)   t  j j d  r |  j d  S  |   S)NZPSYCOPG2_TEST_FASTz	slow test)r   environgetr   )r   )r   r   r   slow_  s    zslow.<locals>.slow_)r   )r   r   r   )r   r   slow  s    r   )*r'   r   rz   r@   r   Zunittest	functoolsr   r   r   r   r9   r   r   r   r   ZTestCase
assertTruer   r   r   rQ   rY   r]   ra   re   rl   ro   rq   ru   rx   r   r   r   r   Zskip_copy_if_greenr   r   r   objectr   r   r   r   r   r   <module>   sX   	j


