Skip to content

Commit

Permalink
Make test variable names more descriptive and move them to test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ppcad committed Jul 21, 2023
1 parent 49b9ddb commit c47fe06
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 126 deletions.
11 changes: 0 additions & 11 deletions tests/unit/framework/rule_tree/shared_constants.py

This file was deleted.

115 changes: 91 additions & 24 deletions tests/unit/framework/rule_tree/test_demorgan_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 ".*", '
Expand All @@ -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
60 changes: 42 additions & 18 deletions tests/unit/framework/rule_tree/test_rule_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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],
],
),
],
)
Expand Down
Loading

0 comments on commit c47fe06

Please sign in to comment.