
5dV(                 @   s>  d  d l  Z  d  d l Z d  d l Z d  d l m Z d d l m Z y d  d l Z e Z Wn$ e	 k
 r d  d l Z d Z Yn Xe
 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	 rWe j j e j _ e j j e j _ e j j e j _ e j j e j _ n  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 e d$  Z d% d&   Z d' d(   Z  d) d*   Z! Gd+ d,   d, e"  Z# d S)-    N)wraps   )dsnskipIfc                s      f d d   } | S)Nc                s(   t         f d d    } | S)Nc                s%     r t  j   d  S |   Sd  S)N)warningswarn)self)condfmsg </tmp/pip-build-0jahl3lb/psycopg2/psycopg2/tests/testutils.pyskipIf__1   s    z)skipIf.<locals>.skipIf_.<locals>.skipIf__)r   )r
   r   )r	   r   )r
   r   skipIf_0   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   skip;   s    r   c             C   s   t  j |  d  S)N)r   r   )r   r   r   r   r   skipTest>   s    r   Zassert_c               @   sa   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z e	 e e  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   setUpY   s    zConnectingTestCase.setUpc             C   s.   x' |  j  D] } | j s
 | j   q
 q
 Wd  S)N)r   closedclose)r   connr   r   r   tearDown\   s    	zConnectingTestCase.tearDownc             K   su   y |  j  Wn5 t k
 rB } z t d |   WYd  d  } ~ Xn Xd d  l } | j t |  } |  j  j |  | S)Nz9%s (did you remember calling ConnectingTestCase.setUp()?)r   )r   AttributeErrorpsycopg2connectr   append)r   kwargser   r   r   r   r   r   b   s    zConnectingTestCase.connectc             C   s(   t  |  d  s! |  j   |  _ n  |  j S)N	_the_conn)hasattrr   r   )r   r   r   r   	_get_conno   s    zConnectingTestCase._get_connc             C   s   | |  _  d  S)N)r   )r   r   r   r   r   	_set_connu   s    zConnectingTestCase._set_connN)__name__
__module____qualname____doc__r   r   r   r!   r"   propertyr   r   r   r   r   r   P   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    r1   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fetchonerollback)r   r2   curZhas)r
   r   r   skip_if_no_uuid_   s    
z)skip_if_no_uuid.<locals>.skip_if_no_uuid_)r   )r
   r9   r   )r
   r   skip_if_no_uuid   s    r:   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)	r   r;   r   r4   r5   r   intr6   r   )r   r;   Zcnnr8   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?   r3   r   )r   r?   )r
   r   r   skip_if_no_namedtuple_   s
    z5skip_if_no_namedtuple.<locals>.skip_if_no_namedtuple_)r   )r
   rA   r   )r
   r   skip_if_no_namedtuple   s    	rB   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.)iorC   r3   r   )r   rC   )r
   r   r   skip_if_no_iobase_   s
    z-skip_if_no_iobase.<locals>.skip_if_no_iobase_)r   )r
   rE   r   )r
   r   skip_if_no_iobase   s    	rF   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
   rJ   )rI   )r
   r   skip_before_postgres_   s    !z3skip_before_postgres.<locals>.skip_before_postgres_)r   )len)rI   rK   r   )rI   r   skip_before_postgres   s    
rM   c                 s.     d d t         f d d   } | S)z>Skip a test on PostgreSQL after (including) a certain version.r   rG   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   rH   r<   r   )r   )r
   rI   r   r   skip_after_postgres__   s    	zPskip_after_postgres.<locals>.skip_after_postgres_.<locals>.skip_after_postgres__)r   )r
   rN   )rI   )r
   r   skip_after_postgres_   s    !z1skip_after_postgres.<locals>.skip_after_postgres_)r   )rL   )rI   rO   r   )rI   r   skip_after_postgres   s    
rP   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_inforL   r   joinmapstr)r   )r
   rI   r   r   skip_before_python__   s    	-zMskip_before_python.<locals>.skip_before_python_.<locals>.skip_before_python__)r   )r
   rW   )rI   )r
   r   skip_before_python_   s    !z/skip_before_python.<locals>.skip_before_python_r   )rI   rX   r   )rI   r   skip_before_python   s    
rY   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 %srQ   )rR   rS   rL   r   rT   rU   rV   )r   )r
   rI   r   r   skip_from_python__   s    	-zGskip_from_python.<locals>.skip_from_python_.<locals>.skip_from_python__)r   )r
   rZ   )rI   )r
   r   skip_from_python_   s    !z+skip_from_python.<locals>.skip_from_python_r   )rI   r[   r   )rI   r   skip_from_python   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   )r;   zskipped because not superuser)r   r;   Zpsycopg2.errorcodesZpgcodeZ
errorcodesZINSUFFICIENT_PRIVILEGEr   )r   r;   r   r   )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
   rb   )ra   )r
   r   skip_if_green_  s    !z%skip_if_green.<locals>.skip_if_green_r   )ra   rc   r   )ra   r   skip_if_green  s    
rd   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())r    rR   r   )r   )r
   r   r   skip_if_no_getrefcount_+  s    z7skip_if_no_getrefcount.<locals>.skip_if_no_getrefcount_)r   )r
   rf   r   )r
   r   skip_if_no_getrefcount*  s    rg   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_5  s    z)skip_if_windows.<locals>.skip_if_windows_)r   )r
   rk   r   )r
   r   skip_if_windows3  s    rl   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   rG   Nsuffixz.pydeleteFfilename)mainzlib2to3.fixesz
--no-diffsz-wz-nzpy3 conversion failed)rR   rS   tempfileNamedTemporaryFilewriteencodeflushnamer   loggingbasicConfigosdevnullZlib2to3.mainrp   	Exceptionopenreadremove)scriptrq   r
   ro   rw   rp   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   rG   T)rR   rS   	TypeErrorAssertionError)r   typeexctbr   r   r   __exit__^  s    zpy3_raises_typeerror.__exit__N)r#   r$   r%   r   r   r   r   r   r   r   Y  s   r   )$ry   ri   rR   	functoolsr   r`   r   Z	unittest2Zunittestr3   r    r   r   r   r   ZTestCaseZ
assertTrueZassertEqualr   r1   r:   r>   rB   rF   rM   rP   rY   r\   r^   rd   Zskip_copy_if_greenrg   rl   r   objectr   r   r   r   r   <module>   sR   
	+
	