mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Testing invalid index and keys in DataTable.get_row*
This commit is contained in:
@@ -644,7 +644,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
|||||||
row_key, column_key = self.coordinate_to_cell_key(coordinate)
|
row_key, column_key = self.coordinate_to_cell_key(coordinate)
|
||||||
return self.get_cell(row_key, column_key)
|
return self.get_cell(row_key, column_key)
|
||||||
|
|
||||||
def get_row(self, row_key: RowKey) -> list[CellType]:
|
def get_row(self, row_key: RowKey | str) -> list[CellType]:
|
||||||
"""Get the values from the row identified by the given row key.
|
"""Get the values from the row identified by the given row key.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -656,6 +656,8 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
|||||||
Raises:
|
Raises:
|
||||||
RowDoesNotExist: When there is no row corresponding to the key.
|
RowDoesNotExist: When there is no row corresponding to the key.
|
||||||
"""
|
"""
|
||||||
|
if row_key not in self._row_locations:
|
||||||
|
raise RowDoesNotExist(f"Row key {row_key!r} is not valid.")
|
||||||
cell_mapping: dict[ColumnKey, CellType] = self._data.get(row_key, {})
|
cell_mapping: dict[ColumnKey, CellType] = self._data.get(row_key, {})
|
||||||
ordered_row: list[CellType] = []
|
ordered_row: list[CellType] = []
|
||||||
for column in self.ordered_columns:
|
for column in self.ordered_columns:
|
||||||
@@ -677,10 +679,12 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
|
|||||||
Raises:
|
Raises:
|
||||||
RowDoesNotExist: If there is no row with the given index.
|
RowDoesNotExist: If there is no row with the given index.
|
||||||
"""
|
"""
|
||||||
|
if not self.is_valid_row_index(row_index):
|
||||||
|
raise RowDoesNotExist(f"Row index {row_index!r} is not valid.")
|
||||||
row_key = self._row_locations.get_key(row_index)
|
row_key = self._row_locations.get_key(row_index)
|
||||||
return self.get_row(row_key)
|
return self.get_row(row_key)
|
||||||
|
|
||||||
def get_column(self, column_key: ColumnKey) -> list[CellType]:
|
def get_column(self, column_key: ColumnKey | str) -> list[CellType]:
|
||||||
"""Get the values from the column identified by the given column key.
|
"""Get the values from the column identified by the given column key.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -5,21 +5,25 @@ from ._data_table import (
|
|||||||
CellKey,
|
CellKey,
|
||||||
CellType,
|
CellType,
|
||||||
Column,
|
Column,
|
||||||
|
ColumnDoesNotExist,
|
||||||
ColumnKey,
|
ColumnKey,
|
||||||
CursorType,
|
CursorType,
|
||||||
DuplicateKey,
|
DuplicateKey,
|
||||||
Row,
|
Row,
|
||||||
|
RowDoesNotExist,
|
||||||
RowKey,
|
RowKey,
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Column",
|
|
||||||
"Row",
|
|
||||||
"RowKey",
|
|
||||||
"ColumnKey",
|
|
||||||
"CellKey",
|
|
||||||
"CursorType",
|
|
||||||
"CellType",
|
|
||||||
"CellDoesNotExist",
|
"CellDoesNotExist",
|
||||||
|
"CellKey",
|
||||||
|
"CellType",
|
||||||
|
"Column",
|
||||||
|
"ColumnDoesNotExist",
|
||||||
|
"ColumnKey",
|
||||||
|
"CursorType",
|
||||||
"DuplicateKey",
|
"DuplicateKey",
|
||||||
|
"Row",
|
||||||
|
"RowDoesNotExist",
|
||||||
|
"RowKey",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,8 +11,16 @@ from textual.events import Click, MouseMove
|
|||||||
from textual.message import Message
|
from textual.message import Message
|
||||||
from textual.message_pump import MessagePump
|
from textual.message_pump import MessagePump
|
||||||
from textual.widgets import DataTable
|
from textual.widgets import DataTable
|
||||||
from textual.widgets._data_table import DuplicateKey
|
from textual.widgets.data_table import (
|
||||||
from textual.widgets.data_table import CellDoesNotExist, CellKey, ColumnKey, Row, RowKey
|
CellDoesNotExist,
|
||||||
|
CellKey,
|
||||||
|
ColumnDoesNotExist,
|
||||||
|
ColumnKey,
|
||||||
|
DuplicateKey,
|
||||||
|
Row,
|
||||||
|
RowDoesNotExist,
|
||||||
|
RowKey,
|
||||||
|
)
|
||||||
|
|
||||||
ROWS = [["0/0", "0/1"], ["1/0", "1/1"], ["2/0", "2/1"]]
|
ROWS = [["0/0", "0/1"], ["1/0", "1/1"], ["2/0", "2/1"]]
|
||||||
|
|
||||||
@@ -363,6 +371,14 @@ async def test_get_row():
|
|||||||
assert table.get_row(second_row) == [3, 2, 1]
|
assert table.get_row(second_row) == [3, 2, 1]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_row_invalid_row_key():
|
||||||
|
app = DataTableApp()
|
||||||
|
async with app.run_test():
|
||||||
|
table = app.query_one(DataTable)
|
||||||
|
with pytest.raises(RowDoesNotExist):
|
||||||
|
table.get_row("abc")
|
||||||
|
|
||||||
|
|
||||||
async def test_get_row_at():
|
async def test_get_row_at():
|
||||||
app = DataTableApp()
|
app = DataTableApp()
|
||||||
async with app.run_test():
|
async with app.run_test():
|
||||||
@@ -381,6 +397,18 @@ async def test_get_row_at():
|
|||||||
assert table.get_row_at(1) == [2, 4, 1]
|
assert table.get_row_at(1) == [2, 4, 1]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("index", (-1, 2))
|
||||||
|
async def test_get_row_at_invalid_index(index):
|
||||||
|
app = DataTableApp()
|
||||||
|
async with app.run_test():
|
||||||
|
table = app.query_one(DataTable)
|
||||||
|
table.add_columns("A", "B", "C")
|
||||||
|
table.add_row(2, 4, 1)
|
||||||
|
table.add_row(3, 2, 1)
|
||||||
|
with pytest.raises(RowDoesNotExist):
|
||||||
|
table.get_row_at(index)
|
||||||
|
|
||||||
|
|
||||||
async def test_update_cell_cell_exists():
|
async def test_update_cell_cell_exists():
|
||||||
app = DataTableApp()
|
app = DataTableApp()
|
||||||
async with app.run_test():
|
async with app.run_test():
|
||||||
|
|||||||
Reference in New Issue
Block a user