From 88940119f64d7626b3e98dbd47610cb86a3690ee Mon Sep 17 00:00:00 2001 From: SET Date: Wed, 12 Aug 2020 12:05:40 -0300 Subject: [PATCH] Added test for XLSX header information. Covering various situations, not all. --- tests/test_plot/test_int_bom.py | 147 +++++++++++++++++- tests/utils/context.py | 29 +++- .../int_bom_simple_xlsx_2.kiplot.yaml | 14 ++ .../int_bom_simple_xlsx_3.kiplot.yaml | 14 ++ .../int_bom_simple_xlsx_4.kiplot.yaml | 15 ++ .../int_bom_simple_xlsx_5.kiplot.yaml | 15 ++ .../int_bom_simple_xlsx_6.kiplot.yaml | 16 ++ 7 files changed, 245 insertions(+), 5 deletions(-) create mode 100644 tests/yaml_samples/int_bom_simple_xlsx_2.kiplot.yaml create mode 100644 tests/yaml_samples/int_bom_simple_xlsx_3.kiplot.yaml create mode 100644 tests/yaml_samples/int_bom_simple_xlsx_4.kiplot.yaml create mode 100644 tests/yaml_samples/int_bom_simple_xlsx_5.kiplot.yaml create mode 100644 tests/yaml_samples/int_bom_simple_xlsx_6.kiplot.yaml diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index b291f021..395bb1c3 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -77,6 +77,8 @@ KIBOM_TEST_GROUPS = 5 LINKS_COMPONENTS = ['J1', 'J2', 'R1'] LINKS_EXCLUDE = ['C1'] LINKS_GROUPS = 2 +INFO_ROWS = ['Schematic:', 'Variant:', 'Revision:', 'Date:', 'KiCad Version:'] +STATS_ROWS = ['Component Groups:', 'Component Count:', 'Fitted Components:', 'Number of PCBs:', 'Total components:'] def check_kibom_test_netlist(rows, ref_column, groups, exclude, comps): @@ -119,6 +121,30 @@ def check_path(rows, comp, ref, sp, val): return +def check_head_xlsx(r, info, stats, title='KiBot Bill of Materials'): + rn = 0 + if title: + # First row is just the title + assert r[rn][0] == title + rn += 1 + logging.debug('Title Ok') + if info: + info_col = 0 + for i, txt in enumerate(info): + assert r[rn+i][info_col] == INFO_ROWS[i] + if txt: + assert r[rn+i][info_col+1] == txt + logging.debug('Info block Ok') + if stats: + stats_col = 0 + if info: + stats_col += 2 + for i, txt in enumerate(stats): + assert r[rn+i][stats_col] == STATS_ROWS[i] + assert r[rn+i][stats_col+1] == txt, 'index: {} title: {}'.format(i, STATS_ROWS[i]) + logging.debug('Stats block Ok') + + def test_int_bom_simple_csv(): prj = 'kibom-test' ext = 'csv' @@ -189,7 +215,14 @@ def test_int_bom_simple_xlsx(): ctx = context.TestContextSCH('test_int_bom_simple_xlsx', prj, 'int_bom_simple_xlsx', BOM_DIR) ctx.run() out = prj + '-bom.' + ext - rows, header = ctx.load_xlsx(out) + rows, header, sh_head = ctx.load_xlsx(out) + check_head_xlsx(sh_head, + [prj, 'default', 'A', '2020-03-12', None], + [KIBOM_TEST_GROUPS+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS)+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS), + 1, + len(KIBOM_TEST_COMPONENTS)]) assert header == KIBOM_TEST_HEAD ref_column = header.index(REF_COLUMN_NAME) qty_column = header.index(QTY_COLUMN_NAME) @@ -620,3 +653,115 @@ def test_int_bom_sub_sheet_alt(): check_path(rows, 'U1', ref_column, sp_column, '/Sub Sheet') check_path(rows, 'U2', ref_column, sp_column, '/Sub Sheet 2') ctx.clean_up() + + +def test_int_bom_simple_xlsx_2(): + """ No title """ + prj = 'kibom-test' + ext = 'xlsx' + ctx = context.TestContextSCH('test_int_bom_simple_xlsx_2', prj, 'int_bom_simple_xlsx_2', BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + rows, header, sh_head = ctx.load_xlsx(out) + check_head_xlsx(sh_head, + [prj, 'default', 'A', '2020-03-12', None], + [KIBOM_TEST_GROUPS+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS)+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS), + 1, + len(KIBOM_TEST_COMPONENTS)], + title=None) + assert header == KIBOM_TEST_HEAD + ref_column = header.index(REF_COLUMN_NAME) + qty_column = header.index(QTY_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) + check_dnc(rows, 'R7', ref_column, qty_column) + ctx.clean_up() + + +def test_int_bom_simple_xlsx_3(): + """ No logo """ + prj = 'kibom-test' + ext = 'xlsx' + ctx = context.TestContextSCH('test_int_bom_simple_xlsx_3', prj, 'int_bom_simple_xlsx_3', BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + rows, header, sh_head = ctx.load_xlsx(out) + check_head_xlsx(sh_head, + [prj, 'default', 'A', '2020-03-12', None], + [KIBOM_TEST_GROUPS+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS)+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS), + 1, + len(KIBOM_TEST_COMPONENTS)]) + assert header == KIBOM_TEST_HEAD + ref_column = header.index(REF_COLUMN_NAME) + qty_column = header.index(QTY_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) + check_dnc(rows, 'R7', ref_column, qty_column) + ctx.clean_up() + + +def test_int_bom_simple_xlsx_4(): + """ No title, no logo """ + prj = 'kibom-test' + ext = 'xlsx' + ctx = context.TestContextSCH('test_int_bom_simple_xlsx_4', prj, 'int_bom_simple_xlsx_4', BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + rows, header, sh_head = ctx.load_xlsx(out) + check_head_xlsx(sh_head, + [prj, 'default', 'A', '2020-03-12', None], + [KIBOM_TEST_GROUPS+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS)+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS), + 1, + len(KIBOM_TEST_COMPONENTS)], + title=None) + assert header == KIBOM_TEST_HEAD + ref_column = header.index(REF_COLUMN_NAME) + qty_column = header.index(QTY_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) + check_dnc(rows, 'R7', ref_column, qty_column) + ctx.clean_up() + + +def test_int_bom_simple_xlsx_5(): + """ No title, no logo, no info """ + prj = 'kibom-test' + ext = 'xlsx' + ctx = context.TestContextSCH('test_int_bom_simple_xlsx_5', prj, 'int_bom_simple_xlsx_5', BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + rows, header, sh_head = ctx.load_xlsx(out) + check_head_xlsx(sh_head, + None, + [KIBOM_TEST_GROUPS+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS)+len(KIBOM_TEST_EXCLUDE), + len(KIBOM_TEST_COMPONENTS), + 1, + len(KIBOM_TEST_COMPONENTS)], + title=None) + assert header == KIBOM_TEST_HEAD + ref_column = header.index(REF_COLUMN_NAME) + qty_column = header.index(QTY_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) + check_dnc(rows, 'R7', ref_column, qty_column) + ctx.clean_up() + + +def test_int_bom_simple_xlsx_6(): + """ No title, no logo, no info, no stats """ + prj = 'kibom-test' + ext = 'xlsx' + ctx = context.TestContextSCH('test_int_bom_simple_xlsx_6', prj, 'int_bom_simple_xlsx_6', BOM_DIR) + ctx.run() + out = prj + '-bom.' + ext + rows, header, sh_head = ctx.load_xlsx(out) + assert len(sh_head) == 0 + assert header == KIBOM_TEST_HEAD + ref_column = header.index(REF_COLUMN_NAME) + qty_column = header.index(QTY_COLUMN_NAME) + check_kibom_test_netlist(rows, ref_column, KIBOM_TEST_GROUPS, KIBOM_TEST_EXCLUDE, KIBOM_TEST_COMPONENTS) + check_dnc(rows, 'R7', ref_column, qty_column) + ctx.clean_up() diff --git a/tests/utils/context.py b/tests/utils/context.py index 5a690bba..993993bb 100644 --- a/tests/utils/context.py +++ b/tests/utils/context.py @@ -408,24 +408,45 @@ class TestContext(object): root = ET.parse(worksheet).getroot() ns = '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}' rnum = 1 + sh_head = [] for r in root.iter(ns+'row'): rcur = int(r.attrib['r']) if rcur > rnum: + sh_head = rows # Discard the sheet header rows = [] rnum = rcur - rows.append([int(cell.text) for cell in r.iter(ns+'v')]) + this_row = [] + for cell in r.iter(ns+'c'): + if 't' in cell.attrib: + type = cell.attrib['t'] + else: + type = 'n' # default: number + value = cell.find(ns+'v') + if value is not None: + if type == 'n': + # Numbers as integers + value = int(value.text) + else: + value = value.text + this_row.append(value) + rows.append(this_row) rnum += 1 # Read the strings strings = self.get_out_path(os.path.join('desc', 'xl', 'sharedStrings.xml')) strs = [t.text for t in ET.parse(strings).getroot().iter(ns+'t')] # Replace the indexes by the strings for r in rows: - for i in range(len(r)): - r[i] = strs[r[i]] + for i, val in enumerate(r): + if isinstance(val, str): + r[i] = strs[int(val)] + for r in sh_head: + for i, val in enumerate(r): + if isinstance(val, str): + r[i] = strs[int(val)] # Separate the headers headers = rows.pop(0) - return rows, headers + return rows, headers, sh_head class TestContextSCH(TestContext): diff --git a/tests/yaml_samples/int_bom_simple_xlsx_2.kiplot.yaml b/tests/yaml_samples/int_bom_simple_xlsx_2.kiplot.yaml new file mode 100644 index 00000000..d52e22c2 --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_xlsx_2.kiplot.yaml @@ -0,0 +1,14 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in HTML format" + type: bom + dir: BoM + options: + format: XLSX + xlsx: + title: '' + diff --git a/tests/yaml_samples/int_bom_simple_xlsx_3.kiplot.yaml b/tests/yaml_samples/int_bom_simple_xlsx_3.kiplot.yaml new file mode 100644 index 00000000..6b848b13 --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_xlsx_3.kiplot.yaml @@ -0,0 +1,14 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in HTML format" + type: bom + dir: BoM + options: + format: XLSX + xlsx: + logo: false + diff --git a/tests/yaml_samples/int_bom_simple_xlsx_4.kiplot.yaml b/tests/yaml_samples/int_bom_simple_xlsx_4.kiplot.yaml new file mode 100644 index 00000000..5b80ebcc --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_xlsx_4.kiplot.yaml @@ -0,0 +1,15 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in HTML format" + type: bom + dir: BoM + options: + format: XLSX + xlsx: + title: '' + logo: false + diff --git a/tests/yaml_samples/int_bom_simple_xlsx_5.kiplot.yaml b/tests/yaml_samples/int_bom_simple_xlsx_5.kiplot.yaml new file mode 100644 index 00000000..7c85e3c8 --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_xlsx_5.kiplot.yaml @@ -0,0 +1,15 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in HTML format" + type: bom + dir: BoM + options: + format: XLSX + xlsx: + title: '' + logo: false + hide_pcb_info: true diff --git a/tests/yaml_samples/int_bom_simple_xlsx_6.kiplot.yaml b/tests/yaml_samples/int_bom_simple_xlsx_6.kiplot.yaml new file mode 100644 index 00000000..56766208 --- /dev/null +++ b/tests/yaml_samples/int_bom_simple_xlsx_6.kiplot.yaml @@ -0,0 +1,16 @@ +# Example KiPlot config file +kiplot: + version: 1 + +outputs: + - name: 'bom_internal' + comment: "Bill of Materials in HTML format" + type: bom + dir: BoM + options: + format: XLSX + xlsx: + title: '' + logo: false + hide_pcb_info: true + hide_stats_info: true