def test_when_base_returns_invalid_when_passed_an_invalid_instance(self): val = _when_base(None, Invalid("abc")) self.assertTrue(isinstance(val, Invalid)) self.assertEqual(str(val), "abc") val = _when_base(None, InvalidGroup("", [Invalid("a"), Invalid("b")])) self.assertTrue(isinstance(val, InvalidGroup)) self.assertEqual(str(val), "a\nb")
def test_creating_an_invalid_exception_saves_the_passed_in_data(self): instance = Invalid("") self.assertIsNone(instance.data) instance = Invalid("", 123) self.assertEqual(instance.data, 123) instance = Invalid("", data=123) self.assertEqual(instance.data, 123)
def test_creating_an_invalid_exception_saves_the_passed_in_path(self): instance = Invalid("") self.assertEqual(instance.path, []) instance = Invalid("", None, [1, "b", 3]) self.assertEqual(instance.path, [1, "b", 3]) instance = Invalid("", path=[1, "b", 3]) self.assertEqual(instance.path, [1, "b", 3])
def inner(data): if data is Undefined: return Undefined if minimum is not None and data < minimum: raise Invalid("Must be at least {0}".format(minimum)) if maximum is not None and data > maximum: raise Invalid("Must be no more than {0}".format(maximum)) return data
def test_any_raises_invalid_when_no_valid_value_exists(self): invalid_1 = Mock(side_effect=Invalid("1")) invalid_2 = Mock(side_effect=Invalid("2")) with self.assertRaises(Invalid): anyfunc = Any(invalid_1) anyfunc("abcd") with self.assertRaises(Invalid): anyfunc = Any(invalid_1, invalid_2) anyfunc("abcd")
def test_require_one_returns_data_with_invalids_removed(self): data = { "a": Invalid("a"), "b": 1, "c": "abc", "d": Invalid("d"), "e": 2, "f": 3 } req = require_one("a", "b", "d", "f") val = req(InvalidGroup("", [data["a"], data["d"]], data)) self.assertEqual(val, {"b": 1, "c": "abc", "e": 2})
def test_any_returns_first_value_where_invalid_is_not_raised(self): invalid_1 = Mock(side_effect=Invalid("1")) invalid_2 = Mock(side_effect=Invalid("2")) invalid_3 = Mock(side_effect=Invalid("3")) invalid_4 = Mock(side_effect=Invalid("4")) valid = Mock(return_value=1) anyfunc = Any(invalid_1, invalid_2, invalid_3, valid, invalid_4) val = anyfunc("abcd") self.assertEqual(val, 1) invalid_1.assert_called_once_with("abcd") invalid_2.assert_called_once_with("abcd") invalid_3.assert_called_once_with("abcd") valid.assert_called_once_with("abcd") self.assertFalse(invalid_4.called)
def test_require_one_returns_an_invalid_group_with_invalids_removed(self): data = { "a": Invalid("a"), "b": 1, "c": Invalid("c"), "d": Invalid("d"), "e": 2, "f": 3 } req = require_one("a", "b", "d", "f") with self.assertRaises(InvalidGroup) as cm: req(InvalidGroup("", [data["a"], data["c"], data["d"]], data)) self.assertEqual(str(cm.exception), "c") self.assertEqual(cm.exception.data, {"b": 1, "c": data["c"], "e": 2})
def test_calling_a_schema_raises_invalid_group_when_data_is_invalid(self): invalid = Invalid("abc") mocked = Mock(side_effect=invalid) with patch.object(Schema, "_validate", mocked): with self.assertRaises(InvalidGroup) as cm: instance = Schema({"a": int}) instance({"a": 1}) self.assertEqual(map(str, cm.exception.errors), ["abc"]) invalid = InvalidGroup("", [Invalid("def"), Invalid("ghi")]) mocked = Mock(side_effect=invalid) with patch.object(Schema, "_validate", mocked): with self.assertRaises(InvalidGroup) as cm: instance = Schema({"a": int}) instance({"a": 1}) self.assertEqual(map(str, cm.exception.errors), ["def", "ghi"])
def percents_add_to_100(data): total = 0 for project in data: total += project["percentage"] if total != 100: raise Invalid("the percentages must add up to 100") return data
def inner(data): if data is Undefined: return Undefined try: return func(data) except (ValueError, TypeError): raise Invalid("Expected {0}".format(func.__name__))
def test_any_should_consider_all_values_valid_unless_invalid_raised(self): def return_func(data): return data mocked = Mock(return_value=1) anyfunc = Any(return_func, mocked) val = anyfunc(0) self.assertEqual(val, 0) self.assertFalse(mocked.called) val = anyfunc(0.0) self.assertEqual(val, 0.0) self.assertFalse(mocked.called) val = anyfunc(None) self.assertEqual(val, None) self.assertFalse(mocked.called) val = anyfunc(False) self.assertEqual(val, False) self.assertFalse(mocked.called) val = anyfunc(Undefined) self.assertEqual(val, Undefined) self.assertFalse(mocked.called) invalid = Invalid("") val = anyfunc(invalid) self.assertEqual(val, invalid) self.assertFalse(mocked.called)
def _when_base(data, func): if func == Invalid: return Invalid("A value is required") if isinstance(func, Invalid): return func if callable(func): return func(data) return func
def test_validate_function_catches_and_raises_invalid_group(self): invalid_group = InvalidGroup( "", [Invalid("abc", 8), Invalid("def", 9, [3])], 4) func = Mock(side_effect=invalid_group) with self.assertRaises(InvalidGroup) as cm: Schema._validate_function(func, 2, []) ex = cm.exception self.assertEqual(map(str, ex.errors), ["abc", "def @ data[3]"]) self.assertEqual([e.data for e in ex.errors], [8, 9]) self.assertEqual(ex.data, 4) with self.assertRaises(InvalidGroup) as cm: Schema._validate_function(func, 3, [1, "a"]) ex = cm.exception self.assertEqual(map(str, ex.errors), ["abc @ data[1][a]", "def @ data[1][a][3]"]) self.assertEqual([e.data for e in ex.errors], [8, 9]) self.assertEqual(ex.data, 4)
def test_require_if_returns_an_invalid_group_with_invalids_removed(self): data = {"a": Invalid("a"), "b": 1, "c": Invalid("c"), "d": 2} req = require_if("a", Mock(return_value=False)) with self.assertRaises(InvalidGroup) as cm: req(InvalidGroup("", [data["a"], data["c"]], data)) self.assertEqual(str(cm.exception), "c") self.assertEqual(cm.exception.data, {"b": 1, "c": data["c"], "d": 2}) data = {"a": Invalid("a"), "b": 1, "c": Invalid("c"), "d": 2} req = require_if("a", Mock(return_value=True)) with self.assertRaises(InvalidGroup) as cm: req(InvalidGroup("", [data["a"], data["c"]], data)) self.assertEqual(str(cm.exception), "a\nc") self.assertEqual(cm.exception.data, { "a": data["a"], "b": 1, "c": data["c"], "d": 2 })
def test_validate_function_catches_and_raises_invalid(self): invalid = Invalid("abc", 4) func = Mock(side_effect=invalid) with self.assertRaises(Invalid) as cm: Schema._validate_function(func, 2, []) self.assertEqual(str(cm.exception), "abc") self.assertEqual(cm.exception.data, 4) with self.assertRaises(Invalid) as cm: Schema._validate_function(func, 3, [1, "a"]) self.assertEqual(str(cm.exception), "abc @ data[1][a]") self.assertEqual(cm.exception.data, 4)
def test_all_should_consider_all_values_valid_unless_invalid_raised(self): def return_func(data): return data allfunc = All(return_func) self.assertEqual(allfunc(0), 0) self.assertEqual(allfunc(0.0), 0.0) self.assertEqual(allfunc(None), None) self.assertEqual(allfunc(False), False) self.assertEqual(allfunc(Undefined), Undefined) invalid = Invalid("") self.assertEqual(allfunc(invalid), invalid)
def inner(data): working_data = data is_invalid = False if isinstance(data, InvalidBase): working_data = data.data is_invalid = True if not isinstance(working_data, dict): raise Invalid("Expected an object") is_required = func(working_data) if not is_required: if is_invalid and isinstance(working_data[field], InvalidBase): data.errors.remove(working_data[field]) del working_data[field] if is_invalid and data.errors: data.data = working_data raise data return working_data
def test_all_calls_validators_until_invalid_is_raised(self): valid_1 = Mock(return_value=1) valid_2 = Mock(return_value=1) valid_3 = Mock(return_value=1) invalid = Mock(side_effect=Invalid("1")) allfunc = All(valid_1, valid_2, valid_3) allfunc(1) valid_1.assert_called_once_with(1) valid_2.assert_called_once_with(1) valid_3.assert_called_once_with(1) valid_1.reset_mock() valid_2.reset_mock() valid_3.reset_mock() with self.assertRaises(Invalid): allfunc = All(valid_1, valid_2, invalid, valid_3) allfunc(1) valid_1.assert_called_once_with(1) valid_2.assert_called_once_with(1) invalid.assert_called_once_with(1) self.assertFalse(valid_3.called)
def inner(data): working_data = data is_invalid = False if isinstance(data, InvalidGroup): working_data = data.data is_invalid = True if not isinstance(working_data, dict): raise Invalid("Expected an object") valid_field = None for field in fields: if not isinstance(working_data[field], InvalidBase): valid_field = field break if valid_field: for field in fields: if field == valid_field: continue if is_invalid and isinstance(working_data[field], InvalidBase): data.errors.remove(working_data[field]) del working_data[field] if is_invalid and data.errors: data.data = working_data raise data return working_data
def test_require_one_returns_data_with_invalids_removed(self): data = {"a": Invalid("a"), "b": 1, "c": "abc", "d": 2} req = require_if("a", Mock(return_value=False)) val = req(InvalidGroup("", [data["a"]], data)) self.assertEqual(val, {"b": 1, "c": "abc", "d": 2})
def test_invalid_group_repr_returns_invalid_group_object(self): instance = InvalidGroup("", [Invalid("a", path=[1]), Invalid("b")]) self.assertEqual( repr(instance), "InvalidGroup([Invalid(\"a\", [1]), Invalid(\"b\", [])])")
def mock_return(schema, data, path): if not isinstance(data, schema): raise Invalid("", data, path) if schema == unicode: return "{0}r".format(data) return data + 1
def test_creating_an_invalid_exception_saves_the_passed_in_message(self): instance = Invalid("Invalid message") self.assertEqual(instance.message, "Invalid message")
def mock_return(schema, data, path): if data is Undefined: return Undefined if not isinstance(data, schema): raise Invalid("err", data, path) return data
def test_invalid_group_returns_all_error_messages_as_its_str(self): instance = InvalidGroup("", [Invalid("a", path=[1]), Invalid("b")]) self.assertEqual(str(instance), "a @ data[1]\nb")
def test_invalid_exception_prints_path_if_given(self): instance = Invalid("Invalid message") self.assertEqual(str(instance), "Invalid message") instance = Invalid("Invalid message", path=[1, "b", 3]) self.assertEqual(str(instance), "Invalid message @ data[1][b][3]")
def inner(data): if data is Undefined: return Undefined if data not in allowed: raise Invalid("Not in list of valid values") return data
def test_invalid_exception_repr_returns_invalid_object(self): instance = Invalid("Invalid message") self.assertEqual(repr(instance), "Invalid(\"Invalid message\", [])") instance = Invalid("Invalid message", path=[1, "b", 3]) self.assertEqual(repr(instance), "Invalid(\"Invalid message\", [1, b, 3])")
def mock_return(schema, data, path): if not isinstance(data, schema): raise Invalid("") return data