Esempio n. 1
0
    def test_do_import_multiple_projects(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer

        # and:
        project_1 = MetadataEntity(concrete_type='project',
                                   domain_type='project',
                                   object_id=1)
        project_2 = MetadataEntity(concrete_type='project',
                                   domain_type='project',
                                   object_id=2)
        worksheet_importer.do_import = MagicMock(
            side_effect=[[project_1, project_2]])

        # and:
        workbook = create_test_workbook('Project')
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        exception_thrown = False
        try:
            workbook_importer.do_import(IngestWorkbook(workbook))
        except MultipleProjectsFound:
            exception_thrown = True

        # then:
        self.assertTrue(
            exception_thrown,
            f'Expected to throw {MultipleProjectsFound.__name__}.')
Esempio n. 2
0
    def test_do_import_no_projects(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer

        # and:
        item = MetadataEntity(concrete_type='product',
                              domain_type='product',
                              object_id=910)
        worksheet_importer.do_import = MagicMock(side_effect=[[item]])

        # and:
        workbook = create_test_workbook('Item')
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        thrown_exception = False
        try:
            workbook_importer.do_import(IngestWorkbook(workbook))
        except NoProjectFound:
            thrown_exception = True

        # then:
        self.assertTrue(thrown_exception,
                        f'Expected to throw {NoProjectFound.__name__}.')
    def test_do_import_no_project(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer
        no_errors = []
        expected_error = {
            'location': 'File',
            'type': 'NoProjectFound',
            'detail': 'The spreadsheet should be associated to a project.'
        }

        # and:
        item = MetadataEntity(concrete_type='product',
                              domain_type='product',
                              object_id=910)
        worksheet_importer.do_import = MagicMock(side_effect=[([item],
                                                               no_errors)])

        # and:
        workbook = create_test_workbook('Item')
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        spreadsheet_json, errors = workbook_importer.do_import(
            IngestWorkbook(workbook), is_update=False)

        # then:
        self.assertIn(
            expected_error, errors,
            f'Errors expected to contain {NoProjectFound.__name__}.')
    def test_do_import(self):
        # given:
        row_template = MagicMock('row_template')
        no_errors = []

        # and:
        john_doe = MetadataEntity(object_id='profile_1')
        emma_jackson = MetadataEntity(object_id='profile_2')
        row_template.do_import = MagicMock('import_row', side_effect=[(john_doe, no_errors), (emma_jackson, no_errors)])

        # and:
        mock_template_manager = MagicMock('template_manager')
        mock_template_manager.create_row_template = MagicMock(return_value=row_template)
        mock_template_manager.get_header_row = MagicMock(return_value=['header1', 'header2'])
        mock_template_manager.get_concrete_type = MagicMock(return_value='concrete_entity')

        # and:
        workbook = Workbook()
        worksheet = workbook.create_sheet('user_profile')
        worksheet['A4'] = 'header'
        worksheet['A6'] = 'john'
        worksheet['A7'] = 'emma'

        # when:
        worksheet_importer = WorksheetImporter(mock_template_manager)
        profiles, errors = worksheet_importer.do_import(IngestWorksheet(worksheet))

        # then:
        self.assertEqual(2, len(profiles))
        self.assertIn(john_doe, profiles)
        self.assertIn(emma_jackson, profiles)
        self.assertEqual(errors, [])

        # and: domain and concrete type should be set
        pass
Esempio n. 5
0
    def test_do_import_no_id_metadata(self):
        # given:
        row_template = MagicMock('row_template')

        # and:
        paper_metadata = MetadataEntity(content={'product_name': 'paper'},
                                        links={'delivery': ['123', '456']})
        pen_metadata = MetadataEntity(content={'product_name': 'pen'},
                                      links={'delivery': ['789']})
        row_template.do_import = MagicMock(side_effect=[paper_metadata, pen_metadata])

        # and:
        mock_template_manager = MagicMock('template_manager')
        mock_template_manager.create_row_template = MagicMock(return_value=row_template)
        mock_template_manager.get_header_row = MagicMock(return_value=['header1', 'header2'])
        mock_template_manager.get_concrete_entity_of_tab = MagicMock(return_value='concrete_entity')

        # and:
        workbook = Workbook()
        worksheet = workbook.create_sheet('product')
        worksheet['A6'] = 'paper'
        worksheet['A7'] = 'pen'

        # when:
        worksheet_importer = WorksheetImporter()
        result = worksheet_importer.do_import(worksheet, mock_template_manager)

        # then:
        self.assertEqual(2, len(result.keys()))
Esempio n. 6
0
    def test_do_import(self):
        # given:
        row_template = MagicMock('row_template')

        # and:
        john_doe_content = {'name': 'John Doe'}
        john_doe_links = {}
        john_doe_external_links = {'organisations': ['org_88', 'org_110', 'org_452']}
        john_doe_linking_details = {'position': 'manager'}
        john_doe = MetadataEntity(object_id='profile_1', content=john_doe_content,
                                  links=john_doe_links, external_links=john_doe_external_links,
                                  linking_details=john_doe_linking_details)

        # and:
        emma_jackson_content = {'name': 'Emma Jackson'}
        emma_jackson_links = {'friends': ['profile_19', 'profile_8']}
        emma_jackson_external_links = {}
        emma_jackson_linking_details = {}
        emma_jackson = MetadataEntity(object_id='profile_2', content=emma_jackson_content,
                                      links=emma_jackson_links,
                                      external_links=emma_jackson_external_links,
                                      linking_details=emma_jackson_linking_details)

        # and:
        row_template.do_import = MagicMock('import_row', side_effect=[john_doe, emma_jackson])

        # and:
        mock_template_manager = MagicMock('template_manager')
        mock_template_manager.create_row_template = MagicMock(return_value=row_template)
        mock_template_manager.get_header_row = MagicMock(return_value=['header1', 'header2'])
        mock_template_manager.get_concrete_entity_of_tab = MagicMock(return_value='concrete_entity')

        # and:
        workbook = Workbook()
        worksheet = workbook.create_sheet('user_profile')
        worksheet['A6'] = 'john'
        worksheet['A7'] = 'emma'

        # when:
        worksheet_importer = WorksheetImporter()
        profile = worksheet_importer.do_import(worksheet, mock_template_manager)

        # then:
        self.assertEqual(2, len(profile.keys()))
        self._assert_correct_profile(profile, 'profile_1', john_doe_content, john_doe_links,
                                     john_doe_external_links, john_doe_linking_details)
        self._assert_correct_profile(profile, 'profile_2', emma_jackson_content,
                                     emma_jackson_links, emma_jackson_external_links,
                                     emma_jackson_linking_details)
    def test_do_import_with_update_spreadsheet(self,
                                               worksheet_importer_constructor):
        # given:
        concrete_type_map = {'project': 'project', 'users': 'users'}
        self.template_mgr.get_concrete_type = lambda key: concrete_type_map.get(
            key)
        sheet_names = ['project', 'users']

        workbook = create_ingest_workbook(sheet_names, ['uuid', 'description'])
        workbook_importer = WorkbookImporter(self.template_mgr)

        # and
        worksheet_importer = WorksheetImporter(self.template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer
        no_errors = []

        expected_errors = []
        for sheet_name in sheet_names:
            expected_errors.append({
                'location':
                f'sheet={sheet_name}',
                'type':
                'UnexpectedEntityUUIDFound',
                'detail':
                f'The {sheet_name} entities in the spreadsheet shouldn’t have UUIDs.'
            })

        # and:
        project = MetadataEntity(domain_type='project',
                                 concrete_type='project',
                                 object_id=910)
        user1 = MetadataEntity(concrete_type='user',
                               domain_type='user',
                               object_id=1,
                               content={'user_name': 'jdelacruz'})

        worksheet_importer.do_import = \
            MagicMock(side_effect=[([project], no_errors), ([user1], no_errors)])

        # when:
        spreadsheet_json, errors = workbook_importer.do_import(workbook,
                                                               is_update=False)

        # then:
        self.assertTrue(
            all(elem in errors for elem in expected_errors),
            f'Errors expected to contain {UnexpectedEntityUUIDFound.__name__}.'
        )
Esempio n. 8
0
    def test_do_import(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer

        # and:
        project = MetadataEntity(concrete_type='project',
                                 domain_type='project')
        jdelacruz = MetadataEntity(concrete_type='user',
                                   domain_type='user',
                                   object_id=1,
                                   content={'user_name': 'jdelacruz'})
        setsuna_f_seiei = MetadataEntity(concrete_type='user',
                                         domain_type='user',
                                         object_id=96,
                                         content={'user_name': 'sayyeah'})
        worksheet_importer.do_import = MagicMock(
            side_effect=[[project], [jdelacruz, setsuna_f_seiei]])

        # and:
        workbook = create_test_workbook('Project', 'Users')
        ingest_workbook = IngestWorkbook(workbook)
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        workbook_json = workbook_importer.do_import(ingest_workbook)

        # then:
        self.assertIsNotNone(workbook_json)

        # and:
        user_map = workbook_json.get('user')
        self.assertIsNotNone(user_map)
        self.assertEqual(2, len(user_map))
        self.assertEqual([jdelacruz.object_id, setsuna_f_seiei.object_id],
                         list(user_map.keys()))

        # and:
        self.assertEqual({'user_name': 'jdelacruz'},
                         user_map.get(1)['content'])
        self.assertEqual({'user_name': 'sayyeah'}, user_map.get(96)['content'])
Esempio n. 9
0
    def test_do_import_no_id_metadata(self):
        # given:
        row_template = MagicMock('row_template')

        # and:
        paper_metadata = MetadataEntity(content={'product_name': 'paper'},
                                        links={'delivery': ['123', '456']})
        pen_metadata = MetadataEntity(content={'product_name': 'pen'},
                                      links={'delivery': ['789']})
        row_template.do_import = MagicMock(
            side_effect=[paper_metadata, pen_metadata])

        # and:
        mock_template_manager = MagicMock('template_manager')
        mock_template_manager.create_row_template = MagicMock(
            return_value=row_template)
        mock_template_manager.get_header_row = MagicMock(
            return_value=['header1', 'header2'])
        mock_template_manager.get_concrete_type = MagicMock(
            return_value='concrete_entity')

        # and:
        workbook = Workbook()
        worksheet = workbook.create_sheet('product')
        worksheet['A6'] = 'paper'
        worksheet['A7'] = 'pen'

        # when:
        worksheet_importer = WorksheetImporter(mock_template_manager)
        results = worksheet_importer.do_import(IngestWorksheet(worksheet))

        # then:
        self.assertEqual(2, len(results))
        self.assertIn(paper_metadata, results)
        self.assertIn(pen_metadata, results)

        # and: object id should be assigned
        paper_id = paper_metadata.object_id
        self.assertIsNotNone(paper_id)
        pen_id = pen_metadata.object_id
        self.assertIsNotNone(pen_id)
        self.assertNotEqual(paper_id, pen_id)
    def test_do_import_with_create_spreadsheet(self,
                                               worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        template_mgr.template.json_schemas = self.mock_json_schemas
        concrete_type_map = {'project': 'project', 'users': 'users'}
        template_mgr.get_concrete_type = lambda key: concrete_type_map.get(key)
        sheet_names = ['project', 'users']

        workbook = create_ingest_workbook(sheet_names, ['name', 'address'])
        workbook_importer = WorkbookImporter(template_mgr)

        # and
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer
        no_errors = []
        expected_errors = []
        for sheet_name in sheet_names:
            expected_errors.append({
                'location':
                f'sheet={sheet_name}',
                'type':
                'MissingEntityUUIDFound',
                'detail':
                f'The {sheet_name} entities in the spreadsheet should have UUIDs.'
            })

        # and:
        item = MetadataEntity(concrete_type='product',
                              domain_type='product',
                              object_id=910)
        worksheet_importer.do_import = MagicMock(side_effect=[([item],
                                                               no_errors)])

        # when:
        spreadsheet_json, errors = workbook_importer.do_import(workbook,
                                                               is_update=True)

        # then:
        self.assertTrue(
            all(elem in errors for elem in expected_errors),
            f'Errors expected to contain {MissingEntityUUIDFound.__name__}.')
 def setUp(self, worksheet_importer_constructor) -> None:
     self.template_mgr = MagicMock(name='template_manager')
     self.template_mgr.template.json_schemas = self.mock_json_schemas
     self.concrete_type_map = {'Project': 'project', 'Users': 'users'}
     self.template_mgr.get_concrete_type = lambda key: self.concrete_type_map.get(
         key)
     self.worksheet_importer = WorksheetImporter(self.template_mgr)
     worksheet_importer_constructor.return_value = self.worksheet_importer
     self.workbook_importer = WorkbookImporter(self.template_mgr)
     self.workbook = create_test_workbook('Project', 'Users')
     self.ingest_workbook = IngestWorkbook(self.workbook)
    def test_do_import_multiple_projects(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        template_mgr.template.json_schemas = self.mock_json_schemas
        template_mgr.get_concrete_type = MagicMock(return_value='project')

        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer
        no_errors = []
        expected_error = {
            'location':
            'sheet=Project',
            'type':
            'MultipleProjectsFound',
            'detail':
            'The spreadsheet should only be associated to a single project.'
        }

        # and:
        project_1 = MetadataEntity(concrete_type='project',
                                   domain_type='project',
                                   object_id=1)
        project_2 = MetadataEntity(concrete_type='project',
                                   domain_type='project',
                                   object_id=2)
        worksheet_importer.do_import = MagicMock(
            side_effect=[([project_1, project_2], no_errors)])

        # and:
        workbook = create_test_workbook('Project')
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        spreadsheet_json, errors = workbook_importer.do_import(
            IngestWorkbook(workbook), is_update=False)

        # then:
        self.assertIn(
            expected_error, errors,
            f'Errors expected to contain {MultipleProjectsFound.__name__}.')
Esempio n. 13
0
    def _fake_worksheet_import(self, worksheet_importer: WorksheetImporter, mock_template_manager):
        projects = {
            'project 1': {'short_name': 'project 1', 'description': 'first project'},
            'project 2': {'short_name': 'project 2', 'description': 'second project'}
        }

        cell_suspensions = {
            'cell_suspension_101': {'biomaterial_id': 'cell_suspension_101', 'biomaterial_name': 'cell suspension'}
        }

        worksheet_iterator = iter([projects, cell_suspensions])
        worksheet_importer.do_import = (
            lambda __, tm: worksheet_iterator.__next__() if tm is mock_template_manager else []
        )

        return {
            'project': projects,
            'biomaterial': cell_suspensions
        }
Esempio n. 14
0
    def test_do_import_with_module_tab(self, worksheet_importer_constructor):
        # given:
        template_mgr = MagicMock(name='template_manager')
        worksheet_importer = WorksheetImporter(template_mgr)
        worksheet_importer_constructor.return_value = worksheet_importer

        # and: stub worksheet importer
        project = MetadataEntity(concrete_type='Project',
                                 domain_type='Project')
        user = MetadataEntity(concrete_type='user',
                              domain_type='user',
                              object_id=773,
                              content={'user_name': 'janedoe'})
        fb_profile = MetadataEntity(concrete_type='sn_profile',
                                    domain_type='user',
                                    object_id=773,
                                    content={
                                        'sn_profiles': {
                                            'name': 'facebook',
                                            'id': '392'
                                        },
                                        'description': 'extra field'
                                    })
        ig_profile = MetadataEntity(concrete_type='sn_profile',
                                    domain_type='user',
                                    object_id=773,
                                    content={
                                        'sn_profiles': {
                                            'name': 'instagram',
                                            'id': 'a92'
                                        },
                                        'description': 'extra field'
                                    })
        worksheet_importer.do_import = MagicMock(
            side_effect=[[project], [user], [fb_profile, ig_profile]])

        # and: create test workbook
        workbook = create_test_workbook('Project', 'User',
                                        'User - SN Profiles')
        ingest_workbook = IngestWorkbook(workbook)
        workbook_importer = WorkbookImporter(template_mgr)

        # when:
        spreadsheet_json = workbook_importer.do_import(ingest_workbook)

        # then:
        self.assertIsNotNone(spreadsheet_json)
        self.assertEqual(2, len(spreadsheet_json))

        # and:
        user_map = spreadsheet_json.get('user')
        self.assertIsNotNone(user_map)

        # and:
        janedoe = user_map.get(773)
        self.assertIsNotNone(janedoe)
        content = janedoe.get('content')
        self.assertEqual('janedoe', content.get('user_name'))
        self.assertEqual(['user_name', 'sn_profiles'], list(content.keys()))

        # and:
        sn_profiles = content.get('sn_profiles')
        self.assertIsNotNone(sn_profiles)
        self.assertEqual(2, len(sn_profiles))

        # and:
        self.assertEqual({'name': 'facebook', 'id': '392'}, sn_profiles[0])
        self.assertEqual({'name': 'instagram', 'id': 'a92'}, sn_profiles[1])