def test_empty_lines(tmpdir): subsheet = Sheet(root_id='ocid') subsheet.add_field('c') parser = MockParser(['a', 'd'], {'b': subsheet}) parser.main_sheet.lines = [] for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name='release', output_name=os.path.join(tmpdir.strpath, 'release'+output.FORMATS_SUFFIX[format_name])) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath) assert wb.get_sheet_names() == ['release', 'b'] assert len(wb['release'].rows) == 1 assert [ x.value for x in wb['release'].rows[0] ] == [ 'a', 'd' ] assert len(wb['b'].rows) == 1 assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ] # Check CSV assert set(tmpdir.join('release').listdir()) == set([ tmpdir.join('release').join('release.csv'), tmpdir.join('release').join('b.csv') ]) assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a,d' assert tmpdir.join('release', 'b.csv').read().strip('\r\n') == 'ocid,c'
def test_empty_lines(tmpdir): subsheet = Sheet(root_id='ocid') subsheet.add_field('c') parser = MockParser(['a', 'd'], {'b': subsheet}) parser.main_sheet.lines = [] for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name='release', output_name=os.path.join( tmpdir.strpath, 'release' + output.FORMATS_SUFFIX[format_name])) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath) assert wb.sheetnames == ['release', 'b'] rows = list(wb['release'].rows) assert len(rows) == 1 assert [x.value for x in rows[0]] == ['a', 'd'] b_rows = list(wb['b'].rows) assert len(b_rows) == 1 assert [x.value for x in b_rows[0]] == ['ocid', 'c'] # Check CSV assert set(tmpdir.join('release').listdir()) == set([ tmpdir.join('release').join('release.csv'), tmpdir.join('release').join('b.csv') ]) assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a,d' assert tmpdir.join('release', 'b.csv').read().strip('\r\n') == 'ocid,c'
def test_populated_lines(tmpdir): subsheet = Sheet(root_id='ocid') subsheet.add_field('c') parser = MockParser(['a'], {}) parser.main_sheet.lines = [{'a': 'cell1'}, {'a': 'cell2'}] subsheet.lines = [{'c': 'cell3'}, {'c': 'cell4'}] parser.sub_sheets['b'] = subsheet for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name='release', output_name=os.path.join(tmpdir.strpath, 'release'+output.FORMATS_SUFFIX[format_name])) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath) assert wb.get_sheet_names() == ['release', 'b'] assert len(wb['release'].rows) == 3 assert [ x.value for x in wb['release'].rows[0] ] == [ 'a' ] assert [ x.value for x in wb['release'].rows[1] ] == [ 'cell1' ] assert [ x.value for x in wb['release'].rows[2] ] == [ 'cell2' ] assert len(wb['b'].rows) == 3 assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ] assert [ x.value for x in wb['b'].rows[1] ] == [ None, 'cell3' ] assert [ x.value for x in wb['b'].rows[2] ] == [ None, 'cell4' ] # Check CSV assert set(tmpdir.join('release').listdir()) == set([ tmpdir.join('release').join('release.csv'), tmpdir.join('release').join('b.csv') ]) assert tmpdir.join('release', 'release.csv').read().strip('\r\n').replace('\r', '') == 'a\ncell1\ncell2' assert tmpdir.join('release', 'b.csv').read().strip('\r\n').replace('\r', '') == 'ocid,c\n,cell3\n,cell4'
def test_populated_lines(tmpdir): subsheet = Sheet(root_id="ocid") subsheet.add_field("c") parser = MockParser(["a"], {}) parser.main_sheet.lines = [{"a": "cell1"}, {"a": "cell2"}] subsheet.lines = [{"c": "cell3"}, {"c": "cell4"}] parser.sub_sheets["b"] = subsheet for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name="release", output_name=os.path.join( tmpdir.strpath, "release" + output.FORMATS_SUFFIX[format_name] ), ) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join("release.xlsx").strpath) assert wb.sheetnames == ["release", "b"] rows = list(wb["release"].rows) assert len(rows) == 3 assert [x.value for x in rows[0]] == ["a"] assert [x.value for x in rows[1]] == ["cell1"] assert [x.value for x in rows[2]] == ["cell2"] b_rows = list(wb["b"].rows) assert len(b_rows) == 3 assert [x.value for x in b_rows[0]] == ["ocid", "c"] assert [x.value for x in b_rows[1]] == [None, "cell3"] assert [x.value for x in b_rows[2]] == [None, "cell4"] # Check CSV assert set(tmpdir.join("release").listdir()) == set( [ tmpdir.join("release").join("release.csv"), tmpdir.join("release").join("b.csv"), ] ) assert ( tmpdir.join("release", "release.csv").read().strip("\r\n").replace("\r", "") == "a\ncell1\ncell2" ) assert ( tmpdir.join("release", "b.csv").read().strip("\r\n").replace("\r", "") == "ocid,c\n,cell3\n,cell4" ) # Check ODS - currently broken test odswb = ODSReader(tmpdir.join("release.ods").strpath) ods_rows = odswb.getSheet("release") assert len(ods_rows) == 3 assert [x for x in ods_rows[0]] == ["a"] assert [x for x in ods_rows[1]] == ["cell1"] assert [x for x in ods_rows[2]] == ["cell2"] ods_b_rows = odswb.getSheet("b") assert len(ods_b_rows) == 3 assert [x for x in ods_b_rows[0]] == ["ocid", "c"] assert [x for x in ods_b_rows[1]] == [None, "cell3"] assert [x for x in ods_b_rows[2]] == [None, "cell4"]
def test_sub_sheet_list_like(): # SubSheet object should be appendable and iterable... # .append() is used in json_input.py at https://github.com/OpenDataServices/flatten-tool/blob/master/flattentool/json_input.py#L33 sub_sheet = Sheet() assert list(sub_sheet) == [] sub_sheet.append('a') sub_sheet.append('b') assert list(sub_sheet) == ['a', 'b'] # ... but also has an add_field method, which also appends sub_sheet.add_field('c') assert list(sub_sheet) == ['a', 'b', 'c'] # but with the option to add an id_field, which appears at the start of the list sub_sheet.add_field('d', id_field=True) assert list(sub_sheet) == ['d', 'a', 'b', 'c']
def test_empty_lines(tmpdir): subsheet = Sheet(root_id="ocid") subsheet.add_field("c") parser = MockParser(["a", "d"], {"b": subsheet}) parser.main_sheet.lines = [] for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name="release", output_name=os.path.join( tmpdir.strpath, "release" + output.FORMATS_SUFFIX[format_name] ), ) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join("release.xlsx").strpath) assert wb.sheetnames == ["release", "b"] rows = list(wb["release"].rows) assert len(rows) == 1 assert [x.value for x in rows[0]] == ["a", "d"] b_rows = list(wb["b"].rows) assert len(b_rows) == 1 assert [x.value for x in b_rows[0]] == ["ocid", "c"] # Check CSV assert set(tmpdir.join("release").listdir()) == set( [ tmpdir.join("release").join("release.csv"), tmpdir.join("release").join("b.csv"), ] ) assert tmpdir.join("release", "release.csv").read().strip("\r\n") == "a,d" assert tmpdir.join("release", "b.csv").read().strip("\r\n") == "ocid,c" # Check ODS odswb = ODSReader(tmpdir.join("release.ods").strpath) ods_rows = odswb.getSheet("release") assert len(ods_rows) == 1 assert [x for x in ods_rows[0]] == ["a", "d"] ods_b_rows = odswb.getSheet("b") assert len(ods_b_rows) == 1 assert [x for x in ods_b_rows[0]] == ["ocid", "c"]
def test_populated_lines(tmpdir): subsheet = Sheet(root_id='ocid') subsheet.add_field('c') parser = MockParser(['a'], {}) parser.main_sheet.lines = [{'a': 'cell1'}, {'a': 'cell2'}] subsheet.lines = [{'c': 'cell3'}, {'c': 'cell4'}] parser.sub_sheets['b'] = subsheet for format_name, spreadsheet_output_class in output.FORMATS.items(): spreadsheet_output = spreadsheet_output_class( parser=parser, main_sheet_name='release', output_name=os.path.join( tmpdir.strpath, 'release' + output.FORMATS_SUFFIX[format_name])) spreadsheet_output.write_sheets() # Check XLSX wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath) assert wb.sheetnames == ['release', 'b'] rows = list(wb['release'].rows) assert len(rows) == 3 assert [x.value for x in rows[0]] == ['a'] assert [x.value for x in rows[1]] == ['cell1'] assert [x.value for x in rows[2]] == ['cell2'] b_rows = list(wb['b'].rows) assert len(b_rows) == 3 assert [x.value for x in b_rows[0]] == ['ocid', 'c'] assert [x.value for x in b_rows[1]] == [None, 'cell3'] assert [x.value for x in b_rows[2]] == [None, 'cell4'] # Check CSV assert set(tmpdir.join('release').listdir()) == set([ tmpdir.join('release').join('release.csv'), tmpdir.join('release').join('b.csv') ]) assert tmpdir.join('release', 'release.csv').read().strip('\r\n').replace( '\r', '') == 'a\ncell1\ncell2' assert tmpdir.join('release', 'b.csv').read().strip('\r\n').replace( '\r', '') == 'ocid,c\n,cell3\n,cell4'