Testing case where you try to update cells which dont exist

This commit is contained in:
Darren Burns
2023-02-01 17:34:03 +00:00
parent 23a34030cd
commit 77b94b005c
2 changed files with 21 additions and 1 deletions

View File

@@ -523,8 +523,17 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
value: The new value to put inside the cell. value: The new value to put inside the cell.
update_width: Whether to resize the column width to accommodate update_width: Whether to resize the column width to accommodate
for the new cell content. 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 self._update_count += 1
# Recalculate widths if necessary # 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 update_width: Whether to resize the column width to accommodate
for the new cell content. for the new cell content.
""" """
# TODO: Validate coordinate and raise exception
row_key, column_key = self.coordinate_to_cell_key(coordinate) row_key, column_key = self.coordinate_to_cell_key(coordinate)
self.update_cell(row_key, column_key, value, update_width=update_width) self.update_cell(row_key, column_key, value, update_width=update_width)

View File

@@ -303,6 +303,16 @@ async def test_update_cell_cell_exists():
assert table.get_cell_value("1", "A") == "NEW_VALUE" 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 # TODO: Test update coordinate