o
    ¯b	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                   @   s  e Zd ZdZd4dd„Zd4dd„Zdedefd	d
„Zdedefdd„Zd4dd„Z	d4dd„Z
d4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd „Zd4d!d"„Zd4d#d$„Zd4d%d&„Zd4d'd(„Zd)edefd*d+„Zd4d,d-„Zd4d.d/„Zd4d0d1„Zd4d2d3„ZdS )5ÚLegacyLogObserverWrapperTestsz0
    Tests for L{LegacyLogObserverWrapper}.
    ÚreturnNc              
   C   sZ   t tjdd„ ƒ}t|ƒ}ztt|ƒ W dS  ty, } z|  |¡ W Y d}~dS d}~ww )zD
        L{LegacyLogObserverWrapper} is an L{ILogObserver}.
        c                 S   ó   d S ©N© ©Úer   r   úA/usr/lib/python3/dist-packages/twisted/logger/test/test_legacy.pyÚ<lambda>"   s    z>LegacyLogObserverWrapperTests.test_interface.<locals>.<lambda>N)r   Ú	legacyLogr   r   r   r   Ú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defdd„Zdejddf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   ó   zHLegacyLogObserverWrapperTests.test_repr.<locals>.LegacyObserver.__repr__Ú	eventDictNc                 S   r   r   r   )r   r%   r   r   r   Ú__call__3   r$   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Úeventc                    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   s   
 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ÚitemsÚ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)rC   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   rC   r=   r-   ©r   Ú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   NrH   rI   r   r   r   Útest_timeAlreadySets   s   z1LegacyLogObserverWrapperTests.test_timeAlreadySetc                 C   ó$   |   tdd�¡}|  |d d¡ dS )zg
        The new-style C{"log_system"} key is copied to the old-style
        C{"system"} key.
        rE   )r8   ÚsystemN©rC   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.
        rE   rF   )r8   rN   rN   NrO   rP   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:   rN   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)rC   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)rC   r=   r   r<   ÚassertNotInrP   r   r   r   Útest_pythonLogLevelNotSet¡   s   	z7LegacyLogObserverWrapperTests.test_pythonLogLevelNotSetc                 C   rM   )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©rY   rY   NrO   rP   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   NrO   rP   r   r   r   Útest_message»   ó   z*LegacyLogObserverWrapperTests.test_messagec                 C   rM   )zV
        The old-style C{"message"} key is not modified if it already exists.
        rD   )r_   r_   NrO   rP   r   r   r   Útest_messageAlreadySetÃ   s   z4LegacyLogObserverWrapperTests.test_messageAlreadySetc                 C   ó(   |   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_formatÚwhoúHello, world!N©rC   r=   r-   r   ÚtextFromEventDictrP   r   r   r   Útest_formatÊ   ó   z)LegacyLogObserverWrapperTests.test_formatc                 C   rc   )zo
        Using the message key, which is special in old-style, works for
        new-style formatting.
        zHello, {message}!rd   )re   r_   rg   Nrh   rP   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!)re   ÚformatNrh   rP   r   r   r   Útest_formatAlreadySetÜ   rk   z3LegacyLogObserverWrapperTests.test_formatAlreadySetÚvaluesc                 K   s&   t tdƒƒ}|  td|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_failurere   Nr   )r
   ÚRuntimeErrorrC   r=   )r   ro   Ú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.
        rs   rq   rT   Úwhyrp   N)rt   ÚassertIsÚ
assertTruer-   rP   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!)rs   rs   N)r
   rr   rt   rv   )r   rs   r0   r   r   r   Útest_failureAlreadySetû   s   z4LegacyLogObserverWrapperTests.test_failureAlreadySetc                 C   ó    | 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   ©rT   rT   N©rt   r-   rP   r   r   r   Útest_isErrorAlreadySet  ra   z4LegacyLogObserverWrapperTests.test_isErrorAlreadySetc                 C   r{   )ry   Úblah)ru   ru   Nr}   rP   r   r   r   Útest_whyAlreadySet  ra   z0LegacyLogObserverWrapperTests.test_whyAlreadySet©r   N)r'   r(   r)   Ú__doc__r!   r/   r   r6   rC   rG   rK   rL   rQ   rR   rS   rX   r[   r^   r`   rb   rj   rl   rn   Úobjectrt   rx   rz   r~   r€   r   r   r   r   r      s2    




	
	















	r   c                   @   s–   e Zd ZdZddd„Zdededejfdd	„Z	dd
d„Z
ddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„Zddd„ZdS )ÚPublishToNewObserverTestsz,
    Tests for L{publishToNewObserver}.
    r   Nc                 C   s   g | _ tt| j jƒ| _d S r   )r3   r   r   r1   r    r"   r   r   r   ÚsetUp  s   zPublishToNewObserverTests.setUpr_   ro   c                 O   sD   t  tj¡pi  ¡ }| |¡ ||d< tƒ |d< d|v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   rT   r   )r   Úgetr   ÚILogContextÚcopyÚupdater   )r   r_   ro   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   ri   r-   r5   r3   r"   r   r   r   Útest_observed/  s   ÿz'PublishToNewObserverTests.test_observedc                 C   ó8   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   ri   r-   r3   r"   r   r   r   rK   8  ó   ÿ$z#PublishToNewObserverTests.test_timec                 C   sN   dt dtf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.
        r0   r   c                 S   s   d  td  | d ¡ƒ¡S )NÚ ú r_   )ÚjoinÚreversed)r0   r   r   r   ri   H  s   zAPublishToNewObserverTests.test_message.<locals>.textFromEventDictzHello,zworld!r   N)r   r*   rŠ   r   r    r-   r   r3   )r   ri   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   ri   r-   r3   r   r<   r"   r   r   r   Útest_defaultLogLevelQ  s   ÿz.PublishToNewObserverTests.test_defaultLogLevelc                 C   ó4   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   r|   r   r:   N)	r   r    rŠ   r   ri   r-   r3   r   rW   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.
        r]   r   r:   N)r   r    rŠ   Ú
py_loggingr\   r   ri   r-   r3   r   rU   r"   r   r   r   Útest_stdlibLogLeveld  s   ýz-PublishToNewObserverTests.test_stdlibLogLevelc                 C   r•   )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.
        r\   r]   r   r:   N)	r   r    rŠ   r   ri   r-   r3   r   rU   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!!!!!r]   r:   r   N)r   r    rŠ   r   ri   rZ   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   Úlog_namespaceÚ
log_legacyNr�   r"   r   r   r   Útest_defaultNamespace‹  s   ÿz/PublishToNewObserverTests.test_defaultNamespacec                 C   rŒ   )zg
        The old-style C{"system"} key is copied to the new-style
        C{"log_system"} key.
        r   r8   rN   Nr�   r"   r   r   r   rQ   •  rŽ   z%PublishToNewObserverTests.test_systemr�   )r'   r(   r)   r‚   r…   r*   rƒ   r   r+   rŠ   r‹   rK   r`   r”   r–   r˜   r™   rš   r�   rQ   r   r   r   r   r„     s    


	



	





r„   )"r‚   Úloggingr—   r   Útypingr   r   Úzope.interfacer   Úzope.interface.exceptionsr   Úzope.interface.verifyr   Útwisted.pythonr   r	   r   Útwisted.python.failurer
   Útwisted.trialr   Ú_formatr   Ú_interfacesr   r   Ú_legacyr   r   Ú_levelsr   ÚTestCaser   r„   r   r   r   r   Ú<module>   s"    }