mirror of
https://github.com/chaisql/chai.git
synced 2025-10-05 23:57:01 +08:00

* Add packaged functions support * Add tests for math.floor func * Export FunctionsTable * Extract func stuff into its own package * Rename stuff * Fix tests * Move doc package to cmd/genji * Adjust naming, typos
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package functions_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/genjidb/genji/document"
|
|
"github.com/genjidb/genji/internal/environment"
|
|
"github.com/genjidb/genji/internal/testutil"
|
|
)
|
|
|
|
var doc document.Document = func() document.Document {
|
|
return document.NewFromJSON([]byte(`{
|
|
"a": 1,
|
|
"b": {"foo bar": [1, 2]},
|
|
"c": [1, {"foo": "bar"}, [1, 2]]
|
|
}`))
|
|
}()
|
|
|
|
var docWithKey document.Document = func() document.Document {
|
|
fb := document.NewFieldBuffer()
|
|
err := fb.Copy(doc)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fb.DecodedKey = document.NewIntegerValue(1)
|
|
fb.EncodedKey, err = fb.DecodedKey.MarshalBinary()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return fb
|
|
}()
|
|
|
|
var envWithDoc = environment.New(doc)
|
|
var envWithDocAndKey = environment.New(docWithKey)
|
|
|
|
func TestPk(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
env *environment.Environment
|
|
res document.Value
|
|
}{
|
|
{"empty env", &environment.Environment{}, document.NewNullValue()},
|
|
{"env with doc", envWithDoc, document.NewNullValue()},
|
|
{"env with doc and key", envWithDocAndKey, document.NewIntegerValue(1)},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
testutil.TestExpr(t, "pk()", test.env, test.res, false)
|
|
})
|
|
}
|
|
}
|