Example #1
0
    def test_list_of_dict(self):
        # like config default
        default_ip_addrs = [
            # {'type': 'private', 'addr': '192.168.0.0'} ...
        ]
        
        base_rule = Dict({
            'type': BaseField(
                validator=Any(Equal('private'), Equal('global'))),
            'addr': BaseField(
                # IPv4 regexp
                validator=Regex(('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
                                 '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')))
        })
        # allow empty ip addr list
        allow_empty_rule = Seq(base_rule)
        disallow_empty_rule = Seq(base_rule, __disallow_empty=True)

        # input config data
        data = [
            {'type': 'private', 'addr': '10.0.0.0'},
            {'type': 'private', 'addr': '192.168.0.0'},
            {'type': 'global', 'addr': '82.94.164.162'},
        ]

        # default data validation
        defaults = StructuredFields.validate(
                default_ip_addrs, allow_empty_rule)
        ok_(isinstance(defaults, list))
        eq_(len(defaults), 0)
        # default data validation with disallow empty flag
        self.assertRaises(ValidationError,
                          StructuredFields.validate,
                          default_ip_addrs, disallow_empty_rule)

        # updated data validation
        defaults.extend(data)
        config = StructuredFields.validate(
                defaults, allow_empty_rule)
        ok_(isinstance(config, list))
        eq_(len(config), 3)
Example #2
0
    def test_twitter_like_result(self):
        data = [
            {
                'id': 123456,
                'name': 'Hoge Piyo',
                'screen_name': 'hogepiyo',
                'followers': [234, 890, 123457]
            },
            {
                'id': 123457,
                'name': 'Zoge Piyo',
                'screen_name': 'zogepiyo',
                'followers': [234, 567, 123456]
            },
            {
                'id': 123458,
                'name': 'Moge Puyo',
                'screen_name': 'mogepuyo',
                'followers': []
            }
        ]

        class IDField(BaseField):
            validator = Number()
            converter = int_converter
        class NameField(BaseField):
            validator = String()
        class ScreenNameField(NameField):
            pass
        rule = Seq(
                Dict(
                    id=IDField(required=True),
                    name=NameField(required=True),
                    screen_name=ScreenNameField(required=True),
                    followers=Seq(
                        IDField()
                    )
                ))

        ret = StructuredFields.validate(data, rule)
        assert isinstance(ret, list)
        assert len(ret) == 3
        for i, user in enumerate(ret):
            assert isinstance(user, dict)
            assert 'id' in user
            assert user['id'] == data[i]['id']
            assert 'name' in user
            assert user['name'] == data[i]['name']
            assert 'screen_name' in user
            assert user['screen_name'] == data[i]['screen_name']
            assert 'followers' in user
            assert isinstance(user['followers'], list), i
            assert user['followers'] == data[i]['followers']