forked from venmo/business-rules
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
|
||
class MissingVariableException(BaseException): | ||
"""If variable is missing(""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pytest | ||
pytest-asyncio | ||
mock==4.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |