Search records now carry each formula's result, worked out like the exports do (relations and rollups included); yes/no results are left out like checkboxes. The search index version goes to 5, so existing indexes are rebuilt once and older records get their formula text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
4.9 KiB
Go
126 lines
4.9 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package convert
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The same cases as formula.test.ts, so the two evaluators agree.
|
|
func TestFormulaLanguage(t *testing.T) {
|
|
props := map[string]fval{"Name": "Ship", "Points": 5.0, "Done": true, "Due": fvDay{"2026-11-30"}, "Jan": fvDay{"2026-01-31"}, "A": fvDay{"2026-03-14"}, "B": fvDay{"2026-01-15"}, "Empty": nil}
|
|
run := func(src string) (string, error) {
|
|
tree, err := parseFormula(src)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sc := &formulaScope{today: "2026-11-17", now: "2026-11-17 09:30", prop: func(n string) (fval, error) {
|
|
v, ok := props[n]
|
|
if !ok {
|
|
return nil, ferr("No property called %q", n)
|
|
}
|
|
return v, nil
|
|
}}
|
|
v, err := sc.eval(tree)
|
|
return formatFormula(v), err
|
|
}
|
|
for src, want := range map[string]string{
|
|
"1 + 2 * 3": "7",
|
|
"(1 + 2) * 3": "9",
|
|
"-2 * -3 + 10 % 4": "8",
|
|
"7 / 2": "3.5",
|
|
"1.5e2": "150",
|
|
"2 > 1 and 3 <= 3": "true",
|
|
"1 == 2 || !false": "true",
|
|
`not ("a" == "a")`: "false",
|
|
`"apple" < "banana"`: "true",
|
|
`prop("Name") + " (" + prop("Points") + ")"`: "Ship (5)",
|
|
`concat("a", 1, true)`: "a1true",
|
|
`upper(prop("Name"))`: "SHIP",
|
|
`length("żółw")`: "4",
|
|
`contains("Release notes", "NOTES")`: "true",
|
|
`if(prop("Done"), "yes", 1 / 0)`: "yes",
|
|
"round(2.345, 2)": "2.35",
|
|
"round(2.5)": "3",
|
|
"min(3, 1, 2) + max(4, 9)": "10",
|
|
"floor(-1.5) + ceil(1.2) + abs(-3)": "3",
|
|
`dateBetween(prop("Due"), today(), "days")`: "13",
|
|
`dateAdd(prop("Jan"), 1, "months")`: "2026-02-28",
|
|
`dateAdd(prop("Due"), -2, "weeks")`: "2026-11-16",
|
|
`dateBetween(prop("A"), prop("B"), "months")`: "1",
|
|
`year(prop("Due")) * 100 + month(prop("Due"))`: "202611",
|
|
`prop("Due") - today()`: "13",
|
|
`prop("Due") > today()`: "true",
|
|
`empty(prop("Empty"))`: "true",
|
|
`prop("Empty") == ""`: "true",
|
|
`true == "true"`: "false",
|
|
} {
|
|
got, err := run(src)
|
|
if err != nil || got != want {
|
|
t.Errorf("%s = %q, %v; want %q", src, got, err, want)
|
|
}
|
|
}
|
|
for src, want := range map[string]string{
|
|
"1 +": "ends too early",
|
|
`prop("A"`: `Expected ")"`,
|
|
"Points * 2": `write prop("Points")`,
|
|
"sum(1, 2)": `Unknown function "sum"`,
|
|
`"open`: "no closing quote",
|
|
"1 # 2": `Unexpected "#" at 3`,
|
|
"": "empty",
|
|
"if(false, 1, 1 / 0)": "Division by zero",
|
|
`"a" * 2`: "needs a number",
|
|
strings.Repeat("(", 200) + "1" + strings.Repeat(")", 200): "nested too deeply",
|
|
} {
|
|
if _, err := run(src); err == nil || !strings.Contains(err.Error(), want) {
|
|
t.Errorf("%q: error %v, want %q", src, err, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormulasInSearch(t *testing.T) {
|
|
recs, err := Records([]byte(`{"type":"doc","content":[{"type":"database","attrs":{"id":"d","data":{
|
|
"columns":[
|
|
{"id":"t","name":"Name","type":"title"},
|
|
{"id":"c","name":"Code","type":"text"},
|
|
{"id":"f","name":"Ref","type":"formula","formula":"concat(\"TASK-\", prop(\"Code\"))"},
|
|
{"id":"y","name":"Long","type":"formula","formula":"length(prop(\"Name\")) > 3"},
|
|
{"id":"e","name":"Broken","type":"formula","formula":"prop(\"Nope\")"}
|
|
],
|
|
"rows":[{"id":"r1","cells":{"t":"Write","c":"42"}}]
|
|
}}}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(recs) != 1 || recs[0].Title != "Write" || recs[0].Text != "42 TASK-42" {
|
|
t.Fatalf("records = %+v", recs)
|
|
}
|
|
}
|
|
|
|
func TestFormulasExport(t *testing.T) {
|
|
formulaNow = func() time.Time { return time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) }
|
|
defer func() { formulaNow = func() time.Time { return time.Now().UTC() } }()
|
|
doc, err := Parse([]byte(`{"type":"doc","content":[{"type":"database","attrs":{"id":"d","data":{
|
|
"columns":[
|
|
{"id":"t","name":"Name","type":"title"},
|
|
{"id":"p","name":"Points","type":"number"},
|
|
{"id":"d","name":"Due","type":"date"},
|
|
{"id":"f","name":"Double","type":"formula","formula":"prop(\"Points\") * 2"},
|
|
{"id":"g","name":"Left","type":"formula","formula":"concat(prop(\"Name\"), \": \", dateBetween(prop(\"Due\"), today(), \"days\"), \" days\")"},
|
|
{"id":"x","name":"Loop","type":"formula","formula":"prop(\"Loop\") + 1"},
|
|
{"id":"b","name":"Big","type":"formula","formula":"prop(\"Double\") > 5"}
|
|
],
|
|
"rows":[{"id":"r1","createdAt":1,"cells":{"t":"Write","p":3,"d":"2026-11-20"}}],
|
|
"views":[{"id":"v","type":"table","hidden":[]}]
|
|
}}}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
md := Markdown(doc, Options{})
|
|
if !strings.Contains(md, "| Write | 3 | 2026-11-20 | 6 | Write: 3 days | | ✓ |") {
|
|
t.Errorf("formulas not worked out:\n%s", md)
|
|
}
|
|
}
|