Beispiel #1
0
    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})
Beispiel #2
0
    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])
Beispiel #3
0
    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))
Beispiel #4
0
    def test(self):
        validator = cast(int) & is_type(int)
        self.assertEqual(1, validator("1"))

        self.assertRaises(ValueError, validator, "monkey")
Beispiel #5
0
 def test(self):
     self.assertEqual(1, cast(int)("1"))