Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_logger.py
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | assert list(logger.flatten([1, 2, ['a', ('x', 1)]])) == [ | ||||
('2_1_1', 1), | ('2_1_1', 1), | ||||
] | ] | ||||
def test_flatten_dict(): | def test_flatten_dict(): | ||||
assert list(logger.flatten({})) == [] | assert list(logger.flatten({})) == [] | ||||
assert list(logger.flatten({'a': 1})) == [('a', 1)] | assert list(logger.flatten({'a': 1})) == [('a', 1)] | ||||
assert sorted(logger.flatten({'a': 1, | assert sorted(logger.flatten({'a': 1, 'b': (2, 3), 'c': {'d': 4, 'e': 'f'}})) == [ | ||||
'b': (2, 3,), | |||||
'c': {'d': 4, 'e': 'f'}})) == [ | |||||
('a', 1), | ('a', 1), | ||||
('b_0', 2), | ('b_0', 2), | ||||
('b_1', 3), | ('b_1', 3), | ||||
('c_d', 4), | ('c_d', 4), | ||||
('c_e', 'f'), | ('c_e', 'f'), | ||||
] | ] | ||||
def test_flatten_dict_binary_keys(): | def test_flatten_dict_binary_keys(): | ||||
d = {b"a": "a"} | d = {b"a": "a"} | ||||
str_d = str(d) | str_d = str(d) | ||||
assert list(logger.flatten(d)) == [("", str_d)] | assert list(logger.flatten(d)) == [("", str_d)] | ||||
assert list(logger.flatten({"a": d})) == [("a", str_d)] | assert list(logger.flatten({"a": d})) == [("a", str_d)] | ||||
assert list(logger.flatten({"a": [d, d]})) == [ | assert list(logger.flatten({"a": [d, d]})) == [("a_0", str_d), ("a_1", str_d)] | ||||
("a_0", str_d), ("a_1", str_d) | |||||
] | |||||
def test_stringify(): | def test_stringify(): | ||||
assert logger.stringify(None) == 'None' | assert logger.stringify(None) == 'None' | ||||
assert logger.stringify(123) == '123' | assert logger.stringify(123) == '123' | ||||
assert logger.stringify('abc') == 'abc' | assert logger.stringify('abc') == 'abc' | ||||
date = datetime(2019, 9, 1, 16, 32) | date = datetime(2019, 9, 1, 16, 32) | ||||
Show All 13 Lines | def test_journal_handler(send): | ||||
send.assert_called_with( | send.assert_called_with( | ||||
'hello world', | 'hello world', | ||||
CODE_FILE=__file__, | CODE_FILE=__file__, | ||||
CODE_FUNC='test_journal_handler', | CODE_FUNC='test_journal_handler', | ||||
CODE_LINE=ln, | CODE_LINE=ln, | ||||
LOGGER='test_logger', | LOGGER='test_logger', | ||||
PRIORITY='6', | PRIORITY='6', | ||||
THREAD_NAME='MainThread') | THREAD_NAME='MainThread', | ||||
) | |||||
@patch('swh.core.logger.send') | @patch('swh.core.logger.send') | ||||
def test_journal_handler_w_data(send): | def test_journal_handler_w_data(send): | ||||
log = logging.getLogger('test_logger') | log = logging.getLogger('test_logger') | ||||
log.addHandler(logger.JournalHandler()) | log.addHandler(logger.JournalHandler()) | ||||
log.setLevel(logging.DEBUG) | log.setLevel(logging.DEBUG) | ||||
_, ln = log.debug('something cool %s', ['with', {'extra': 'data'}]), lineno() # noqa | _, ln = ( | ||||
log.debug('something cool %s', ['with', {'extra': 'data'}]), | |||||
lineno() - 1, | |||||
) # noqa | |||||
send.assert_called_with( | send.assert_called_with( | ||||
"something cool ['with', {'extra': 'data'}]", | "something cool ['with', {'extra': 'data'}]", | ||||
CODE_FILE=__file__, | CODE_FILE=__file__, | ||||
CODE_FUNC='test_journal_handler_w_data', | CODE_FUNC='test_journal_handler_w_data', | ||||
CODE_LINE=ln, | CODE_LINE=ln, | ||||
LOGGER='test_logger', | LOGGER='test_logger', | ||||
PRIORITY='7', | PRIORITY='7', | ||||
THREAD_NAME='MainThread', | THREAD_NAME='MainThread', | ||||
SWH_LOGGING_ARGS_0_0='with', | SWH_LOGGING_ARGS_0_0='with', | ||||
SWH_LOGGING_ARGS_0_1_EXTRA='data' | SWH_LOGGING_ARGS_0_1_EXTRA='data', | ||||
) | ) |