From dc9aa1ff6ebc2f26544f4eedd26c6664cffca1b6 Mon Sep 17 00:00:00 2001 From: ekneg54 Date: Mon, 23 Oct 2023 13:26:11 +0000 Subject: [PATCH] refactor exceptions test module --- tests/unit/exceptions/base.py | 38 +++++++++++++++++++ tests/unit/exceptions/processor/__init__.py | 0 ...rning.py => test_processing_exceptions.py} | 38 +------------------ 3 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 tests/unit/exceptions/base.py delete mode 100644 tests/unit/exceptions/processor/__init__.py rename tests/unit/exceptions/{processor/test_processing_warning.py => test_processing_exceptions.py} (58%) diff --git a/tests/unit/exceptions/base.py b/tests/unit/exceptions/base.py new file mode 100644 index 000000000..15072192b --- /dev/null +++ b/tests/unit/exceptions/base.py @@ -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 diff --git a/tests/unit/exceptions/processor/__init__.py b/tests/unit/exceptions/processor/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/unit/exceptions/processor/test_processing_warning.py b/tests/unit/exceptions/test_processing_exceptions.py similarity index 58% rename from tests/unit/exceptions/processor/test_processing_warning.py rename to tests/unit/exceptions/test_processing_exceptions.py index 75a7f5d8b..4d59584cc 100644 --- a/tests/unit/exceptions/processor/test_processing_warning.py +++ b/tests/unit/exceptions/test_processing_exceptions.py @@ -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):