U
    P|j	9  ã                   @   sÔ   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ G dd„ dejƒZ G dd„ dejƒZ!dS )z+
Test cases for L{twisted.logger._legacy}.
é    N)Útime)ÚListÚcast)Úimplementer)ÚBrokenMethodImplementation)ÚverifyObject)ÚcontextÚlog)ÚFailure)Úunittesté   )ÚformatEvent)ÚILogObserverÚLogEvent)ÚLegacyLogObserverWrapperÚpublishToNewObserver)ÚLogLevelc                   @   sX  e Zd ZdZddœdd„Zddœdd„Zeedœd	d
„Zeedœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd „Zddœd!d"„Zddœd#d$„Zddœd%d&„Zddœd'd(„Zeed)œd*d+„Zddœd,d-„Zddœd.d/„Zddœd0d1„Zddœd2d3„ZdS )4ÚLegacyLogObserverWrapperTestsz0
    Tests for L{LegacyLogObserverWrapper}.
    N©Úreturnc              
   C   sX   t tjdd„ ƒ}t|ƒ}ztt|ƒ W n, tk
rR } z|  |¡ W 5 d}~X Y nX dS )zD
        L{LegacyLogObserverWrapper} is an L{ILogObserver}.
        c                 S   s   d S ©N© ©Úer   r   úC/tmp/pip-unpacked-wheel-audpp6lv/twisted/logger/test/test_legacy.pyÚ<lambda>"   ó    z>LegacyLogObserverWrapperTests.test_interface.<locals>.<lambda>N)r   Ú	legacyLogr   r   r   r   Zfail)ÚselfÚlegacyObserverÚobserverr   r   r   r   Útest_interface   s    z,LegacyLogObserverWrapperTests.test_interfacec                 C   s6   t tjƒG dd„ dƒƒ}t|ƒ ƒ}|  t|ƒd¡ dS )zJ
        L{LegacyLogObserverWrapper} returns the expected string.
        c                   @   s,   e Zd Zedœdd„Zejddœdd„ZdS )z?LegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserverr   c                 S   s   dS )Nz<Legacy Observer>r   ©r   r   r   r   Ú__repr__0   s    zHLegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserver.__repr__N)Ú	eventDictr   c                 S   s   d S r   r   )r   r$   r   r   r   Ú__call__3   s    zHLegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserver.__call__)Ú__name__Ú
__module__Ú__qualname__Ústrr#   r   Ú	EventDictr%   r   r   r   r   ÚLegacyObserver.   s   r+   z+LegacyLogObserverWrapper(<Legacy Observer>)N)r   r   r   r   ÚassertEqualÚrepr)r   r+   r    r   r   r   Ú	test_repr)   s    
z'LegacyLogObserverWrapperTests.test_repr©Úeventr   c                    s@   g ‰ t tj‡ fdd„ƒ}t|ƒ}||ƒ |  tˆ ƒd¡ ˆ d S )zÒ
        Send an event to a wrapped legacy observer and capture the event as
        seen by that observer.

        @param event: an event

        @return: the event as observed by the legacy wrapper
        c                    s
   ˆ   | ¡S r   )Úappendr   ©Úeventsr   r   r   E   r   z7LegacyLogObserverWrapperTests.observe.<locals>.<lambda>é   r   )r   r   r   r   r,   Úlen)r   r0   r   r    r   r2   r   Úobserve:   s    	z%LegacyLogObserverWrapperTests.observec                 C   sX   |  dtƒ ¡ |  dd¡ |  dtj¡ |  t|ƒ¡}| ¡ D ]\}}|  ||¡ q>|S )zÉ
        Send an event to a wrapped legacy observer and verify that its data is
        preserved.

        @param event: an event

        @return: the event as observed by the legacy wrapper
        Úlog_timeÚ
log_systemú-Ú	log_level)Ú
setdefaultr   r   Úinfor6   ÚdictÚitemsZassertIn)r   r0   ÚobservedÚkeyÚvaluer   r   r   ÚforwardAndVerifyL   s    
z.LegacyLogObserverWrapperTests.forwardAndVerifyc                 C   s   |   tddd�¡ dS )ze
        Basic forwarding: event keys as observed by a legacy observer are the
        same.
        r4   r   ©ÚfooÚbarN)rB   r=   r"   r   r   r   Útest_forwardc   s    z*LegacyLogObserverWrapperTests.test_forwardc                 C   s*   t ƒ }|  t|d�¡}|  |d |¡ dS )zc
        The new-style C{"log_time"} key is copied to the old-style C{"time"}
        key.
        )r7   r   N©r   rB   r=   r,   ©r   Zstampr0   r   r   r   Ú	test_timej   s    z'LegacyLogObserverWrapperTests.test_timec                 C   s0   t ƒ }|  t|d |d�¡}|  |d |¡ dS )zr
        The new-style C{"log_time"} key does not step on a pre-existing
        old-style C{"time"} key.
        r4   )r7   r   r   NrG   rH   r   r   r   Útest_timeAlreadySets   s    z1LegacyLogObserverWrapperTests.test_timeAlreadySetc                 C   s$   |   tdd�¡}|  |d d¡ dS )zg
        The new-style C{"log_system"} key is copied to the old-style
        C{"system"} key.
        rD   )r8   ÚsystemN©rB   r=   r,   ©r   r0   r   r   r   Útest_system|   s    z)LegacyLogObserverWrapperTests.test_systemc                 C   s&   |   tddd�¡}|  |d d¡ dS )zv
        The new-style C{"log_system"} key does not step on a pre-existing
        old-style C{"system"} key.
        rD   rE   )r8   rK   rK   NrL   rM   r   r   r   Útest_systemAlreadySet„   s    z3LegacyLogObserverWrapperTests.test_systemAlreadySetc                 C   s2   t tƒ tjd�}|  t |ƒ¡}|  |d d¡ dS )zy
        If the new-style C{"log_system"} key is absent, the old-style
        C{"system"} key is set to C{"-"}.
        )r7   r:   rK   r9   N)r=   r   r   r<   r6   r,   )r   r0   r?   r   r   r   Útest_noSystemŒ   s    z+LegacyLogObserverWrapperTests.test_noSystemc                 C   sT   |   ttjdd�¡ |   ttjdd�¡ |   ttjdd�¡ |   ttjdd�¡ dS )zÁ
        If explicitly set, the C{isError} key will be preserved when forwarding
        from a new-style logging emitter to a legacy logging observer,
        regardless of log level.
        r4   )r:   ÚisErrorr   N)rB   r=   r   r<   ÚwarnÚerrorÚcriticalr"   r   r   r   Útest_levelNotChange–   s    z1LegacyLogObserverWrapperTests.test_levelNotChangec                 C   s"   |   ttjd�¡}|  d|¡ dS )a)  
        The new-style C{"log_level"} key is not translated to the old-style
        C{"logLevel"} key.

        Events are forwarded from the old module from to new module and are
        then seen by old-style observers.
        We don't want to add unexpected keys to old-style events.
        )r:   ÚlogLevelN)rB   r=   r   r<   ÚassertNotInrM   r   r   r   Útest_pythonLogLevelNotSet¡   s    	z7LegacyLogObserverWrapperTests.test_pythonLogLevelNotSetc                 C   s$   |   tdd�¡}|  |d d¡ dS )a  
        If a stdlib log level was provided as a string (eg. C{"WARNING"}) in
        the legacy "logLevel" key, it does not get converted to a number.
        The documentation suggested that numerical values should be used but
        this was not a requirement.
        ÚWARNING©rV   rV   NrL   rM   r   r   r   Útest_stringPythonLogLevel­   s    ÿÿz7LegacyLogObserverWrapperTests.test_stringPythonLogLevelc                 C   s    |   tƒ ¡}|  |d d¡ dS )z–
        The old-style C{"message"} key is added, even if no new-style
        C{"log_format"} is given, as it is required, but may be empty.
        Úmessager   NrL   rM   r   r   r   Útest_message»   s    z*LegacyLogObserverWrapperTests.test_messagec                 C   s$   |   tdd�¡}|  |d d¡ dS )zV
        The old-style C{"message"} key is not modified if it already exists.
        rC   )r\   r\   NrL   rM   r   r   r   Útest_messageAlreadySetÃ   s    z4LegacyLogObserverWrapperTests.test_messageAlreadySetc                 C   s(   |   tddd�¡}|  t |¡d¡ dS )z—
        Formatting is translated such that text is rendered correctly, even
        though old-style logging doesn't use PEP 3101 formatting.
        zHello, {who}!Úworld)Ú
log_formatZwhoúHello, world!N©rB   r=   r,   r   ÚtextFromEventDictrM   r   r   r   Útest_formatÊ   s    z)LegacyLogObserverWrapperTests.test_formatc                 C   s(   |   tddd�¡}|  t |¡d¡ dS )zo
        Using the message key, which is special in old-style, works for
        new-style formatting.
        zHello, {message}!r_   )r`   r\   ra   Nrb   rM   r   r   r   Útest_formatMessageÒ   s    
ÿz0LegacyLogObserverWrapperTests.test_formatMessagec                 C   s(   |   tddd�¡}|  t |¡d¡ dS )zd
        Formatting is not altered if the old-style C{"format"} key already
        exists.
        zHello!zHowdy!)r`   ÚformatNrb   rM   r   r   r   Útest_formatAlreadySetÜ   s    z3LegacyLogObserverWrapperTests.test_formatAlreadySet)Úvaluesr   c                 K   s&   t tdƒƒ}|  tf |ddœ|—Ž¡S )z¦
        Create a new-style event with a captured failure.

        @param values: Additional values to include in the event.

        @return: the new event
        znyargh!ú	oopsie...)Úlog_failurer`   )r
   ÚRuntimeErrorrB   r=   )r   rh   Úfailurer   r   r   ÚeventWithFailureä   s    ÿz.LegacyLogObserverWrapperTests.eventWithFailurec                 C   s>   |   ¡ }|  |d |d ¡ |  |d ¡ |  |d d¡ dS )z}
        Captured failures in the new style set the old-style C{"failure"},
        C{"isError"}, and C{"why"} keys.
        rl   rj   rQ   Úwhyri   N)rm   ÚassertIsÚ
assertTruer,   rM   r   r   r   Útest_failureñ   s    z*LegacyLogObserverWrapperTests.test_failurec                 C   s,   t tdƒƒ}| j|d�}|  |d |¡ dS )úv
        Captured failures in the new style do not step on a pre-existing
        old-style C{"failure"} key.
        zWeak salsa!)rl   rl   N)r
   rk   rm   ro   )r   rl   r0   r   r   r   Útest_failureAlreadySetû   s    z4LegacyLogObserverWrapperTests.test_failureAlreadySetc                 C   s    | j dd�}|  |d d¡ dS )zv
        Captured failures in the new style do not step on a pre-existing
        old-style C{"isError"} key.
        r   ©rQ   rQ   N©rm   r,   rM   r   r   r   Útest_isErrorAlreadySet  s    z4LegacyLogObserverWrapperTests.test_isErrorAlreadySetc                 C   s    | j dd�}|  |d d¡ dS )rr   Zblah)rn   rn   Nru   rM   r   r   r   Útest_whyAlreadySet  s    z0LegacyLogObserverWrapperTests.test_whyAlreadySet)r&   r'   r(   Ú__doc__r!   r.   r   r6   rB   rF   rI   rJ   rN   rO   rP   rU   rX   r[   r]   r^   rd   re   rg   Úobjectrm   rq   rs   rv   rw   r   r   r   r   r      s0   		


	r   c                   @   s¾   e Zd ZdZddœdd„Zeeejdœdd„Z	ddœd	d
„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„ZdS )ÚPublishToNewObserverTestsz,
    Tests for L{publishToNewObserver}.
    Nr   c                 C   s   g | _ tt| j jƒ| _d S r   )r3   r   r   r1   r    r"   r   r   r   ÚsetUp  s    zPublishToNewObserverTests.setUp)r\   rh   r   c                 O   sD   t  tj¡pi  ¡ }| |¡ ||d< tƒ |d< d|kr@d|d< |S )a  
        Return a basic old-style event as would be created by L{legacyLog.msg}.

        @param message: a message event value in the legacy event format
        @param values: additional event values in the legacy event format

        @return: a legacy event
        r\   r   rQ   r   )r   Úgetr   ZILogContextÚcopyÚupdater   )r   r\   rh   r0   r   r   r   ÚlegacyEvent  s    	

z%PublishToNewObserverTests.legacyEventc                 C   s*   t | j|  ¡ tjƒ |  t| jƒd¡ dS )z6
        The observer is called exactly once.
        r4   N)r   r    r   r   rc   r,   r5   r3   r"   r   r   r   Útest_observed/  s      ÿz'PublishToNewObserverTests.test_observedc                 C   s8   t | j|  ¡ tjƒ |  | jd d | jd d ¡ dS )zc
        The old-style C{"time"} key is copied to the new-style C{"log_time"}
        key.
        r   r7   r   N©r   r    r   r   rc   r,   r3   r"   r   r   r   rI   8  s      ÿz#PublishToNewObserverTests.test_timec                 C   sL   t tdœdd„}|  dd¡}||ƒ}t| j||ƒ |  t| jd ƒ|¡ dS )z—
        A published old-style event should format as text in the same way as
        the given C{textFromEventDict} callable would format it.
        r/   c                 S   s   d  td  | d ¡ƒ¡S )NÚ ú r\   )ÚjoinÚreversed)r0   r   r   r   rc   H  s    zAPublishToNewObserverTests.test_message.<locals>.textFromEventDictzHello,zworld!r   N)r   r)   r   r   r    r,   r   r3   )r   rc   r0   Útextr   r   r   r]   B  s
    z&PublishToNewObserverTests.test_messagec                 C   s0   t | j|  ¡ tjƒ |  | jd d tj¡ dS )zL
        Published event should have log level of L{LogLevel.info}.
        r   r:   N)	r   r    r   r   rc   r,   r3   r   r<   r"   r   r   r   Útest_defaultLogLevelQ  s      ÿz.PublishToNewObserverTests.test_defaultLogLevelc                 C   s4   t | j| jdd�tjƒ |  | jd d tj¡ dS )z“
        If C{"isError"} is set to C{1} (true) on the legacy event, the
        C{"log_level"} key should get set to L{LogLevel.critical}.
        r4   rt   r   r:   N)	r   r    r   r   rc   r,   r3   r   rT   r"   r   r   r   Útest_isErrorZ  s     
 ÿz&PublishToNewObserverTests.test_isErrorc                 C   s6   t | j| jtjd�tjƒ |  | jd d t	j
¡ dS )zæ
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using a predefined (L{int}) constant, the new-style
        C{"log_level"} key should get set to the corresponding log level.
        rZ   r   r:   N)r   r    r   Ú
py_loggingrY   r   rc   r,   r3   r   rR   r"   r   r   r   Útest_stdlibLogLeveld  s    ýz-PublishToNewObserverTests.test_stdlibLogLevelc                 C   s4   t | j| jdd�tjƒ |  | jd d tj¡ dS )zÖ
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using a string value, the new-style C{"log_level"} key should
        get set to the corresponding log level.
        rY   rZ   r   r:   N)	r   r    r   r   rc   r,   r3   r   rR   r"   r   r   r   Útest_stdlibLogLevelWithStringq  s    
ýz7PublishToNewObserverTests.test_stdlibLogLevelWithStringc                 C   s.   t | j| jdd�tjƒ |  d| jd ¡ dS )z½
        If the old-style C{"logLevel"} key is set to a standard library logging
        level, using an unknown value, the new-style C{"log_level"} key should
        not get set.
        zFoo!!!!!rZ   r:   r   N)r   r    r   r   rc   rW   r3   r"   r   r   r   Útest_stdlibLogLevelWithGarbage~  s    
ýz8PublishToNewObserverTests.test_stdlibLogLevelWithGarbagec                 C   s.   t | j|  ¡ tjƒ |  | jd d d¡ dS )z‹
        Published event should have a namespace of C{"log_legacy"} to indicate
        that it was forwarded from legacy logging.
        r   Zlog_namespaceZ
log_legacyNr�   r"   r   r   r   Útest_defaultNamespace‹  s      ÿz/PublishToNewObserverTests.test_defaultNamespacec                 C   s8   t | j|  ¡ tjƒ |  | jd d | jd d ¡ dS )zg
        The old-style C{"system"} key is copied to the new-style
        C{"log_system"} key.
        r   r8   rK   Nr�   r"   r   r   r   rN   •  s      ÿz%PublishToNewObserverTests.test_system)r&   r'   r(   rx   r{   r)   ry   r   r*   r   r€   rI   r]   r‡   rˆ   rŠ   r‹   rŒ   r�   rN   r   r   r   r   rz     s   	
	

rz   )"rx   Úloggingr‰   r   Útypingr   r   Zzope.interfacer   Zzope.interface.exceptionsr   Zzope.interface.verifyr   Ztwisted.pythonr   r	   r   Ztwisted.python.failurer
   Ztwisted.trialr   Ú_formatr   Z_interfacesr   r   Z_legacyr   r   Z_levelsr   ZTestCaser   rz   r   r   r   r   Ú<module>   s     }