Skip to content

Commit cbebdb4

Browse files
committed
test: add tests for logfmt parser
1 parent 4a12f10 commit cbebdb4

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

tests/test_formatter.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from logfmter.formatter import Logfmter
10+
from logfmter.formatter import Logfmter, parse_logfmt
1111

1212
STRING_ESCAPE_RULES = [
1313
# If the string contains a space, then it must be quoted.
@@ -89,6 +89,96 @@ def test_format_params(value, expected):
8989
assert Logfmter.format_params(value) == expected
9090

9191

92+
@pytest.mark.parametrize(
93+
"value",
94+
[
95+
{"at": "INFO"},
96+
{"levelname": "INFO", "a": "1"},
97+
{"at": "INFO", "msg": "test", "a": "1"},
98+
{"at": "INFO", "msg": "="},
99+
{"at": "INFO", "msg": "alpha", "exc_info": 'exc\n"info"\ntb'},
100+
{"a": " "},
101+
{"_": " "},
102+
{"a": '"'},
103+
{"a": "\\"},
104+
{"a": "\\", "b": "c"},
105+
{"a": "\n"},
106+
{"a": "", "b": "c"},
107+
{"a": "1", "b": "2"},
108+
{"a": "1.2", "b": "2"},
109+
{"a": "1.2.3", "b": "0.2", "c": "1.0", "d": ".", "e": "1..2"},
110+
{"foo.bar": "baz", "foo.blah": "blub"},
111+
],
112+
)
113+
def test_parse_logfmt_round_trip(value):
114+
assert parse_logfmt(Logfmter.format_params(value)) == value
115+
116+
117+
@pytest.mark.parametrize(
118+
"value",
119+
[
120+
{"a": 1},
121+
{"a": 1, "b": 2},
122+
{"a": 1.2, "b": 2},
123+
{"a": "1.2.3", "b": 0.2, "c": 1.0, "d": ".", "e": "1..2"},
124+
],
125+
)
126+
def test_parse_logfmt_numeric_round_trip(value):
127+
assert parse_logfmt(Logfmter.format_params(value), convert_numeric=True) == value
128+
129+
130+
@pytest.mark.parametrize(
131+
"value,expected,kwargs",
132+
[
133+
("at=INFO", {"at": "INFO"}, {}),
134+
('at="INFO"', {"at": "INFO"}, {"reverse": False}),
135+
(
136+
"at=INFO a=1",
137+
{"levelname": "INFO", "a": "1"},
138+
{"aliases": {"at": "levelname"}, "reverse": False},
139+
),
140+
("at=INFO msg=test a=1", {"at": "INFO", "msg": "test", "a": "1"}, {}),
141+
('at=INFO msg="="', {"at": "INFO", "msg": "="}, {}),
142+
(
143+
"at=INFO first_name=josh",
144+
{"at": "INFO", "first name": "josh"},
145+
{"aliases": {"first_name": "first name"}, "reverse": False},
146+
),
147+
(
148+
r'at=INFO msg=alpha exc_info="exc\n\"info\"\ntb"',
149+
{"at": "INFO", "msg": "alpha", "exc_info": 'exc\n"info"\ntb'},
150+
{},
151+
),
152+
('a=" "', {"a": " "}, {}),
153+
('_=" "', {"_": " "}, {}),
154+
('a="\\""', {"a": '"'}, {}),
155+
(r'a="\\"', {"a": "\\"}, {"reverse": False}),
156+
(r'a="\n"', {"a": "\n"}, {}),
157+
(r"a=\n", {"a": "n"}, {"reverse": False}),
158+
("a= b=c", {"a": "", "b": "c"}, {}),
159+
("a b=c", {"a": "", "b": "c"}, {"reverse": False}),
160+
("a=1", {"a": 1}, {"convert_numeric": True}),
161+
("a=1 b=2", {"a": "1", "b": "2"}, {}),
162+
("a=1 b=2", {"a": 1, "b": 2}, {"convert_numeric": True}),
163+
("a=1.2 b=2", {"a": "1.2", "b": "2"}, {}),
164+
("a=1.2 b=2", {"a": 1.2, "b": 2}, {"convert_numeric": True}),
165+
(
166+
"a=1.2.3 b=.2 c=1. d=. e=1..2",
167+
{"a": "1.2.3", "b": 0.2, "c": 1.0, "d": ".", "e": "1..2"},
168+
{"convert_numeric": True, "reverse": False},
169+
),
170+
("foo.bar=baz foo.blah=blub", {"foo.bar": "baz", "foo.blah": "blub"}, {}),
171+
("\\n=foo", {"n": "foo"}, {"reverse": False}),
172+
("a=' '", {"a": " "}, {"reverse": False}),
173+
],
174+
)
175+
def test_parse_logfmt(value, kwargs, expected):
176+
reverse = kwargs.pop("reverse", True)
177+
assert parse_logfmt(value, **kwargs) == expected
178+
if reverse:
179+
assert Logfmter.format_params(expected) == value
180+
181+
92182
@pytest.mark.parametrize(
93183
"value,expected",
94184
[

0 commit comments

Comments
 (0)