Skip to content

Commit

Permalink
refactor exceptions test module
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 23, 2023
1 parent 71d9835 commit dc9aa1f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
38 changes: 38 additions & 0 deletions tests/unit/exceptions/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from logging import getLogger
from typing import Callable

import pytest

from logprep.factory import Factory
from logprep.processor.base.rule import Rule


class ExceptionBaseTest:
exception: Callable
"""Class of exception to test"""

error_message: str
"""regex string to match the error message"""

counted_metric_name: str
"""name of the metric that should be counted"""

def setup_method(self):
self.processor = Factory.create(
{"my_dissector": {"type": "dissector", "specific_rules": [], "generic_rules": []}},
getLogger(),
)
self.rule = Rule._create_from_dict({"filter": "message", "rule": {}})
self.event = {"message": "test_event"}
self.exception_args = ("the error message", self.rule, self.event)

def test_error_message(self):
with pytest.raises(self.exception, match=self.error_message):
raise self.exception(*self.exception_args)

def test_metrics_counts(self):
setattr(self.rule.metrics, self.counted_metric_name, 0)
self.rule.metrics.number_of_warnings = 0
with pytest.raises(self.exception):
raise self.exception(*self.exception_args)
assert getattr(self.rule.metrics, self.counted_metric_name) == 1
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,14 @@
# pylint: disable=attribute-defined-outside-init
# pylint: disable=protected-access
# pylint: disable=line-too-long
from logging import getLogger
from typing import Callable

import pytest

from logprep.factory import Factory
from logprep.processor.base.exceptions import (
FieldExistsWarning,
ProcessingCriticalError,
ProcessingError,
ProcessingWarning,
)
from logprep.processor.base.rule import Rule


class ExceptionBaseTest:
exception: Callable
"""Class of exception to test"""

error_message: str
"""regex string to match the error message"""

counted_metric_name: str
"""name of the metric that should be counted"""

def setup_method(self):
self.processor = Factory.create(
{"my_dissector": {"type": "dissector", "specific_rules": [], "generic_rules": []}},
getLogger(),
)
self.rule = Rule._create_from_dict({"filter": "message", "rule": {}})
self.event = {"message": "test_event"}
self.exception_args = ("the error message", self.rule, self.event)

def test_error_message(self):
with pytest.raises(self.exception, match=self.error_message):
raise self.exception(*self.exception_args)

def test_metrics_counts(self):
setattr(self.rule.metrics, self.counted_metric_name, 0)
self.rule.metrics.number_of_warnings = 0
with pytest.raises(self.exception):
raise self.exception(*self.exception_args)
assert getattr(self.rule.metrics, self.counted_metric_name) == 1
from tests.unit.exceptions.base import ExceptionBaseTest


class TestProcessingWarning(ExceptionBaseTest):
Expand Down

0 comments on commit dc9aa1f

Please sign in to comment.