示例#1
0
    def test_data2rec(self):
        right = [
            "10.0.0.1",
            "10 mbox.example.com",
            "10 20 30 example.com",
        ]
        schemas = [
            OrderedDict((("address", ipaddress.IPv4Address),)),
            OrderedDict((("preference", int), ("name", str),)),
            OrderedDict(
                (("prio", int), ("weight", int), ("port", _to_port), ("name", str),)
            ),
        ]

        results = [
            ipaddress.IPv4Address(right[0]),
            {"preference": 10, "name": "mbox.example.com"},
            {"prio": 10, "weight": 20, "port": 30, "name": "example.com"},
        ]

        for rdata, rschema, res in zip(right, schemas, results):
            self.assertEqual(_data2rec(rschema, rdata), res)

        wrong = [
            "not-an-ip",
            "hundred 20 30 interror.example.com",
            "10 toolittle.example.com",
        ]

        for rdata, rschema in zip(wrong, schemas):
            self.assertRaises(ValueError, _data2rec, rschema, rdata)
示例#2
0
    def test_data2rec(self):
        right = [
            '10.0.0.1',
            '10 mbox.example.com',
            '10 20 30 example.com',
        ]
        schemas = [
            OrderedDict((('address', ipaddress.IPv4Address), )),
            OrderedDict((
                ('preference', int),
                ('name', str),
            )),
            OrderedDict((
                ('prio', int),
                ('weight', int),
                ('port', _to_port),
                ('name', str),
            ))
        ]

        results = [
            ipaddress.IPv4Address(right[0]), {
                'preference': 10,
                'name': 'mbox.example.com'
            }, {
                'prio': 10,
                'weight': 20,
                'port': 30,
                'name': 'example.com'
            }
        ]

        for rdata, rschema, res in zip(right, schemas, results):
            self.assertEqual(_data2rec(rschema, rdata), res)

        wrong = [
            'not-an-ip',
            'hundred 20 30 interror.example.com',
            '10 toolittle.example.com',
        ]

        for rdata, rschema in zip(wrong, schemas):
            self.assertRaises(ValueError, _data2rec, rschema, rdata)