From cf9e0585a9f972f8d3a1166f1fb98e1ea119c125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Sadzi=C5=84ski?= Date: Mon, 6 Dec 2021 14:43:20 +0100 Subject: [PATCH] (core) Undo bug - restoring dependencies for trigger formulas Summary: Undo wasn't restoring trigger formulas dependencies. Test Plan: Python tests Reviewers: alexmojaki Reviewed By: alexmojaki Subscribers: alexmojaki Differential Revision: https://phab.getgrist.com/D3163 --- sandbox/grist/docactions.py | 4 ++++ sandbox/grist/test_trigger_formulas.py | 30 ++++++++++++++++++++++++++ sandbox/grist/useractions.py | 4 ---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/sandbox/grist/docactions.py b/sandbox/grist/docactions.py index cc8f5583..592cb6c7 100644 --- a/sandbox/grist/docactions.py +++ b/sandbox/grist/docactions.py @@ -94,6 +94,10 @@ class DocActions(object): # anything that depends on them). self._engine.invalidate_records(table_id, row_ids, col_ids=columns.keys()) + # If the column update changes its trigger-formula conditions, rebuild dependencies. + if (table_id == "_grist_Tables_column" and + ("recalcWhen" in columns or "recalcDeps" in columns)): + self._engine.trigger_columns_changed() def ReplaceTableData(self, table_id, row_ids, column_values): old_data = self._engine.fetch_table(table_id, formulas=False) diff --git a/sandbox/grist/test_trigger_formulas.py b/sandbox/grist/test_trigger_formulas.py index 72ae2047..216cfe51 100644 --- a/sandbox/grist/test_trigger_formulas.py +++ b/sandbox/grist/test_trigger_formulas.py @@ -667,3 +667,33 @@ class TestTriggerFormulas(test_engine.EngineTestCase): self.assertFormulaError(error, ZeroDivisionError, 'float division by zero') self.assertEqual(error._message, 'float division by zero') self.assertEqual(error.details, objtypes.RaisedException(ZeroDivisionError()).no_traceback().details) + + + def test_undo_should_restore_dependencies(self): + """ + Test case for a bug. Undo wasn't restoring trigger formula dependencies. + """ + self.load_sample(self.sample_math) + self.add_record("Math", A=1, B=1) + self.assertTableData("Math", data=[ + ["id", "A", "B", "C"], + [1, 1, 1, 1/1 + 1/1], + ]) + + # Remove deps from C. + out_actions = self.update_record("_grist_Tables_column", 3, recalcDeps=None) + # Make sure that trigger is not fired. + self.update_record("Math", 1, A=0.5) + self.assertTableData("Math", data=[ + ["id", "A", "B", "C"], + [1, 0.5, 1, 1/1 + 1/1], # C is not recalculated + ]) + + # Apply undo action. + self.apply_undo_actions(out_actions.undo) + # Invoke trigger by updating A, and make sure C is updated. + self.update_record("Math", 1, A=0.2) + self.assertTableData("Math", data=[ + ["id", "A", "B", "C"], + [1, 0.2, 1, 1/0.2 + 1/1], # C is recalculated + ]) diff --git a/sandbox/grist/useractions.py b/sandbox/grist/useractions.py index 7fb0253a..1fb9b76c 100644 --- a/sandbox/grist/useractions.py +++ b/sandbox/grist/useractions.py @@ -581,10 +581,6 @@ class UserActions(object): self._docmodel.update([f for c in type_changed for f in c.viewFields], widgetOptions='', displayCol=0) - # If the column update changes its trigger-formula conditions, rebuild dependencies. - if any(("recalcWhen" in values or "recalcDeps" in values) for c, values in update_pairs): - self._engine.trigger_columns_changed() - self.doBulkUpdateFromPairs(table_id, update_pairs) for table_id in rebuild_summary_tables: