You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gristlabs_grist-core/sandbox/grist/test_recordlist.py

103 lines
3.3 KiB

(core) Implement trigger formulas (generalizing default formulas) Summary: Trigger formulas can be calculated for new records, or for new records and updates to certain fields, or all fields. They do not recalculate on open, and they MAY be set directly by the user, including for data-cleaning. - Column metadata now includes recalcWhen and recalcDeps fields. - Trigger formulas are NOT recalculated on open or on schema changes. - When recalcWhen is "never", formula isn't calculated even for new records. - When recalcWhen is "allupdates", formula is calculated for new records and any manual (non-formula) updates to the record. - When recalcWhen is "", formula is calculated for new records, and changes to recalcDeps fields (which may be formula fields or column itself). - A column whose recalcDeps includes itself is a "data-cleaning" column; a value set by the user will still trigger the formula. - All trigger-formulas receive a "value" argument (to support the case above). Small changes - Update RefLists (used for recalcDeps) when target rows are deleted. - Add RecordList.__contains__ (for `rec in refList` or `id in refList` checks) - Clarify that Calculate action has replaced load_done() in practice, and use it in tests too, to better match reality. Left for later: - UI for setting recalcWhen / recalcDeps. - Implementation of actions such as "Recalculate for all cells". - Allowing trigger-formulas access to the current user's info. Test Plan: Added a comprehensive python-side test for various trigger combinations Reviewers: paulfitz, alexmojaki Reviewed By: paulfitz Differential Revision: https://phab.getgrist.com/D2872
3 years ago
import testutil
import test_engine
from objtypes import RecordSetStub
(core) Implement trigger formulas (generalizing default formulas) Summary: Trigger formulas can be calculated for new records, or for new records and updates to certain fields, or all fields. They do not recalculate on open, and they MAY be set directly by the user, including for data-cleaning. - Column metadata now includes recalcWhen and recalcDeps fields. - Trigger formulas are NOT recalculated on open or on schema changes. - When recalcWhen is "never", formula isn't calculated even for new records. - When recalcWhen is "allupdates", formula is calculated for new records and any manual (non-formula) updates to the record. - When recalcWhen is "", formula is calculated for new records, and changes to recalcDeps fields (which may be formula fields or column itself). - A column whose recalcDeps includes itself is a "data-cleaning" column; a value set by the user will still trigger the formula. - All trigger-formulas receive a "value" argument (to support the case above). Small changes - Update RefLists (used for recalcDeps) when target rows are deleted. - Add RecordList.__contains__ (for `rec in refList` or `id in refList` checks) - Clarify that Calculate action has replaced load_done() in practice, and use it in tests too, to better match reality. Left for later: - UI for setting recalcWhen / recalcDeps. - Implementation of actions such as "Recalculate for all cells". - Allowing trigger-formulas access to the current user's info. Test Plan: Added a comprehensive python-side test for various trigger combinations Reviewers: paulfitz, alexmojaki Reviewed By: paulfitz Differential Revision: https://phab.getgrist.com/D2872
3 years ago
class TestRecordList(test_engine.EngineTestCase):
col = testutil.col_schema_row
sample_desc = {
"SCHEMA": [
[1, "Creatures", [
col(1, "Name", "Text", False),
col(2, "Class", "Ref:Class", False),
]],
[2, "Class", [
col(11, "Name", "Text", False),
col(12, "Creatures", "RefList:Creatures", False),
]],
],
"DATA": {
"Class": [
["id", "Name", "Creatures"],
[1, "Mammals", [1, 3]],
[2, "Reptilia", [2, 4]],
],
"Creatures": [
["id","Name", "Class"],
[1, "Cat", 1],
[2, "Chicken", 2],
[3, "Dolphin", 1],
[4, "Turtle", 2],
],
}
}
sample = testutil.parse_test_sample(sample_desc)
def test_removals(self):
# Removing target rows should remove them from RefList columns.
self.load_sample(self.sample)
self.assertTableData("Class", data=[
["id", "Name", "Creatures"],
[1, "Mammals", [1, 3]],
[2, "Reptilia", [2, 4]],
])
self.remove_record("Creatures", 2)
self.assertTableData("Class", data=[
["id", "Name", "Creatures"],
[1, "Mammals", [1, 3]],
[2, "Reptilia", [4]],
])
self.remove_record("Creatures", 4)
self.assertTableData("Class", data=[
["id", "Name", "Creatures"],
[1, "Mammals", [1, 3]],
[2, "Reptilia", None]
])
def test_contains(self):
self.load_sample(self.sample)
self.add_column('Class', 'ContainsInt', type='Any', isFormula=True,
formula="2 in $Creatures")
self.add_column('Class', 'ContainsRec', type='Any', isFormula=True,
formula="Creatures.lookupOne(Name='Chicken') in $Creatures")
self.add_column('Class', 'ContainsWrong', type='Any', isFormula=True,
formula="Class.lookupOne(Name='Reptilia') in $Creatures")
self.assertTableData("Class", data=[
["id", "Name", "Creatures", "ContainsInt", "ContainsRec", "ContainsWrong"],
[1, "Mammals", [1, 3], False, False, False],
[2, "Reptilia", [2, 4], True, True, False]
])
def test_equals(self):
self.load_sample(self.sample)
self.add_column('Class', 'Lookup', type='RefList:Creatures', isFormula=True,
formula="Creatures.lookupRecords(Class=$id)")
self.add_column('Class', 'Equal', type='Any', isFormula=True,
formula="$Lookup == $Creatures")
self.assertTableData("Class", data=[
["id", "Name", "Creatures", "Lookup", "Equal"],
[1, "Mammals", [1, 3], [1, 3], True],
[2, "Reptilia", [2, 4], [2, 4], True],
])
def test_attribute_chain(self):
self.load_sample(self.sample)
self.add_column('Class', 'Names', type='Any', isFormula=True,
formula="$Creatures.Class.Name")
self.add_column('Class', 'Creatures2', type='Any', isFormula=True,
formula="$Creatures.Class.Creatures")
mammals = RecordSetStub("Creatures", [1, 3])
reptiles = RecordSetStub("Creatures", [2, 4])
self.assertTableData("Class", data=[
["id", "Name", "Creatures", "Names", "Creatures2"],
[1, "Mammals", [1, 3], ["Mammals", "Mammals"], [mammals, mammals]],
[2, "Reptilia", [2, 4], ["Reptilia", "Reptilia"], [reptiles, reptiles]],
])