2020-07-27 18:57:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-07-18 15:20:02 +00:00
|
|
|
import logging
|
2023-09-07 10:01:42 +00:00
|
|
|
import unittest
|
|
|
|
|
2023-09-11 15:14:47 +00:00
|
|
|
from asttokens.util import fstring_positions_work
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
import testutil
|
|
|
|
import test_engine
|
|
|
|
|
2023-07-18 15:20:02 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRenames(test_engine.EngineTestCase):
|
|
|
|
# Simpler cases of column renames in formulas. Here's the list of cases we support and test.
|
|
|
|
|
|
|
|
# $COLUMN where NAME is a column (formula or non-formula)
|
|
|
|
# $ref.COLUMN when $ref is a non-formula Reference column
|
|
|
|
# $ref.column.COLUMN
|
|
|
|
# $ref.COLUMN when $ref is a function with a Ref type.
|
|
|
|
# $ref.COLUMN when $ref is a function with Any type but clearly returning a Ref.
|
|
|
|
# Table.lookupFunc(COLUMN1=value, COLUMN2=value) and for .lookupRecords
|
|
|
|
# Table.lookupFunc(...).COLUMN and for .lookupRecords
|
|
|
|
# Table.lookupFunc(...).foo.COLUMN and for .lookupRecords
|
|
|
|
# [x.COLUMN for x in Table.lookupRecords(...)] for different kinds of comprehensions
|
|
|
|
# TABLE.lookupFunc(...) where TABLE is a user-defined table.
|
|
|
|
|
|
|
|
sample = testutil.parse_test_sample({
|
|
|
|
"SCHEMA": [
|
|
|
|
[1, "Address", [
|
|
|
|
[21, "city", "Text", False, "", "", ""],
|
|
|
|
]],
|
|
|
|
[2, "People", [
|
|
|
|
[22, "name", "Text", False, "", "", ""],
|
|
|
|
[23, "addr", "Ref:Address", False, "", "", ""],
|
|
|
|
[24, "city", "Any", True, "$addr.city", "", ""],
|
|
|
|
]]
|
|
|
|
],
|
|
|
|
"DATA": {
|
|
|
|
"Address": [
|
|
|
|
["id", "city" ],
|
|
|
|
[11, "New York" ],
|
|
|
|
[12, "Colombia" ],
|
|
|
|
[13, "New Haven" ],
|
|
|
|
[14, "West Haven" ],
|
|
|
|
],
|
|
|
|
"People": [
|
|
|
|
["id", "name" , "addr" ],
|
|
|
|
[1, "Bob" , 12 ],
|
|
|
|
[2, "Alice" , 13 ],
|
|
|
|
[3, "Doug" , 12 ],
|
|
|
|
[4, "Sam" , 11 ],
|
|
|
|
],
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_rename_rec_attribute(self):
|
|
|
|
# Simple case: we are renaming `$COLUMN`.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "addr", "address"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "addr", "address"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$address.city"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [23, 24], {
|
|
|
|
"colId": ["address", "city"],
|
|
|
|
"formula": ["", "$address.city"]
|
|
|
|
}],
|
|
|
|
],
|
|
|
|
# Things should get recomputed, but produce same results, hence no calc actions.
|
|
|
|
"calc": []
|
|
|
|
})
|
|
|
|
|
|
|
|
# Make sure renames of formula columns are also recognized.
|
|
|
|
self.add_column("People", "CityUpper", formula="$city.upper()")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "CityUpper", {"formula": "$ciudad.upper()"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [24, 25], {
|
|
|
|
"colId": ["ciudad", "CityUpper"],
|
|
|
|
"formula": ["$address.city", "$ciudad.upper()"]
|
|
|
|
}]
|
|
|
|
]})
|
|
|
|
|
2023-09-11 15:14:47 +00:00
|
|
|
@unittest.skipUnless(fstring_positions_work(), "Python 3.10+ only")
|
2023-09-07 10:01:42 +00:00
|
|
|
def test_rename_inside_fstring(self):
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("People", "CityUpper", formula="f'{$city.upper()}'")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "CityUpper", {"formula": "f'{$ciudad.upper()}'"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [24, 25], {
|
|
|
|
"colId": ["ciudad", "CityUpper"],
|
|
|
|
"formula": ["$addr.city", "f'{$ciudad.upper()}'"]
|
|
|
|
}]
|
|
|
|
]})
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
def test_rename_reference_attribute(self):
|
|
|
|
# Slightly harder: renaming `$ref.COLUMN`
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24], {
|
|
|
|
"colId": ["ciudad", "city"],
|
|
|
|
"formula": ["", "$addr.ciudad"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_ref_ref_attr(self):
|
|
|
|
# Slightly harder still: renaming $ref.column.COLUMN.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "person", type="Ref:People")
|
|
|
|
self.add_column("Address", "person_city", formula="$person.addr.city")
|
|
|
|
self.add_column("Address", "person_city2", formula="a = $person.addr\nreturn a.city")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["ModifyColumn", "Address", "person_city", {"formula": "$person.addr.ciudad"}],
|
|
|
|
["ModifyColumn", "Address", "person_city2", {"formula":
|
|
|
|
"a = $person.addr\nreturn a.ciudad"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 26, 27], {
|
|
|
|
"colId": ["ciudad", "city", "person_city", "person_city2"],
|
|
|
|
"formula": ["", "$addr.ciudad", "$person.addr.ciudad", "a = $person.addr\nreturn a.ciudad"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_typed_ref_func_attr(self):
|
|
|
|
# Renaming `$ref.COLUMN` when $ref is a function with a Ref type.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("People", "addr_func", type="Ref:Address", isFormula=True, formula="$addr")
|
|
|
|
self.add_column("People", "city2", formula="$addr_func.city")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["ModifyColumn", "People", "city2", {"formula": "$addr_func.ciudad"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 26], {
|
|
|
|
"colId": ["ciudad", "city", "city2"],
|
|
|
|
"formula": ["", "$addr.ciudad", "$addr_func.ciudad"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_any_ref_func_attr(self):
|
|
|
|
# Renaming `$ref.COLUMN` when $ref is a function with Any type but clearly returning a Ref.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("People", "addr_func", isFormula=True, formula="$addr")
|
|
|
|
self.add_column("People", "city3", formula="$addr_func.city")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["ModifyColumn", "People", "city3", {"formula": "$addr_func.ciudad"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 26], {
|
|
|
|
"colId": ["ciudad", "city", "city3"],
|
|
|
|
"formula": ["", "$addr.ciudad", "$addr_func.ciudad"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_reflist_attr(self):
|
|
|
|
# Renaming `$ref.COLUMN` where $ref is a data or function with RefList type (most importantly
|
|
|
|
# applies to the $group column of summary tables).
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("People", "addr_list", type="RefList:Address", isFormula=False)
|
|
|
|
self.add_column("People", "addr_func", type="RefList:Address", isFormula=True, formula="[1,2]")
|
|
|
|
self.add_column("People", "citysum", formula="sum($addr_func.city) + sum($addr_list.city)")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["ModifyColumn", "People", "citysum", {"formula":
|
|
|
|
"sum($addr_func.ciudad) + sum($addr_list.ciudad)"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 27], {
|
|
|
|
"colId": ["ciudad", "city", "citysum"],
|
|
|
|
"formula": ["", "$addr.ciudad", "sum($addr_func.ciudad) + sum($addr_list.ciudad)"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
|
|
|
|
def test_rename_lookup_param(self):
|
|
|
|
# Renaming `Table.lookupOne(COLUMN1=value, COLUMN2=value)` and for `.lookupRecords`
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "people", formula="People.lookupOne(addr=$id, city=$city)")
|
|
|
|
self.add_column("Address", "people2", formula="People.lookupRecords(addr=$id)")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "addr", "ADDRESS"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "addr", "ADDRESS"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$ADDRESS.city"}],
|
|
|
|
["ModifyColumn", "Address", "people",
|
|
|
|
{"formula": "People.lookupOne(ADDRESS=$id, city=$city)"}],
|
|
|
|
["ModifyColumn", "Address", "people2",
|
|
|
|
{"formula": "People.lookupRecords(ADDRESS=$id)"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [23, 24, 25, 26], {
|
|
|
|
"colId": ["ADDRESS", "city", "people", "people2"],
|
|
|
|
"formula": ["", "$ADDRESS.city",
|
|
|
|
"People.lookupOne(ADDRESS=$id, city=$city)",
|
|
|
|
"People.lookupRecords(ADDRESS=$id)"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
# Another rename that should affect the second parameter.
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "Address", "people",
|
|
|
|
{"formula": "People.lookupOne(ADDRESS=$id, ciudad=$city)"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [24, 25], {
|
|
|
|
"colId": ["ciudad", "people"],
|
|
|
|
"formula": ["$ADDRESS.city", "People.lookupOne(ADDRESS=$id, ciudad=$city)"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
# This is kind of unnecessary, but checks how the values of params are affected separately.
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "city2"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "city2"],
|
|
|
|
["ModifyColumn", "People", "ciudad", {"formula": "$ADDRESS.city2"}],
|
|
|
|
["ModifyColumn", "Address", "people",
|
|
|
|
{"formula": "People.lookupOne(ADDRESS=$id, ciudad=$city2)"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 25], {
|
|
|
|
"colId": ["city2", "ciudad", "people"],
|
|
|
|
"formula": ["", "$ADDRESS.city2", "People.lookupOne(ADDRESS=$id, ciudad=$city2)"]
|
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_lookup_result_attr(self):
|
|
|
|
# Renaming `Table.lookupOne(...).COLUMN` and for `.lookupRecords`
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "people", formula="People.lookupOne(addr=$id, city=$city).name")
|
|
|
|
self.add_column("Address", "people2", formula="People.lookupRecords(addr=$id).name")
|
2022-07-11 19:18:36 +00:00
|
|
|
self.add_column("Address", "people3", formula="People.all.name")
|
2020-07-27 18:57:36 +00:00
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "name", "nombre"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "name", "nombre"],
|
|
|
|
["ModifyColumn", "Address", "people", {"formula":
|
|
|
|
"People.lookupOne(addr=$id, city=$city).nombre"}],
|
|
|
|
["ModifyColumn", "Address", "people2", {"formula":
|
|
|
|
"People.lookupRecords(addr=$id).nombre"}],
|
2022-07-11 19:18:36 +00:00
|
|
|
["ModifyColumn", "Address", "people3", {"formula":
|
|
|
|
"People.all.nombre"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [22, 25, 26, 27], {
|
|
|
|
"colId": ["nombre", "people", "people2", "people3"],
|
2020-07-27 18:57:36 +00:00
|
|
|
"formula": ["",
|
|
|
|
"People.lookupOne(addr=$id, city=$city).nombre",
|
2022-07-11 19:18:36 +00:00
|
|
|
"People.lookupRecords(addr=$id).nombre",
|
|
|
|
"People.all.nombre"]
|
2020-11-02 15:48:47 +00:00
|
|
|
}],
|
2020-07-27 18:57:36 +00:00
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_lookup_ref_attr(self):
|
|
|
|
# Renaming `Table.lookupOne(...).foo.COLUMN` and for `.lookupRecords`
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "people", formula="People.lookupOne(addr=$id, city=$city).addr.city")
|
|
|
|
self.add_column("Address", "people2", formula="People.lookupRecords(addr=$id).addr.city")
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
|
|
|
["ModifyColumn", "Address", "people", {"formula":
|
|
|
|
"People.lookupOne(addr=$id, city=$ciudad).addr.ciudad"}],
|
|
|
|
["ModifyColumn", "Address", "people2", {"formula":
|
|
|
|
"People.lookupRecords(addr=$id).addr.ciudad"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 25, 26], {
|
|
|
|
"colId": ["ciudad", "city", "people", "people2"],
|
|
|
|
"formula": ["", "$addr.ciudad",
|
|
|
|
"People.lookupOne(addr=$id, city=$ciudad).addr.ciudad",
|
|
|
|
"People.lookupRecords(addr=$id).addr.ciudad"]
|
|
|
|
}]
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_lookup_iter_attr(self):
|
|
|
|
# Renaming `[x.COLUMN for x in Table.lookupRecords(...)]`.
|
2022-07-21 16:02:01 +00:00
|
|
|
self.check_comprehension_rename("People.lookupRecords(addr=$id)",
|
|
|
|
"People.lookupRecords(ADDRESS=$id)")
|
|
|
|
|
|
|
|
def test_rename_all_iter_attr(self):
|
|
|
|
# Renaming `[x.COLUMN for x in Table.all]`.
|
|
|
|
self.check_comprehension_rename("People.all", "People.all")
|
|
|
|
|
|
|
|
def check_comprehension_rename(self, iter_expr1, iter_expr2):
|
2020-07-27 18:57:36 +00:00
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "people",
|
2022-07-21 16:02:01 +00:00
|
|
|
formula="','.join(x.addr.city for x in %s)" % iter_expr1)
|
2020-07-27 18:57:36 +00:00
|
|
|
self.add_column("Address", "people2",
|
2022-07-21 16:02:01 +00:00
|
|
|
formula="','.join([x.addr.city for x in %s])" % iter_expr1)
|
2020-07-27 18:57:36 +00:00
|
|
|
self.add_column("Address", "people3",
|
2022-07-21 16:02:01 +00:00
|
|
|
formula="','.join({x.addr.city for x in %s})" % iter_expr1)
|
2020-07-27 18:57:36 +00:00
|
|
|
self.add_column("Address", "people4",
|
2022-07-21 16:02:01 +00:00
|
|
|
formula="{x.addr.city:x.addr for x in %s}" % iter_expr1)
|
2020-07-27 18:57:36 +00:00
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "addr", "ADDRESS"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "addr", "ADDRESS"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$ADDRESS.city"}],
|
|
|
|
["ModifyColumn", "Address", "people",
|
2022-07-21 16:02:01 +00:00
|
|
|
{"formula": "','.join(x.ADDRESS.city for x in %s)" % iter_expr2}],
|
2020-07-27 18:57:36 +00:00
|
|
|
["ModifyColumn", "Address", "people2",
|
2022-07-21 16:02:01 +00:00
|
|
|
{"formula": "','.join([x.ADDRESS.city for x in %s])" % iter_expr2}],
|
2020-07-27 18:57:36 +00:00
|
|
|
["ModifyColumn", "Address", "people3",
|
2022-07-21 16:02:01 +00:00
|
|
|
{"formula": "','.join({x.ADDRESS.city for x in %s})" % iter_expr2}],
|
2020-07-27 18:57:36 +00:00
|
|
|
["ModifyColumn", "Address", "people4",
|
2022-07-21 16:02:01 +00:00
|
|
|
{"formula": "{x.ADDRESS.city:x.ADDRESS for x in %s}" % iter_expr2}],
|
2020-07-27 18:57:36 +00:00
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [23, 24, 25, 26, 27, 28], {
|
|
|
|
"colId": ["ADDRESS", "city", "people", "people2", "people3", "people4"],
|
|
|
|
"formula": ["", "$ADDRESS.city",
|
2022-07-21 16:02:01 +00:00
|
|
|
"','.join(x.ADDRESS.city for x in %s)" % iter_expr2,
|
|
|
|
"','.join([x.ADDRESS.city for x in %s])" % iter_expr2,
|
|
|
|
"','.join({x.ADDRESS.city for x in %s})" % iter_expr2,
|
|
|
|
"{x.ADDRESS.city:x.ADDRESS for x in %s}" % iter_expr2],
|
2020-07-27 18:57:36 +00:00
|
|
|
}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_table(self):
|
|
|
|
# Renaming TABLE.lookupFunc(...) where TABLE is a user-defined table.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "people", formula="People.lookupRecords(addr=$id)")
|
|
|
|
self.add_column("Address", "people2", type="Ref:People", formula="People.lookupOne(addr=$id)")
|
|
|
|
out_actions = self.apply_user_action(["RenameTable", "People", "Persons"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["ModifyColumn", "Address", "people2", {"type": "Int"}],
|
|
|
|
["RenameTable", "People", "Persons"],
|
|
|
|
["UpdateRecord", "_grist_Tables", 2, {"tableId": "Persons"}],
|
|
|
|
["ModifyColumn", "Address", "people2", {
|
|
|
|
"type": "Ref:Persons", "formula": "Persons.lookupOne(addr=$id)" }],
|
|
|
|
["ModifyColumn", "Address", "people", {"formula": "Persons.lookupRecords(addr=$id)"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [26, 25], {
|
|
|
|
"type": ["Ref:Persons", "Any"],
|
|
|
|
"formula": ["Persons.lookupOne(addr=$id)", "Persons.lookupRecords(addr=$id)"]
|
|
|
|
}],
|
2021-08-20 20:35:41 +00:00
|
|
|
["BulkUpdateRecord", "Address", [11, 12, 13, 14], {
|
|
|
|
"people": [["r", "Persons", [4]],
|
|
|
|
["r", "Persons", [1, 3]],
|
|
|
|
["r", "Persons", [2]],
|
|
|
|
["r", "Persons", []]]
|
|
|
|
}],
|
2020-07-27 18:57:36 +00:00
|
|
|
]})
|
|
|
|
|
|
|
|
def test_rename_table_autocomplete(self):
|
2021-07-15 00:45:53 +00:00
|
|
|
user = {
|
|
|
|
'Name': 'Foo',
|
|
|
|
'UserID': 1,
|
2022-10-03 14:45:44 +00:00
|
|
|
'UserRef': '1',
|
2021-07-15 00:45:53 +00:00
|
|
|
'LinkKey': {},
|
|
|
|
'Origin': None,
|
|
|
|
'Email': 'foo@example.com',
|
(core) add a `user.SessionID` value for trigger formulas and granular access rules
Summary:
This makes a `user.SessionID` value available in information about the user, for use with trigger formulas and granular access rules. The ID should be constant within a browser session for anonymous user. For logged in users it simply reflects their user id.
This ID makes it possible to write access rules and trigger formulas that allow different anonymous users to create, view, and edit their own records in a document.
For example, you could have a brain-storming document for puns, and allow anyone to add to it (without logging in), letting people edit their own records, but not showing the records to others until they are approved by a moderator. Without something like this, we could only let anonymous people add one field of a record, and not have a secure way to let them edit that field or others in the same record.
Also adds a `user.IsLoggedIn` flag in passing.
Test Plan: Added a test, updated tests. The test added is a mini-moderation doc, don't use it for real because it allows users to edit their entries after a moderator has approved them.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Subscribers: dsagal
Differential Revision: https://phab.getgrist.com/D3273
2022-02-22 15:42:06 +00:00
|
|
|
'Access': 'owners',
|
|
|
|
'SessionID': 'u1',
|
|
|
|
'IsLoggedIn': True
|
2021-07-15 00:45:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 18:57:36 +00:00
|
|
|
# Renaming a table should not leave the old name available for auto-complete.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
names = {"People", "Persons"}
|
(core) Show example values in formula autocomplete
Summary:
This diff adds a preview of the value of certain autocomplete suggestions, especially of the form `$foo.bar` or `user.email`. The main initial motivation was to show the difference between `$Ref` and `$Ref.DisplayCol`, but the feature is more general.
The client now sends the row ID of the row being edited (along with the table and column IDs which were already sent) to the server to fetch autocomplete suggestions. The returned suggestions are now tuples `(suggestion, example_value)` where `example_value` is a string or null. The example value is simply obtained by evaluating (in a controlled way) the suggestion in the context of the given record and the current user. The string representation is similar to the standard `repr` but dates and datetimes are formatted, and the whole thing is truncated for efficiency.
The example values are shown in the autocomplete popup separated from the actual suggestion by a number of spaces calculated to:
1. Clearly separate the suggestion from the values
2. Left-align the example values in most cases
3. Avoid having so much space such that connecting suggestions and values becomes visually difficult.
The tokenization of the row is then tweaked to show the example in light grey to deemphasise it.
Main discussion where the above was decided: https://grist.slack.com/archives/CDHABLZJT/p1661795588100009
The diff also includes various other small improvements and fixes:
- The autocomplete popup is much wider to make room for long suggestions, particularly lookups, as pointed out in https://phab.getgrist.com/D3580#inline-41007. The wide popup is the reason a fancy solution was needed to position the example values. I didn't see a way to dynamically resize the popup based on suggestions, and it didn't seem like a good idea to try.
- The `grist` and `python` labels previously shown on the right are removed. They were not helpful (https://grist.slack.com/archives/CDHABLZJT/p1659697086155179) and would get in the way of the example values.
- Fixed a bug in our custom tokenization that caused function arguments to be weirdly truncated in the middle: https://grist.slack.com/archives/CDHABLZJT/p1661956353699169?thread_ts=1661953258.342739&cid=CDHABLZJT and https://grist.slack.com/archives/C069RUP71/p1659696778991339
- Hide suggestions involving helper columns like `$gristHelper_Display` or `Table.lookupRecords(gristHelper_Display=` (https://grist.slack.com/archives/CDHABLZJT/p1661953258342739). The former has been around for a while and seems to be a mistake. The fix is simply to use `is_visible_column` instead of `is_user_column`. Since the latter is not used anywhere else, and using it in the first place seems like a mistake more than anything else, I've also removed the function to prevent similar mistakes in the future.
- Don't suggest private columns as lookup arguments: https://grist.slack.com/archives/CDHABLZJT/p1662133416652499?thread_ts=1661795588.100009&cid=CDHABLZJT
- Only fetch fresh suggestions specifically after typing `lookupRecords(` or `lookupOne(` rather than just `(`, as this would needlessly hide function suggestions which could still be useful to see the arguments. However this only makes a difference when there are still multiple matching suggestions, otherwise Ace hides them anyway.
Test Plan: Extended and updated several Python and browser tests.
Reviewers: paulfitz
Reviewed By: paulfitz
Differential Revision: https://phab.getgrist.com/D3611
2022-09-28 14:47:55 +00:00
|
|
|
autocomplete = self.engine.autocomplete("Pe", "Address", "city", 1, user)
|
|
|
|
suggestions = {suggestion for suggestion, value in autocomplete}
|
2021-07-07 16:03:01 +00:00
|
|
|
self.assertEqual(
|
(core) Show example values in formula autocomplete
Summary:
This diff adds a preview of the value of certain autocomplete suggestions, especially of the form `$foo.bar` or `user.email`. The main initial motivation was to show the difference between `$Ref` and `$Ref.DisplayCol`, but the feature is more general.
The client now sends the row ID of the row being edited (along with the table and column IDs which were already sent) to the server to fetch autocomplete suggestions. The returned suggestions are now tuples `(suggestion, example_value)` where `example_value` is a string or null. The example value is simply obtained by evaluating (in a controlled way) the suggestion in the context of the given record and the current user. The string representation is similar to the standard `repr` but dates and datetimes are formatted, and the whole thing is truncated for efficiency.
The example values are shown in the autocomplete popup separated from the actual suggestion by a number of spaces calculated to:
1. Clearly separate the suggestion from the values
2. Left-align the example values in most cases
3. Avoid having so much space such that connecting suggestions and values becomes visually difficult.
The tokenization of the row is then tweaked to show the example in light grey to deemphasise it.
Main discussion where the above was decided: https://grist.slack.com/archives/CDHABLZJT/p1661795588100009
The diff also includes various other small improvements and fixes:
- The autocomplete popup is much wider to make room for long suggestions, particularly lookups, as pointed out in https://phab.getgrist.com/D3580#inline-41007. The wide popup is the reason a fancy solution was needed to position the example values. I didn't see a way to dynamically resize the popup based on suggestions, and it didn't seem like a good idea to try.
- The `grist` and `python` labels previously shown on the right are removed. They were not helpful (https://grist.slack.com/archives/CDHABLZJT/p1659697086155179) and would get in the way of the example values.
- Fixed a bug in our custom tokenization that caused function arguments to be weirdly truncated in the middle: https://grist.slack.com/archives/CDHABLZJT/p1661956353699169?thread_ts=1661953258.342739&cid=CDHABLZJT and https://grist.slack.com/archives/C069RUP71/p1659696778991339
- Hide suggestions involving helper columns like `$gristHelper_Display` or `Table.lookupRecords(gristHelper_Display=` (https://grist.slack.com/archives/CDHABLZJT/p1661953258342739). The former has been around for a while and seems to be a mistake. The fix is simply to use `is_visible_column` instead of `is_user_column`. Since the latter is not used anywhere else, and using it in the first place seems like a mistake more than anything else, I've also removed the function to prevent similar mistakes in the future.
- Don't suggest private columns as lookup arguments: https://grist.slack.com/archives/CDHABLZJT/p1662133416652499?thread_ts=1661795588.100009&cid=CDHABLZJT
- Only fetch fresh suggestions specifically after typing `lookupRecords(` or `lookupOne(` rather than just `(`, as this would needlessly hide function suggestions which could still be useful to see the arguments. However this only makes a difference when there are still multiple matching suggestions, otherwise Ace hides them anyway.
Test Plan: Extended and updated several Python and browser tests.
Reviewers: paulfitz
Reviewed By: paulfitz
Differential Revision: https://phab.getgrist.com/D3611
2022-09-28 14:47:55 +00:00
|
|
|
names.intersection(suggestions),
|
2021-07-07 16:03:01 +00:00
|
|
|
{"People"}
|
|
|
|
)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# Rename the table and ensure that "People" is no longer present among top-level names.
|
(core) Show example values in formula autocomplete
Summary:
This diff adds a preview of the value of certain autocomplete suggestions, especially of the form `$foo.bar` or `user.email`. The main initial motivation was to show the difference between `$Ref` and `$Ref.DisplayCol`, but the feature is more general.
The client now sends the row ID of the row being edited (along with the table and column IDs which were already sent) to the server to fetch autocomplete suggestions. The returned suggestions are now tuples `(suggestion, example_value)` where `example_value` is a string or null. The example value is simply obtained by evaluating (in a controlled way) the suggestion in the context of the given record and the current user. The string representation is similar to the standard `repr` but dates and datetimes are formatted, and the whole thing is truncated for efficiency.
The example values are shown in the autocomplete popup separated from the actual suggestion by a number of spaces calculated to:
1. Clearly separate the suggestion from the values
2. Left-align the example values in most cases
3. Avoid having so much space such that connecting suggestions and values becomes visually difficult.
The tokenization of the row is then tweaked to show the example in light grey to deemphasise it.
Main discussion where the above was decided: https://grist.slack.com/archives/CDHABLZJT/p1661795588100009
The diff also includes various other small improvements and fixes:
- The autocomplete popup is much wider to make room for long suggestions, particularly lookups, as pointed out in https://phab.getgrist.com/D3580#inline-41007. The wide popup is the reason a fancy solution was needed to position the example values. I didn't see a way to dynamically resize the popup based on suggestions, and it didn't seem like a good idea to try.
- The `grist` and `python` labels previously shown on the right are removed. They were not helpful (https://grist.slack.com/archives/CDHABLZJT/p1659697086155179) and would get in the way of the example values.
- Fixed a bug in our custom tokenization that caused function arguments to be weirdly truncated in the middle: https://grist.slack.com/archives/CDHABLZJT/p1661956353699169?thread_ts=1661953258.342739&cid=CDHABLZJT and https://grist.slack.com/archives/C069RUP71/p1659696778991339
- Hide suggestions involving helper columns like `$gristHelper_Display` or `Table.lookupRecords(gristHelper_Display=` (https://grist.slack.com/archives/CDHABLZJT/p1661953258342739). The former has been around for a while and seems to be a mistake. The fix is simply to use `is_visible_column` instead of `is_user_column`. Since the latter is not used anywhere else, and using it in the first place seems like a mistake more than anything else, I've also removed the function to prevent similar mistakes in the future.
- Don't suggest private columns as lookup arguments: https://grist.slack.com/archives/CDHABLZJT/p1662133416652499?thread_ts=1661795588.100009&cid=CDHABLZJT
- Only fetch fresh suggestions specifically after typing `lookupRecords(` or `lookupOne(` rather than just `(`, as this would needlessly hide function suggestions which could still be useful to see the arguments. However this only makes a difference when there are still multiple matching suggestions, otherwise Ace hides them anyway.
Test Plan: Extended and updated several Python and browser tests.
Reviewers: paulfitz
Reviewed By: paulfitz
Differential Revision: https://phab.getgrist.com/D3611
2022-09-28 14:47:55 +00:00
|
|
|
self.apply_user_action(["RenameTable", "People", "Persons"])
|
|
|
|
autocomplete = self.engine.autocomplete("Pe", "Address", "city", 1, user)
|
|
|
|
suggestions = {suggestion for suggestion, value in autocomplete}
|
2021-07-07 16:03:01 +00:00
|
|
|
self.assertEqual(
|
(core) Show example values in formula autocomplete
Summary:
This diff adds a preview of the value of certain autocomplete suggestions, especially of the form `$foo.bar` or `user.email`. The main initial motivation was to show the difference between `$Ref` and `$Ref.DisplayCol`, but the feature is more general.
The client now sends the row ID of the row being edited (along with the table and column IDs which were already sent) to the server to fetch autocomplete suggestions. The returned suggestions are now tuples `(suggestion, example_value)` where `example_value` is a string or null. The example value is simply obtained by evaluating (in a controlled way) the suggestion in the context of the given record and the current user. The string representation is similar to the standard `repr` but dates and datetimes are formatted, and the whole thing is truncated for efficiency.
The example values are shown in the autocomplete popup separated from the actual suggestion by a number of spaces calculated to:
1. Clearly separate the suggestion from the values
2. Left-align the example values in most cases
3. Avoid having so much space such that connecting suggestions and values becomes visually difficult.
The tokenization of the row is then tweaked to show the example in light grey to deemphasise it.
Main discussion where the above was decided: https://grist.slack.com/archives/CDHABLZJT/p1661795588100009
The diff also includes various other small improvements and fixes:
- The autocomplete popup is much wider to make room for long suggestions, particularly lookups, as pointed out in https://phab.getgrist.com/D3580#inline-41007. The wide popup is the reason a fancy solution was needed to position the example values. I didn't see a way to dynamically resize the popup based on suggestions, and it didn't seem like a good idea to try.
- The `grist` and `python` labels previously shown on the right are removed. They were not helpful (https://grist.slack.com/archives/CDHABLZJT/p1659697086155179) and would get in the way of the example values.
- Fixed a bug in our custom tokenization that caused function arguments to be weirdly truncated in the middle: https://grist.slack.com/archives/CDHABLZJT/p1661956353699169?thread_ts=1661953258.342739&cid=CDHABLZJT and https://grist.slack.com/archives/C069RUP71/p1659696778991339
- Hide suggestions involving helper columns like `$gristHelper_Display` or `Table.lookupRecords(gristHelper_Display=` (https://grist.slack.com/archives/CDHABLZJT/p1661953258342739). The former has been around for a while and seems to be a mistake. The fix is simply to use `is_visible_column` instead of `is_user_column`. Since the latter is not used anywhere else, and using it in the first place seems like a mistake more than anything else, I've also removed the function to prevent similar mistakes in the future.
- Don't suggest private columns as lookup arguments: https://grist.slack.com/archives/CDHABLZJT/p1662133416652499?thread_ts=1661795588.100009&cid=CDHABLZJT
- Only fetch fresh suggestions specifically after typing `lookupRecords(` or `lookupOne(` rather than just `(`, as this would needlessly hide function suggestions which could still be useful to see the arguments. However this only makes a difference when there are still multiple matching suggestions, otherwise Ace hides them anyway.
Test Plan: Extended and updated several Python and browser tests.
Reviewers: paulfitz
Reviewed By: paulfitz
Differential Revision: https://phab.getgrist.com/D3611
2022-09-28 14:47:55 +00:00
|
|
|
names.intersection(suggestions),
|
2021-07-07 16:03:01 +00:00
|
|
|
{"Persons"}
|
|
|
|
)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
def test_rename_to_id(self):
|
|
|
|
# Check that we renaming a column to "Id" disambiguates it with a suffix.
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "name", "Id"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "name", "Id2"],
|
|
|
|
["UpdateRecord", "_grist_Tables_column", 22, {"colId": "Id2"}],
|
|
|
|
]})
|
|
|
|
|
|
|
|
def test_renames_with_non_ascii(self):
|
|
|
|
# Test that presence of unicode does not interfere with formula adjustments for renaming.
|
|
|
|
self.load_sample(self.sample)
|
2021-06-24 12:23:33 +00:00
|
|
|
self.add_column("Address", "CityUpper", formula=u"'Øî'+$city.upper()+'áü'")
|
2020-07-27 18:57:36 +00:00
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "Address", "city", "ciudad"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "Address", "city", "ciudad"],
|
|
|
|
["ModifyColumn", "People", "city", {"formula": "$addr.ciudad"}],
|
2021-06-24 12:23:33 +00:00
|
|
|
["ModifyColumn", "Address", "CityUpper", {"formula": u"'Øî'+$ciudad.upper()+'áü'"}],
|
2020-07-27 18:57:36 +00:00
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [21, 24, 25], {
|
|
|
|
"colId": ["ciudad", "city", "CityUpper"],
|
2021-06-24 12:23:33 +00:00
|
|
|
"formula": ["", "$addr.ciudad", u"'Øî'+$ciudad.upper()+'áü'"],
|
2020-07-27 18:57:36 +00:00
|
|
|
}]
|
|
|
|
]})
|
|
|
|
self.assertTableData("Address", cols="all", data=[
|
|
|
|
["id", "ciudad", "CityUpper"],
|
2021-06-24 12:23:33 +00:00
|
|
|
[11, "New York", u"ØîNEW YORKáü"],
|
|
|
|
[12, "Colombia", u"ØîCOLOMBIAáü"],
|
|
|
|
[13, "New Haven", u"ØîNEW HAVENáü"],
|
|
|
|
[14, "West Haven", u"ØîWEST HAVENáü"],
|
2020-07-27 18:57:36 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_rename_updates_properties(self):
|
|
|
|
# This tests for the following bug: a column A of type Any with formula Table1.lookupOne(B=$B)
|
|
|
|
# will return a correct reference; when column Table1.X is renamed to Y, $A.X will be changed
|
|
|
|
# to $A.Y correctly. The bug was that the fixed $A.Y formula would fail incorrectly with
|
|
|
|
# "Table1 has no column 'Y'".
|
|
|
|
#
|
|
|
|
# The cause was that Record objects created by $A were not affected by the
|
|
|
|
# rename, or recomputed after it, and contained a stale list of allowed column names (the fix
|
|
|
|
# removes reliance on storing column names in the Record class).
|
|
|
|
|
|
|
|
self.load_sample(self.sample)
|
|
|
|
self.add_column("Address", "person", formula="People.lookupOne(addr=$id)")
|
|
|
|
self.add_column("Address", "name", formula="$person.name")
|
|
|
|
from datetime import date
|
|
|
|
# A helper for comparing Record objects below.
|
|
|
|
people_table = self.engine.tables['People']
|
2021-07-16 18:15:04 +00:00
|
|
|
people_rec = lambda row_id: people_table.Record(row_id, None)
|
2020-07-27 18:57:36 +00:00
|
|
|
|
|
|
|
# Verify the data and calculations are correct.
|
|
|
|
self.assertTableData("Address", cols="all", data=[
|
|
|
|
["id", "city", "person", "name"],
|
|
|
|
[11, "New York", people_rec(4), "Sam"],
|
|
|
|
[12, "Colombia", people_rec(1), "Bob"],
|
|
|
|
[13, "New Haven", people_rec(2), "Alice"],
|
|
|
|
[14, "West Haven", people_rec(0), ""],
|
|
|
|
])
|
|
|
|
|
|
|
|
# Do the rename.
|
|
|
|
out_actions = self.apply_user_action(["RenameColumn", "People", "name", "name2"])
|
|
|
|
self.assertPartialOutActions(out_actions, { "stored": [
|
|
|
|
["RenameColumn", "People", "name", "name2"],
|
|
|
|
["ModifyColumn", "Address", "name", {"formula": "$person.name2"}],
|
|
|
|
["BulkUpdateRecord", "_grist_Tables_column", [22, 26], {
|
|
|
|
"colId": ["name2", "name"],
|
|
|
|
"formula": ["", "$person.name2"],
|
|
|
|
}]
|
|
|
|
]})
|
|
|
|
|
|
|
|
# Verify the data and calculations are correct after the rename.
|
|
|
|
self.assertTableData("Address", cols="all", data=[
|
|
|
|
["id", "city", "person", "name"],
|
|
|
|
[11, "New York", people_rec(4), "Sam"],
|
|
|
|
[12, "Colombia", people_rec(1), "Bob"],
|
|
|
|
[13, "New Haven", people_rec(2), "Alice"],
|
|
|
|
[14, "West Haven", people_rec(0), ""],
|
|
|
|
])
|