Example #1
0
def test_issue_17():
    doc = Document("tests/data/issue-17.numbers")
    sheets = doc.sheets
    table = sheets[0].tables[0]
    assert table.cell(0, 0).value == 123.0
    assert table.cell(0, 0).is_merged == False
    assert table.cell(0, 0).formula == None
Example #2
0
def test_many_columns():
    doc = Document("tests/data/test-3.numbers")
    sheets = doc.sheets
    tables = sheets["Sheet_2"].tables
    data = tables[0].rows(values_only=True)
    ref = [["COLUMN" + str(x) for x in range(1, 601)]]
    assert data == ref
Example #3
0
def test_issue_10():
    doc = Document("tests/data/issue-10.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    for i, test_value in enumerate(ISSUE_10_REF):
        assert table.cell(i + 1, 1).value == test_value
Example #4
0
def test_empty_document():
    doc = Document()
    sheets = doc.sheets
    tables = sheets[0].tables
    data = doc.sheets[0].tables[0].rows()
    assert len(data) == 22
    assert len(data[0]) == 7
    assert type(data[0][0]) == EmptyCell
Example #5
0
def test_table_functions():
    doc = Document("tests/data/test-10.numbers")
    sheets = doc.sheets
    table = sheets[0].tables[0]
    compare_tables(table, TABLE_1_FORMULAS)

    table = sheets[1].tables[0]
    compare_tables(table, TABLE_2_FORMULAS)
Example #6
0
def test_table_contents_as_cells():
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    rows = tables[0].rows()
    for index in range(len(rows)):
        data = [cell.value or None for cell in rows[index]]
        assert data == ZZZ_TABLE_1_REF[index]
Example #7
0
def test_table_contents():
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    data = tables[0].rows(values_only=True)
    assert data == ZZZ_TABLE_1_REF
    data = tables[1].rows(values_only=True)
    assert data == ZZZ_TABLE_2_REF
Example #8
0
def test_many_rows():
    doc = Document("tests/data/test-3.numbers")
    sheets = doc.sheets
    tables = sheets["Sheet_1"].tables
    data = tables[0].rows(values_only=True)
    ref = []
    _ = [ref.append(["ROW" + str(x)]) for x in range(1, 601)]
    assert data == ref
Example #9
0
def test_issue_4():
    doc = Document("tests/data/issue-4.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    assert table.cell(1, 0).value == ISSUE_4_REF_1
    assert table.cell(1, 1).value == ISSUE_4_REF_2
    assert table.cell(2, 0).value == ISSUE_4_REF_3
    assert table.cell(2, 1).value == ISSUE_4_REF_4
Example #10
0
def test_empty_cells():
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_2"].tables
    assert len(tables) == 1
    table = tables["XXX_Table_1"]
    assert table.num_cols == 6
    assert table.num_rows == 4
    assert table.rows(values_only=True) == XXX_TABLE_1_REF
Example #11
0
def test_issue_7():
    doc = Document("tests/data/issue-7.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    assert table.cell(1, 1).value == ISSUE_7_REF_1
    assert table.cell(2, 1).value == ISSUE_7_REF_2
    table.cell(1, 1).bullets[0] == ISSUE_7_REF_1.split("\n")[0] + "\n"
    table.cell(2, 1).bullets[2] == ISSUE_7_REF_2.split("\n")[2] + "\n"
Example #12
0
def test_save_document(tmp_path):
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    cell_values = tables["ZZZ_Table_1"].rows(values_only=True)

    sheets["ZZZ_Sheet_1"].name = "ZZZ_Sheet_1 NEW"
    tables["ZZZ_Table_1"].name = "ZZZ_Table_1 NEW"

    new_filename = tmp_path / "test-1-new.numbers"
    doc.save(new_filename)

    new_doc = Document(new_filename)
    new_sheets = new_doc.sheets
    new_tables = new_sheets["ZZZ_Sheet_1 NEW"].tables
    new_cell_values = new_tables["ZZZ_Table_1 NEW"].rows(values_only=True)

    assert cell_values == new_cell_values
Example #13
0
def test_issue_14():
    doc = Document("tests/data/issue-14.numbers")
    sheets = doc.sheets
    table = sheets["Ex 1"].tables[0]
    assert table.cell("G2").value == 19
    assert table.cell("G2").formula == "XLOOKUP(F2,A2:A15,B2:B15)"
    table = sheets["Ex 6"].tables[0]
    assert table.cell("F2").value == "Pam"
    assert table.cell("F2").formula == "XLOOKUP(F1,$B$2:$B$15,$A$2:$A$15,,,-1)"
Example #14
0
def test_cell_wide_ref():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_2"].tables
    table = tables["XXX_Table_1"]
    assert table.cell("").value == "excepteur"  #  defaults to [0, 0]
    assert table.cell("A1").value == "excepteur"
    assert table.cell("Z1").value == "ea"
    assert table.cell("BA1").value == "veniam"
    assert table.cell("CZ1").value == "dolore"
Example #15
0
def test_cell_lookup():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_1"]
    assert table.num_cols == 5
    assert table.num_rows == 4
    assert table.cell(0, 1).value == "XXX_COL_2"
    assert table.cell(2, 2).value is None
    assert table.cell(3, 4).value == "XXX_3_5"
Example #16
0
def test_api_change():
    doc = Document("tests/data/test-1.numbers")

    with pytest.warns(DeprecationWarning) as record:
        sheets = doc.sheets()
    assert len(record) == 1
    assert "sheets() is deprecated and will be removed" in record[0].message.args[0]
    assert sheets[0].name == "ZZZ_Sheet_1"

    sheet = doc.sheets[0]
    with pytest.warns(DeprecationWarning) as record:
        tables = sheet.tables()
    assert len(record) == 1
    assert "tables() is deprecated and will be removed" in record[0].message.args[0]
    assert tables[0].name == "ZZZ_Table_1"

    assert len(doc.sheets) == 2
    assert len(doc.sheets[0].tables) == 2
    assert doc.sheets[0].tables[0].cell(1, 0).value == "YYY_ROW_1"
Example #17
0
def test_sheet_exceptions():
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    with pytest.raises(IndexError) as e:
        _ = sheets[3]
    assert "out of range" in str(e.value)
    with pytest.raises(KeyError) as e:
        _ = sheets["invalid"]
    assert "no sheet named" in str(e.value)
    with pytest.raises(LookupError) as e:
        _ = sheets[float(1)]
    assert "invalid index" in str(e.value)
Example #18
0
def test_names_refs():
    doc = Document("tests/data/test-1.numbers")
    sheets = doc.sheets
    assert len(sheets) == 2
    assert sheets[0].name == "ZZZ_Sheet_1"
    assert sheets[1].name == "ZZZ_Sheet_2"
    tables = sheets["ZZZ_Sheet_1"].tables
    assert len(tables) == 2
    assert tables[0].name == "ZZZ_Table_1"
    assert tables[1].name == "ZZZ_Table_2"
    assert tables["ZZZ_Table_1"].num_cols == 3
    assert tables["ZZZ_Table_1"].num_rows == 5
Example #19
0
def test_edit_cell_values(tmp_path):
    doc = Document("tests/data/test-save-1.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    table.write("B2", "new_b2")
    table.write(1, 2, "new_c2")
    table.write(2, 0, True)
    table.write(2, 1, 7890)
    table.write(2, 2, 78.90)
    table.write(5, 3, datetime(2020, 12, 25))
    table.write(5, 4, timedelta(seconds=7890))
    table.write(5, 5, "7890")

    assert isinstance(table.cell(3, 4), EmptyCell)
    assert isinstance(table.cell(4, 4), EmptyCell)

    new_filename = tmp_path / "test-save-1-new.numbers"
    doc.save(new_filename)

    doc = Document(new_filename)
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]

    assert table.cell(1, 1).value == "new_b2"
    assert table.cell("C2").value == "new_c2"
    assert table.cell(2, 0).value == True
    assert table.cell(2, 1).value == 7890
    assert table.cell(2, 2).value == 78.90
    assert table.cell(5, 3).value == datetime(2020, 12, 25)
    assert table.cell(5, 4).value == timedelta(seconds=7890)
    assert table.cell(5, 5).value == "7890"
Example #20
0
def test_large_table():
    row = [None for i in range(0, 270)]
    ref = [row.copy() for i in range(0, 90)]
    for i in range(0, 90):
        ref[i][i] = ref_cell_text(i, i)
        ref[i][90 + i] = ref_cell_text(i, 90 + i)
        ref[i][180 + i] = ref_cell_text(i, 180 + i)

    doc = Document("tests/data/test-6.numbers")
    sheets = doc.sheets
    tables = sheets["Sheet"].tables
    data = tables[0].rows(values_only=True)
    assert data == ref
Example #21
0
def print_table(args, filename):
    writer = csv.writer(sys.stdout, dialect="excel")
    for sheet in Document(filename).sheets:
        if args.sheet is not None and sheet.name not in args.sheet:
            continue
        for table in sheet.tables:
            if args.table is not None and table.name not in args.table:
                continue
            for row in table.rows():
                cells = [cell_as_string(args, cell) for cell in row]
                if not args.brief:
                    sys.stdout.write(f"{filename}: {sheet.name}: {table.name}: ")
                writer.writerow(cells)
Example #22
0
def test_issue_3():
    doc = Document("tests/data/issue-3.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    ref = []
    for row in tables[0].iter_rows():
        ref.append(tuple([x.value for x in row]))
    assert ref == ISSUE_3_REF

    ref = []
    for row in tables[0].iter_rows(values_only=True):
        ref.append(row)
    assert ref == ISSUE_3_REF
Example #23
0
def test_cell_ref_lookup():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_1"]
    assert table.cell("A1").value == "XXX_COL_1"
    assert table.cell("C3").value is None
    assert table.cell("E4").value == "XXX_3_5"
    with pytest.raises(IndexError) as e:
        _ = table.cell("E5")
    assert "out of range" in str(e.value)
    with pytest.raises(IndexError) as e:
        _ = table.cell("A5")
    assert "out of range" in str(e.value)
Example #24
0
def test_bullets():
    doc = Document("tests/data/test-bullets.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]
    assert table.cell(0, 1).value == TEST_REF_1
    assert table.cell(1, 1).value == TEST_REF_2
    assert table.cell(2, 1).value == TEST_REF_3
    assert table.cell(0, 1).bullets[0] == TEST_REF_1.split("\n")[0]
    assert table.cell(0, 1).bullets[2] == TEST_REF_1.split("\n")[2]
    assert table.cell(0, 1).bullets[3] == TEST_REF_1.split("\n")[3]
    assert table.cell(2, 0).value == "t"
    assert table.cell(0, 0).is_bulleted == False
    assert table.cell(1, 1).is_bulleted == True
    assert table.cell(0, 0).bullets is None
Example #25
0
def test_bullets():
    doc = Document("tests/data/test-formats.numbers")
    sheets = doc.sheets
    tables = sheets[0].tables
    table = tables[0]

    assert table.cell(4, 2).bullets[0] == "star-bullet-1"
    assert table.cell(4, 2).bullets[2] == "dash-bullet"
    assert table.cell(3, 2).formatted_bullets[4] == "• bullet-5"
    assert table.cell(4, 2).formatted_bullets[0] == "★ star-bullet-1"
    assert table.cell(4, 2).formatted_bullets[2] == "- dash-bullet"
    assert table.cell(6, 2).value == "the quick brown fox jumped"
    assert table.cell(8, 2).formatted_bullets[1] == "2. blue"
    assert table.cell(8, 3).formatted_bullets[2] == "C. apple"
    assert table.cell(9, 2).formatted_bullets == TEST_NUMBERED_REF
Example #26
0
def test_create_sheet(tmp_path, pytestconfig):
    _EXPERIMENTAL_NUMBERS_PARSER = True
    doc = Document()
    sheets = doc.sheets
    sheets[0].tables[0].write("B2", "data")
    sheets[0].tables[0].write("G2", "data")

    with pytest.raises(IndexError) as e:
        _ = sheets[0].add_table("TablE 1")
    assert "table 'TablE 1' already exists" in str(e.value)

    # sheets[0].modify_table()
    # table = sheets[0].tables[0]

    table = sheets[0].add_table()
    assert table.name == "Table 2"
    table.write("B1", "Column B")
    table.write("C1", "Column C")
    table.write("D1", "Column D")
    table.write("B2", "Mary had")
    table.write("C2", "a little")
    table.write("D2", "lamb")

    # with pytest.raises(IndexError) as e:
    #     _ = doc.add_sheet("SheeT 1")
    # assert "sheet 'SheeT 1' already exists" in str(e.value)

    # doc.add_sheet("New Sheet", "New Table")
    # sheet = doc.sheets["New Sheet"]
    # table = sheet.tables["New Table"]
    # table.write(0, 1, "Column 1")
    # table.write(0, 2, "Column 2")
    # table.write(0, 3, "Column 3")
    # table.write(1, 1, 1000)
    # table.write(1, 2, 2000)
    # table.write(1, 3, 3000)

    if pytestconfig.getoption("save_file") is not None:
        new_filename = pytestconfig.getoption("save_file")
    else:
        new_filename = tmp_path / "test-1-new.numbers"
    doc.save(new_filename)

    doc = Document(new_filename)
    sheets = doc.sheets

    table = sheets[0].tables[0]
    # table = sheets[0].tables[1]
    # assert sheets[0].tables[1].name == "Table 2"
    assert table.cell("B1").value == "Column B"
    assert table.cell("C1").value == "Column C"
    assert table.cell("D1").value == "Column D"
    assert table.cell("B2").value == "Mary had"
    assert table.cell("C2").value == "a little"
    assert table.cell("D2").value == "lamb"
Example #27
0
def test_exceptions():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_1"]
    with pytest.raises(IndexError) as e:
        _ = table.cell("XYZ")
    assert "invalid cell" in str(e.value)
    with pytest.raises(IndexError) as e:
        _ = table.cell(4, 0)
    assert "out of range" in str(e.value)
    with pytest.raises(IndexError) as e:
        _ = table.cell(0, 5)
    assert "out of range" in str(e.value)
    with pytest.raises(IndexError) as e:
        _ = table.cell(0, 5, 2)
    assert "invalid cell reference" in str(e.value)
Example #28
0
def test_iter_col_exceptions():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_1"]
    with pytest.raises(IndexError) as e:
        _ = [x for x in table.iter_cols(max_row=999)]
    assert str(e.value) == "row 999 out of range"
    with pytest.raises(IndexError) as e:
        _ = [x for x in table.iter_cols(min_row=-1)]
    assert str(e.value) == "row -1 out of range"
    with pytest.raises(IndexError) as e:
        _ = [x for x in table.iter_cols(max_col=999)]
    assert str(e.value) == "column 999 out of range"
    with pytest.raises(IndexError) as e:
        _ = [x for x in table.iter_cols(min_col=-1)]
    assert str(e.value) == "column -1 out of range"
Example #29
0
def test_col_iterator():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_2"]
    val = 0
    for col in table.iter_cols():
        val += col[2].value if col[2] is not None else 0.0
    assert val == 164.224
    val = 0.0
    for col in table.iter_cols(min_col=1, max_col=3, values_only=True):
        val += col[8] or 0.0
    assert val == 336.572
    val = 0.0
    for row in table.iter_cols(min_row=5, max_row=6, min_col=1, max_col=2):
        val += row[0].value + row[1].value
    assert val == 522.108
Example #30
0
def test_row_iterator():
    doc = Document("tests/data/test-7.numbers")
    sheets = doc.sheets
    tables = sheets["ZZZ_Sheet_1"].tables
    table = tables["XXX_Table_2"]
    val = 0
    for row in table.iter_rows():
        val += row[0].value if row[0] is not None else 0.0
    assert val == 252
    val = 0.0
    for row in table.iter_rows(min_row=2, max_row=7, values_only=True):
        val += row[2] or 0.0
    assert val == 978.0
    val = 0.0
    for row in table.iter_rows(min_row=5, max_row=6, min_col=1, max_col=2):
        val += row[0].value + row[1].value
    assert val == 522.108