Ejemplo n.º 1
0
    def test_match_type_mixed(self):
        datatype = Union(
            str,
            # XXX add dedicated enums
            Enum(int, (1, 2, 3)),
            Basic,
            Array(str),
            Array(int),
            Union(int, bool),
        )
        param = UnionParameter(datatype)

        tests = [
            ('spam', SimpleParameter.HANDLER(str)),
            (2, EnumParameter.HANDLER(int, (1, 2, 3))),
            (BASIC_FULL, ComplexParameter(Basic).match_type(BASIC_FULL)),
            (['spam'], ArrayParameter.HANDLER(Array(str))),
            ([], ArrayParameter.HANDLER(Array(str))),
            ([10], ArrayParameter.HANDLER(Array(int))),
            (10, SimpleParameter.HANDLER(int)),
            (True, SimpleParameter.HANDLER(bool)),
            # no match
            (Integer(2), None),
            ([True], None),
            ({}, None),
        ]
        for value, expected in tests:
            with self.subTest(value):
                handler = param.match_type(value)

                self.assertEqual(handler, expected)
Ejemplo n.º 2
0
    def test_match_type_catchall(self):
        NOOP = DatatypeHandler(ANY)
        param = UnionParameter(Union(int, str, ANY))
        tests = [
            ('spam', SimpleParameter.HANDLER(str)),
            (10, SimpleParameter.HANDLER(int)),
            # catchall
            (BASIC_FULL, NOOP),
            (['spam'], NOOP),
            (True, NOOP),
            (Integer(2), NOOP),
            ([10], NOOP),
            ({}, NOOP),
        ]
        for value, expected in tests:
            with self.subTest(value):
                handler = param.match_type(value)

                self.assertEqual(handler, expected)
Ejemplo n.º 3
0
 def test_validate_valid(self):
     tests = {
         str: 'spam',
         int: 10,
         bool: True,
     }
     for datatype, value in tests.items():
         with self.subTest(datatype):
             handler = SimpleParameter.HANDLER(datatype)
             handler.validate(value)
Ejemplo n.º 4
0
    def test_coerce(self):
        tests = [
            (str, 'spam', 'spam'),
            (str, String('spam'), 'spam'),
            (int, 10, 10),
            (bool, True, True),
            # did not match, but still coercible
            (str, 10, '10'),
            (str, str, "<class 'str'>"),
            (int, 10.0, 10),
            (int, '10', 10),
            (bool, 1, True),
        ]
        for datatype, value, expected in tests:
            with self.subTest((datatype, value)):
                handler = SimpleParameter.HANDLER(datatype)
                coerced = handler.coerce(value)

                self.assertEqual(coerced, expected)
Ejemplo n.º 5
0
    def test_validate_invalid(self):
        tests = [
            (int, 'spam'),
            # coercible
            (str, String('spam')),
            (str, 10),
            (int, 10.0),
            (int, '10'),
            (bool, 1),
            # semi-coercible
            (str, b'spam'),
            (int, 10 + 0j),
            (int, b'\10'),
        ]
        for datatype, value in tests:
            with self.subTest((datatype, value)):
                handler = SimpleParameter.HANDLER(datatype)

                with self.assertRaises(ValueError):
                    handler.validate(value)
Ejemplo n.º 6
0
    def test_as_data(self):
        tests = [
            (str, 'spam'),
            (int, 10),
            (bool, True),
            # did not match, but still coercible
            (str, String('spam')),
            (str, 10),
            (str, str),
            (int, 10.0),
            (int, '10'),
            (bool, 1),
            # semi-coercible
            (str, b'spam'),
            (int, 10 + 0j),
            (int, b'\10'),
        ]
        for datatype, value in tests:
            with self.subTest((datatype, value)):
                handler = SimpleParameter.HANDLER(datatype)
                data = handler.as_data(value)

                self.assertIs(data, value)