Example #1
0
def test_row_access_errors():
    """Test various exceptions upon bad access."""
    table = Table(value=_TABLE_DATA["dict"])
    table._set_row("r1", [9, 9, 9])
    assert table._get_row("r1") == [9, 9, 9]
    with pytest.raises(KeyError):
        table._get_row("nonsense")
    with pytest.raises(KeyError):
        table._set_row("nonsense", [1, 2, 3])
    with pytest.raises(KeyError):
        table._del_row("nonsense")
    with pytest.raises(IndexError):
        table._get_rowi(10)

    assert table._assert_col(0) == 0
    with pytest.raises(IndexError):
        table._assert_col(10)
Example #2
0
def test_adding_deleting_to_empty_table():
    """Test the dict-like api starting with empty table."""
    table = Table()
    assert not any(table.data.to_list())
    assert table.shape == (0, 0)
    # add a new column
    table["c1"] = [1, 2, 3, 4]
    assert table["c1"] == [1, 2, 3, 4]
    assert table.shape == (4, 1)
    # add one with more data
    table["c2"] = [1, 2, 3, 4, 5, 6]
    assert table.shape == (6, 2)
    # add one with less data
    table["c3"] = [1, 2, 3]
    assert table.shape == (6, 3)
    assert table.size == 18
    assert table["c3"][5] is None  # it fills to meet the rows

    assert table.row_headers == (0, 1, 2, 3, 4, 5)
    assert table._get_row(table.row_headers[0]) == [1, 1, 1]
    table._del_row(table.row_headers[0])
    assert table._get_row(table.row_headers[0]) == [2, 2, 2]

    # we can use dict methods
    table.update({"c1": [1, 1]})  # it will clear the extra...
    assert table["c1"] == [1, 1, None, None, None]

    # we can del
    del table["c2"]
    assert "c2" not in table
    with pytest.raises(KeyError):
        table["c2"]  # does not exist

    with pytest.raises(TypeError):
        # not a collection
        table["c5"] = 1  # type: ignore

    with pytest.raises(KeyError):
        del table["c219"]  # does not exist

    table.clear()
    assert table.shape == (0, 0)
    assert not table.column_headers
    assert not table.row_headers