def test_is_data_accepted(self): """Tests calling DataFilter.is_data_accepted()""" data_filter = DataFilter(all=False) data_filter.add_filter({'name': 'input_a', 'type': 'media-type', 'condition': '==', 'values': ['application/json']}) data_filter.add_filter({'name': 'input_b', 'type': 'string', 'condition': 'contains', 'values': ['abcde']}) data_filter.add_filter({'name': 'input_c', 'type': 'integer', 'condition': '>', 'values': ['0']}) data_filter.add_filter({'name': 'input_d', 'type': 'integer', 'condition': 'between', 'values': ['0', '100']}) data_filter.add_filter({'name': 'input_f', 'type': 'meta-data', 'condition': 'in', 'values': [['foo', 'baz']], 'fields': [['a', 'b']]}) data = Data() file_value = FileValue('input_a', [self.file1.id]) data.add_value(file_value) # first filter passes, so data is accepted if all is set to false self.assertTrue(data_filter.is_data_accepted(data)) data_filter.all = True # other filters fail so data is not accepted self.assertFalse(data_filter.is_data_accepted(data)) # get other filters to pass json_value = JsonValue('input_b', 'abcdefg') data.add_value(json_value) json_value = JsonValue('input_c', '10') data.add_value(json_value) json_value = JsonValue('input_d', 50) data.add_value(json_value) file_value = FileValue('input_f', [self.file2.id]) data.add_value(file_value) self.assertTrue(data_filter.is_data_accepted(data))
def get_filter(self): """Returns the data filter represented by this JSON :returns: The data filter :rtype: :class:`data.filter.filter.DataFilter`: """ data_filter = DataFilter([], self._data_filter['all']) for f in self._data_filter['filters']: data_filter.add_filter(f) return data_filter
def test_validate(self): """Tests calling DataFilter.validate()""" data_filter = DataFilter(all=False) data_filter.add_filter({'name': 'input_a', 'type': 'media-type', 'condition': '==', 'values': ['application/json']}) data_filter.add_filter({'name': 'input_b', 'type': 'string', 'condition': 'contains', 'values': ['abcde']}) data_filter.add_filter({'name': 'input_c', 'type': 'integer', 'condition': '>', 'values': ['0']}) data_filter.add_filter({'name': 'input_d', 'type': 'integer', 'condition': 'between', 'values': ['0', '100']}) data_filter.add_filter({'name': 'input_f', 'type': 'meta-data', 'condition': 'in', 'values': [['foo','baz']], 'fields': [['a','b']]}) interface = Interface() interface.add_parameter(FileParameter('input_a', ['application/json'])) warnings = data_filter.validate(interface) self.assertEqual(len(warnings), 4) self.assertEqual(warnings[0].name, 'UNMATCHED_FILTER') interface.add_parameter(JsonParameter('input_e', 'integer')) warnings = data_filter.validate(interface) self.assertEqual(len(warnings), 5) self.assertEqual(warnings[3].name, 'UNMATCHED_FILTER') self.assertEqual(warnings[4].name, 'UNMATCHED_PARAMETERS') interface.add_parameter(JsonParameter('input_b', 'integer')) with self.assertRaises(InvalidDataFilter) as context: data_filter.validate(interface) self.assertEqual(context.exception.error.name, 'MISMATCHED_TYPE') interface2 = Interface() interface2.add_parameter(FileParameter('input_a', ['application/json'])) interface2.add_parameter(JsonParameter('input_b', 'string')) interface2.add_parameter(JsonParameter('input_c', 'integer')) interface2.add_parameter(JsonParameter('input_d', 'integer')) interface2.add_parameter(FileParameter('input_f', ['integer'])) warnings = data_filter.validate(interface2) self.assertEqual(len(warnings), 0)
def test_add_filter(self): """Tests calling DataFilter.add_value()""" data_filter = DataFilter() data_filter.add_filter({'name': 'input_a', 'type': 'media-type', 'condition': '==', 'values': ['application/json']}) data_filter.add_filter({'name': 'input_b', 'type': 'string', 'condition': 'contains', 'values': ['abcde']}) data_filter.add_filter({'name': 'input_c', 'type': 'integer', 'condition': '>', 'values': ['0']}) data_filter.add_filter({'name': 'input_d', 'type': 'integer', 'condition': 'between', 'values': ['0', '100']}) filter_dict = {'version': '6', 'filters': [ {'name': 'input_a', 'type': 'media-type', 'condition': '==', 'values': ['application/json']}, {'name': 'input_b', 'type': 'string', 'condition': 'contains', 'values': ['abcde']}, {'name': 'input_c', 'type': 'integer', 'condition': '>', 'values': [0]}, {'name': 'input_d', 'type': 'integer', 'condition': 'between', 'values': [0,100]} ]} self.assertItemsEqual(data_filter.filter_list, filter_dict['filters']) with self.assertRaises(InvalidDataFilter) as context: data_filter.add_filter({}) self.assertEqual(context.exception.error.name, 'MISSING_NAME') with self.assertRaises(InvalidDataFilter) as context: data_filter.add_filter({'name': 'input_a'}) self.assertEqual(context.exception.error.name, 'MISSING_TYPE') with self.assertRaises(InvalidDataFilter) as context: data_filter.add_filter({'name': 'input_a', 'type': 'integer'}) self.assertEqual(context.exception.error.name, 'MISSING_CONDITION')