Skip to content

Commit

Permalink
missing variable exception
Browse files Browse the repository at this point in the history
  • Loading branch information
litwisha committed Sep 28, 2020
1 parent 23c6a6f commit ebb4d63
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
9 changes: 8 additions & 1 deletion business_rules/engine.py
Expand Up @@ -5,6 +5,7 @@
from .actions import BaseActions
from .fields import FIELD_NO_INPUT
from .variables import BaseVariables
from .exceptions import MissingVariableException

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,7 +84,13 @@ async def check_condition(condition, defined_variables):
name = condition['name']
op = condition['operator']
value = condition['value']
operator_type = await _get_variable_value(defined_variables, name)

try:
operator_type = await _get_variable_value(defined_variables, name)
except MissingVariableException:
""""""
return False

if 'value_is_variable' in condition and condition['value_is_variable']:
variable_name = value
temp_value = await _get_variable_value(defined_variables, variable_name)
Expand Down
4 changes: 4 additions & 0 deletions business_rules/exceptions.py
@@ -0,0 +1,4 @@


class MissingVariableException(BaseException):
"""If variable is missing("""
1 change: 1 addition & 0 deletions dev-requirements.txt
@@ -1,2 +1,3 @@
pytest
pytest-asyncio
mock==4.0.2
88 changes: 88 additions & 0 deletions tests/test_missing.py
@@ -0,0 +1,88 @@
from business_rules.variables import BaseVariables, string_rule_variable
from business_rules.actions import BaseActions, rule_action
from business_rules.exceptions import MissingVariableException
from business_rules import run
import pytest


class Payment:
def __init__(self, amount, type):
self.amount = amount
self.type = type


class Order:
def __init__(self, name, payment):
self.name = name
self.payment = payment


class Variables(BaseVariables):

def __init__(self, order):
self.order = order

@string_rule_variable()
async def order_payment_type(self):
if self.order.payment is None:
raise MissingVariableException()

return self.order.payment.type


class Actions(BaseActions):
@rule_action()
async def approve(self):
return {'action': 'approve'}


@pytest.mark.asyncio
async def test_missing_variable_exception():
rule = {
'conditions': {
'all': [
{
'name': 'order_payment_type',
'operator': 'not_equal_to',
'value': 'paypal'
}
]
},
'actions': [
{
'name': 'approve'
}
]
}
order = Order(
name='order',
payment=Payment(amount=10, type='credit_card')
)
result = await run(
rule=rule,
defined_variables=Variables(order),
defined_actions=Actions()
)
assert result == {'action_name': 'approve', 'action_params': {}, 'action_result': {'action': 'approve'}}

order = Order(
name='order',
payment=Payment(amount=10, type='paypal')
)
result = await run(
rule=rule,
defined_variables=Variables(order),
defined_actions=Actions()
)
assert result is None

order = Order(
name='order',
payment=None
)
result = await run(
rule=rule,
defined_variables=Variables(order),
defined_actions=Actions()
)
assert result is None

0 comments on commit ebb4d63

Please sign in to comment.