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_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__}.')
def test_get_module_field_name(self): # given: workbook = create_test_workbook('Product - Reviews', 'User - SN Profiles', 'Log - file-names', 'Account') # and: simple reviews_sheet = workbook.get_sheet_by_name('Product - Reviews') reviews = IngestWorksheet(reviews_sheet) # and: with space in between sn_profiles_sheet = workbook.get_sheet_by_name('User - SN Profiles') sn_profiles = IngestWorksheet(sn_profiles_sheet) # and: with hyphen file_names_sheet = workbook.get_sheet_by_name('Log - file-names') file_names = IngestWorksheet(file_names_sheet) # and: not module worksheet account_sheet = workbook.get_sheet_by_name('Account') account = IngestWorksheet(account_sheet) # expect: self.assertEqual('reviews', reviews.get_module_field_name()) self.assertEqual('sn_profiles', sn_profiles.get_module_field_name()) self.assertEqual('file_names', file_names.get_module_field_name()) self.assertIsNone(account.get_module_field_name())
def test_do_import_project_worksheet(self, worksheet_importer_constructor): # given: template_mgr = MagicMock(name='template_manager') worksheet_importer = WorkbookImporter(template_mgr) worksheet_importer_constructor.return_value = worksheet_importer # and: project = MetadataEntity(domain_type='project', concrete_type='project', content={'description': 'test project'}) jsmith = MetadataEntity(domain_type='project', concrete_type='contact', content={ 'contributors': { 'name': 'John', 'email': '*****@*****.**' } }) ppan = MetadataEntity(domain_type='project', concrete_type='contact', content={ 'contributors': { 'name': 'Peter', 'email': '*****@*****.**' } }) worksheet_importer.do_import = MagicMock( side_effect=[[project], [jsmith, ppan]]) # and: workbook = create_test_workbook('Project', 'Project - Contributors') ingest_workbook = IngestWorkbook(workbook) workbook_importer = WorkbookImporter(template_mgr) # when: spreadsheet_json = workbook_importer.do_import(ingest_workbook) # then: project_map = spreadsheet_json.get('project') self.assertEqual(1, len(project_map)) project_content = list(project_map.values())[0].get('content') self.assertEqual('test project', project_content.get('description')) # and: contributors = project_content.get('contributors') self.assertEqual(2, len(contributors)) self.assertIn({ 'name': 'John', 'email': '*****@*****.**' }, contributors) self.assertIn({ 'name': 'Peter', 'email': '*****@*****.**' }, contributors)
def test_is_module_tab(self): # given: workbook = create_test_workbook('Product', 'Product - History') product_sheet = workbook.get_sheet_by_name('Product') history_sheet = workbook.get_sheet_by_name('Product - History') # and: product = IngestWorksheet(product_sheet) history = IngestWorksheet(history_sheet) # expect: self.assertFalse(product.is_module_tab()) self.assertTrue(history.is_module_tab())
def test_get_title(self): # given: workbook = create_test_workbook('User', 'User - SN Profiles') user_sheet = workbook.get_sheet_by_name('User') sn_profiles_sheet = workbook.get_sheet_by_name('User - SN Profiles') # and: user = IngestWorksheet(user_sheet) sn_profiles = IngestWorksheet(sn_profiles_sheet) # expect: self.assertEqual('User', user.title) self.assertEqual('User - SN Profiles', sn_profiles.title)
def test_importable_worksheets(self): # given: importable_names = ['Organ From Donor', 'Cell Suspension', 'Project'] workbook = create_test_workbook(*importable_names) workbook.create_sheet('Schemas') # and: ingest_workbook = IngestWorkbook(workbook) # when: actual_worksheets = ingest_workbook.importable_worksheets() # then: actual_titles = [ingest_worksheet.title for ingest_worksheet in actual_worksheets] self.assertEqual(importable_names, actual_titles)
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'])
def test_create_row_template_for_module_worksheet(self, determine_strategy, look_up): # given: template = MagicMock(name='schema_template') ingest_api = MagicMock(name='ingest_api') # TODO define method in SchemaTemplate that returns domain and concrete types #module-tabs # and: concrete_type = 'product' template.get_tab_key = MagicMock(return_value=concrete_type) # and: spec_map = { 'product': { 'schema': { 'domain_entity': 'merchandise/product' } } } template.lookup = lambda key: spec_map.get(key, None) # and: template_mgr = TemplateManager(template, ingest_api) # and: workbook = create_test_workbook('Product - Reviews') reviews_worksheet = workbook.get_sheet_by_name('Product - Reviews') reviews_worksheet['A4'] = 'product.info.id' reviews_worksheet['B4'] = 'product.reviews.rating' # and: set up dummy look up results id_spec = MagicMock(name='id_spec') rating_spec = MagicMock(name='rating_spec') look_up.side_effect = [id_spec, rating_spec] # and: set up strategies id_strategy = MagicMock(name='id_strategy') rating_strategy = MagicMock(name='rating_strategy') determine_strategy.side_effect = { id_spec: id_strategy, rating_spec: rating_strategy }.get # when: row_template = template_mgr.create_row_template( IngestWorksheet(reviews_worksheet)) # then: expected_calls = [ call(template, 'product.info.id', concrete_type, order_of_occurrence=1, context='product.reviews'), call(template, 'product.reviews.rating', concrete_type, order_of_occurrence=1, context='product.reviews') ] look_up.assert_has_calls(expected_calls) # and: self.assertIsNotNone(row_template) self.assertIn(id_strategy, row_template.cell_conversions) self.assertIn(rating_strategy, row_template.cell_conversions)
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])