From 77b94b005ce3251f741f30423091ab3cf1013fe8 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 1 Feb 2023 17:34:03 +0000 Subject: [PATCH] Testing case where you try to update cells which dont exist --- src/textual/widgets/_data_table.py | 12 +++++++++++- tests/test_data_table.py | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index a9a00fbc3..0668630c9 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -523,8 +523,17 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): value: The new value to put inside the cell. update_width: Whether to resize the column width to accommodate for the new cell content. + + Raises: + CellDoesNotExist: When the supplied `row_key` and `column_key` + cannot be found in the table. """ - self.data[row_key][column_key] = value + try: + self.data[row_key][column_key] = value + except KeyError: + raise CellDoesNotExist( + f"No cell exists for row_key={row_key!r}, column_key={column_key!r}." + ) from None self._update_count += 1 # Recalculate widths if necessary @@ -545,6 +554,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): update_width: Whether to resize the column width to accommodate for the new cell content. """ + # TODO: Validate coordinate and raise exception row_key, column_key = self.coordinate_to_cell_key(coordinate) self.update_cell(row_key, column_key, value, update_width=update_width) diff --git a/tests/test_data_table.py b/tests/test_data_table.py index 23499e3eb..b4f20f811 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -303,6 +303,16 @@ async def test_update_cell_cell_exists(): assert table.get_cell_value("1", "A") == "NEW_VALUE" +async def test_update_cell_cell_doesnt_exist(): + app = DataTableApp() + async with app.run_test(): + table = app.query_one(DataTable) + table.add_column("A", key="A") + table.add_row("1", key="1") + with pytest.raises(CellDoesNotExist): + table.update_cell("INVALID", "CELL", "Value") + + # TODO: Test update coordinate