î
5dÝV¼  ã               @   sŸ   d  d l  Z  d d l m Z 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)é    Né   )ÚunittestÚConnectingTestCase)Úbc               @   sR   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z d S)ÚQuotingTestCaseaM  Checks the correct quoting of strings and binary objects.

    Since ver. 8.1, PostgreSQL is moving towards SQL standard conforming
    strings, where the backslash (\) is treated as literal character,
    not as escape. To treat the backslash as a C-style escapes, PG supports
    the E'' quotes.

    This test case checks that the E'' quotes are used whenever they are
    needed. The tests are expected to pass with all PostgreSQL server versions
    (currently tested with 7.4 <= PG <= 8.3beta) and with any
    'standard_conforming_strings' server parameter value.
    The tests also check that no warning is raised ('escape_string_warning'
    should be on).

    http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
    http://www.postgresql.org/docs/current/static/runtime-config-compatible.html
    c             C   s‹   d } | d j  t t t t d d ƒ ƒ ƒ ƒ 7} |  j j ƒ  } | j d | f ƒ | j ƒ  d } |  j	 | | ƒ |  j
 |  j j ƒ d  S)NzWsome data with 	 chars
        to escape into, 'quotes' and \ a backslash too.
        Ú r   é   z
SELECT %s;r   )ÚjoinÚmapÚchrÚlistÚrangeÚconnÚcursorÚexecuteÚfetchoneÚassertEqualÚ
assertTrueÚnotices)ÚselfÚdataÚcursÚres© r   ú=/tmp/pip-build-0jahl3lb/psycopg2/psycopg2/tests/test_quote.pyÚtest_string2   s    +zQuotingTestCase.test_stringc             C   s0  d } t  j d d k  rD | d j t t t t d ƒ ƒ ƒ ƒ 7} n | t t t d ƒ ƒ ƒ 7} |  j j	 ƒ  } | j
 d t j | ƒ f ƒ t  j d d k  r· t | j ƒ  d ƒ } n | j ƒ  d j ƒ  } | d d t d ƒ f k r|  j j d k r|  j d	 ƒ S|  j | | ƒ |  j |  j j ƒ d  S)
NsU   some data with   binary
        stuff into, 'quotes' and \ a backslash too.
        r   é   r   é   zSELECT %s::bytea;ó   xi_ z*bytea broken with server >= 9.0, libpq < 9)ÚsysÚversion_infor	   r
   r   r   r   Úbytesr   r   r   Úpsycopg2ÚBinaryÚstrr   ÚtobytesÚordZserver_versionÚskipTestr   r   r   )r   r   r   r   r   r   r   Útest_binary?   s    +.zQuotingTestCase.test_binaryc             C   sø   |  j  j ƒ  } | j d ƒ | j ƒ  d } | d k rI |  j d | ƒ Sd } | d j t t d d „  t d	 d
 ƒ Dƒ ƒ ƒ 7} |  j  j	 d ƒ t
 j j t
 j j |  j  ƒ | j d | f ƒ | j ƒ  d } |  j | | ƒ |  j |  j  j ƒ d  S)NzSHOW server_encodingr   ÚUTF8z0Unicode test skipped since server encoding is %suf   some data with 	 chars
        to escape into, 'quotes', â‚¬ euro sign and \ a backslash too.
        r   c             S   s2   g  |  ]( } d  | k o# d k n s | ‘ q S)i Ø  iÿß  r   )Ú.0Úur   r   r   ú
<listcomp>b   s   	 z0QuotingTestCase.test_unicode.<locals>.<listcomp>r   i   ÚUNICODEzSELECT %s::text;)r   r   r   r   r'   r	   r
   r   r   Úset_client_encodingr"   Ú
extensionsÚregister_typer-   r   r   r   )r   r   Zserver_encodingr   r   r   r   r   Útest_unicodeV   s    2zQuotingTestCase.test_unicodec             C   sy  |  j  j d ƒ |  j  j ƒ  } t j d d k  ro d j t t t t	 d d ƒ ƒ t t	 d d ƒ ƒ ƒ ƒ } n7 t
 t t	 d d ƒ ƒ t t	 d d ƒ ƒ ƒ j d	 ƒ } | j d
 | f ƒ | j ƒ  d } |  j | | ƒ |  j |  j  j ƒ t j d d k  rut j j t j j |  j  ƒ | j d	 ƒ } | j d
 | f ƒ | j ƒ  d } |  j | | ƒ |  j |  j  j ƒ n  d  S)NZLATIN1r   r   r   é    r   é    r   Úlatin1zSELECT %s::text;)r   r.   r   r   r    r	   r
   r   r   r   r!   Údecoder   r   r   r   r   r"   r/   r0   r-   )r   r   r   r   r   r   r   Útest_latin1m   s     =7zQuotingTestCase.test_latin1c             C   sy  |  j  j d ƒ |  j  j ƒ  } t j d d k  ro d j t t t t	 d d ƒ ƒ t t	 d d ƒ ƒ ƒ ƒ } n7 t
 t t	 d d ƒ ƒ t t	 d d ƒ ƒ ƒ j d	 ƒ } | j d
 | f ƒ | j ƒ  d } |  j | | ƒ |  j |  j  j ƒ t j d d k  rut j j t j j |  j  ƒ | j d	 ƒ } | j d
 | f ƒ | j ƒ  d } |  j | | ƒ |  j |  j  j ƒ n  d  S)NZKOI8r   r   r   r2   r   é€   r   Úkoi8_rzSELECT %s::text;)r   r.   r   r   r    r	   r
   r   r   r   r!   r5   r   r   r   r   r   r"   r/   r0   r-   )r   r   r   r   r   r   r   Ú	test_koi8…   s     =7zQuotingTestCase.test_koi8N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r1   r6   r9   r   r   r   r   r       s   r   c               @   s   e  Z d  Z d d „  Z d S)ÚTestQuotedStringc             C   s\   t  j j d ƒ } |  j | j d ƒ |  j j d ƒ | j |  j ƒ |  j | j d ƒ d  S)NÚhir4   Úutf_8)r"   r/   ÚQuotedStringr   Úencodingr   r.   Úprepare)r   Úqr   r   r   Útest_encodingŸ   s
    zTestQuotedString.test_encodingN)r:   r;   r<   rE   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    rF   Ú__main__)r   Z	testutilsr   r   r"   Zpsycopg2.extensionsr   r   r>   rF   r:   Úmainr   r   r   r   Ú<module>   s   ~
