Skip to content

Commit

Permalink
Decoupling tests from analyze_log_stream (#67)
Browse files Browse the repository at this point in the history
* fixing error counter test

* fixing duplicate counter test

* fixing warning counter test

* un-commenting analyze_log_stream test
  • Loading branch information
kitrady authored Jul 28, 2024
1 parent cd151a2 commit 34acd28
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/alogamous/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def analyze_log_stream(analyzers, in_stream, out_stream):
analyzer.read_log_line(line.rstrip())
for analyzer in analyzers:
analyzer.report(out_stream)
# out_stream.write("\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n")
out_stream.write("\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n")
24 changes: 13 additions & 11 deletions tests/analyze_log_stream_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io

from alogamous import analyzer


Expand All @@ -17,14 +19,14 @@ def report(self, out_stream):
pass


# def test_analyze_log_stream():
# in_stream = io.StringIO("""line1
# line2
# line3""")
# out_stream = io.StringIO()
#
# analyzer.analyze_log_stream([TestAnalyzer1(), TestAnalyzer2()], in_stream, out_stream)
# assert (
# out_stream.getvalue()
# == "\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n"
# )
def test_analyze_log_stream():
in_stream = io.StringIO("""line1
line2
line3""")
out_stream = io.StringIO()

analyzer.analyze_log_stream([TestAnalyzer1(), TestAnalyzer2()], in_stream, out_stream)
assert (
out_stream.getvalue()
== "\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n\n>>>>>>>>>> a report has been reported <<<<<<<<<<\n"
)
14 changes: 11 additions & 3 deletions tests/flag_duplicate_log_messages_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import io

from alogamous import analyzer, flag_duplicate_log_messages
from alogamous import flag_duplicate_log_messages


def test_flag_duplicate_log_messages():
flagger = flag_duplicate_log_messages.FlagDuplicateLogMessages()
in_stream = io.StringIO("""Date - root - INFO - log message 1
Date - root - WARNING - log message 2
Date - root - WARNING - log message 1""")
out_stream = io.StringIO()
analyzer.analyze_log_stream([flag_duplicate_log_messages.FlagDuplicateLogMessages()], in_stream, out_stream)
for line in in_stream:
flagger.read_log_line(line)
flagger.report(out_stream)
assert out_stream.getvalue() == """Duplicate Log Messages:\nlog message 1\n"""


def test_flag_duplicate_log_messages_no_duplicates():
flagger = flag_duplicate_log_messages.FlagDuplicateLogMessages()
in_stream = io.StringIO("""Date - root - INFO - log message 1
Date - root - WARNING - log message 2
Date - root - WARNING - log message 3""")
out_stream = io.StringIO()
analyzer.analyze_log_stream([flag_duplicate_log_messages.FlagDuplicateLogMessages()], in_stream, out_stream)
for line in in_stream:
flagger.read_log_line(line)
flagger.report(out_stream)
assert out_stream.getvalue() == """No duplicate log messages\n"""
29 changes: 17 additions & 12 deletions tests/test_error_counter.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import io

from alogamous import analyzer, error_counter_analyzer
from alogamous import error_counter_analyzer


def test_error_counter():
in_stream = io.StringIO("""line1
blahblahERRORblah
oh_no_another_ERROR""")
counter = error_counter_analyzer.ErrorCounterAnalyzer()
in_stream = io.StringIO(
"""2024-06-20 17:16:03,660 - root - ERROR - Caught exception N/A.
2024-06-20 17:16:03,660 - root - ERROR - Caught exception N/A. Message: Unclosed connector NoneType: None"""
)
out_stream = io.StringIO()
analyzer.analyze_log_stream([error_counter_analyzer.ErrorCounterAnalyzer()], in_stream, out_stream)
for line in in_stream:
counter.read_log_line(line)
counter.report(out_stream)
assert out_stream.getvalue().strip() == "Number of error lines: 2"


def test_no_errors():
in_stream = io.StringIO("""good
nice
very good
""")
counter = error_counter_analyzer.ErrorCounterAnalyzer()
in_stream = io.StringIO("""2024-06-20 17:17:04,278 - root - INFO - Updating prices
2024-06-20 17:24:34,091 - root - INFO - Closing client connection.""")
out_stream = io.StringIO()
analyzer.analyze_log_stream([error_counter_analyzer.ErrorCounterAnalyzer()], in_stream, out_stream)
for line in in_stream:
counter.read_log_line(line)
counter.report(out_stream)
assert out_stream.getvalue().strip() == "Number of error lines: 0"


def test_no_input_lines():
in_stream = io.StringIO("")
counter = error_counter_analyzer.ErrorCounterAnalyzer()
out_stream = io.StringIO()
analyzer.analyze_log_stream([error_counter_analyzer.ErrorCounterAnalyzer()], in_stream, out_stream)
counter.report(out_stream)
assert out_stream.getvalue().strip() == "Number of error lines: 0"
7 changes: 5 additions & 2 deletions tests/warning_analyzer_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import io

from alogamous import analyzer, warning_analyzer
from alogamous import warning_analyzer


def test_warning_count():
counter = warning_analyzer.WarningAnalyzer()
in_stream = io.StringIO("""2024-06-20 11:00:18,185 - root - INFO - Kafka reading from start of day 2024-06-20 05:00:00+00:00 on topic internal from kafka.servers:9092
2024-06-20 11:00:19,328 - root - INFO - Kafka source starting for topic internal at current offset 7924032 end offset 7928950 on servers kafka.servers:9092
2024-06-20 11:00:22,329 - root - INFO - Kafka topic internal is caught up at offset 7928949
Expand All @@ -13,5 +14,7 @@ def test_warning_count():
2024-06-20 11:40:43,527 - root - WARNING - instrument not found for sedol BYP321337
2024-06-20 17:25:08,029 - root - ERROR - Exception in message handler <bound method TrackingService.method of <app.tracking_service.TrackingService object at 0x7feba0d0>> TrackingService.on_order_change() missing 1 required positional argument: 'order_identifier'""")
out_stream = io.StringIO()
analyzer.analyze_log_stream([warning_analyzer.WarningAnalyzer()], in_stream, out_stream)
for line in in_stream:
counter.read_log_line(line)
counter.report(out_stream)
assert out_stream.getvalue() == "\n2 Warnings were detected."

0 comments on commit 34acd28

Please sign in to comment.