U
    P|j  ã                   @   s¤   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZ ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZdS )z$
Tests for L{twisted.names.common}.
é    )ÚverifyClass)Ú	IResolver)ÚResolverBase)ÚEFORMATÚENAMEÚENOTIMPÚEREFUSEDÚESERVERÚQuery)ÚDNSFormatErrorÚDNSNameErrorÚDNSNotImplementedErrorÚDNSQueryRefusedErrorÚDNSServerErrorÚDNSUnknownError)ÚFailure)ÚSynchronousTestCasec                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚExceptionForCodeTestsz5
    Tests for L{ResolverBase.exceptionForCode}.
    c                 C   s   t ƒ j| _d S ©N)r   ÚexceptionForCode©Úself© r   úB/tmp/pip-unpacked-wheel-audpp6lv/twisted/names/test/test_common.pyÚsetUp   s    zExceptionForCodeTests.setUpc                 C   s   |   |  t¡t¡ dS )zd
        L{ResolverBase.exceptionForCode} converts L{EFORMAT} to
        L{DNSFormatError}.
        N)ÚassertIsr   r   r   r   r   r   r   Útest_eformat"   s    z"ExceptionForCodeTests.test_eformatc                 C   s   |   |  t¡t¡ dS )zd
        L{ResolverBase.exceptionForCode} converts L{ESERVER} to
        L{DNSServerError}.
        N)r   r   r	   r   r   r   r   r   Útest_eserver)   s    z"ExceptionForCodeTests.test_eserverc                 C   s   |   |  t¡t¡ dS )zX
        L{ResolverBase.exceptionForCode} converts L{ENAME} to L{DNSNameError}.
        N)r   r   r   r   r   r   r   r   Ú
test_ename0   s    z ExceptionForCodeTests.test_enamec                 C   s   |   |  t¡t¡ dS )zl
        L{ResolverBase.exceptionForCode} converts L{ENOTIMP} to
        L{DNSNotImplementedError}.
        N)r   r   r   r   r   r   r   r   Útest_enotimp6   s    z"ExceptionForCodeTests.test_enotimpc                 C   s   |   |  t¡t¡ dS )zk
        L{ResolverBase.exceptionForCode} converts L{EREFUSED} to
        L{DNSQueryRefusedError}.
        N)r   r   r   r   r   r   r   r   Útest_erefused=   s    z#ExceptionForCodeTests.test_erefusedc                 C   s   |   |  tƒ ¡t¡ dS )zr
        L{ResolverBase.exceptionForCode} converts any other response code to
        L{DNSUnknownError}.
        N)r   r   Úobjectr   r   r   r   r   Ú
test_otherD   s    z ExceptionForCodeTests.test_otherN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r    r"   r   r   r   r   r      s   r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú
QueryTestsz*
    Tests for L{ResolverBase.query}.
    c                 C   s   t ttƒ dS )zF
        L{ResolverBase} provides the L{IResolver} interface.
        N)r   r   r   r   r   r   r   Ú"test_resolverBaseProvidesIResolverQ   s    z-QueryTests.test_resolverBaseProvidesIResolverc                    sF   g ‰ t ƒ }d‡ fdd„i|_tddd�}| |d¡ |  dgˆ ¡ dS )	zŸ
        L{ResolverBase.query} looks up a method to invoke using the type of the
        query passed to it and the C{typeToMethod} mapping on itself.
        é90  c                    s   ˆ   | |f¡S r   )Úappend©ÚqueryÚtimeout©Úresultsr   r   Ú<lambda>_   ó    z6QueryTests.test_typeToMethodDispatch.<locals>.<lambda>ó   example.com©ÚnameÚtypeé{   )r2   r6   N)r   ÚtypeToMethodr
   r,   ÚassertEqual)r   Úresolverr,   r   r.   r   Útest_typeToMethodDispatchW   s     
ÿz$QueryTests.test_typeToMethodDispatchc                    sZ   t ƒ ‰ tƒ }d‡ fdd„i|_tddd�}| |d¡}g }| |j¡ |  ˆ |d ¡ dS )	zÄ
        L{ResolverBase.query} returns a L{Deferred} which fires with the result
        of the method found in the C{typeToMethod} mapping for the type of the
        query passed to it.
        i1Ô  c                    s   ˆ S r   r   r+   ©Úexpectedr   r   r0   m   r1   z4QueryTests.test_typeToMethodResult.<locals>.<lambda>r2   r3   r6   r   N)r!   r   r7   r
   r,   ÚaddBothr*   r8   ©r   r9   r,   ZqueryDeferredÚresultr   r;   r   Útest_typeToMethodResulte   s    z"QueryTests.test_typeToMethodResultc                 C   sV   t ƒ }i |_tddd�}| |d¡}g }| |j¡ |  |d t¡ |d  t	¡ dS )zÇ
        L{ResolverBase.query} returns a L{Deferred} which fails with
        L{NotImplementedError} when called with a query of a type not present in
        its C{typeToMethod} dictionary.
        r2   r)   r3   r6   r   N)
r   r7   r
   r,   r=   r*   ZassertIsInstancer   ZtrapÚNotImplementedErrorr>   r   r   r   Útest_unknownQueryTypet   s    z QueryTests.test_unknownQueryTypeN)r#   r$   r%   r&   r(   r:   r@   rB   r   r   r   r   r'   L   s
   r'   N)r&   Zzope.interface.verifyr   Ztwisted.internet.interfacesr   Ztwisted.names.commonr   Ztwisted.names.dnsr   r   r   r   r	   r
   Ztwisted.names.errorr   r   r   r   r   r   Ztwisted.python.failurer   Ztwisted.trial.unittestr   r   r'   r   r   r   r   Ú<module>   s     2