Skip to content

Commit d88ef9c

Browse files
committed
business-rules extension updates
1 parent 19b3dba commit d88ef9c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

business_rules/engine.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ def check_condition(condition, defined_variables):
5858
"""
5959
name, op, value, other_name = condition['name'], condition['operator'], condition.get('value'), condition.get('other_name')
6060
operator_type = _get_variable_value(defined_variables, name)
61-
if other_name:
61+
if other_name is not None:
6262
other_operator_type = _get_variable_value(defined_variables, other_name)
6363
if other_operator_type != operator_type:
64-
raise ValueError("Both variables in a comparison must be of the same type")
64+
raise ValueError("Both variables in a dual variable comparison must be of the same type")
6565
return _do_operator_comparison(operator_type, op, other_operator_type)
66-
else:
66+
elif value is not None:
6767
return _do_operator_comparison(operator_type, op, value)
68+
else:
69+
raise ValueError("Condition must have a 'value' or 'other_name' key")
70+
6871

6972
def _get_variable_value(defined_variables, name):
7073
""" Call the function provided on the defined_variables object with the

tests/test_engine_logic.py

+25
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,28 @@ def test_do_with_invalid_action() -> None:
237237
err_string = "Action fakeone is not defined in class BaseActions"
238238
with pytest.raises(AssertionError, match=err_string):
239239
engine.do_actions(actions, BaseActions())
240+
241+
242+
@pytest.mark.parametrize(("variable_value", "expected"), [
243+
("test_value", True),
244+
("test_value2", False),
245+
])
246+
def test_check_condition_with_value(variable_value, expected) -> None:
247+
"""
248+
DOCUMENT ME.
249+
"""
250+
condition = {"name": "name", "operator": "equal_to", "value": "test_value"}
251+
with patch.object(engine, "_get_variable_value", return_value=StringType(variable_value)):
252+
assert engine.check_condition(condition, BaseVariables()) is expected
253+
254+
255+
def test_check_condition_raises_if_both_value_and_other_name_missing() -> None:
256+
"""
257+
DOCUMENT ME.
258+
"""
259+
condition = {"name": "name", "operator": "operator",}
260+
with (
261+
patch.object(engine, "_get_variable_value", return_value=StringType("test_value")),
262+
pytest.raises(ValueError, match="Condition must have a 'value' or 'other_name' key"),
263+
):
264+
engine.check_condition(condition, BaseVariables())

0 commit comments

Comments
 (0)