def test_validate_parameter_object_array_invalid(self): parameter_api = { 'name': 'name1', 'schema': { 'type': 'array', 'items': { 'type': 'object', 'properties': { 'A': { 'type': 'string' }, 'B': { 'type': 'integer' } } } } } value = [{ 'A': 'string in', 'B': '33' }, { 'A': 'string2', 'B': 'invalid' }] with self.assertRaises(InvalidArgument): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_primitive_no_formats_not_required_valid_bool(self): parameter_api = {'name': 'name1', 'type': 'boolean'} true_values = ['true', 'True', 'TRUE', 'TrUe'] false_values = ['false', 'False', 'FALSE', 'FaLSe'] for true_value in true_values: self.assertEqual(validate_parameter(true_value, parameter_api, self.message), True) for false_value in false_values: self.assertEqual(validate_parameter(false_value, parameter_api, self.message), False)
def test_validate_parameter_primitive_no_formats_required_string(self): parameter_api = {'name': 'name1', 'type': 'string', 'required': True} value = 'test string' self.assertEqual( validate_parameter(value, parameter_api, self.message), value) value = '' self.assertEqual( validate_parameter(value, parameter_api, self.message), value)
def test_validate_parameter_primitive_no_formats_required_none(self): parameter_apis = [{'name': 'name1', 'type': 'string', 'required': True}, {'name': 'name1', 'type': 'number', 'required': True}, {'name': 'name1', 'type': 'integer', 'required': True}, {'name': 'name1', 'type': 'boolean', 'required': True}] for parameter_api in parameter_apis: with self.assertRaises(InvalidInput): validate_parameter(None, parameter_api, self.message)
def test_validate_parameter_primitive_string_format_invalid(self): parameter_api = { 'name': 'name1', 'type': 'string', 'minLength': 1, 'maxLength': 3 } value = 'test string' with self.assertRaises(InvalidArgument): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_object_array(self): parameter_api = { 'name': 'name1', 'schema': {'type': 'array', 'items': {'type': 'object', 'properties': {'A': {'type': 'string'}, 'B': {'type': 'integer'}}} }} value = [{'A': 'string in', 'B': '33'}, {'A': 'string2', 'B': '7'}] validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_invalid_data_type(self): parameter_api = { 'name': 'name1', 'type': 'invalid', 'minLength': 1, 'maxLength': 25, 'enum': ['test', 'test3'] } value = 'test2' with self.assertRaises(InvalidArgument): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_object(self): parameter_api = { 'name': 'name1', 'schema': {'type': 'object', 'required': ['a', 'b'], 'properties': {'a': {'type': 'number'}, 'b': {'type': 'string'}, 'c': {'type': 'boolean'}}}} value = {'a': 435.6, 'b': 'aaaa', 'c': True} validate_parameter(value, parameter_api, self.message)
def __call__(self, data_in, accumulator): data = data_in for filter_element in self.filters: data = filter_element(data, accumulator) try: data = validate_parameter(data, self.data_in_api, 'Flag {0}'.format(self.action)) args = dereference_step_routing(self.args, accumulator, 'In Flag {0}'.format(self.name)) callbacks.FlagSuccess.send(self) logger.debug('Arguments passed to flag {0} are valid'.format( self.ancestry)) args.update({self.data_in_api['name']: data}) return get_flag(self.action)(**args) except InvalidInput as e: logger.error( 'Flag {0} has invalid input {1} which was converted to {2}. Error: {3}. ' 'Returning False'.format(self.action, data_in, data, format_exception_message(e))) callbacks.FlagError.send(self) return False except Exception as e: logger.error('Error encountered executing ' 'flag {0} with arguments {1} and value {2}: ' 'Error {3}. Returning False'.format( self.action, self.args, data, format_exception_message(e))) callbacks.FlagError.send(self) return False
def __call__(self, data_in, accumulator): """ Executes the flag Args: data_in: The input to the flag. Typically from the last step of the workflow or the input to a trigger accumulator (dict): A record of executed steps and their results. Of form {step_name: result} Returns: (bool): Is the flag true for the given data and accumulator """ original_data_in = deepcopy(data_in) try: data_in = validate_parameter(data_in, self.data_in_api, 'Filter {0}'.format(self.action)) args = dereference_step_routing(self.args, accumulator, 'In Filter {0}'.format(self.name)) args.update({self.data_in_api['name']: data_in}) result = get_filter(self.action)(**args) callbacks.FilterSuccess.send(self) return result except InvalidInput as e: callbacks.FilterError.send(self) logger.error('Filter {0} has invalid input {1}. Error: {2}. ' 'Returning unmodified data'.format( self.action, original_data_in, str(e))) except Exception as e: callbacks.FilterError.send(self) logger.error( 'Filter {0} encountered an error: {1}. Returning unmodified data' .format(self.action, str(e))) return original_data_in
def test_validate_parameter_primitive_not_required_none(self): parameter_apis = [{'name': 'name1', 'type': 'string', 'required': False}, {'name': 'name1', 'type': 'number', 'required': False}, {'name': 'name1', 'type': 'integer'}, {'name': 'name1', 'type': 'boolean'}] for parameter_api in parameter_apis: self.assertIsNone(validate_parameter(None, parameter_api, self.message))
def test_validate_parameter_primitive_string_format_valid(self): parameter_api = { 'name': 'name1', 'type': 'string', 'minLength': 1, 'maxLength': 25 } value = 'test string' self.assertEqual( validate_parameter(value, parameter_api, self.message), value)
def test_validate_parameter_object_invalid(self): parameter_api = { 'name': 'name1', 'schema': { 'type': 'object', 'required': ['a', 'b'], 'properties': { 'a': { 'type': 'number' }, 'b': { 'type': 'string' }, 'c': { 'type': 'boolean' } } } } value = {'a': 435.6, 'invalid': 'aaaa', 'c': True} with self.assertRaises(InvalidArgument): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_primitive_string_format_enum_invalid(self): parameter_api = {'name': 'name1', 'type': 'string', 'minLength': 1, 'maxLength': 25, 'enum': ['test', 'test3']} value = 'test2' with self.assertRaises(InvalidInput): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_primitive_no_formats_invalid_integer_cause_float_string(self): parameter_api = {'name': 'name1', 'type': 'integer'} value = '3.27' with self.assertRaises(InvalidInput): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_primitive_no_formats_not_required_valid_negative_int(self): parameter_api = {'name': 'name1', 'type': 'integer'} value = '-3' self.assertEqual(validate_parameter(value, parameter_api, self.message), -3)
def test_validate_parameter_primitive_no_formats_not_required_valid_int_from_float(self): parameter_api = {'name': 'name1', 'type': 'integer'} value = 3.27 self.assertEqual(validate_parameter(value, parameter_api, self.message), 3)
def test_validate_parameter_primitive_no_formats_not_required_valid_number(self): parameter_api = {'name': 'name1', 'type': 'number'} value = '3.27' self.assertEqual(validate_parameter(value, parameter_api, self.message), 3.27)
def test_validate_parameter_primitive_no_formats_invalid_boolean(self): parameter_api = {'name': 'name1', 'type': 'boolean'} value = 'abc' with self.assertRaises(InvalidInput): validate_parameter(value, parameter_api, self.message)
def test_validate_parameter_primitive_no_formats_invalid_number(self): parameter_api = {'name': 'name1', 'type': 'number'} value = 'abc' with self.assertRaises(InvalidArgument): validate_parameter(value, parameter_api, self.message)