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

Date type #19

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Modify tests for variable parameters
  • Loading branch information
Alexander Kurihara committed Dec 9, 2014
commit a97d90810e6335c4359183736d785df9051d72f9
25 changes: 22 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@ class SomeVariables(BaseVariables):
def foo(self):
return "foo"

@numeric_rule_variable(params=[{'fieldType': FIELD_NUMERIC, 'name': 'x', 'label': 'X'}])
def x_plus_one(self, x):
return x + 1

@numeric_rule_variable(label="Diez")
def ten(self):
return 10
@@ -71,6 +75,13 @@ def test_check_false_condition_happy_path(self):
'value': 'm'}
self.assertFalse(check_condition(condition, SomeVariables()))

def test_numeric_variable_with_params(self):
condition = {'name': 'x_plus_one',
'operator': 'equal_to',
'value': 10,
'params': {'x': 9}}
self.assertTrue(check_condition(condition, SomeVariables()))

def test_check_incorrect_method_name(self):
condition = {'name': 'food',
'operator': 'equal_to',
@@ -115,15 +126,23 @@ def test_export_rule_data(self):
[{"name": "foo",
"label": "Foo",
"field_type": "string",
"options": []},
"options": [],
"params": []},
{"name": "ten",
"label": "Diez",
"field_type": "numeric",
"options": []},
"options": [],
"params": []},
{'name': 'true_bool',
'label': 'True Bool',
'field_type': 'boolean',
'options': []}])
'options': [],
'params': []},
{'name': 'x_plus_one',
'label': 'X Plus One',
'field_type': 'numeric',
'options': [],
'params': [{'fieldType':'numeric', 'name': 'x', 'label': 'X'}]}])

self.assertEqual(all_data.get("variable_type_operators"),
{'boolean': [{'input_type': 'none',
29 changes: 27 additions & 2 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import TestCase
from business_rules.utils import fn_name_to_pretty_label
from business_rules.fields import FIELD_NUMERIC
from business_rules.variables import (rule_variable,
numeric_rule_variable,
string_rule_variable,
@@ -34,13 +35,15 @@ def test_rule_variable_decorator_internals(self):
""" Make sure that the expected attributes are attached to a function
by the variable decorators.
"""
def some_test_function(self): pass
wrapper = rule_variable(StringType, 'Foo Name', options=['op1', 'op2'])
def some_test_function(self, param1): pass
params = [{'fieldType': FIELD_NUMERIC, 'name': 'param1', 'label': 'Param1'}]
wrapper = rule_variable(StringType, 'Foo Name', options=['op1', 'op2'], params=params)
func = wrapper(some_test_function)
self.assertTrue(func.is_rule_variable)
self.assertEqual(func.label, 'Foo Name')
self.assertEqual(func.field_type, StringType)
self.assertEqual(func.options, ['op1', 'op2'])
self.assertEqual(func.params, params)

def test_rule_variable_works_as_decorator(self):
@rule_variable(StringType, 'Blah')
@@ -52,6 +55,28 @@ def test_rule_variable_decorator_auto_fills_label(self):
def some_test_function(self): pass
self.assertTrue(some_test_function.label, 'Some Test Function')

def test_rule_variable_doesnt_allow_unknown_field_types(self):
""" Tests that the variable decorator throws an error if a param
is defined with an invalid field type.
"""
params = [{'fieldType': 'blah', 'name': 'foo', 'label': 'Foo'}]
err_string = "Unknown field type blah specified for variable "\
"some_test_function param foo"
with self.assertRaisesRegexp(AssertionError, err_string):
@rule_variable(StringType, params=params)
def some_test_function(self, foo): pass

def test_rule_variable_doesnt_allow_unknown_parameter_name(self):
""" Tests that decorator throws an error if a param name does not match
an argument in the function definition.
"""
params = [{'fieldType': FIELD_NUMERIC, 'name': 'bar', 'label': 'Bar'}]
err_string = "Unknown parameter name bar specified for variable "\
"some_test_function"
with self.assertRaisesRegexp(AssertionError, err_string):
@rule_variable(StringType, params=params)
def some_test_function(self, foo): pass

def test_rule_variable_decorator_caches_value(self):
foo = 1
@rule_variable(NumericType)
6 changes: 4 additions & 2 deletions tests/test_variables_class.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from business_rules.variables import BaseVariables, rule_variable
from business_rules.operators import StringType
from business_rules.fields import FIELD_TEXT
from . import TestCase

class VariablesClassTests(TestCase):
@@ -14,8 +15,8 @@ def test_get_all_variables(self):
"""
class SomeVariables(BaseVariables):

@rule_variable(StringType)
def this_is_rule_1(self):
@rule_variable(StringType, params=[{'fieldType': FIELD_TEXT, 'name': 'foo', 'label': 'Foo'}])
def this_is_rule_1(self, foo):
return "blah"

def non_rule(self):
@@ -27,6 +28,7 @@ def non_rule(self):
self.assertEqual(vars[0]['label'], 'This Is Rule 1')
self.assertEqual(vars[0]['field_type'], 'string')
self.assertEqual(vars[0]['options'], [])
self.assertEqual(vars[0]['params'], [{'fieldType': FIELD_TEXT, 'name': 'foo', 'label': 'Foo'}])

# should work on an instance of the class too
self.assertEqual(len(SomeVariables().get_all_variables()), 1)