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_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_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_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 _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