Skip to content

Commit

Permalink
Merge pull request venmo#16 from yoyowallet/CP-455_run_all_boolean_re…
Browse files Browse the repository at this point in the history
…sults

CP-455: Return a list of booleans from run_all
  • Loading branch information
mjwestcott authored Aug 22, 2017
2 parents 6c2b502 + 705a779 commit 0c1dd31
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ for product in Products.objects.all():
run_all(rule_list=rules,
defined_variables=ProductVariables(product),
defined_actions=ProductActions(product),
stop_on_first_trigger=True
stop_on_first_trigger=True,
)
```

Expand Down
2 changes: 1 addition & 1 deletion business_rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.1.0'
__version__ = '1.2.0'

from .engine import run_all
from .utils import export_rule_data, validate_rule_data
Expand Down
11 changes: 6 additions & 5 deletions business_rules/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@


def run_all(rule_list, defined_variables, defined_actions, stop_on_first_trigger=False):
rule_was_triggered = False
for rule in rule_list:
# type: (...) -> List[bool]
results = [False] * len(rule_list)
for i, rule in enumerate(rule_list):
result = run(rule, defined_variables, defined_actions)
if result:
rule_was_triggered = True
results[i] = True
if stop_on_first_trigger:
return True
return rule_was_triggered
break
return results


def run(rule, defined_variables, defined_actions):
Expand Down
17 changes: 9 additions & 8 deletions tests/test_engine_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class EngineTests(TestCase):

@patch.object(engine, 'run')
def test_run_all_some_rule_triggered(self, *args):
""" By default, does not stop on first triggered rule. Returns True if
any rule was triggered, otherwise False
"""
By default, does not stop on first triggered rule. Returns a list of
booleans indicating whether each rule was triggered.
"""
rule1 = {'conditions': 'condition1', 'actions': 'action name 1'}
rule2 = {'conditions': 'condition2', 'actions': 'action name 2'}
Expand All @@ -29,15 +30,15 @@ def return_action1(rule, *args, **kwargs):

engine.run.side_effect = return_action1

result = engine.run_all([rule1, rule2], variables, actions)
self.assertTrue(result)
results = engine.run_all([rule1, rule2], variables, actions)
self.assertEqual(results, [True, False])
self.assertEqual(engine.run.call_count, 2)

# switch order and try again
engine.run.reset_mock()

result = engine.run_all([rule2, rule1], variables, actions)
self.assertTrue(result)
results = engine.run_all([rule2, rule1], variables, actions)
self.assertEqual(results, [False, True])
self.assertEqual(engine.run.call_count, 2)

@patch.object(engine, 'run', return_value=True)
Expand All @@ -48,9 +49,9 @@ def test_run_all_stop_on_first(self, *args):
variables = BaseVariables()
actions = BaseActions()

result = engine.run_all([rule1, rule2], variables, actions, stop_on_first_trigger=True)
results = engine.run_all([rule1, rule2], variables, actions, stop_on_first_trigger=True)

self.assertEqual(result, True)
self.assertEqual(results, [True, False])
self.assertEqual(engine.run.call_count, 1)
engine.run.assert_called_once_with(rule1, variables, actions)

Expand Down

0 comments on commit 0c1dd31

Please sign in to comment.