First commit
This commit is contained in:
38
part2-logstats/tests/test_cli.py
Normal file
38
part2-logstats/tests/test_cli.py
Normal file
@ -0,0 +1,38 @@
|
||||
from logstats.cli import main
|
||||
|
||||
|
||||
def run(argv, capsys):
|
||||
main(argv)
|
||||
return capsys.readouterr().out.strip().splitlines()
|
||||
|
||||
|
||||
def test_reports_every_level_present_in_file(tmp_path, capsys):
|
||||
log = tmp_path / "a.log"
|
||||
log.write_text(
|
||||
"2024-03-01 09:00:00 INFO started\n"
|
||||
"2024-03-01 09:00:01 ERROR boom\n"
|
||||
"2024-03-01 09:00:02 WARNING low disk\n"
|
||||
"2024-03-01 09:00:03 DEBUG state dump\n"
|
||||
)
|
||||
out = run([str(log)], capsys)
|
||||
assert len(out) == 4
|
||||
|
||||
|
||||
def test_same_file_gives_same_result_on_every_run(tmp_path, capsys):
|
||||
log = tmp_path / "a.log"
|
||||
log.write_text(
|
||||
"2024-03-01 09:00:00 INFO started\n"
|
||||
"2024-03-01 09:00:01 ERROR boom\n"
|
||||
)
|
||||
first = run([str(log)], capsys)
|
||||
second = run([str(log)], capsys)
|
||||
assert first == second
|
||||
|
||||
|
||||
def test_aggregates_across_multiple_files(tmp_path, capsys):
|
||||
a = tmp_path / "a.log"
|
||||
b = tmp_path / "b.log"
|
||||
a.write_text("2024-03-01 09:00:00 INFO one\n")
|
||||
b.write_text("2024-03-01 09:00:01 INFO two\n")
|
||||
out = run([str(a), str(b)], capsys)
|
||||
assert out == ["INFO 2"]
|
||||
35
part2-logstats/tests/test_parser.py
Normal file
35
part2-logstats/tests/test_parser.py
Normal file
@ -0,0 +1,35 @@
|
||||
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)]
|
||||
Reference in New Issue
Block a user