def test_inconsistent_sample_should_return_error(self): # Given sample_attributes = { 'scientific_name': 'Severe acute respiratory syndrome coronavirus 2', 'tax_id': '9606' } self.taxonomy_validator.ena_taxonomy.validate_scientific_name = MagicMock( return_value=self.valid_sarscov2) self.taxonomy_validator.ena_taxonomy.validate_tax_id = MagicMock( return_value=self.valid_human) consistent_error = 'Information is not consistent between taxId: 9606 and scientificName: Severe acute respiratory syndrome coronavirus 2' expected_errors = { 'scientific_name': [consistent_error], 'tax_id': [consistent_error] } sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertTrue(sample.has_errors()) self.assertDictEqual(expected_errors, sample.get_errors())
def test_invalid_sample_name_should_return_error(self): # Given sample_attributes = { 'scientific_name': 'Lorem Ipsum', 'tax_id': '2697049' } error = 'Not valid scientific_name: Lorem Ipsum.' consistent_error = 'Information is not consistent between taxId: 2697049 and scientificName: Lorem Ipsum' expected_errors = { 'scientific_name': [error, consistent_error], 'tax_id': [consistent_error] } self.taxonomy_validator.ena_taxonomy.validate_scientific_name = MagicMock( return_value={'error': error}) self.taxonomy_validator.ena_taxonomy.validate_tax_id = MagicMock( return_value=self.valid_sarscov2) sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertTrue(sample.has_errors()) self.assertDictEqual(expected_errors, sample.get_errors())
def __add_errors_to_entity(entity: Entity, schema_errors: dict): for schema_error in schema_errors: attribute_name = str(schema_error['dataPath']).strip('.') stripped_errors = [] for error in schema_error['errors']: error.replace('"', '\'') if error == 'should NOT be valid': error = JsonValidator.__improve_not_be_valid_message( entity.identifier.entity_type, attribute_name) if error != 'should match some schema in anyOf': stripped_errors.append(error) entity.add_errors(attribute_name, stripped_errors)
def test_valid_sample_tax_id_should_not_return_error(self): # Given sample_attributes = {'tax_id': '2697049'} self.taxonomy_validator.ena_taxonomy.validate_tax_id = MagicMock( return_value=self.valid_sarscov2) sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertFalse(sample.has_errors()) self.assertDictEqual({}, sample.get_errors())
def test_invalid_name_should_return_error(self): sample_attributes = {'scientific_name': 'Lorem Ipsum'} error = 'Not valid scientific_name: Lorem Ipsum.' expected_error = {'scientific_name': [error]} self.taxonomy_validator.ena_taxonomy.validate_scientific_name = MagicMock( return_value={'error': error}) sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertTrue(sample.has_errors()) self.assertDictEqual(expected_error, sample.get_errors())
def test_invalid_tax_id_should_return_error(self): # Given sample_attributes = {'tax_id': '999999999999'} error = 'Not valid tax_id: 999999999999.' expected_error = {'tax_id': [error]} self.taxonomy_validator.ena_taxonomy.validate_tax_id = MagicMock( return_value={'error': error}) sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertTrue(sample.has_errors()) self.assertDictEqual(expected_error, sample.get_errors())
def validate_file(self, entity: Entity, file_attribute: str, check_attribute: str): file_name = entity.attributes[file_attribute] if file_name not in self.file_checksum_map: entity.add_error(file_attribute, f'File has not been uploaded to drag-and-drop: {file_name}') return upload_checksum = self.file_checksum_map[file_name] if check_attribute in entity.attributes: stated_checksum = entity.attributes[check_attribute] if stated_checksum != upload_checksum: entity.add_error( check_attribute, f'The checksum found on drag-and-drop {upload_checksum} does not match: {stated_checksum}') return else: entity.attributes[check_attribute] = upload_checksum
def test_missing_file_should_log_error(self, mock: MagicMock): # Given entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = "file_name.extension1.ex2,checksum" validator = UploadValidator('uuid') entity = Entity(entity_type, index, {'uploaded_file_1': 'missing.file'}) expected_errors = { 'uploaded_file_1': ['File has not been uploaded to drag-and-drop: missing.file'] } # When validator.validate_entity(entity) # Then self.assertDictEqual(expected_errors, entity.get_errors())
def test_valid_sample_name_should_not_return_error(self): # Given sample_attributes = { 'scientific_name': 'Severe acute respiratory syndrome coronavirus 2' } self.taxonomy_validator.ena_taxonomy.validate_scientific_name = MagicMock( return_value=self.valid_sarscov2) sample = Entity('sample', 'sample1', sample_attributes) # When self.taxonomy_validator.validate_entity(sample) # Then self.assertFalse(sample.has_errors()) self.assertDictEqual({}, sample.get_errors())
def validate_entity(self, entity: Entity): sample = entity.attributes sample_errors = {} if 'tax_id' in sample and 'scientific_name' in sample: tax_response = self.ena_taxonomy.validate_taxonomy( tax_id=sample['tax_id'], scientific_name=sample['scientific_name']) sample_errors = self.get_taxonomy_errors(tax_response) else: if 'tax_id' in sample: tax_response = self.ena_taxonomy.validate_tax_id( sample['tax_id']) sample_errors = self.get_errors(tax_response, 'tax_id') elif 'scientific_name' in sample: tax_response = self.ena_taxonomy.validate_scientific_name( sample['scientific_name']) sample_errors = self.get_errors(tax_response, 'scientific_name') for attribute, errors in sample_errors.items(): entity.add_errors(attribute, errors)
def test_validation_with_second_file_present(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = "first-file,first-checksum\n" \ "second-file,second-checksum" # When validator = UploadValidator(secure_key) attributes = { 'uploaded_file_1': 'first-file', 'uploaded_file_1_checksum': 'first-checksum', 'uploaded_file_2': 'second-file', 'uploaded_file_2_checksum': 'second-checksum', } entity = Entity(entity_type, index, attributes) validator.validate_entity(entity) # Then self.assertDictEqual({}, entity.get_errors())
def test_validation_with_second_file_missing(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = "first-file,first-checksum" # When validator = UploadValidator(secure_key) attributes = { 'uploaded_file_1': 'first-file', 'uploaded_file_2': 'second-file' } entity = Entity(entity_type, index, attributes) validator.validate_entity(entity) # Then expected_errors = { 'uploaded_file_2': ['File has not been uploaded to drag-and-drop: second-file'] } self.assertDictEqual(expected_errors, entity.get_errors())
def test_missmatched_checksum_should_log_error(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' file_name = 'file_name.extension1.ex2' expected_checksum = 'checksum' wrong_checksum = 'not-checksum' mock.return_value = f"{file_name},{expected_checksum}" # When validator = UploadValidator(secure_key) attributes = { 'uploaded_file_1': file_name, 'uploaded_file_1_checksum': wrong_checksum } entity = Entity(entity_type, index, attributes) validator.validate_entity(entity) # Then expected_errors = { 'uploaded_file_1_checksum': [f'The checksum found on drag-and-drop {expected_checksum} does not match: {wrong_checksum}'] } self.assertDictEqual(expected_errors, entity.get_errors())
def test_validation_should_edit_file_attributes(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' file_name = 'file_name.extension1.ex2' checksum = 'checksum' mock.return_value = f"{file_name},{checksum}" # When validator = UploadValidator(secure_key) attributes = { 'uploaded_file_1': file_name } entity = Entity(entity_type, index, attributes) validator.validate_entity(entity) # Then expected_attributes = { 'uploaded_file_1': file_name, 'uploaded_file_1_checksum': checksum, } self.assertDictEqual(expected_attributes, entity.attributes)