diff --git a/sandbox/grist/column.py b/sandbox/grist/column.py index e3d51951..d9625dff 100644 --- a/sandbox/grist/column.py +++ b/sandbox/grist/column.py @@ -358,6 +358,10 @@ class PositionColumn(NumericColumn): # This is a list of row_ids, ordered by the position. self._sorted_rows = SortedListWithKey(key=lambda x: SafeSortKey(self.raw_get(x))) + def clear(self): + super(PositionColumn, self).clear() + self._sorted_rows.clear() + def set(self, row_id, value): self._sorted_rows.discard(row_id) super(PositionColumn, self).set(row_id, value) diff --git a/sandbox/grist/test_replace_table_data.py b/sandbox/grist/test_replace_table_data.py new file mode 100644 index 00000000..357b0407 --- /dev/null +++ b/sandbox/grist/test_replace_table_data.py @@ -0,0 +1,47 @@ +import logging + +import test_engine +from test_engine import Table, Column + +log = logging.getLogger(__name__) + +class TestReplaceTableData(test_engine.EngineTestCase): + + @test_engine.test_undo + def test_replace_and_add(self): + # This tests a fix for a bug where after ReplaceTableData, subsequent adds were causing an + # error with "relabeling" (updating manualSort column). + + # Add a table with a couple of columns and records. + self.apply_user_action(["AddTable", "Vessels", []]) + self.apply_user_action(["AddColumn", "Vessels", "Type", {}]) + self.apply_user_action(["AddColumn", "Vessels", "Size", {}]) + self.apply_user_action(["BulkAddRecord", "Vessels", [None, None], + {"Type": ["cup", "pot"], "Size": [8, 64]}]) + + # Check that we guessed correct column types, and the values are there. + self.assertTables([ + Table(1, "Vessels", primaryViewId=1, summarySourceTable=0, columns=[ + Column(1, "manualSort", "ManualSortPos", False, "", 0), + Column(2, "Type", "Text", False, "", 0), + Column(3, "Size", "Numeric", False, "", 0), + ]) + ]) + self.assertTableData("Vessels", cols="subset", rows="all", data=[ + [ "id", "Type", "Size" ], + [ 1, "cup", 8 ], + [ 2, "pot", 64 ], + ]) + + # Now do ReplaceTableData, and add more rows. + self.apply_user_action(["ReplaceTableData", "Vessels", [], {}]) + + # The bug used to happen here, manifesting as error + # "docactions.[Bulk]UpdateRecord for non-existent # record #1" + self.apply_user_action(["BulkAddRecord", "Vessels", [None, None], + {"Type": ["shot", "bucket"], "Size": [1.5, 640.0]}]) + self.assertTableData("Vessels", cols="subset", rows="all", data=[ + [ "id", "Type", "Size" ], + [ 1, "shot", 1.5 ], + [ 2, "bucket", 640 ], + ]) diff --git a/test/nbrowser/gristUtils.ts b/test/nbrowser/gristUtils.ts index 8a6dade6..695afc92 100644 --- a/test/nbrowser/gristUtils.ts +++ b/test/nbrowser/gristUtils.ts @@ -2824,9 +2824,12 @@ export async function onNewTab(action: () => Promise) { await driver.executeScript("window.open('about:blank', '_blank')"); const tabs = await driver.getAllWindowHandles(); await driver.switchTo().window(tabs[tabs.length - 1]); - await action(); - await driver.close(); - await driver.switchTo().window(tabs[tabs.length - 2]); + try { + await action(); + } finally { + await driver.close(); + await driver.switchTo().window(tabs[tabs.length - 2]); + } } /**