diff --git a/tests/helpers_tests/test_condition.py b/tests/helpers_tests/test_condition.py index 7200e460..f5fb9aae 100644 --- a/tests/helpers_tests/test_condition.py +++ b/tests/helpers_tests/test_condition.py @@ -201,37 +201,6 @@ def test_exact_int__returns_true__when_user_provided_value_is_equal_to_condition self.assertStrictTrue(evaluator.evaluate(0)) - def test_exact_int__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_int_condition_list, {'lasers_count': long(9000)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(long(9000)) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_int_condition_list, {'lasers_count': 9000} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=False) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(9000) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_int_condition_list, {'lasers_count': 9000.0} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=False) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(9000.0) - def test_exact_float__returns_true__when_user_provided_value_is_equal_to_condition_value(self): if PY2: @@ -253,37 +222,6 @@ def test_exact_float__returns_true__when_user_provided_value_is_equal_to_conditi self.assertStrictTrue(evaluator.evaluate(0)) - def test_exact_float__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_float_condition_list, {'lasers_count': long(9000)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(long(9000)) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_float_condition_list, {'lasers_count': 9000} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=False) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(9000) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - exact_float_condition_list, {'lasers_count': 9000.0} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=False) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(9000.0) - def test_exact_int__returns_false__when_user_provided_value_is_not_equal_to_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -344,6 +282,27 @@ def test_exact_float__returns_null__when_no_user_provided_value(self): self.assertIsNone(evaluator.evaluate(0)) + def test_exact__given_number_values__calls_is_finite_number(self): + """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ + + evaluator = condition_helper.CustomAttributeConditionEvaluator( + exact_int_condition_list, {'lasers_count': 9000} + ) + + with mock.patch('optimizely.helpers.validator.is_finite_number', + return_value=True) as is_finite: + self.assertTrue(evaluator.evaluate(0)) + is_finite.assert_called_with(9000) + + evaluator = condition_helper.CustomAttributeConditionEvaluator( + exact_int_condition_list, {'lasers_count': 9000.0} + ) + + with mock.patch('optimizely.helpers.validator.is_finite_number', + return_value=False) as is_finite: + self.assertIsNone(evaluator.evaluate(0)) + is_finite.assert_called_with(9000.0) + def test_exact_bool__returns_true__when_user_provided_value_is_equal_to_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -429,42 +388,6 @@ def test_greater_than_int__returns_true__when_user_value_greater_than_condition_ self.assertStrictTrue(evaluator.evaluate(0)) - def test_greater_than_int__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_int_condition_list, {'meters_travelled': 48.1} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(48.1) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_int_condition_list, {'meters_travelled': 49} - ) - - def side_effect(*args): - if args[0] == 49: - return False - return True - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_any_call(49) - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_int_condition_list, {'meters_travelled': long(49)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(long(49)) - def test_greater_than_float__returns_true__when_user_value_greater_than_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -486,42 +409,6 @@ def test_greater_than_float__returns_true__when_user_value_greater_than_conditio self.assertStrictTrue(evaluator.evaluate(0)) - def test_greater_than_float__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_float_condition_list, {'meters_travelled': 48.3} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(48.3) - - def side_effect(*args): - if args[0] == 49: - return False - return True - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_float_condition_list, {'meters_travelled': 49} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(49) - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - gt_float_condition_list, {'meters_travelled': long(49)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(long(49)) - def test_greater_than_int__returns_false__when_user_value_not_greater_than_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -629,42 +516,6 @@ def test_less_than_int__returns_true__when_user_value_less_than_condition_value( self.assertStrictTrue(evaluator.evaluate(0)) - def test_less_than_int__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_int_condition_list, {'meters_travelled': 47.9} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(47.9) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_int_condition_list, {'meters_travelled': 47} - ) - - def side_effect(*args): - if args[0] == 47: - return False - return True - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_any_call(47) - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_int_condition_list, {'meters_travelled': long(47)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(long(47)) - def test_less_than_float__returns_true__when_user_value_less_than_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -686,42 +537,6 @@ def test_less_than_float__returns_true__when_user_value_less_than_condition_valu self.assertStrictTrue(evaluator.evaluate(0)) - def test_less_than_float__calls_is_finite_number(self): - """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_float_condition_list, {'meters_travelled': 48.1} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - return_value=True) as is_finite: - self.assertStrictTrue(evaluator.evaluate(0)) - is_finite.assert_called_with(48.1) - - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_float_condition_list, {'meters_travelled': 48} - ) - - def side_effect(*args): - if args[0] == 48: - return False - return True - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_any_call(48) - - if PY2: - evaluator = condition_helper.CustomAttributeConditionEvaluator( - lt_float_condition_list, {'meters_travelled': long(48)} - ) - - with mock.patch('optimizely.helpers.validator.is_finite_number', - side_effect=side_effect) as is_finite: - self.assertIsNone(evaluator.evaluate(0)) - is_finite.assert_called_with(long(48)) - def test_less_than_int__returns_false__when_user_value_not_less_than_condition_value(self): evaluator = condition_helper.CustomAttributeConditionEvaluator( @@ -796,6 +611,82 @@ def test_less_than_float__returns_null__when_no_user_provided_value(self): self.assertIsNone(evaluator.evaluate(0)) + def test_greater_than__calls_is_finite_number(self): + """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ + + evaluator = condition_helper.CustomAttributeConditionEvaluator( + gt_int_condition_list, {'meters_travelled': 48.1} + ) + + def is_finite_number__rejecting_condition_value(value): + if value == 48: + return False + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__rejecting_condition_value) as is_finite_mock: + self.assertIsNone(evaluator.evaluate(0)) + + # assert that isFiniteNumber only needs to reject condition value to stop evaluation. + is_finite_mock.assert_called_once_with(48) + + def is_finite_number__rejecting_user_attribute_value(value): + if value == 48.1: + return False + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__rejecting_user_attribute_value) as is_finite_mock: + self.assertIsNone(evaluator.evaluate(0)) + + # assert that isFiniteNumber evaluates user value only if it has accepted condition value. + is_finite_mock.assert_has_calls([mock.call(48), mock.call(48.1)]) + + def is_finite_number__accepting_both_values(value): + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__accepting_both_values): + self.assertTrue(evaluator.evaluate(0)) + + def test_less_than__calls_is_finite_number(self): + """ Returns True if is_finite_number returns True. Returns None if is_finite_number returns False. """ + + evaluator = condition_helper.CustomAttributeConditionEvaluator( + lt_int_condition_list, {'meters_travelled': 47} + ) + + def is_finite_number__rejecting_condition_value(value): + if value == 48: + return False + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__rejecting_condition_value) as is_finite_mock: + self.assertIsNone(evaluator.evaluate(0)) + + # assert that isFiniteNumber only needs to reject condition value to stop evaluation. + is_finite_mock.assert_called_once_with(48) + + def is_finite_number__rejecting_user_attribute_value(value): + if value == 47: + return False + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__rejecting_user_attribute_value) as is_finite_mock: + self.assertIsNone(evaluator.evaluate(0)) + + # assert that isFiniteNumber evaluates user value only if it has accepted condition value. + is_finite_mock.assert_has_calls([mock.call(48), mock.call(47)]) + + def is_finite_number__accepting_both_values(value): + return True + + with mock.patch('optimizely.helpers.validator.is_finite_number', + side_effect=is_finite_number__accepting_both_values): + self.assertTrue(evaluator.evaluate(0)) + class ConditionDecoderTests(base.BaseTest): diff --git a/tests/helpers_tests/test_validator.py b/tests/helpers_tests/test_validator.py index 23b28757..cca4a0c5 100644 --- a/tests/helpers_tests/test_validator.py +++ b/tests/helpers_tests/test_validator.py @@ -220,6 +220,7 @@ def test_is_finite_number(self): self.assertTrue(validator.is_finite_number(0)) self.assertTrue(validator.is_finite_number(5)) self.assertTrue(validator.is_finite_number(5.5)) + # float(2**53) + 1.0 evaluates to float(2**53) self.assertTrue(validator.is_finite_number(float(2**53) + 1.0)) self.assertTrue(validator.is_finite_number(-float(2**53) - 1.0)) self.assertTrue(validator.is_finite_number(int(2**53)))