Skip to content

Commit

Permalink
Fix issue value is mandatory even for no input operators venmo#84
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanemartin committed Jul 26, 2022
1 parent b5c1a3c commit 362ce25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion business_rules/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check_condition(condition, defined_variables):
variables, values, and the comparison operator. The defined_variables
object must have a variable defined for any variables in this condition.
"""
name, op, value = condition['name'], condition['operator'], condition['value']
name, op, value = condition['name'], condition['operator'], condition.get('value', ValueError)
operator_type = _get_variable_value(defined_variables, name)
return _do_operator_comparison(operator_type, op, value)

Expand Down Expand Up @@ -82,6 +82,8 @@ def fallback(*args, **kwargs):
method = getattr(operator_type, operator_name, fallback)
if getattr(method, 'input_type', '') == FIELD_NO_INPUT:
return method()
if comparison_value == ValueError:
raise AssertionError("Operator '{0}' needs a value property".format(operator_name))
return method(comparison_value)


Expand Down
13 changes: 11 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from business_rules.engine import check_condition
from business_rules import export_rule_data
from business_rules.actions import rule_action, BaseActions
Expand Down Expand Up @@ -45,7 +47,6 @@ def test_true_boolean_variable(self):
condition = {
'name': 'true_bool',
'operator': 'is_true',
'value': ''
}
res = check_condition(condition, SomeVariables())
self.assertTrue(res)
Expand All @@ -54,11 +55,19 @@ def test_false_boolean_variable(self):
condition = {
'name': 'true_bool',
'operator': 'is_false',
'value': ''
}
res = check_condition(condition, SomeVariables())
self.assertFalse(res)

def test_check_when_incomplete_rule(self):
condition = {'name': 'foo',
'operator': 'contains'}
with pytest.raises(AssertionError) as exc_info:
self.assertTrue(check_condition(condition, SomeVariables()))

assert str(exc_info.value) == "Operator 'contains' needs a value property"


def test_check_true_condition_happy_path(self):
condition = {'name': 'foo',
'operator': 'contains',
Expand Down

0 comments on commit 362ce25

Please sign in to comment.