def test_bad_media_type(self): """Tests calling JobData.validate_input_files() with a file that has an invalid media type""" data = {'input_data': [{'name': 'File1', 'file_id': self.file_1.id}]} file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type('text/plain') files = {'File1': (True, False, file_desc_1)} warnings = JobData(data).validate_input_files(files) self.assertTrue(warnings)
def test_bad_media_type(self): """Tests calling RecipeData.validate_input_files() with a file that has an invalid media type""" data = {"input_data": [{"name": "File1", "file_id": self.file_1.id}]} file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type("text/plain") files = {"File1": (True, False, file_desc_1)} warnings = RecipeData(data).validate_input_files(files) self.assertTrue(warnings)
def test_bad_media_type(self): '''Tests calling RecipeData.validate_input_files() with a file that has an invalid media type''' data = {u'input_data': [{u'name': u'File1', u'file_id': self.file_1.id}]} file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type(u'text/plain') files = {u'File1': (True, False, file_desc_1)} warnings = RecipeData(data).validate_input_files(files) self.assertTrue(warnings)
def test_accept_specific(self): """Tests calling ScaleFileDescription.is_media_type_allowed() when accepting specific media types.""" file_desc = ScaleFileDescription() file_desc.add_allowed_media_type(None) # Don't blow up file_desc.add_allowed_media_type('application/json') file_desc.add_allowed_media_type('text/plain') self.assertTrue(file_desc.is_media_type_allowed('application/json')) self.assertTrue(file_desc.is_media_type_allowed('text/plain')) self.assertFalse(file_desc.is_media_type_allowed('application/x-some-crazy-thing'))
def test_bad_media_type(self): '''Tests calling JobConnection.validate_input_files() with a bad media type''' file_desc = ScaleFileDescription() file_desc.add_allowed_media_type(u'application/json') files = {u'Param1': (True, True, file_desc)} conn = JobConnection() conn.add_input_file(u'Param1', True, None, False) warnings = conn.validate_input_files(files) self.assertTrue(warnings)
def _create_validation_dicts(self): """Creates the validation dicts required by job_data to perform its validation""" for input_data in self.definition['input_data']: name = input_data['name'] required = input_data['required'] if input_data['type'] == 'property': self._property_validation_dict[name] = required elif input_data['type'] == 'file': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, False, file_desc) elif input_data['type'] == 'files': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, True, file_desc) for ouput_data in self.definition['output_data']: output_type = ouput_data['type'] if output_type in ['file', 'files']: name = ouput_data['name'] required = ouput_data['required'] self._output_file_validation_list.append(name) self._output_file_manifest_dict[name] = ( output_type == 'files', required)
def test_bad_media_type(self): '''Tests calling RecipeData.validate_input_files() with a file that has an invalid media type''' data = { u'input_data': [{ u'name': u'File1', u'file_id': self.file_1.id }] } file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type(u'text/plain') files = {u'File1': (True, False, file_desc_1)} warnings = RecipeData(data).validate_input_files(files) self.assertTrue(warnings)
def test_successful(self): """Tests calling JobData.add_file_list_input() successfully.""" data = {'input_data': []} job_data = JobData(data) # Method to test, we will test it by calling validate below job_data.add_file_list_input('File1', [self.file_1.id]) file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type('application/json') files = {'File1': (True, True, file_desc_1)} # No exception is success warnings = JobData(data).validate_input_files(files) self.assertFalse(warnings)
def test_multiple_non_integrals(self): """Tests calling RecipeData.validate_input_files() with a multiple file param with a list of non-integrals for file_ids field""" data = {'input_data': [{'name': 'File1', 'file_ids': [123, 'STRING']}]} files = {'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, LegacyRecipeData(data).validate_input_files, files)
def test_multiple_missing_file_ids(self): """Tests calling RecipeData.validate_input_files() with a multiple file param missing the file_ids field""" data = {'input_data': [{'name': 'File1'}]} files = {'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, LegacyRecipeData(data).validate_input_files, files)
def test_bad_file_id(self): '''Tests calling RecipeData.validate_input_files() with a file that has an invalid ID''' data = {u'input_data': [{u'name': u'File1', u'file_id': 999999}]} files = {u'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, RecipeData(data).validate_input_files, files)
def test_single_non_integral(self): '''Tests calling RecipeData.validate_input_files() with a single file param with a non-integral for file_id field''' data = {u'input_data': [{u'name': u'File1', u'file_id': 'STRING'}]} files = {u'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, RecipeData(data).validate_input_files, files)
def test_single_missing_file_id(self): '''Tests calling RecipeData.validate_input_files() with a single file param missing the file_id field''' data = {u'input_data': [{u'name': u'File1'}]} files = {u'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, RecipeData(data).validate_input_files, files)
def test_bad_file_id(self): """Tests calling JobData.validate_input_files() with a file that has an invalid ID""" data = {'input_data': [{'name': 'File1', 'file_id': 9999999999}]} files = {'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_single_non_integral(self): """Tests calling JobData.validate_input_files() with a single file param and non-integral file_id field""" data = {'input_data': [{'name': 'File1', 'file_id': 'STRING'}]} files = {'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_missing_required(self): '''Tests calling RecipeData.validate_input_files() when a file is required, but missing''' data = {u'input_data': []} files = {u'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, RecipeData(data).validate_input_files, files)
def test_single_missing_file_id(self): """Tests calling JobData.validate_input_files() with a single file param missing the file_id field""" data = {'input_data': [{'name': 'File1'}]} files = {'File1': (True, False, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_multiple_non_list(self): """Tests calling JobData.validate_input_files() with a multiple file param with a non-list for file_ids field""" data = {'input_data': [{'name': 'File1', 'file_ids': 'STRING'}]} files = {'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_missing_required(self): """Tests calling JobData.validate_input_files() when a file is required, but missing""" data = {'input_data': []} files = {'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_multiple_missing_file_ids(self): '''Tests calling JobData.validate_input_files() with a multiple file param missing the file_ids field''' data = {u'input_data': [{u'name': u'File1'}]} files = {u'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def test_multiple_non_list(self): '''Tests calling RecipeData.validate_input_files() with a multiple file param with a non-list for file_ids field''' data = {u'input_data': [{u'name': u'File1', u'file_ids': u'STRING'}]} files = {u'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidRecipeData, RecipeData(data).validate_input_files, files)
def test_multiple_given_single(self): """Tests calling JobData.validate_input_files() with a multiple file param given with a single file ID""" data = {'input_data': [{'name': 'File1', 'file_id': self.file_1.id}]} files = {'File1': (True, True, ScaleFileDescription())} # No exception is success warnings = JobData(data).validate_input_files(files) self.assertFalse(warnings)
def test_not_required(self): """Tests calling JobData.validate_input_files() when a file is missing, but required""" data = {'input_data': []} files = {'File1': (False, True, ScaleFileDescription())} # No exception is success warnings = JobData(data).validate_input_files(files) self.assertFalse(warnings)
def test_optional_and_required(self): '''Tests calling JobConnection.validate_input_files() when the connection has optional data for required input''' files = {u'Param1': (True, True, ScaleFileDescription())} conn = JobConnection() conn.add_input_file(u'Param1', True, None, True, False) self.assertRaises(InvalidConnection, conn.validate_input_files, files)
def test_required_missing(self): """Tests calling JobConnection.validate_input_files() when a required file parameter is missing""" files = {'Param1': (True, True, ScaleFileDescription()), 'Param2': (True, True, ScaleFileDescription())} conn = JobConnection() conn.add_input_file('Param1', True, None, False, False) self.assertRaises(InvalidConnection, conn.validate_input_files, files)
def test_jamming_multiple_into_single(self): '''Tests calling JobConnection.validate_input_files() when passing multiple files into a single file''' files = {u'Param1': (True, False, ScaleFileDescription())} conn = JobConnection() conn.add_input_file(u'Param1', True, None, False, False) self.assertRaises(InvalidConnection, conn.validate_input_files, files)
def validate_input_files(self, files): """Validates the given file parameters to make sure they are valid with respect to the job interface. :param files: List of Seed Input Files :type files: [:class:`job.seed.types.SeedInputFiles`] :returns: A list of warnings discovered during validation. :rtype: [:class:`job.configuration.data.job_data.ValidationWarning`] :raises :class:`job.configuration.data.exceptions.InvalidData`: If there is a configuration problem. """ warnings = [] for input_file in files: name = input_file.name required = input_file.required file_desc = ScaleFileDescription() if name in self._new_data.values: if isinstance(self._new_data.values[name], FileValue): # Have this input, make sure it is valid file_input = self._new_data.values[name] file_ids = [] for media_type in input_file.media_types: file_desc.add_allowed_media_type(media_type) for file_id in file_input.file_ids: if not isinstance(file_id, Integral): msg = ( 'Invalid job data: Data input %s must have a list of integers in its "file_ids" ' 'field') raise InvalidData(msg % name) file_ids.append(long(file_id)) warnings.extend( self._validate_file_ids(file_ids, file_desc)) else: # Don't have this input, check if it is required if required: raise InvalidData( 'Invalid job data: Data input %s is required and was not provided' % name) return warnings
def test_multiple_non_integrals(self): '''Tests calling JobData.validate_input_files() with a multiple file param and non-integral file_ids field''' data = { u'input_data': [{ u'name': u'File1', u'file_ids': [123, u'STRING'] }] } files = {u'File1': (True, True, ScaleFileDescription())} self.assertRaises(InvalidData, JobData(data).validate_input_files, files)
def _create_validation_dicts(self): """Creates the validation dicts required by job_data to perform its validation""" for input_data in self.definition['input_data']: name = input_data['name'] required = input_data['required'] if input_data['type'] == 'property': self._property_validation_dict[name] = required elif input_data['type'] == 'file': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, False, file_desc) elif input_data['type'] == 'files': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, True, file_desc) for ouput_data in self.definition['output_data']: output_type = ouput_data['type'] if output_type in ['file', 'files']: name = ouput_data['name'] required = ouput_data['required'] self._output_file_validation_list.append(name) self._output_file_manifest_dict[name] = (output_type == 'files', required)
def test_successful(self): """Tests calling RecipeData.validate_input_files() with a valid set of job data""" data = {'input_data': [{'name': 'File1', 'file_id': self.file_1.id}, {'name': 'File3', 'file_ids': [self.file_2.id]}]} file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type('application/json') file_desc_3 = ScaleFileDescription() file_desc_3.add_allowed_media_type('text/plain') files = {'File1': (True, False, file_desc_1), 'File3': (True, True, file_desc_3)} # No exception is success warnings = RecipeData(data).validate_input_files(files) self.assertFalse(warnings)
def test_multiple_given_single(self): '''Tests calling RecipeData.validate_input_files() with a multiple file param that is provided with a single file ID''' data = { u'input_data': [{ u'name': u'File1', u'file_id': self.file_1.id }] } files = {u'File1': (True, True, ScaleFileDescription())} # No exception is success warnings = RecipeData(data).validate_input_files(files) self.assertFalse(warnings)
def test_successful(self): '''Tests calling JobConnection.validate_input_files() successfully''' file_desc = ScaleFileDescription() file_desc.add_allowed_media_type(u'application/json') file_desc_2 = ScaleFileDescription() file_desc_2.add_allowed_media_type(u'application/json') file_desc_2.add_allowed_media_type(u'text/plain') files = {u'Param1': (True, True, file_desc), u'Param2': (True, False, ScaleFileDescription()), u'Param3': (False, True, file_desc_2), u'Param4': (False, True, file_desc_2)} conn = JobConnection() conn.add_input_file(u'Param1', True, [u'application/json'], False) conn.add_input_file(u'Param2', False, [u'text/plain'], False) conn.add_input_file(u'Param3', False, [u'text/plain'], False) # No exception is success warnings = conn.validate_input_files(files) self.assertFalse(warnings)
def _create_validation_dicts(self): """Creates the validation dicts required by recipe_data to perform its validation""" for input_data in self._definition['input_data']: name = input_data['name'] required = input_data['required'] if input_data['type'] == 'property': self._property_validation_dict[name] = required elif input_data['type'] == 'file': file_desc = ScaleFileDescription() if 'media_types' in input_data: for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, False, file_desc) elif input_data['type'] == 'files': file_desc = ScaleFileDescription() if 'media_types' in input_data: for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, True, file_desc)
def test_successful(self): '''Tests calling RecipeData.validate_input_files() with a valid set of job data''' data = {u'input_data': [{u'name': u'File1', u'file_id': self.file_1.id}, {u'name': u'File3', u'file_ids': [self.file_2.id]}]} file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type(u'application/json') file_desc_3 = ScaleFileDescription() file_desc_3.add_allowed_media_type(u'text/plain') files = {u'File1': (True, False, file_desc_1), u'File3': (True, True, file_desc_3)} # No exception is success warnings = RecipeData(data).validate_input_files(files) self.assertFalse(warnings)
def test_successful(self): """Tests calling RecipeData.validate_input_files() with a valid set of job data""" data = { "input_data": [ {"name": "File1", "file_id": self.file_1.id}, {"name": "File3", "file_ids": [self.file_2.id]}, ] } file_desc_1 = ScaleFileDescription() file_desc_1.add_allowed_media_type("application/json") file_desc_3 = ScaleFileDescription() file_desc_3.add_allowed_media_type("text/plain") files = {"File1": (True, False, file_desc_1), "File3": (True, True, file_desc_3)} # No exception is success warnings = RecipeData(data).validate_input_files(files) self.assertFalse(warnings)
def _create_validation_dicts(self): '''Creates the validation dicts required by recipe_data to perform its validation''' for input_data in self._definition['input_data']: name = input_data['name'] required = input_data['required'] if input_data['type'] == 'property': self._property_validation_dict[name] = required elif input_data['type'] == 'file': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, False, file_desc) elif input_data['type'] == 'files': file_desc = ScaleFileDescription() for media_type in input_data['media_types']: file_desc.add_allowed_media_type(media_type) self._input_file_validation_dict[name] = (required, True, file_desc)
def test_accept_specific(self): '''Tests calling ScaleFileDescription.is_media_type_allowed() when accepting specific media types.''' file_desc = ScaleFileDescription() file_desc.add_allowed_media_type(None) # Don't blow up file_desc.add_allowed_media_type('application/json') file_desc.add_allowed_media_type('text/plain') self.assertTrue(file_desc.is_media_type_allowed('application/json')) self.assertTrue(file_desc.is_media_type_allowed('text/plain')) self.assertFalse( file_desc.is_media_type_allowed('application/x-some-crazy-thing'))
def test_successful(self): '''Tests calling JobConnection.validate_input_files() successfully''' file_desc = ScaleFileDescription() file_desc.add_allowed_media_type(u'application/json') file_desc_2 = ScaleFileDescription() file_desc_2.add_allowed_media_type(u'application/json') file_desc_2.add_allowed_media_type(u'text/plain') files = { u'Param1': (True, True, file_desc), u'Param2': (True, False, ScaleFileDescription()), u'Param3': (False, True, file_desc_2), u'Param4': (False, True, file_desc_2) } conn = JobConnection() conn.add_input_file(u'Param1', True, [u'application/json'], False, False) conn.add_input_file(u'Param2', False, [u'text/plain'], False, False) conn.add_input_file(u'Param3', False, [u'text/plain'], False, False) # No exception is success warnings = conn.validate_input_files(files) self.assertFalse(warnings)