Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-7475 Proposed feature for ['key'] accessor #50

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "drools_jpy"
version = "0.3.4"
version = "0.3.5"
authors = [
{ name="Madhu Kanoor", email="author@example.com" },
]
Expand Down Expand Up @@ -44,8 +44,8 @@ dev = [
]

[project.urls]
"Homepage" = "https://github.com/mkanoor/drools_jpy"
"Bug Tracker" = "https://github.com/mkanoor/drools_jpy/issues"
"Homepage" = "https://github.com/ansible/drools_jpy"
"Bug Tracker" = "https://github.com/ansible/drools_jpy/issues"

[tool.setuptools]
include-package-data = true
Expand Down
Binary file not shown.
96 changes: 96 additions & 0 deletions tests/asts/test_squaredaccessor_ast.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
- RuleSet:
hosts:
- all
name: test squared accessor
rules:
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range['pi']
rhs:
Float: 3.1415
enabled: true
name: r1
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range["pi"]
rhs:
Float: 3.1415
enabled: true
name: r2
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range["pi"].value
rhs:
Float: 3.1415
enabled: true
name: r3
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range[0]
rhs:
Float: 3.1415
enabled: true
name: r4
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range[-1]
rhs:
Float: 3.1415
enabled: true
name: r5
- Rule:
action:
Action:
action: debug
action_args: {}
condition:
AllCondition:
- EqualsExpression:
lhs:
Fact: range["x"][1][2].a["b"]
rhs:
Float: 3.1415
enabled: true
name: r6
sources:
- EventSource:
name: range
source_args:
limit: 5
source_filters: []
source_name: range
35 changes: 35 additions & 0 deletions tests/asts/test_squaredaccessor_cases_ast.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- RuleSet:
hosts:
- all
name: Test squared accessor
rules:
- Rule:
actions:
- Action:
action: debug
action_args:
msg: 'Testcase #1 passes'
condition:
AllCondition:
- EqualsExpression:
lhs:
Event: asd["x"][1][2].a["b"]
rhs:
Float: 3.1415
enabled: true
name: 'r1 squared accessor'
sources:
- EventSource:
name: generic
source_args:
payload:
- asd:
x:
- 0
- - 0
- 0
- a:
b: 3.1415
id: 'Testcase #1'
source_filters: []
source_name: generic
40 changes: 40 additions & 0 deletions tests/asts/test_squaredaccessor_selectattr_cases_ast.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- RuleSet:
hosts:
- all
name: Test squared accessor with selectattr operator
rules:
- Rule:
actions:
- Action:
action: debug
action_args:
msg: Output for testcase
condition:
AllCondition:
- SelectAttrExpression:
lhs:
Event: asd["x"][1][2].a
rhs:
key:
String: b
operator:
String: ==
value:
Float: 3.1415
enabled: true
name: r1 selectattr and squared accessor interaction
sources:
- EventSource:
name: generic
source_args:
payload:
- asd:
x:
- 0
- - 0
- 0
- a:
b: 3.1415
id: 'Testcase #1'
source_filters: []
source_name: generic
51 changes: 51 additions & 0 deletions tests/test_ruleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,55 @@ def test_assert_multiple_facts():
my_callback2.assert_called_with(result2)


def test_squared_accessors():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tarilabs if you use the tools/convert_to_ast.py from the ansible-rulebook repo and convert the rulebook to ast format you will get the sources data included and then if we add the ast yml file to this list

"asts/test_delayed_comparison_ast.yml",
it will validate all the rules. The integrated test knows how to insert the payload data from sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 324f2de

test_data = load_ast("asts/test_squaredaccessor_ast.yml")

my_callback1 = mock.Mock()
my_callback2 = mock.Mock()
my_callback3 = mock.Mock()
my_callback4 = mock.Mock()
my_callback5 = mock.Mock()
my_callback6 = mock.Mock()
result1_and_2 = Matches(data={"m": {"id": "A", "range": {"pi": 3.1415}}})
result3 = Matches(
data={"m": {"id": "B", "range": {"pi": {"value": 3.1415}}}}
)
result4_and_5 = Matches(data={"m": {"id": "C", "range": [3.1415]}})
result6 = Matches(
data={
"m": {"id": "D", "range": {"x": [0, [0, 0, {"a": {"b": 3.1415}}]]}}
}
)

ruleset_data = test_data[0]["RuleSet"]
rs = Ruleset(
name=ruleset_data["name"], serialized_ruleset=json.dumps(ruleset_data)
)
rs.add_rule(Rule("r1", my_callback1))
rs.add_rule(Rule("r2", my_callback2))
rs.add_rule(Rule("r3", my_callback3))
rs.add_rule(Rule("r4", my_callback4))
rs.add_rule(Rule("r5", my_callback5))
rs.add_rule(Rule("r6", my_callback6))

rs.assert_fact(json.dumps({"id": "A", "range": {"pi": 3.1415}}))
rs.assert_fact(json.dumps({"id": "B", "range": {"pi": {"value": 3.1415}}}))
rs.assert_fact(json.dumps({"id": "C", "range": [3.1415]}))
rs.assert_fact(
json.dumps(
{"id": "D", "range": {"x": [0, [0, 0, {"a": {"b": 3.1415}}]]}}
)
)

rs.end_session()
my_callback1.assert_called_with(result1_and_2)
my_callback2.assert_called_with(result1_and_2)
my_callback3.assert_called_with(result3)
my_callback4.assert_called_with(result4_and_5)
my_callback5.assert_called_with(result4_and_5)
my_callback6.assert_called_with(result6)


def test_multiple_rulesets():
test_data = load_ast("asts/multiple_rule_ast.yml")
fired_callbacks = []
Expand Down Expand Up @@ -933,6 +982,8 @@ def test_assert_event_string_search():
"asts/test_select_with_same_event_ast.yml",
"asts/test_self_referential_ast.yml",
"asts/test_delayed_comparison_ast.yml",
"asts/test_squaredaccessor_cases_ast.yml",
"asts/test_squaredaccessor_selectattr_cases_ast.yml",
],
)
def test_integrated(rulebook):
Expand Down