From 362ce250de1f8eb7e350b690620bc3b8c96552df Mon Sep 17 00:00:00 2001 From: Stephane Martin Date: Tue, 26 Jul 2022 10:35:17 +0200 Subject: [PATCH] Fix issue value is mandatory even for no input operators #84 --- business_rules/engine.py | 4 +++- tests/test_integration.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/business_rules/engine.py b/business_rules/engine.py index eb3c00ad..74f4ec2b 100644 --- a/business_rules/engine.py +++ b/business_rules/engine.py @@ -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) @@ -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) diff --git a/tests/test_integration.py b/tests/test_integration.py index 008228cf..437c3659 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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 @@ -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) @@ -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',