Files
Docwell/Server/internal/convert/formula_test.go
T
tajniak81andClaude Opus 5.5 8814293988
CI / frontend (push) Successful in 14m34s
CI / desktop (ubuntu-latest) (push) Successful in 1h16m25s
CI / docker (push) Successful in 31m59s
CI / mobile (push) Failing after 4m3s
CI / go (Media) (push) Successful in 14m9s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / go (Server) (push) Failing after 20m28s
CI / desktop (windows-latest) (push) Canceled after 0s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / go (Web-App) (push) Canceled after 0s
Add formula properties to databases
A formula column keeps an expression and works it out for each record
when the database is read, after relations and rollups, like rollups;
nothing is stored per record. The language is small and hand-parsed:
prop("Name"), numbers, text, yes/no and days, arithmetic, comparisons,
and/or/not, if(), rounding, min/max, text functions and date arithmetic
(today, dateAdd, dateBetween, year/month/day). Errors, loops between
formulas included, stay in their cells with the reason as a tooltip.

The column's editor shows the first record's result or the error as
you type and inserts properties by name. Formulas sort, filter and chart
like numbers when they are numbers. The server evaluates the same
language for exports (convert/formula.go), with the same test cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:34:04 +01:00

107 lines
4.2 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 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)
}
}