Ejemplo n.º 1
0
    def __init__(self, data_filter=None, do_validate=False):
        """Creates a v6 data filter JSON object from the given dictionary

        :param data_filter: The data filter JSON dict
        :type data_filter: dict
        :param do_validate: Whether to perform validation on the JSON schema
        :type do_validate: bool

        :raises :class:`data.filter.exceptions.InvalidDataFilter`: If the given data filter is invalid
        """

        if not data_filter:
            data_filter = {'filters': [], 'all': True}
        self._data_filter = data_filter

        if 'version' not in self._data_filter:
            self._data_filter['version'] = SCHEMA_VERSION

        if self._data_filter['version'] not in SCHEMA_VERSIONS:
            msg = '%s is an unsupported version number'
            raise InvalidDataFilter('INVALID_VERSION',
                                    msg % self._data_filter['version'])

        if 'all' not in self._data_filter:
            self._data_filter['all'] = True

        if 'filters' not in self._data_filter:
            self._data_filter['filters'] = []

        try:
            if do_validate:
                validate(self._data_filter, DATA_FILTER_SCHEMA)
                for f in data_filter['filters']:
                    DataFilter.validate_filter(f)
        except ValidationError as ex:
            raise InvalidDataFilter('INVALID_DATA_FILTER',
                                    'Invalid data filter: %s' % unicode(ex))
Ejemplo n.º 2
0
    def test_validate_filter(self):
        """Tests calling DataFilter.validate_filter()"""

        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({})
        self.assertEqual(context.exception.error.name, 'MISSING_NAME')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a'})
        self.assertEqual(context.exception.error.name, 'MISSING_TYPE')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'integer'})
        self.assertEqual(context.exception.error.name, 'MISSING_CONDITION')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'integer', 'condition': '>'})
        self.assertEqual(context.exception.error.name, 'MISSING_VALUES')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'integer', 'condition': 'BAD', 'values': [0]})
        self.assertEqual(context.exception.error.name, 'INVALID_CONDITION')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'string', 'condition': 'between', 'values': ['0']})
        self.assertEqual(context.exception.error.name, 'INVALID_CONDITION')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'integer', 'condition': 'contains', 'values': [0]})
        self.assertEqual(context.exception.error.name, 'INVALID_CONDITION')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'boolean', 'condition': 'contains', 'values': [0]})
        self.assertEqual(context.exception.error.name, 'INVALID_CONDITION')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'bad', 'condition': 'contains', 'values': [0]})
        self.assertEqual(context.exception.error.name, 'INVALID_TYPE')
        
        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'integer', 'condition': '<', 'values': ['not a number']})
        self.assertEqual(context.exception.error.name, 'VALUE_ERROR')

        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'object', 'condition': '<', 'values': [['not a number']]})
        self.assertEqual(context.exception.error.name, 'INVALID_CONDITION')

        with self.assertRaises(InvalidDataFilter) as context:
            DataFilter.validate_filter({'name': 'input_a', 'type': 'object', 'condition': '<', 'values': [['not a number']], 'fields': [['a'],['b']]})
        self.assertEqual(context.exception.error.name, 'INVALID_FIELDS')