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 map(self, entity_type: str, index: str, attributes: dict) -> Entity: if entity_type in self.__map and index in self.__map[entity_type]: entity = self.__handle_collision(entity_type, index, attributes) else: entity = Entity(entity_type, index, attributes) self.__map.setdefault(entity_type, {})[index] = entity return entity
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 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 test_missing_file_should_log_error(self, mock: MagicMock): # Given entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = f"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_validation_with_second_file_present(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = f"first-file,first-checksum\n" \ f"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_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)
def test_passed_bio_study_entity_returns_correct_json_representative(self): bio_study_attributes = { "study_accession": "PRJEB12345", "study_alias": "SARS-CoV-2 genomes 123ABC alias", "email_address": "*****@*****.**", "center_name": "EBI", 'study_name': 'SARS-CoV-2 genomes 123ABC name', "short_description": "test short description", "abstract": "test abstract", "release_date": "2020-08-21" } bio_study_entity = Entity(entity_type="study", index=bio_study_attributes["study_alias"], attributes=bio_study_attributes) expected_payload = self.__get_expected_payload(bio_study_entity) bio_study_json_payload = BioStudyConverter.convert_study( bio_study_entity) self.assertDictEqual(expected_payload, bio_study_json_payload)
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_validation_with_second_file_missing(self, mock: MagicMock): # Given secure_key = 'uuid' entity_type = 'run_experiment' index = f'{entity_type}1' mock.return_value = f"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_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())