Passed
Push — main ( aae69a...152cdf )
by Jochen
02:03
created

tests.test_message_formatting   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B test_format_message() 0 48 1
1
"""
2
:Copyright: 2007-2021 Jochen Kupperschmidt
3
:License: MIT, see LICENSE for details.
4
"""
5
6
from datetime import datetime
7
8
import pytest
9
from syslogmp import Facility, Message, Severity
10
11
from syslog2irc.formatting import format_message
12
13
14
@pytest.mark.parametrize(
15
    'source_address, facility, severity, timestamp, hostname, message, expected',
16
    [
17
        (
18
            ('10.10.0.2', 2234),
19
            Facility.clock9,
20
            Severity.warning,
21
            datetime(2013, 7, 8, 0, 12, 55),
22
            '10.10.0.2',
23
            b'Tick, tack, watch the clock!',
24
            '10.10.0.2:2234 [2013-07-08 00:12:55] (10.10.0.2) [warning]: Tick, tack, watch the clock!',
25
        ),
26
        (
27
            ('10.10.0.3', 3234),
28
            Facility.ntp,
29
            Severity.debug,
30
            datetime(2021, 5, 4, 10, 0, 27),
31
            'ntp.local',
32
            b'What time is it?',
33
            '10.10.0.3:3234 [2021-05-04 10:00:27] (ntp.local) [debug]: What time is it?',
34
        ),
35
        (
36
            ('10.10.0.4', 4234),
37
            Facility.kernel,
38
            Severity.emergency,
39
            datetime(2008, 10, 18, 17, 34, 7),
40
            'mainframe',
41
            b'WTF? S.O.S.!',
42
            '10.10.0.4:4234 [2008-10-18 17:34:07] (mainframe) [emergency]: WTF? S.O.S.!',
43
        ),
44
        (
45
            # Strip leading and trailing newlines.
46
            ('10.10.0.5', 5234),
47
            Facility.user,
48
            Severity.informational,
49
            datetime(2021, 5, 4, 10, 1, 4),
50
            '10.10.0.5',
51
            b'\nIgnore my surroundings.\n',
52
            '10.10.0.5:5234 [2021-05-04 10:01:04] (10.10.0.5) [informational]: Ignore my surroundings.',
53
        ),
54
    ],
55
)
56
def test_format_message(
57
    source_address, facility, severity, timestamp, hostname, message, expected
58
):
59
    """Test string representation of a syslog message."""
60
    message = Message(facility, severity, timestamp, hostname, message)
61
    assert format_message(source_address, message) == expected
62