Beispiel #1
0
    def test_dont_reraise_builtin_nonstring(self):
        thrown = ValueError(1)

        def inner(value):
            raise thrown

        with self.assertRaises(ValueError) as cm:
            validate_tuple(("value", ), schema=(inner, ))
        caught = cm.exception

        self.assertIs(caught, thrown)
Beispiel #2
0
    def test_reraise_builtin_nomessage(self):
        thrown = TypeError()

        def inner(value):
            raise thrown

        with self.assertRaises(TypeError) as cm:
            validate_tuple(("value", ), schema=(inner, ))
        caught = cm.exception

        self.assertIs(caught, thrown)
Beispiel #3
0
    def test_reraise_builtin(self):
        thrown = TypeError("message")

        def inner(value):
            raise thrown

        with self.assertRaises(TypeError) as cm:
            validate_tuple(("value", ), schema=(inner, ))
        caught = cm.exception

        self.assertIsNot(caught, thrown)
        self.assertEqual(str(caught), "invalid value at index 0: message")
Beispiel #4
0
    def test_dont_reraise_builtin_subclass(self):
        class DerivedException(ValueError):
            pass

        thrown = DerivedException("message")

        def inner(value):
            raise thrown

        with self.assertRaises(ValueError) as cm:
            validate_tuple(("value", ), schema=(inner, ))
        caught = cm.exception

        self.assertIs(caught, thrown)
Beispiel #5
0
    def test_schema_valid(self):  # type: () -> None
        validator = validate_tuple(schema=(
            validate_text(),
            validate_int(),
        ))

        validator((u"hello world", 9001))
Beispiel #6
0
    def test_schema_invalid_value(self):  # type: () -> None
        validator = validate_tuple(schema=(
            validate_text(),
            validate_int(min_value=0),
        ))

        with self.assertRaises(ValueError):
            validator((u"string", -1))
Beispiel #7
0
    def test_schema_invalid_value_type(self):
        validator = validate_tuple(schema=(
            validate_text(),
            validate_int(),
        ))

        with self.assertRaises(TypeError):
            validator((u"string", '1000'))
Beispiel #8
0
    def test_schema_positional_argument(self):  # type: () -> None
        def validator(*args):
            assert len(args) == 1

        validate_tuple((u"value", ), schema=(validator, ))
Beispiel #9
0
 def test_repr_2(self):  # type: () -> None
     validator = validate_tuple(length=2, required=False)
     self.assertEqual(
         repr(validator),
         'validate_tuple(length=2, required=False)',
     )
Beispiel #10
0
 def test_not_required(self):  # type: () -> None
     validate_tuple(None, required=False)
Beispiel #11
0
 def test_repr_1(self):  # type: () -> None
     validator = validate_tuple(schema=(validate_int(), ))
     self.assertEqual(
         repr(validator),
         'validate_tuple(schema=(validate_int(),))',
     )
Beispiel #12
0
 def test_length_just_right(self):  # type: () -> None
     validator = validate_tuple(length=2)
     validator((1, 2))
Beispiel #13
0
    def test_length_too_short(self):
        validator = validate_tuple(length=2)

        with self.assertRaises(TypeError):
            validator((1, ))
Beispiel #14
0
    def test_schema_too_short(self):
        validator = validate_tuple(schema=(validate_int(), validate_int()))

        with self.assertRaises(TypeError):
            validator((1, ))
Beispiel #15
0
 def test_basic_valid(self):  # type: () -> None
     validate_tuple((1, 'string'))
Beispiel #16
0
 def test_required(self):
     with self.assertRaises(TypeError):
         validate_tuple(None)
Beispiel #17
0
 def test_schema_length_mutually_exclusive(self):
     with self.assertRaises(TypeError):
         validate_tuple(schema=(validate_int(), ), length=1)
Beispiel #18
0
 def test_invalid_container_type(self):
     with self.assertRaises(TypeError):
         validate_tuple([])