Skip to content

Commit

Permalink
Improve the test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyLeslie committed Jun 15, 2024
1 parent 270c7a9 commit b9ea716
Showing 1 changed file with 68 additions and 26 deletions.
94 changes: 68 additions & 26 deletions sandbox/grist/test_dropdown_condition_renames.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,114 @@ def setUp(self):
self.load_sample(testsamples.sample_students)

self.engine.apply_user_actions([useractions.from_repr(ua) for ua in (
# Add some irrelevant columns to the table Schools. These should never be renamed.
["AddColumn", "Schools", "name2", {
"type": "Text"
}],
["AddColumn", "Schools", "choice", {
"type": "Ref:Address"
}],
["AddColumn", "Address", "rec", {
"type": "Text"
}],
# Add a dropdown condition formula to Schools.address (column #12).
["ModifyColumn", "Schools", "address", {
"widgetOptions": json.dumps({
"dropdownCondition": {
"text": "'New' in choice.city and $name == rec.name",
"text": "'New' in choice.city and $name == rec.name + rec.choice.city or choice.rec.city != $name2",
}
}),
}],
# Create a similar column with an invalid dropdown condition formula.
# This formula should never be touched.
# This column will have the ID 25.
["AddColumn", "Schools", "address2", {
"type": "Ref:Address",
"widgetOptions": json.dumps({
"dropdownCondition": {
"text": "+ 'New' in choice.city and $name == rec.name",
}
}),
}],
)])

# This is what we"ll have at the beginning, for later tests to refer to.
# This is what we'll have at the beginning, for later tests to refer to.
# Table Schools is 2.
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "type", "widgetOptions"],
[12, "Ref:Address", json.dumps({
["id", "parentId", "colId", "widgetOptions"],
[12, 2, "address", json.dumps({
"dropdownCondition": {
"text": "'New' in choice.city and $name == rec.name",
"text": "'New' in choice.city and $name == rec.name + rec.choice.city or choice.rec.city != $name2",
# The ModifyColumn user action should trigger an auto parse.
# "parsed" is stored as dumped JSON, so we need to explicitly dump it here as well.
"parsed": json.dumps(["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "city"]],
["Eq", ["Attr", ["Name", "rec"], "name"], ["Attr", ["Name", "rec"], "name"]]])
"parsed": json.dumps(["Or", ["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "city"]], ["Eq", ["Attr", ["Name", "rec"], "name"], ["Add", ["Attr", ["Name", "rec"], "name"], ["Attr", ["Attr", ["Name", "rec"], "choice"], "city"]]]], ["NotEq", ["Attr", ["Attr", ["Name", "choice"], "rec"], "city"], ["Attr", ["Name", "rec"], "name2"]]])
}
})],
])
self.assert_invalid_formula_untouched()

def assert_invalid_formula_untouched(self):
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "parentId", "colId", "widgetOptions"],
[25, 2, "address2", json.dumps({
"dropdownCondition": {
"text": "+ 'New' in choice.city and $name == rec.name",
}
})]
])

def apply_user_action_ignore_syntax_error(self, user_action_repr):
try:
self.apply_user_action(user_action_repr)
except ValueError as e:
if str(e) == "Unsupported syntax at 1:1":
pass
else:
raise e
raise e

def test_referred_column_renames(self):
# Rename the column "city" in table "Address" to "area". Schools.address refers to it.
self.apply_user_action(["RenameColumn", "Address", "city", "area"])
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Address", "city", "area"])
# Now choice.city should become choice.area. This should also be reflected in the parsed formula.
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "type", "widgetOptions"],
[12, "Ref:Address", json.dumps({
["id", "parentId", "colId", "widgetOptions"],
[12, 2, "address", json.dumps({
"dropdownCondition": {
"text": "'New' in choice.area and $name == rec.name",
"parsed": json.dumps(["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "area"]],
["Eq", ["Attr", ["Name", "rec"], "name"], ["Attr", ["Name", "rec"], "name"]]])
"text": "'New' in choice.area and $name == rec.name + rec.choice.city or choice.rec.city != $name2",
"parsed": json.dumps(["Or", ["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "area"]], ["Eq", ["Attr", ["Name", "rec"], "name"], ["Add", ["Attr", ["Name", "rec"], "name"], ["Attr", ["Attr", ["Name", "rec"], "choice"], "city"]]]], ["NotEq", ["Attr", ["Attr", ["Name", "choice"], "rec"], "city"], ["Attr", ["Name", "rec"], "name2"]]])
}
})],
])
self.assert_invalid_formula_untouched()

def test_record_column_renames(self):
# Rename the column "name" in table "Schools" to "identifier". Schools.address refers to it in two ways -
# the dollar sign and "rec.".
self.apply_user_action(["RenameColumn", "Schools", "name", "identifier"])
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Schools", "name", "identifier"])
# Now "$name" should become "$identifier" while "rec.name" should become "rec.identifier".
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "type", "widgetOptions"],
[12, "Ref:Address", json.dumps({
["id", "parentId", "colId", "widgetOptions"],
[12, 2, "address", json.dumps({
"dropdownCondition": {
"text": "'New' in choice.city and $identifier == rec.identifier",
"parsed": json.dumps(["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "city"]],
["Eq", ["Attr", ["Name", "rec"], "identifier"], ["Attr", ["Name", "rec"], "identifier"]]])
"text": "'New' in choice.city and $identifier == rec.identifier + rec.choice.city or choice.rec.city != $name2",
"parsed": json.dumps(["Or", ["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "city"]], ["Eq", ["Attr", ["Name", "rec"], "identifier"], ["Add", ["Attr", ["Name", "rec"], "identifier"], ["Attr", ["Attr", ["Name", "rec"], "choice"], "city"]]]], ["NotEq", ["Attr", ["Attr", ["Name", "choice"], "rec"], "city"], ["Attr", ["Name", "rec"], "name2"]]])
}
})],
])
self.assert_invalid_formula_untouched()

def test_multiple_renames(self):
# Put all renames together.
self.apply_user_action(["RenameColumn", "Address", "city", "area"])
self.apply_user_action(["RenameColumn", "Schools", "name", "identifier"])
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Address", "city", "area"])
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Schools", "name", "identifier"])
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "type", "widgetOptions"],
[12, "Ref:Address", json.dumps({
["id", "parentId", "colId", "widgetOptions"],
[12, 2, "address", json.dumps({
"dropdownCondition": {
"text": "'New' in choice.area and $identifier == rec.identifier",
"parsed": json.dumps(["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "area"]],
["Eq", ["Attr", ["Name", "rec"], "identifier"], ["Attr", ["Name", "rec"], "identifier"]]])
"text": "'New' in choice.area and $identifier == rec.identifier + rec.choice.city or choice.rec.city != $name2",
"parsed": json.dumps(["Or", ["And", ["In", ["Const", "New"], ["Attr", ["Name", "choice"], "area"]], ["Eq", ["Attr", ["Name", "rec"], "identifier"], ["Add", ["Attr", ["Name", "rec"], "identifier"], ["Attr", ["Attr", ["Name", "rec"], "choice"], "city"]]]], ["NotEq", ["Attr", ["Attr", ["Name", "choice"], "rec"], "city"], ["Attr", ["Name", "rec"], "name2"]]])
}
})],
])
self.assert_invalid_formula_untouched()

0 comments on commit b9ea716

Please sign in to comment.