Ensuring new caches cleared, location mapping reset when clear called

This commit is contained in:
Darren Burns
2023-02-16 10:27:56 +00:00
parent 3d74f339c8
commit 921a65816e
2 changed files with 37 additions and 0 deletions

View File

@@ -755,6 +755,8 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
self._cell_render_cache.clear()
self._line_cache.clear()
self._styles_cache.clear()
self._offset_cache.clear()
self._ordered_row_cache.clear()
def get_row_height(self, row_key: RowKey) -> int:
"""Given a row key, return the height of that row in terminal cells.
@@ -1018,8 +1020,10 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
self._y_offsets.clear()
self._data.clear()
self.rows.clear()
self._row_locations = TwoWayDict({})
if columns:
self.columns.clear()
self._column_locations = TwoWayDict({})
self._require_update_dimensions = True
self.cursor_coordinate = Coordinate(0, 0)
self.hover_coordinate = Coordinate(0, 0)

View File

@@ -279,10 +279,13 @@ async def test_clear():
assert table._data == {}
assert table.rows == {}
assert table.row_count == 0
assert len(table._row_locations) == 0
assert len(table._column_locations) == 1
assert len(table.columns) == 1
# Clearing the columns too
table.clear(columns=True)
assert len(table._column_locations) == 0
assert len(table.columns) == 0
@@ -891,6 +894,36 @@ async def test_column_cursor_highlight_events():
assert latest_message.cursor_column == 0
async def test_reuse_row_key_after_clear():
"""Regression test for https://github.com/Textualize/textual/issues/1806"""
app = DataTableApp()
async with app.run_test():
table = app.query_one(DataTable)
table.add_columns("A", "B")
table.add_row(0, 1, key="ROW1")
table.add_row(2, 3, key="ROW2")
table.clear()
table.add_row(4, 5, key="ROW1") # Reusing the same keys as above
table.add_row(7, 8, key="ROW2")
assert table.get_row("ROW1") == [4, 5]
assert table.get_row("ROW2") == [7, 8]
async def test_reuse_column_key_after_clear():
"""Regression test for https://github.com/Textualize/textual/issues/1806"""
app = DataTableApp()
async with app.run_test():
table = app.query_one(DataTable)
table.add_column("A", key="COLUMN1")
table.add_column("B", key="COLUMN2")
table.clear(columns=True)
table.add_column("C", key="COLUMN1") # Reusing the same keys as above
table.add_column("D", key="COLUMN2")
table.add_row(1, 2)
assert list(table.get_column("COLUMN1")) == [1]
assert list(table.get_column("COLUMN2")) == [2]
def test_key_equals_equivalent_string():
text = "Hello"
key = RowKey(text)