(core) Suggest correct table when converting to RefList

Summary: RecordSets now have new encoding and rendering analogous to Records: `['r', 'Table', [1, 2, 3]]` and `Table[[1, 2, 3]]`.

Test Plan: Added to nbrowser/TypeChange.ts.

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2987
This commit is contained in:
Alex Hall
2021-08-20 22:35:41 +02:00
parent f53ab2cb30
commit 9916a2d919
7 changed files with 97 additions and 26 deletions

View File

@@ -183,11 +183,12 @@ def encode_object(value):
return ['d', moment.date_to_ts(value)]
elif isinstance(value, RaisedException):
return ['E'] + value.encode_args()
elif isinstance(value, (list, tuple, RecordList)):
elif isinstance(value, (list, tuple)):
return ['L'] + [encode_object(item) for item in value]
elif isinstance(value, records.RecordSet):
# Represent RecordSet (e.g. result of lookupRecords) in the same way as a RecordList.
return ['L'] + [encode_object(int(item)) for item in value]
return ['r', value._table.table_id, value._row_ids]
elif isinstance(value, RecordSetStub):
return ['r', value.table_id, value.row_ids]
elif isinstance(value, dict):
if not all(isinstance(key, six.string_types) for key in value):
raise UnmarshallableError("Dict with non-string keys")
@@ -217,6 +218,8 @@ def decode_object(value):
args = value[1:]
if code == 'R':
return RecordStub(args[0], args[1])
elif code == 'r':
return RecordSetStub(args[0], args[1])
elif code == 'D':
return moment.ts_to_dt(args[0], moment.Zone(args[1]))
elif code == 'd':
@@ -323,3 +326,9 @@ class RecordStub(object):
def __init__(self, table_id, row_id):
self.table_id = table_id
self.row_id = row_id
class RecordSetStub(object):
def __init__(self, table_id, row_ids):
self.table_id = table_id
self.row_ids = row_ids

View File

@@ -185,6 +185,9 @@ class RecordSet(object):
return self._get_col(name)
return self._table._attribute_error(name, self._source_relation)
def __repr__(self):
return "%s[%s]" % (self._table.table_id, self._row_ids)
def _clone_with_relation(self, src_relation):
return self._table.RecordSet(self._row_ids,
relation=src_relation.compose(self._source_relation),

View File

@@ -1,6 +1,8 @@
# pylint: disable=line-too-long
import testsamples
import test_engine
from objtypes import RecordSetStub
class TestFormulaUndo(test_engine.EngineTestCase):
def setUp(self):
@@ -26,13 +28,13 @@ return '#%s %s' % (table.my_counter, $schoolName)
}])
self.assertTableData("Students", cols="subset", data=[
["id", "schoolName", "schoolCities", "counter" ],
[1, "Columbia", [1, 2], "#1 Columbia",],
[2, "Yale", [3, 4], "#2 Yale", ],
[3, "Columbia", [1, 2], "#3 Columbia",],
[4, "Yale", [3, 4], "#4 Yale", ],
[5, "Eureka", [], "#5 Eureka", ],
[6, "Yale", [3, 4], "#6 Yale", ],
["id", "schoolName", "schoolCities", "counter" ],
[1, "Columbia", RecordSetStub("Schools", [1, 2]), "#1 Columbia",],
[2, "Yale", RecordSetStub("Schools", [3, 4]), "#2 Yale", ],
[3, "Columbia", RecordSetStub("Schools", [1, 2]), "#3 Columbia",],
[4, "Yale", RecordSetStub("Schools", [3, 4]), "#4 Yale", ],
[5, "Eureka", RecordSetStub("Schools", []), "#5 Eureka", ],
[6, "Yale", RecordSetStub("Schools", [3, 4]), "#6 Yale", ],
])
# Applying an action produces expected changes to all formula columns, and corresponding undos.
@@ -41,14 +43,14 @@ return '#%s %s' % (table.my_counter, $schoolName)
"stored": [
["UpdateRecord", "Students", 6, {"schoolName": "Columbia"}],
["UpdateRecord", "Students", 6, {"counter": "#7 Columbia"}],
["UpdateRecord", "Students", 6, {"schoolCities": ["L", 1, 2]}],
["UpdateRecord", "Students", 6, {"schoolCities": ["r", "Schools", [1, 2]]}],
["UpdateRecord", "Students", 6, {"schoolIds": "1:2"}],
],
"direct": [True, False, False, False],
"undo": [
["UpdateRecord", "Students", 6, {"schoolName": "Yale"}],
["UpdateRecord", "Students", 6, {"counter": "#6 Yale"}],
["UpdateRecord", "Students", 6, {"schoolCities": ["L", 3, 4]}],
["UpdateRecord", "Students", 6, {"schoolCities": ["r", "Schools", [3, 4]]}],
["UpdateRecord", "Students", 6, {"schoolIds": "3:4"}],
],
})
@@ -63,7 +65,7 @@ return '#%s %s' % (table.my_counter, $schoolName)
self.assertOutActions(out_actions, {
"stored": [
["UpdateRecord", "Students", 6, {"schoolIds": "3:4"}],
["UpdateRecord", "Students", 6, {"schoolCities": ["L", 3, 4]}],
["UpdateRecord", "Students", 6, {"schoolCities": ["r", "Schools", [3, 4]]}],
["UpdateRecord", "Students", 6, {"counter": "#6 Yale"}],
["UpdateRecord", "Students", 6, {"schoolName": "Yale"}],
["UpdateRecord", "Students", 6, {"counter": "#8 Yale"}],
@@ -71,7 +73,7 @@ return '#%s %s' % (table.my_counter, $schoolName)
"direct": [True, True, True, True, False], # undos currently fully direct; formula update is indirect.
"undo": [
["UpdateRecord", "Students", 6, {"schoolIds": "1:2"}],
["UpdateRecord", "Students", 6, {"schoolCities": ["L", 1, 2]}],
["UpdateRecord", "Students", 6, {"schoolCities": ["r", "Schools", [1, 2]]}],
["UpdateRecord", "Students", 6, {"counter": "#7 Columbia"}],
["UpdateRecord", "Students", 6, {"schoolName": "Columbia"}],
["UpdateRecord", "Students", 6, {"counter": "#6 Yale"}],
@@ -79,13 +81,15 @@ return '#%s %s' % (table.my_counter, $schoolName)
})
self.assertTableData("Students", cols="subset", data=[
["id", "schoolName", "schoolCities", "counter" ],
[1, "Columbia", [1, 2], "#1 Columbia",],
[2, "Yale", [3, 4], "#2 Yale", ],
[3, "Columbia", [1, 2], "#3 Columbia",],
[4, "Yale", [3, 4], "#4 Yale", ],
[5, "Eureka", [], "#5 Eureka", ],
[6, "Yale", [3, 4], "#8 Yale", ], # This counter got updated
["id", "schoolName", "schoolCities", "counter" ],
[1, "Columbia", RecordSetStub("Schools", [1, 2]), "#1 Columbia"],
[2, "Yale", RecordSetStub("Schools", [3, 4]), "#2 Yale", ],
[3, "Columbia", RecordSetStub("Schools", [1, 2]), "#3 Columbia"],
[4, "Yale", RecordSetStub("Schools", [3, 4]), "#4 Yale", ],
[5, "Eureka", RecordSetStub("Schools", []), "#5 Eureka", ],
# This counter got updated
[6, "Yale", RecordSetStub("Schools", [3, 4]), "#8 Yale", ],
])
def test_save_to_empty_column(self):

View File

@@ -299,6 +299,12 @@ class TestRenames(test_engine.EngineTestCase):
"type": ["Ref:Persons", "Any"],
"formula": ["Persons.lookupOne(addr=$id)", "Persons.lookupRecords(addr=$id)"]
}],
["BulkUpdateRecord", "Address", [11, 12, 13, 14], {
"people": [["r", "Persons", [4]],
["r", "Persons", [1, 3]],
["r", "Persons", [2]],
["r", "Persons", []]]
}],
]})
def test_rename_table_autocomplete(self):