from logstats.parser import count_levels, top_levels def test_count_levels_basic(): lines = [ "2024-03-01 09:00:00 INFO service started", "2024-03-01 09:00:04 ERROR payment gateway timeout", ] assert count_levels(lines) == {"INFO": 1, "ERROR": 1} def test_count_levels_is_fresh_between_calls(): count_levels(["2024-03-01 09:00:00 INFO service started"]) second = count_levels(["2024-03-01 09:00:04 ERROR payment gateway timeout"]) assert second == {"ERROR": 1} def test_level_is_taken_from_level_field_not_message(): lines = ["2024-03-01 09:00:06 INFO user saw ERROR dialog on checkout"] assert count_levels(lines) == {"INFO": 1} def test_ignores_lines_without_a_level_field(): lines = ["", "-- rotated --", "2024-03-01 09:00:00"] assert count_levels(lines) == {} def test_top_levels_returns_n_entries(): counts = {"INFO": 5, "ERROR": 3, "DEBUG": 1} assert top_levels(counts, 2) == [("INFO", 5), ("ERROR", 3)] def test_top_levels_breaks_ties_alphabetically(): counts = {"WARNING": 2, "ERROR": 2, "INFO": 7} assert top_levels(counts, 3) == [("INFO", 7), ("ERROR", 2), ("WARNING", 2)]