Esempio n. 1
0
 def _create_test_entity_map(self) -> EntityMap:
     product = Entity('product', 'product_1', {'k': 'v'})
     project = Entity('project', 'id', {'k': 'v'})
     user1 = Entity('user', 'user_1', {'k': 'v'})
     user2 = Entity('user',
                    'user_2', {'k': 'v'}, {
                        'content': {
                            'k': 'v0'
                        },
                        '_links': {
                            'self': {
                                'href': 'url'
                            }
                        }
                    },
                    is_reference=True)
     user3 = Entity('user',
                    'user_3', {'k': 'v'}, {
                        'content': {
                            'k': 'v0'
                        },
                        '_links': {
                            'self': {
                                'href': 'url'
                            }
                        }
                    },
                    is_reference=True)
     entity_map = EntityMap(product, user1, user2, user3, project)
     return entity_map
Esempio n. 2
0
 def _load_external_links(self, entity: Entity):
     external_links = entity.external_links
     for external_link_type, external_link_uuids in external_links.items():
         for entity_uuid in external_link_uuids:
             external_link_entity = Entity(
                 entity_type=external_link_type,
                 entity_id=entity_uuid,
                 content=None,
                 spreadsheet_location=entity.spreadsheet_location,
                 is_linking_reference=True)
             external_link_entity.add_link(external_link_type, entity_uuid)
             self.entity_map.add_entity(external_link_entity)
Esempio n. 3
0
    def link_entity(self, from_entity: Entity, to_entity: Entity, relationship: str, is_collection=True):
        if from_entity.is_linking_reference and not from_entity.ingest_json:
            from_entity.ingest_json = self.ingest_api.get_entity_by_uuid(ENTITY_LINK[from_entity.type],
                                                                         from_entity.id)

        if to_entity.is_linking_reference and not to_entity.ingest_json:
            to_entity.ingest_json = self.ingest_api.get_entity_by_uuid(ENTITY_LINK[to_entity.type], to_entity.id)

        from_entity_ingest = from_entity.ingest_json
        to_entity_ingest = to_entity.ingest_json

        self.ingest_api.link_entity(from_entity_ingest, to_entity_ingest, relationship, is_collection)
    def test_add_entity_uuids(self):
        # given:
        entities = [Entity('type', 'AA', 'content', concrete_type='a', ingest_json={'uuid': {'uuid': 'A-1-uuid'}},
                           spreadsheet_location={'row_index': 5, 'worksheet_title': 'A'}),
                    Entity('type', 'AA', 'content', concrete_type='a', ingest_json={'uuid': {'uuid': 'A-2-uuid'}},
                           spreadsheet_location={'row_index': 6, 'worksheet_title': 'A'}),
                    Entity('type', 'AA', 'content', concrete_type='a', ingest_json={'uuid': {'uuid': 'A-3-uuid'}},
                           spreadsheet_location={'row_index': 7, 'worksheet_title': 'A'}),
                    Entity('type', 'BB', 'content', concrete_type='b', ingest_json={'uuid': {'uuid': 'B-1-uuid'}},
                           spreadsheet_location={'row_index': 5, 'worksheet_title': 'B'}),
                    Entity('type', 'BB', 'content', concrete_type='b', ingest_json={'uuid': {'uuid': 'B-2-uuid'}},
                           spreadsheet_location={'row_index': 6, 'worksheet_title': 'B'}),
                    Entity('type', 'BB', 'content', concrete_type='b', ingest_json={'uuid': {'uuid': 'B-3-uuid'}},
                           spreadsheet_location={'row_index': 7, 'worksheet_title': 'B'}),
                    Entity('type', 'CC', 'content', concrete_type='c', ingest_json={'uuid': {'uuid': 'C-1-uuid'}},
                           spreadsheet_location={'row_index': 5, 'worksheet_title': 'C'}),
                    Entity('type', 'CC', 'content', concrete_type='c', ingest_json={'uuid': {'uuid': 'C-2-uuid'}},
                           spreadsheet_location={'row_index': 6, 'worksheet_title': 'C'}),
                    Entity('type', 'CC', 'content', concrete_type='c', ingest_json={'uuid': {'uuid': 'C-3-uuid'}},
                           spreadsheet_location={'row_index': 7, 'worksheet_title': 'C'}),
                    Entity('type', 'XX', 'content', concrete_type='x', ingest_json={'uuid': {'uuid': 'X-3-uuid'}})]

        mock_submission = Mock('submission')
        mock_submission.get_entities = Mock(return_value=entities)

        sheets = ['A', 'B', 'C', 'X']
        workbook = create_test_workbook(*sheets)
        wb = IngestWorkbook(workbook)

        # when
        wb.add_entity_uuids(mock_submission)

        # then
        self.assertEqual(wb.workbook['A'].cell(4, 1).value, 'a.uuid')
        self.assertEqual(wb.workbook['A'].cell(5, 1).value, 'A-1-uuid')
        self.assertEqual(wb.workbook['A'].cell(6, 1).value, 'A-2-uuid')
        self.assertEqual(wb.workbook['A'].cell(7, 1).value, 'A-3-uuid')

        self.assertEqual(wb.workbook['B'].cell(4, 1).value, 'b.uuid')
        self.assertEqual(wb.workbook['B'].cell(5, 1).value, 'B-1-uuid')
        self.assertEqual(wb.workbook['B'].cell(6, 1).value, 'B-2-uuid')
        self.assertEqual(wb.workbook['B'].cell(7, 1).value, 'B-3-uuid')

        self.assertEqual(wb.workbook['C'].cell(4, 1).value, 'c.uuid')
        self.assertEqual(wb.workbook['C'].cell(5, 1).value, 'C-1-uuid')
        self.assertEqual(wb.workbook['C'].cell(6, 1).value, 'C-2-uuid')
        self.assertEqual(wb.workbook['C'].cell(7, 1).value, 'C-3-uuid')

        self.assertFalse(wb.workbook['X'].cell(5, 1).value, None)
Esempio n. 5
0
    def test_update_entity__given_content_has_no_update__then_do_not_patch(
            self):
        # given:
        ingest_json = {
            'content': {
                'k': 'v2',
                'k2': 'v2'
            },
            '_links': {
                'self': {
                    'href': 'url'
                }
            }
        }
        self.ingest_api.get_entity_by_uuid = Mock(return_value=ingest_json)

        # and:
        user1 = Entity('biomaterial', 'biomaterial_uuid', {'k': 'v2'}, None)

        # when:
        submitter = IngestSubmitter(self.ingest_api)
        submitter.update_entity(user1)

        # then:
        self.ingest_api.patch.assert_not_called()
Esempio n. 6
0
    def _create_or_get_process(self, entity: Entity) -> Entity:
        external_links = entity.external_links
        processes = external_links.get('process', [])
        external_process_id = processes[0] if len(processes) > 0 else None

        links_by_entity = entity.links_by_entity
        process_id = links_by_entity['process'][0] if links_by_entity.get(
            'process') else None

        if not process_id:
            process_id = self._generate_empty_process_id()

        linking_details = entity.linking_details

        process = self.entity_map.get_entity('process', process_id)

        if not process:
            process_json = self._create_process_json(process_id,
                                                     linking_details)
            process = Entity(entity_type='process',
                             entity_id=external_process_id or process_id,
                             content=process_json,
                             is_linking_reference=bool(external_process_id),
                             is_reference=bool(external_process_id))

        return process
Esempio n. 7
0
    def test_update_entity__given_empty_ingest_json__then_fetch_resource(self):
        # given:
        ingest_json = {
            'content': {
                'k': 'v',
                'k2': 'v2'
            },
            '_links': {
                'self': {
                    'href': 'url'
                }
            }
        }
        self.ingest_api.get_entity_by_uuid = Mock(return_value=ingest_json)

        # and:
        user1 = Entity('biomaterial', 'biomaterial_uuid', {'k': 'v2'}, None)

        # when:
        submitter = IngestSubmitter(self.ingest_api)
        submitter.update_entity(user1)

        # then:
        self.ingest_api.patch.assert_called_with(
            'url', {'content': {
                'k': 'v2',
                'k2': 'v2'
            }})
Esempio n. 8
0
    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())
Esempio n. 9
0
    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)
Esempio n. 10
0
    def update_entity(self, entity: Entity):
        if not entity.ingest_json:
            entity.ingest_json = self.ingest_api.get_entity_by_uuid(ENTITY_LINK[entity.type], entity.id)

        patch = copy.deepcopy(entity.ingest_json.get('content'))
        patch.update(entity.content)
        if not json_equals(entity.ingest_json.get('content'), patch):
            self.ingest_api.patch(entity.url, {'content': patch})

        return entity
Esempio n. 11
0
    def test_submit(self, submission_constructor):
        # given:
        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(self.ingest_api)
        submitter.add_entity = MagicMock()
        submitter.add_entities(entity_map, submission_url='url')

        # then:
        submission_constructor.assert_called_with(self.ingest_api, 'url')
        submission.define_manifest.assert_called_with(entity_map)
        submission.add_entity.assert_has_calls(
            [call(product), call(user)], any_order=True)
Esempio n. 12
0
    def test_get_project__returns_project(self):
        # given
        entity_map = EntityMap()
        project_entity = Entity('project', 'project_0', {})
        entity_map.add_entity(project_entity)

        # when
        output = entity_map.get_project()

        # then
        self.assertEqual(output, project_entity)
Esempio n. 13
0
    def test_submit_linked_entity(self, submission_constructor):
        # given:
        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],
                                is_reference=False,
                                is_linking_reference=False)
        project = Entity('project',
                         'id', {},
                         is_reference=False,
                         is_linking_reference=False)
        entity_map.add_entity(linked_product)
        entity_map.add_entity(project)

        # when:
        submitter = IngestSubmitter(self.ingest_api)
        submitter.add_entity = MagicMock()
        submitter.link_submission_to_project = MagicMock()
        submitter.PROGRESS_CTR = 1
        submitter.add_entities(entity_map, submission_url='url')

        # then:
        submission_constructor.assert_called_with(self.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)
Esempio n. 14
0
    def add_entity(self, entity: Entity, submission_url: str):
        link_name = ENTITY_LINK[entity.type]

        if entity.type == 'file':
            file_name = entity.content['file_core']['file_name']
            response = self.ingest_api.create_file(submission_url,
                                                   file_name,
                                                   entity.content)
        elif entity.type == 'project':
            response = self.ingest_api.create_project(submission_url,
                                                      entity.content)
        else:
            response = self.ingest_api.create_entity(submission_url,
                                                     {"content": entity.content},
                                                     link_name)
        entity.ingest_json = response

        return entity
Esempio n. 15
0
    def load(entity_json):
        entity_map = EntityMap()

        for entity_type, entities_dict in entity_json.items():
            for entity_id, entity_body in entities_dict.items():
                entity = Entity(
                    entity_type=entity_type,
                    entity_id=entity_id,
                    content=entity_body.get('content'),
                    external_links=entity_body.get('external_links_by_entity'),
                    links_by_entity=entity_body.get('links_by_entity', {}),
                    is_reference=entity_body.get('is_reference', False),
                    is_linking_reference=entity_body.get(
                        'is_linking_reference', False),
                    linking_details=entity_body.get('linking_details', {}),
                    concrete_type=entity_body.get('concrete_type'),
                    spreadsheet_location=entity_body.get(
                        'spreadsheet_location'))
                entity_map.add_entity(entity)

        return entity_map
Esempio n. 16
0
    def test_add_entity(self):
        new_entity_mock_response = {
            'content': {},
            'submissionDate': '2018-05-08T10:17:49.476Z',
            'updateDate': '2018-05-08T10:17:57.254Z',
            'uuid': {
                'uuid': '5a36689b-302b-40e4-bef1-837b47f0cb51'
            },
            'validationState': 'Draft'
        }

        ingest.api.ingestapi.requests.get = MagicMock()
        mock_ingest_api = MagicMock(name='mock_ingest_api')
        mock_ingest_api.load_root = MagicMock()
        mock_ingest_api.create_entity = MagicMock(
            return_value=new_entity_mock_response)

        submitter = IngestSubmitter(mock_ingest_api)
        entity = Entity(entity_id='id', entity_type='biomaterial', content={})
        entity = submitter.add_entity(entity, 'url')

        self.assertEqual(new_entity_mock_response, entity.ingest_json)
Esempio n. 17
0
    def test_update_entity(self):
        # given:
        user1 = Entity('user', 'user_1', {'k': 'v2'}, {
            'content': {
                'k': 'v',
                'k2': 'v2'
            },
            '_links': {
                'self': {
                    'href': 'url'
                }
            }
        })

        # when:
        submitter = IngestSubmitter(self.ingest_api)
        submitter.update_entity(user1)

        # then:
        self.ingest_api.patch.assert_called_with(
            'url', {'content': {
                'k': 'v2',
                'k2': 'v2'
            }})
Esempio n. 18
0
 def test_add_metadata(self):
     mock_ingest_api = MagicMock(name='mock_ingest_api')
     submission = Submission(mock_ingest_api, submission_url='url')
     entity = Entity(entity_id='id', entity_type='biomaterial', content={})
     entity = submission.add_entity(entity)
     self.assertEqual(entity, submission.get_entity('biomaterial', 'id'))