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 create_row_template(self, ingest_worksheet: IngestWorksheet): concrete_type = self.get_concrete_type(ingest_worksheet.title) domain_type = self.get_domain_type(concrete_type) column_headers = ingest_worksheet.get_column_headers() cell_conversions = [] context = self._determine_context(concrete_type, ingest_worksheet) header_counter = {} for header in column_headers: if not header_counter.get(header): header_counter[header] = 0 header_counter[header] = header_counter[header] + 1 column_spec = ColumnSpecification(self.template, header, concrete_type, context=context, order_of_occurrence=header_counter[header]) strategy = conversion_strategy.determine_strategy(column_spec) cell_conversions.append(strategy) default_values = None if not ingest_worksheet.is_module_tab(): default_values = self._define_default_values(concrete_type) return RowTemplate(domain_type, concrete_type, cell_conversions, default_values=default_values)
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