mirror of
https://github.com/chaisql/chai.git
synced 2025-10-06 16:18:14 +08:00

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.
59 lines
1.3 KiB
Go
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)
|
|
})
|
|
}
|
|
}
|