From c47fe06c1f4a88c959de9bb3fedf88920f552bb0 Mon Sep 17 00:00:00 2001 From: Piotr Pauksztelo Date: Fri, 21 Jul 2023 07:30:25 +0200 Subject: [PATCH] Make test variable names more descriptive and move them to test modules --- .../framework/rule_tree/shared_constants.py | 11 - .../rule_tree/test_demorgan_resolver.py | 115 ++++++++-- .../framework/rule_tree/test_rule_parser.py | 60 +++-- .../rule_tree/test_rule_segmenter.py | 214 +++++++++++++++--- .../framework/rule_tree/test_rule_sorter.py | 120 ++++++++-- .../framework/rule_tree/test_rule_tagger.py | 140 +++++++++--- 6 files changed, 534 insertions(+), 126 deletions(-) delete mode 100644 tests/unit/framework/rule_tree/shared_constants.py diff --git a/tests/unit/framework/rule_tree/shared_constants.py b/tests/unit/framework/rule_tree/shared_constants.py deleted file mode 100644 index 5287f5126..000000000 --- a/tests/unit/framework/rule_tree/shared_constants.py +++ /dev/null @@ -1,11 +0,0 @@ -# pylint: disable=missing-docstring -from logprep.filter.expression.filter_expression import StringFilterExpression, Exists - -sfe_1 = StringFilterExpression(["key1"], "value1") -sfe_2 = StringFilterExpression(["key2"], "value2") -sfe_3 = StringFilterExpression(["key3"], "value3") -sfe_4 = StringFilterExpression(["key4"], "value4") -sfe_5 = StringFilterExpression(["key5", "subkey5"], "value5") - -ex_1 = Exists(["ABC.def"]) -ex_2 = Exists(["xyz"]) diff --git a/tests/unit/framework/rule_tree/test_demorgan_resolver.py b/tests/unit/framework/rule_tree/test_demorgan_resolver.py index 26e9f85be..065bb6063 100644 --- a/tests/unit/framework/rule_tree/test_demorgan_resolver.py +++ b/tests/unit/framework/rule_tree/test_demorgan_resolver.py @@ -10,13 +10,17 @@ Or, Not, CompoundFilterExpression, + StringFilterExpression, ) from logprep.framework.rule_tree.demorgan_resolver import ( DeMorganResolver, DeMorganResolverException, ) -from tests.unit.framework.rule_tree.shared_constants import sfe_1, sfe_2, sfe_3, sfe_4 +string_filter_expression_1 = StringFilterExpression(["key1"], "value1") +string_filter_expression_2 = StringFilterExpression(["key2"], "value2") +string_filter_expression_3 = StringFilterExpression(["key3"], "value3") +string_filter_expression_4 = StringFilterExpression(["key4"], "value4") @pytest.fixture(name="demorgan_resolver") @@ -26,42 +30,105 @@ def fixture_demorgan_resolver(): class TestDeMorganResolver: @pytest.mark.parametrize( - "expression, resolved_expr", + "expression, expected_resolved", [ - (sfe_1, sfe_1), - (Not(sfe_1), Not(sfe_1)), - (Not(And(Not(sfe_1))), Or(Not(Not(sfe_1)))), - (Not(Or(sfe_1, sfe_2)), And(Not(sfe_1), Not(sfe_2))), - (Not(And(sfe_1, sfe_2)), Or(Not(sfe_1), Not(sfe_2))), - (And(Not(Or(sfe_1, sfe_2)), sfe_3), And(And(Not(sfe_1), Not(sfe_2)), sfe_3)), - (Or(Not(Or(sfe_1, sfe_2)), sfe_3), Or(And(Not(sfe_1), Not(sfe_2)), sfe_3)), - (Not(Or(And(sfe_1, sfe_2), sfe_3)), And(Or(Not(sfe_1), Not(sfe_2)), Not(sfe_3))), - (Not(And(Or(sfe_1, sfe_2), sfe_3)), Or(And(Not(sfe_1), Not(sfe_2)), Not(sfe_3))), - (And(Not(And(sfe_1, sfe_2)), sfe_3), And(Or(Not(sfe_1), Not(sfe_2)), sfe_3)), + (string_filter_expression_1, string_filter_expression_1), + (Not(string_filter_expression_1), Not(string_filter_expression_1)), + (Not(And(Not(string_filter_expression_1))), Or(Not(Not(string_filter_expression_1)))), ( - And(Not(Or(sfe_1, sfe_2)), Not(And(sfe_3, sfe_4))), - And(And(Not(sfe_1), Not(sfe_2)), Or(Not(sfe_3), Not(sfe_4))), + Not(Or(string_filter_expression_1, string_filter_expression_2)), + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + ), + ( + Not(And(string_filter_expression_1, string_filter_expression_2)), + Or(Not(string_filter_expression_1), Not(string_filter_expression_2)), + ), + ( + And( + Not(Or(string_filter_expression_1, string_filter_expression_2)), + string_filter_expression_3, + ), + And( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + string_filter_expression_3, + ), + ), + ( + Or( + Not(Or(string_filter_expression_1, string_filter_expression_2)), + string_filter_expression_3, + ), + Or( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + string_filter_expression_3, + ), + ), + ( + Not( + Or( + And(string_filter_expression_1, string_filter_expression_2), + string_filter_expression_3, + ) + ), + And( + Or(Not(string_filter_expression_1), Not(string_filter_expression_2)), + Not(string_filter_expression_3), + ), + ), + ( + Not( + And( + Or(string_filter_expression_1, string_filter_expression_2), + string_filter_expression_3, + ) + ), + Or( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + Not(string_filter_expression_3), + ), + ), + ( + And( + Not(And(string_filter_expression_1, string_filter_expression_2)), + string_filter_expression_3, + ), + And( + Or(Not(string_filter_expression_1), Not(string_filter_expression_2)), + string_filter_expression_3, + ), + ), + ( + And( + Not(Or(string_filter_expression_1, string_filter_expression_2)), + Not(And(string_filter_expression_3, string_filter_expression_4)), + ), + And( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + Or(Not(string_filter_expression_3), Not(string_filter_expression_4)), + ), ), ], ) - def test_resolve(self, expression, resolved_expr, demorgan_resolver): - assert demorgan_resolver.resolve(expression) == resolved_expr + def test_resolve(self, expression, expected_resolved, demorgan_resolver): + assert demorgan_resolver.resolve(expression) == expected_resolved @pytest.mark.parametrize( - "expression, resolved_expr, error", + "expression, expected_resolved, error", [ - (Not(sfe_1), Not(sfe_1), None), + (Not(string_filter_expression_1), Not(string_filter_expression_1), None), ( - sfe_1, - sfe_1, + string_filter_expression_1, + string_filter_expression_1, ( DeMorganResolverException, r'Can\'t resolve expression ".*", since it\'s not of the type "NOT."', ), ), ( - Not(CompoundFilterExpression(sfe_1, sfe_2)), - Not(sfe_1), + Not( + CompoundFilterExpression(string_filter_expression_1, string_filter_expression_2) + ), + Not(string_filter_expression_1), ( DeMorganResolverException, r'Could not resolve expression ".*", ' @@ -70,9 +137,9 @@ def test_resolve(self, expression, resolved_expr, demorgan_resolver): ), ], ) - def test_resolve_not_expression(self, expression, resolved_expr, error, demorgan_resolver): + def test_resolve_not_expression(self, expression, expected_resolved, error, demorgan_resolver): if error: with pytest.raises(error[0], match=error[1]): demorgan_resolver._resolve_not_expression(expression) else: - assert demorgan_resolver._resolve_not_expression(expression) == resolved_expr + assert demorgan_resolver._resolve_not_expression(expression) == expected_resolved diff --git a/tests/unit/framework/rule_tree/test_rule_parser.py b/tests/unit/framework/rule_tree/test_rule_parser.py index d40a61e46..d42b8222c 100644 --- a/tests/unit/framework/rule_tree/test_rule_parser.py +++ b/tests/unit/framework/rule_tree/test_rule_parser.py @@ -9,10 +9,14 @@ from logprep.framework.rule_tree.rule_parser import RuleParser from logprep.processor.pre_detector.rule import PreDetectorRule -from tests.unit.framework.rule_tree.shared_constants import sfe_1, sfe_2, sfe_3, sfe_4, sfe_5 - pytest.importorskip("logprep.processor.pre_detector") +string_filter_expression_1 = StringFilterExpression(["key1"], "value1") +string_filter_expression_2 = StringFilterExpression(["key2"], "value2") +string_filter_expression_3 = StringFilterExpression(["key3"], "value3") +string_filter_expression_4 = StringFilterExpression(["key4"], "value4") +string_filter_expression_with_subkey = StringFilterExpression(["key5", "subkey5"], "value5") + class TestRuleParser: @pytest.mark.parametrize( @@ -548,45 +552,65 @@ def test_parse_rule_param(self, rule, priority_dict, tag_map, expected_expressio "rule_list, expected", [ ( - [[sfe_1, sfe_2, sfe_3, sfe_4]], + [ + [ + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + string_filter_expression_4, + ] + ], [ [ Exists(["key1"]), - sfe_1, + string_filter_expression_1, Exists(["key2"]), - sfe_2, + string_filter_expression_2, Exists(["key3"]), - sfe_3, + string_filter_expression_3, Exists(["key4"]), - sfe_4, + string_filter_expression_4, ] ], ), ( - [[sfe_1, sfe_3, sfe_5]], + [ + [ + string_filter_expression_1, + string_filter_expression_3, + string_filter_expression_with_subkey, + ] + ], [ [ Exists(["key1"]), - sfe_1, + string_filter_expression_1, Exists(["key3"]), - sfe_3, + string_filter_expression_3, Exists(["key5", "subkey5"]), - sfe_5, + string_filter_expression_with_subkey, ] ], ), ( - [[sfe_1], [sfe_2], [sfe_3]], [ - [Exists(["key1"]), sfe_1], - [Exists(["key2"]), sfe_2], - [Exists(["key3"]), sfe_3], + [string_filter_expression_1], + [string_filter_expression_2], + [string_filter_expression_3], + ], + [ + [Exists(["key1"]), string_filter_expression_1], + [Exists(["key2"]), string_filter_expression_2], + [Exists(["key3"]), string_filter_expression_3], ], ), - ([[Not(sfe_1)]], [[Not(sfe_1)]]), + ([[Not(string_filter_expression_1)]], [[Not(string_filter_expression_1)]]), ( - [[sfe_1, Exists(["key1"])], [sfe_1]], - [[sfe_1, Exists(["key1"])], [Exists(["key1"]), sfe_1]], + [[string_filter_expression_1, Exists(["key1"])], [string_filter_expression_1]], + [ + [string_filter_expression_1, Exists(["key1"])], + [Exists(["key1"]), string_filter_expression_1], + ], ), ], ) diff --git a/tests/unit/framework/rule_tree/test_rule_segmenter.py b/tests/unit/framework/rule_tree/test_rule_segmenter.py index 9c7200a8d..d66284e1e 100644 --- a/tests/unit/framework/rule_tree/test_rule_segmenter.py +++ b/tests/unit/framework/rule_tree/test_rule_segmenter.py @@ -5,19 +5,50 @@ import pytest -from logprep.filter.expression.filter_expression import And, Or, Not +from logprep.filter.expression.filter_expression import And, Or, Not, StringFilterExpression from logprep.framework.rule_tree.rule_segmenter import RuleSegmenter, CnfToDnfConverter -from tests.unit.framework.rule_tree.shared_constants import sfe_1, sfe_2, sfe_3, sfe_4 + +string_filter_expression_1 = StringFilterExpression(["key1"], "value1") +string_filter_expression_2 = StringFilterExpression(["key2"], "value2") +string_filter_expression_3 = StringFilterExpression(["key3"], "value3") +string_filter_expression_4 = StringFilterExpression(["key4"], "value4") class TestRuleSegmenter: @pytest.mark.parametrize( "expression, expected", [ - (And(sfe_1, sfe_2), [sfe_1, sfe_2]), - (And(sfe_1, sfe_2, sfe_3), [sfe_1, sfe_2, sfe_3]), - (And(sfe_1, Not(sfe_2)), [sfe_1, Not(sfe_2)]), - (And(sfe_1, And(Not(sfe_2), sfe_3)), [sfe_1, Not(sfe_2), sfe_3]), + ( + And(string_filter_expression_1, string_filter_expression_2), + [string_filter_expression_1, string_filter_expression_2], + ), + ( + And( + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + ), + [ + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + ], + ), + ( + And(string_filter_expression_1, Not(string_filter_expression_2)), + [string_filter_expression_1, Not(string_filter_expression_2)], + ), + ( + And( + string_filter_expression_1, + And(Not(string_filter_expression_2), string_filter_expression_3), + ), + [ + string_filter_expression_1, + Not(string_filter_expression_2), + string_filter_expression_3, + ], + ), ], ) def test_parse_and_expression(self, expression, expected): @@ -26,27 +57,122 @@ def test_parse_and_expression(self, expression, expected): @pytest.mark.parametrize( "expression, expected", [ - (Or(sfe_1, sfe_2), [[sfe_1], [sfe_2]]), - (And(sfe_1, Or(sfe_2, sfe_3)), [[sfe_1, sfe_2], [sfe_1, sfe_3]]), - (And(sfe_1, Or(sfe_2, sfe_3), sfe_4), [[sfe_1, sfe_4, sfe_2], [sfe_1, sfe_4, sfe_3]]), - (Or(And(Not(sfe_1), Not(sfe_2)), sfe_3), [[Not(sfe_1), Not(sfe_2)], [sfe_3]]), ( - And(Or(Not(sfe_1), Not(sfe_2)), Not(sfe_3)), - [[Not(sfe_3), Not(sfe_1)], [Not(sfe_3), Not(sfe_2)]], + Or(string_filter_expression_1, string_filter_expression_2), + [[string_filter_expression_1], [string_filter_expression_2]], + ), + ( + And( + string_filter_expression_1, + Or(string_filter_expression_2, string_filter_expression_3), + ), + [ + [string_filter_expression_1, string_filter_expression_2], + [string_filter_expression_1, string_filter_expression_3], + ], ), ( - And(Not(sfe_1), Not(sfe_2), Or(Not(sfe_3), Not(sfe_4))), - [[Not(sfe_1), Not(sfe_2), Not(sfe_3)], [Not(sfe_1), Not(sfe_2), Not(sfe_4)]], + And( + string_filter_expression_1, + Or(string_filter_expression_2, string_filter_expression_3), + string_filter_expression_4, + ), + [ + [ + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_2, + ], + [ + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_3, + ], + ], ), ( - And(And(Not(sfe_1), Not(sfe_2)), Or(Not(sfe_3), Not(sfe_4))), - [[Not(sfe_2), Not(sfe_1), Not(sfe_3)], [Not(sfe_2), Not(sfe_1), Not(sfe_4)]], + Or( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + string_filter_expression_3, + ), + [ + [Not(string_filter_expression_1), Not(string_filter_expression_2)], + [string_filter_expression_3], + ], ), ( - And(Or(sfe_1, sfe_2), Or(sfe_3, sfe_4)), - [[sfe_1, sfe_3], [sfe_1, sfe_4], [sfe_2, sfe_3], [sfe_2, sfe_4]], + And( + Or(Not(string_filter_expression_1), Not(string_filter_expression_2)), + Not(string_filter_expression_3), + ), + [ + [Not(string_filter_expression_3), Not(string_filter_expression_1)], + [Not(string_filter_expression_3), Not(string_filter_expression_2)], + ], + ), + ( + And( + Not(string_filter_expression_1), + Not(string_filter_expression_2), + Or(Not(string_filter_expression_3), Not(string_filter_expression_4)), + ), + [ + [ + Not(string_filter_expression_1), + Not(string_filter_expression_2), + Not(string_filter_expression_3), + ], + [ + Not(string_filter_expression_1), + Not(string_filter_expression_2), + Not(string_filter_expression_4), + ], + ], + ), + ( + And( + And(Not(string_filter_expression_1), Not(string_filter_expression_2)), + Or(Not(string_filter_expression_3), Not(string_filter_expression_4)), + ), + [ + [ + Not(string_filter_expression_2), + Not(string_filter_expression_1), + Not(string_filter_expression_3), + ], + [ + Not(string_filter_expression_2), + Not(string_filter_expression_1), + Not(string_filter_expression_4), + ], + ], + ), + ( + And( + Or(string_filter_expression_1, string_filter_expression_2), + Or(string_filter_expression_3, string_filter_expression_4), + ), + [ + [string_filter_expression_1, string_filter_expression_3], + [string_filter_expression_1, string_filter_expression_4], + [string_filter_expression_2, string_filter_expression_3], + [string_filter_expression_2, string_filter_expression_4], + ], + ), + ( + Or( + And( + string_filter_expression_1, + Or(string_filter_expression_2, string_filter_expression_3), + ), + string_filter_expression_4, + ), + [ + [string_filter_expression_1, string_filter_expression_2], + [string_filter_expression_1, string_filter_expression_3], + [string_filter_expression_4], + ], ), - (Or(And(sfe_1, Or(sfe_2, sfe_3)), sfe_4), [[sfe_1, sfe_2], [sfe_1, sfe_3], [sfe_4]]), ], ) def test_parse_or_expression(self, expression, expected): @@ -55,13 +181,13 @@ def test_parse_or_expression(self, expression, expected): @pytest.mark.parametrize( "expression, expected", [ - (And(sfe_1, sfe_2), False), - (Or(sfe_1, sfe_2), True), - (Not(sfe_1), False), - (Not(And(sfe_1, sfe_2)), False), - (Not(Or(sfe_1, sfe_2)), True), - (And(Not(Or(sfe_1, sfe_2))), True), - (And(Not(And(sfe_1, sfe_2))), False), + (And(string_filter_expression_1, string_filter_expression_2), False), + (Or(string_filter_expression_1, string_filter_expression_2), True), + (Not(string_filter_expression_1), False), + (Not(And(string_filter_expression_1, string_filter_expression_2)), False), + (Not(Or(string_filter_expression_1, string_filter_expression_2)), True), + (And(Not(Or(string_filter_expression_1, string_filter_expression_2))), True), + (And(Not(And(string_filter_expression_1, string_filter_expression_2))), False), ], ) def test_has_or_expression(self, expression, expected): @@ -70,10 +196,38 @@ def test_has_or_expression(self, expression, expected): @pytest.mark.parametrize( "expression_cnf, expected_dnf", [ - ([sfe_1, [[sfe_2]]], [[sfe_1, sfe_2]]), - ([[[sfe_1]], [[sfe_2]]], [[sfe_1, sfe_2]]), - ([sfe_1, [[sfe_2]], sfe_3], [[sfe_1, sfe_3, sfe_2]]), - ([sfe_1, [[sfe_2], [sfe_3]]], [[sfe_1, sfe_2], [sfe_1, sfe_3]]), + ( + [string_filter_expression_1, [[string_filter_expression_2]]], + [[string_filter_expression_1, string_filter_expression_2]], + ), + ( + [[[string_filter_expression_1]], [[string_filter_expression_2]]], + [[string_filter_expression_1, string_filter_expression_2]], + ), + ( + [ + string_filter_expression_1, + [[string_filter_expression_2]], + string_filter_expression_3, + ], + [ + [ + string_filter_expression_1, + string_filter_expression_3, + string_filter_expression_2, + ] + ], + ), + ( + [ + string_filter_expression_1, + [[string_filter_expression_2], [string_filter_expression_3]], + ], + [ + [string_filter_expression_1, string_filter_expression_2], + [string_filter_expression_1, string_filter_expression_3], + ], + ), ], ) def test_convert_cnf_to_dnf(self, expression_cnf, expected_dnf): diff --git a/tests/unit/framework/rule_tree/test_rule_sorter.py b/tests/unit/framework/rule_tree/test_rule_sorter.py index 499414fa6..cfcbff2a4 100644 --- a/tests/unit/framework/rule_tree/test_rule_sorter.py +++ b/tests/unit/framework/rule_tree/test_rule_sorter.py @@ -5,27 +5,116 @@ import pytest -from logprep.filter.expression.filter_expression import Not, Always, CompoundFilterExpression +from logprep.filter.expression.filter_expression import ( + Not, + Always, + CompoundFilterExpression, + StringFilterExpression, + Exists, +) from logprep.framework.rule_tree.rule_sorter import RuleSorter, RuleSorterException -from tests.unit.framework.rule_tree.shared_constants import sfe_1, sfe_2, sfe_3, sfe_4, ex_1, ex_2 pytest.importorskip("logprep.processor.pre_detector") +string_filter_expression_1 = StringFilterExpression(["key1"], "value1") +string_filter_expression_2 = StringFilterExpression(["key2"], "value2") +string_filter_expression_3 = StringFilterExpression(["key3"], "value3") +string_filter_expression_4 = StringFilterExpression(["key4"], "value4") + +exists_expression_1 = Exists(["ABC.def"]) +exists_expression_2 = Exists(["xyz"]) + class TestRuleSorter: @pytest.mark.parametrize( "rule_list, priority_dict, expected", [ - ([[sfe_1, sfe_4, sfe_3, sfe_2]], {}, [[sfe_1, sfe_2, sfe_3, sfe_4]]), - ([[sfe_1, sfe_4, sfe_3, sfe_2]], {"key2": "1"}, [[sfe_2, sfe_1, sfe_3, sfe_4]]), - ([[sfe_1, sfe_3, ex_1, sfe_2, ex_2]], {}, [[ex_1, sfe_1, sfe_2, sfe_3, ex_2]]), ( - [[sfe_1, sfe_3, ex_1, sfe_2, ex_2]], + [ + [ + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_3, + string_filter_expression_2, + ] + ], + {}, + [ + [ + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + string_filter_expression_4, + ] + ], + ), + ( + [ + [ + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_3, + string_filter_expression_2, + ] + ], + {"key2": "1"}, + [ + [ + string_filter_expression_2, + string_filter_expression_1, + string_filter_expression_3, + string_filter_expression_4, + ] + ], + ), + ( + [ + [ + string_filter_expression_1, + string_filter_expression_3, + exists_expression_1, + string_filter_expression_2, + exists_expression_2, + ] + ], + {}, + [ + [ + exists_expression_1, + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + exists_expression_2, + ] + ], + ), + ( + [ + [ + string_filter_expression_1, + string_filter_expression_3, + exists_expression_1, + string_filter_expression_2, + exists_expression_2, + ] + ], {"xyz": "1"}, - [[ex_2, ex_1, sfe_1, sfe_2, sfe_3]], + [ + [ + exists_expression_2, + exists_expression_1, + string_filter_expression_1, + string_filter_expression_2, + string_filter_expression_3, + ] + ], + ), + ( + [[string_filter_expression_2, Not(string_filter_expression_1)]], + {"key1": "1"}, + [[Not(string_filter_expression_1), string_filter_expression_2]], ), - ([[sfe_2, Not(sfe_1)]], {"key1": "1"}, [[Not(sfe_1), sfe_2]]), ], ) def test_sort_rule_segments(self, rule_list, priority_dict, expected): @@ -37,17 +126,20 @@ def test_sort_rule_segments(self, rule_list, priority_dict, expected): [ (Always("foo"), None), (Not(Always("foo")), None), - (sfe_1, str(sfe_1)), - (Not(sfe_1), str(sfe_1)), - (Not(Not(sfe_1)), str(sfe_1)), - (ex_1, str(ex_1)), - (Not(ex_1), str(ex_1)), + (string_filter_expression_1, str(string_filter_expression_1)), + (Not(string_filter_expression_1), str(string_filter_expression_1)), + (Not(Not(string_filter_expression_1)), str(string_filter_expression_1)), + (exists_expression_1, str(exists_expression_1)), + (Not(exists_expression_1), str(exists_expression_1)), ], ) def test_get_sorting_key_succeeds(self, expression, expected): assert RuleSorter._get_sorting_key(expression, {}) == expected - @pytest.mark.parametrize("expression", [CompoundFilterExpression(sfe_1, sfe_2), "foo"]) + @pytest.mark.parametrize( + "expression", + [CompoundFilterExpression(string_filter_expression_1, string_filter_expression_2), "foo"], + ) def test_get_sorting_key_raises_exception(self, expression): with pytest.raises(RuleSorterException, match=f'Could not sort "{str(expression)}"'): RuleSorter._get_sorting_key(expression, {}) diff --git a/tests/unit/framework/rule_tree/test_rule_tagger.py b/tests/unit/framework/rule_tree/test_rule_tagger.py index 08617fa7a..a7e4f512e 100644 --- a/tests/unit/framework/rule_tree/test_rule_tagger.py +++ b/tests/unit/framework/rule_tree/test_rule_tagger.py @@ -7,68 +7,150 @@ from logprep.filter.expression.filter_expression import StringFilterExpression, Not, Exists from logprep.framework.rule_tree.rule_tagger import RuleTagger -from tests.unit.framework.rule_tree.shared_constants import sfe_1, sfe_2, sfe_3, sfe_4, ex_2 pytest.importorskip("logprep.processor.pre_detector") +string_filter_expression_1 = StringFilterExpression(["key1"], "value1") +string_filter_expression_2 = StringFilterExpression(["key2"], "value2") +string_filter_expression_3 = StringFilterExpression(["key3"], "value3") +string_filter_expression_4 = StringFilterExpression(["key4"], "value4") + +exists_expression = Exists(["xyz"]) + + class TestRuleTagger: @pytest.mark.parametrize( "rule_list, tag_map, expected", [ - ([[sfe_1, sfe_2]], {"key2": "TAG"}, [[Exists(["TAG"]), sfe_1, sfe_2]]), ( - [[sfe_1, sfe_2], [sfe_1, sfe_3]], + [[string_filter_expression_1, string_filter_expression_2]], + {"key2": "TAG"}, + [[Exists(["TAG"]), string_filter_expression_1, string_filter_expression_2]], + ), + ( + [ + [string_filter_expression_1, string_filter_expression_2], + [string_filter_expression_1, string_filter_expression_3], + ], {"key2": "TAG2", "key3": "TAG3"}, - [[Exists(["TAG2"]), sfe_1, sfe_2], [Exists(["TAG3"]), sfe_1, sfe_3]], + [ + [Exists(["TAG2"]), string_filter_expression_1, string_filter_expression_2], + [Exists(["TAG3"]), string_filter_expression_1, string_filter_expression_3], + ], ), ( - [[sfe_1, sfe_4, sfe_2], [sfe_2, sfe_3], [sfe_2], [sfe_4, sfe_3]], + [ + [ + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_2, + ], + [string_filter_expression_2, string_filter_expression_3], + [string_filter_expression_2], + [string_filter_expression_4, string_filter_expression_3], + ], {"key1": "TAG1", "key2": "TAG2"}, [ - [Exists(["TAG2"]), Exists(["TAG1"]), sfe_1, sfe_4, sfe_2], - [Exists(["TAG2"]), sfe_2, sfe_3], - [Exists(["TAG2"]), sfe_2], - [sfe_4, sfe_3], + [ + Exists(["TAG2"]), + Exists(["TAG1"]), + string_filter_expression_1, + string_filter_expression_4, + string_filter_expression_2, + ], + [Exists(["TAG2"]), string_filter_expression_2, string_filter_expression_3], + [Exists(["TAG2"]), string_filter_expression_2], + [string_filter_expression_4, string_filter_expression_3], ], ), ( - [[sfe_1, sfe_3], [sfe_2, sfe_4]], + [ + [string_filter_expression_1, string_filter_expression_3], + [string_filter_expression_2, string_filter_expression_4], + ], {"key1": "TAG1", "key2": "TAG2.SUBTAG2"}, [ - [Exists(["TAG1"]), sfe_1, sfe_3], - [Exists(["TAG2", "SUBTAG2"]), sfe_2, sfe_4], + [Exists(["TAG1"]), string_filter_expression_1, string_filter_expression_3], + [ + Exists(["TAG2", "SUBTAG2"]), + string_filter_expression_2, + string_filter_expression_4, + ], ], ), ( - [[sfe_1, sfe_3], [sfe_2, sfe_4]], + [ + [string_filter_expression_1, string_filter_expression_3], + [string_filter_expression_2, string_filter_expression_4], + ], {"key1": "TAG1:Value1", "key2": "TAG2.SUBTAG2"}, [ - [StringFilterExpression(["TAG1"], "Value1"), sfe_1, sfe_3], - [Exists(["TAG2", "SUBTAG2"]), sfe_2, sfe_4], + [ + StringFilterExpression(["TAG1"], "Value1"), + string_filter_expression_1, + string_filter_expression_3, + ], + [ + Exists(["TAG2", "SUBTAG2"]), + string_filter_expression_2, + string_filter_expression_4, + ], ], ), ( - [[sfe_1, sfe_3], [sfe_2, sfe_4]], + [ + [string_filter_expression_1, string_filter_expression_3], + [string_filter_expression_2, string_filter_expression_4], + ], {"key1": "TAG1.SUBTAG1:Value1", "key2": "TAG2.SUBTAG2"}, [ - [StringFilterExpression(["TAG1", "SUBTAG1"], "Value1"), sfe_1, sfe_3], - [Exists(["TAG2", "SUBTAG2"]), sfe_2, sfe_4], + [ + StringFilterExpression(["TAG1", "SUBTAG1"], "Value1"), + string_filter_expression_1, + string_filter_expression_3, + ], + [ + Exists(["TAG2", "SUBTAG2"]), + string_filter_expression_2, + string_filter_expression_4, + ], ], ), ( - [[sfe_1, ex_2]], + [[string_filter_expression_1, exists_expression]], {"xyz": "TAG:VALUE"}, - [[StringFilterExpression(["TAG"], "VALUE"), sfe_1, ex_2]], + [ + [ + StringFilterExpression(["TAG"], "VALUE"), + string_filter_expression_1, + exists_expression, + ] + ], ), - ([[Not(sfe_1)]], {"key1": "TAG"}, [[Exists(["TAG"]), Not(sfe_1)]]), - ([[sfe_1]], {"key1": "key1:value1"}, [[sfe_1]]), ( - [[sfe_1, sfe_2, ex_2]], + [[Not(string_filter_expression_1)]], + {"key1": "TAG"}, + [[Exists(["TAG"]), Not(string_filter_expression_1)]], + ), + ( + [[string_filter_expression_1]], + {"key1": "key1:value1"}, + [[string_filter_expression_1]], + ), + ( + [[string_filter_expression_1, string_filter_expression_2, exists_expression]], {"key1": "TAG1", "key2": "xyz"}, - [[Exists(["TAG1"]), sfe_1, sfe_2, ex_2]], + [ + [ + Exists(["TAG1"]), + string_filter_expression_1, + string_filter_expression_2, + exists_expression, + ] + ], ), - ([[sfe_1]], {}, [[sfe_1]]), + ([[string_filter_expression_1]], {}, [[string_filter_expression_1]]), ], ) def test_add_special_tags(self, rule_list, tag_map, expected): @@ -79,10 +161,10 @@ def test_add_special_tags(self, rule_list, tag_map, expected): @pytest.mark.parametrize( "expression, tag, tag_map, expected", [ - (ex_2, "xyz", {"key1": "xyz"}, True), - (ex_2, "foo", {"key1": "foo"}, False), - (sfe_1, "key1:value1", {"key1": "key1:value1"}, True), - (sfe_1, "foo:bar", {"key1": "foo:bar"}, False), + (exists_expression, "xyz", {"key1": "xyz"}, True), + (exists_expression, "foo", {"key1": "foo"}, False), + (string_filter_expression_1, "key1:value1", {"key1": "key1:value1"}, True), + (string_filter_expression_1, "foo:bar", {"key1": "foo:bar"}, False), ], ) def test_tag_exists(self, expression, tag, tag_map, expected):