def test_load(self): # given: spreadsheet_json = _create_spreadsheet_json() # when: entity_map = EntityMap.load(spreadsheet_json) # then: self.assertEqual(['project', 'biomaterial', 'file', 'protocol'], list(entity_map.get_entity_types())) # and: # TODO shouldn't entity id's be unique and that there's no need to specify entity type? biomaterial1 = entity_map.get_entity('biomaterial', 'biomaterial_id_1') self._assert_correct_entity(biomaterial1, entity_id='biomaterial_id_1', entity_type='biomaterial', content={'key': 'biomaterial_1'}) # and: biomaterial2 = entity_map.get_entity('biomaterial', 'biomaterial_id_2') links = {'biomaterial': ['biomaterial_id_1'], 'process': ['process_id_1']} self._assert_correct_entity(biomaterial2, entity_id='biomaterial_id_2', entity_type='biomaterial', content={'key': 'biomaterial_2'}, links=links) # and: protocol1 = entity_map.get_entity('protocol', 'protocol_id_1') self.assertEqual({'key': 'protocol_1'}, protocol1.content)
def test_generate_direct_links_link_not_found_error(self): # given spreadsheet_json = { 'project': { 'dummy-project-id': { 'content': { 'key': 'project_1' } } }, 'file': { 'file_id_1': { 'content': { 'key': 'file_1' }, 'links_by_entity': { 'biomaterial': ['biomaterial_id_1'], 'protocol': ['protocol_id_1', 'protocol_id_2'] } } } } entities_dictionaries = EntityMap.load(spreadsheet_json) entity_linker = EntityLinker(self.mocked_template_manager) with self.assertRaises(LinkedEntityNotFound) as context: entity_linker.process_links_from_spreadsheet(entities_dictionaries) self.assertEqual('biomaterial', context.exception.entity) self.assertEqual('biomaterial_id_1', context.exception.id)
def test_submit_linked_entity(self, submission_constructor): # given: ingest_api = MagicMock('ingest_api') ingest_api.getSubmissionEnvelope = MagicMock() ingest_api.patch = MagicMock() ingest_api.get_link_from_resource = MagicMock() submission = self._mock_submission(submission_constructor) # and: user = Entity('user', 'user_1', {}) entity_map = EntityMap(user) # and: link_to_user = { 'entity': 'user', 'id': 'user_1', 'relationship': 'wish_list' } linked_product = Entity('product', 'product_1', {}, direct_links=[link_to_user]) project = Entity('project', 'id', {}) entity_map.add_entity(linked_product) entity_map.add_entity(project) # when: submitter = IngestSubmitter(ingest_api) submitter.PROGRESS_CTR = 1 submitter.submit(entity_map, submission_url='url') # then: submission_constructor.assert_called_with(ingest_api, 'url') submission.define_manifest.assert_called_with(entity_map) submission.add_entity.assert_has_calls([call(user), call(linked_product)], any_order=True) submission.link_entity.assert_called_with(linked_product, user, relationship='wish_list') ingest_api.patch.assert_called_once()
def test_generate_direct_links_multiple_process_links(self): # given spreadsheet_json = { 'project': { 'dummy-project-id': { 'content': { 'key': 'project_1' } } }, 'biomaterial': { 'biomaterial_id_1': { 'content': { 'key': 'biomaterial_1' }, 'links_by_entity': { 'process': ['process_id_1', 'process_id_2'] } } }, 'process': { 'process_id_1': { 'content': { 'key': 'process_1' } }, 'process_id_2': { 'content': { 'key': 'process_2' } } } } entities_dictionaries = EntityMap.load(spreadsheet_json) entity_linker = EntityLinker(self.mocked_template_manager) with self.assertRaises(MultipleProcessesFound) as context: entity_linker.process_links_from_spreadsheet(entities_dictionaries) self.assertEqual('biomaterial', context.exception.from_entity.type) self.assertEqual(['process_id_1', 'process_id_2'], context.exception.process_ids)
def test_submit(self, submission_constructor): # given: ingest_api = MagicMock('ingest_api') ingest_api.getSubmissionEnvelope = MagicMock() submission = self._mock_submission(submission_constructor) # and: product = Entity('product', 'product_1', {}) project = Entity('project', 'id', {}) user = Entity('user', 'user_1', {}) entity_map = EntityMap(product, user, project) # when: submitter = IngestSubmitter(ingest_api) submitter.submit(entity_map, submission_url='url') # then: submission_constructor.assert_called_with(ingest_api, 'url') submission.define_manifest.assert_called_with(entity_map) submission.add_entity.assert_has_calls([call(product), call(user)], any_order=True)
def test_generate_direct_links_invalid_spreadsheet_link(self): # given spreadsheet_json = { 'project': { 'dummy-project-id': { 'content': { 'key': 'project_1' } } }, 'biomaterial': { 'biomaterial_id_1': { 'content': { 'key': 'biomaterial_1' }, 'links_by_entity': { 'file': ['file_id_1'] } } }, 'file':{ 'file_id_1': { 'content': { 'key': 'file_1' } } } } entities_dictionaries = EntityMap.load(spreadsheet_json) entity_linker = EntityLinker(self.mocked_template_manager) with self.assertRaises(InvalidLinkInSpreadsheet) as context: entity_linker.process_links_from_spreadsheet(entities_dictionaries) self.assertEqual('biomaterial', context.exception.from_entity.type) self.assertEqual('file', context.exception.link_entity_type) self.assertEqual('biomaterial_id_1', context.exception.from_entity.id) self.assertEqual('file_id_1', context.exception.link_entity_id)
def test_generate_direct_links_file_to_biomaterial_has_process(self): # given spreadsheet_json = { 'project': { 'dummy-project-id': { 'content': { 'key': 'project_1' } } }, 'file': { 'file_id_1': { 'content': { 'key': 'file_1' }, 'links_by_entity': { 'biomaterial': ['biomaterial_id_1'], 'process': ['process_id_1'], 'protocol': ['protocol_id_1', 'protocol_id_2'] } } }, 'biomaterial': { 'biomaterial_id_1': { 'content': { 'key': 'biomaterial_1' } } }, 'protocol': { 'protocol_id_1': { 'content': { 'key': 'protocol_1' } }, 'protocol_id_2': { 'content': { 'key': 'protocol_2' } } } } expected_json = { 'project': { 'dummy-project-id': { 'content': { 'key': 'project_1' } } }, 'biomaterial': { 'biomaterial_id_1': { 'content': { 'key': 'biomaterial_1' }, 'direct_links': [ { 'entity': 'project', 'id': 'dummy-project-id', 'relationship': 'projects' }, { 'entity': 'process', 'id': 'process_id_1', 'relationship': 'inputToProcesses' } ] } }, 'file': { 'file_id_1': { 'content': { 'key': 'file_1' }, 'direct_links': [ { 'entity': 'process', 'id': 'process_id_1', 'relationship': 'derivedByProcesses' } ] }, }, 'process': { 'process_id_1': { 'content': { 'key': 'process_1' }, 'direct_links': [ { 'entity': 'project', 'id': 'dummy-project-id', 'relationship': 'projects' }, { 'entity': 'protocol', 'id': 'protocol_id_1', 'relationship': 'protocols' }, { 'entity': 'protocol', 'id': 'protocol_id_2', 'relationship': 'protocols' } ] } }, 'protocol': { 'protocol_id_1': { 'content': { 'key': 'protocol_1' }, 'direct_links': [ ] }, 'protocol_id_2': { 'content': { 'key': 'protocol_2' }, 'direct_links': [ ] } } } entity_linker = EntityLinker(self.mocked_template_manager) entities_dictionaries = EntityMap.load(spreadsheet_json) output = entity_linker.process_links_from_spreadsheet(entities_dictionaries) self._assert_equal_direct_links(expected_json, output)
def test_count_links(self): entity_map = EntityMap() # no element self.assertEqual(entity_map.count_links(), 0) # has 1 element without links entity_map.add_entity(Entity('product', 'product_0', {})) self.assertEqual(entity_map.count_links(), 0) # has 1 element with links entity_map.add_entity(Entity('product', 'product_1', {}, direct_links=[{}, {}, {}])) self.assertEqual(entity_map.count_links(), 3) # has many element with links entity_map.add_entity(Entity('product', 'product_2', {}, direct_links=[{}, {}, {}, {}])) self.assertEqual(entity_map.count_links(), 7)
def test_count_total(self): # given: zero_map = EntityMap() # and: one_map = EntityMap() one_map.add_entity(Entity('product', 'product_1', {})) # and: three_map = EntityMap() three_map.add_entity(Entity('profile', 'profile_1', {})) for product_id in range(0, 2): three_map.add_entity(Entity('product', f'product_{product_id}', {})) # expect: self.assertEqual(0, zero_map.count_total()) self.assertEqual(1, one_map.count_total()) self.assertEqual(3, three_map.count_total())
def update_entities(self, dsp_submission_uuid: str, entity_map: EntityMap): archive_submission = self.ingest_api.get_archive_submission_by_dsp_uuid( dsp_submission_uuid) archive_submission_url = archive_submission['_links']['self']['href'] for entity in entity_map.get_entities(): self.update_entity(archive_submission_url, entity)
def _process_links_from_spreadsheet(template_mgr, spreadsheet_json): entity_map = EntityMap.load(spreadsheet_json) entity_linker = EntityLinker(template_mgr) entity_map = entity_linker.process_links_from_spreadsheet(entity_map) return entity_map