# :coding: utf-8
# :copyright: Copyright (c) 2013 <NAME>
# :license: See LICENSE.txt.

from email.message import Message

import pytest

from sawmill.log import Log
from sawmill.formatter.email import Email


def test_format():
    '''Test formatted log result is as expected.'''
    log = Log(
        timestamp=123456789,
        name='test.log',
        message='A message',
        level='info'
    )
    template = Email('Test', '<EMAIL>', '<EMAIL>')
    data = template.format([log])
    assert len(data) == 1

    datum = data[0]
    assert isinstance(datum, Message)
    assert datum['Subject'] == 'Test'
    assert datum['From'] == '<EMAIL>'
    assert datum['To'] == '<EMAIL>'
    assert datum.is_multipart() is True

    html = datum.get_payload(1)
    assert html.get_payload() == '''
<html>
    <body>
        <h1>Logs</h1>
        <span class='info'>
            123456789:test.log:A message
        </span><br/>
    </body>
</html>
'''

    text = datum.get_payload(0)
    assert text.get_payload() == '''# Logs

123456789:test.log:A message  

'''


@pytest.mark.parametrize(('key', 'value'), [
    ('subject', 'A Test'),
    ('sender', '<EMAIL>'),
    ('recipients', '<EMAIL>')
])
def test_callable(key, value):
    '''Test callable'''
    kwargs = {
        'subject': 'Test',
        'sender': '<EMAIL>',
        'recipients': '<EMAIL>'
    }
    kwargs[key] = lambda logs: value

    template = Email(**kwargs)
    log = Log()
    data = template.format([log])
    assert len(data) == 1

    datum = data[0]
    mapping = {
        'subject': 'Subject',
        'sender': 'From',
        'recipients': 'To'
    }
    assert datum[mapping[key]] == value


# :coding: utf-8
# :copyright: Copyright (c) 2013 <NAME>
# :license: See LICENSE.txt.

from email.message import Message

import pytest

from sawmill.log import Log
from sawmill.formatter.email import Email


def test_format():
    '''Test formatted log result is as expected.'''
    log = Log(
        timestamp=123456789,
        name='test.log',
        message='A message',
        level='info'
    )
    template = Email('Test', '<EMAIL>', '<EMAIL>')
    data = template.format([log])
    assert len(data) == 1

    datum = data[0]
    assert isinstance(datum, Message)
    assert datum['Subject'] == 'Test'
    assert datum['From'] == '<EMAIL>'
    assert datum['To'] == '<EMAIL>'
    assert datum.is_multipart() is True

    html = datum.get_payload(1)
    assert html.get_payload() == '''
<html>
    <body>
        <h1>Logs</h1>
        <span class='info'>
            123456789:test.log:A message
        </span><br/>
    </body>
</html>
'''

    text = datum.get_payload(0)
    assert text.get_payload() == '''# Logs

123456789:test.log:A message  

'''


@pytest.mark.parametrize(('key', 'value'), [
    ('subject', 'A Test'),
    ('sender', '<EMAIL>'),
    ('recipients', '<EMAIL>')
])
def test_callable(key, value):
    '''Test callable'''
    kwargs = {
        'subject': 'Test',
        'sender': '<EMAIL>',
        'recipients': '<EMAIL>'
    }
    kwargs[key] = lambda logs: value

    template = Email(**kwargs)
    log = Log()
    data = template.format([log])
    assert len(data) == 1

    datum = data[0]
    mapping = {
        'subject': 'Subject',
        'sender': 'From',
        'recipients': 'To'
    }
    assert datum[mapping[key]] == value


# :coding: utf-8 # :copyright: Copyright (c) 2013 <NAME> # :license: See LICENSE.txt. Test formatted log result is as expected. <html>     <body>         <h1>Logs</h1>         <span class='info'>             123456789:test.log:A message         </span><br/>     </body> </html> # Logs  123456789:test.log:A message Test callable