Files
chaisql/internal/expr/functions/builtins_test.go
Asdine El Hrychy f966172cee Introduce Value interface (#422)
This replaces the Value struct by an interface to allow us to override some
values behavior in the future.
It also introduces a new package types, which contains type definitions, comparison,
and arithmetics.
Concerning encoding, Genji now only uses on type of encoding for values. This simplifies
indexing logic as well as table access in general.
2021-07-21 22:05:44 +04:00

59 lines
1.3 KiB
Go

package functions_test
import (
"bytes"
"testing"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/environment"
"github.com/genjidb/genji/internal/testutil"
"github.com/genjidb/genji/types"
)
var doc types.Document = func() types.Document {
return document.NewFromJSON([]byte(`{
"a": 1,
"b": {"foo bar": [1, 2]},
"c": [1, {"foo": "bar"}, [1, 2]]
}`))
}()
var docWithKey types.Document = func() types.Document {
fb := document.NewFieldBuffer()
err := fb.Copy(doc)
if err != nil {
panic(err)
}
fb.DecodedKey = types.NewIntegerValue(1)
var buf bytes.Buffer
err = types.NewValueEncoder(&buf).Encode(fb.DecodedKey)
if err != nil {
panic(err)
}
fb.EncodedKey = buf.Bytes()
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 types.Value
}{
{"empty env", &environment.Environment{}, types.NewNullValue()},
{"env with doc", envWithDoc, types.NewNullValue()},
{"env with doc and key", envWithDocAndKey, types.NewIntegerValue(1)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testutil.TestExpr(t, "pk()", test.env, test.res, false)
})
}
}