def test_get_data_rows(self): # given: start_row_idx = 6 header_row_idx = 4 header_row = ['name', 'address', 'mobile', 'email'] expected_data_row = ['Jane Doe', 'Cambridge', '12-345-67', '*****@*****.**'] rows = [[], [], [], [], [], []] # initialise 6 rows rows[header_row_idx-1] = header_row rows[start_row_idx-1] = expected_data_row worksheet = spreadsheet_utils.create_worksheet('person', rows) # when ingest_worksheet = IngestWorksheet(worksheet, header_row_idx=header_row_idx) data_rows = ingest_worksheet.get_data_rows(start_row=start_row_idx) data_row_values = [] for row in data_rows: cell_values = [cell.value for cell in row] data_row_values.append(cell_values) # then: self.assertEqual(len(data_row_values), 1) self.assertEqual(data_row_values, [expected_data_row])
def do_import(self, ingest_worksheet: IngestWorksheet): row_template = self.template.create_row_template(ingest_worksheet) records = [] rows = ingest_worksheet.get_data_rows() for index, row in enumerate(rows): metadata = row_template.do_import(row) if not metadata.object_id: metadata.object_id = self._generate_id() records.append(metadata) return records
def do_import(self, ingest_worksheet: IngestWorksheet): records = [] worksheet_errors = [] try: row_template = self.template.create_row_template(ingest_worksheet) rows = ingest_worksheet.get_data_rows() for index, row in enumerate(rows): metadata, row_errors = row_template.do_import(row, ingest_worksheet.is_module_tab()) for error in row_errors: if 'location' in error: error["location"] = f'sheet={ingest_worksheet.title} row={index}, {error["location"]}' else: error["location"] = f'sheet={ingest_worksheet.title} row={index}' worksheet_errors.append(error) if not metadata.object_id: metadata.object_id = self._generate_id() records.append(metadata) except Exception as e: worksheet_errors.append({ "location": f'sheet={ingest_worksheet.title}', "type": e.__class__.__name__, "detail": str(e) }) return records, worksheet_errors