def test_set_composite(self): validator = is_type({cast(int) & passes(lambda x: x >= 10)}) self.assertEqual({10, 11, 12}, validator({10,11,12})) self.assertRaises(ValueError, validator, {"foo"}) self.assertRaises(AssertionError, validator, {1})
def test_list_composite(self): validator = is_type([cast(int) & passes(lambda x: x >= 10)]) self.assertEqual([10, 11, 12], validator([10,11,12])) self.assertRaises(ValueError, validator, ["foo"]) self.assertRaises(AssertionError, validator, [1])
def test_complex(self): validator = is_type( [ { "name": is_type(str) & passes(len), "age": cast(int) & passes(lambda x: 18 <= x <= 99), "sex": cast(str.lower) & passes(lambda s: s in ["male", "female"]) } ]) data = [{'name': 'Sue', 'age': '28', 'sex': 'FEMALE'}, {'name': 'Sam', 'age': '42', 'sex': 'Male'}, {'name': 'Sacha', 'age': '20', 'sex': 'Male'}] self.assertEqual( [{'name': 'Sue', 'age': 28, 'sex': 'female'}, {'name': 'Sam', 'age': 42, 'sex': 'male'}, {'name': 'Sacha', 'age': 20, 'sex': 'male'}], validator(data))
def test(self): self.assertEqual([1], passes(len)([1])) self.assertRaises(AssertionError, passes(len), [])