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

Fix issue #84: value is mandatory even for no input operators #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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