mirror of
https://github.com/chaisql/chai.git
synced 2025-10-04 23:32:52 +08:00

All new error handling code now rely on internal/errors package which provides a compilation time toggle that enables to capture stacktraces for easier debugging while developing. It also comes with a new testutil/assert package which replaces the require package when it comes to checking or comparing errors and printing the stack traces if needed. Finally, the test target of the Makefile uses the debug build tag by default. A testnodebug target is also provided for convenience and to make sure no tests are broken due to not having used the internal/errors or testutil/assert package. See #431 for more details
98 lines
1.9 KiB
Go
98 lines
1.9 KiB
Go
package expr_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/genjidb/genji/document"
|
|
"github.com/genjidb/genji/internal/environment"
|
|
"github.com/genjidb/genji/internal/sql/parser"
|
|
"github.com/genjidb/genji/internal/testutil/assert"
|
|
"github.com/genjidb/genji/types"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
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)
|
|
|
|
var nullLiteral = types.NewNullValue()
|
|
|
|
func testExpr(t testing.TB, exprStr string, env *environment.Environment, want types.Value, fails bool) {
|
|
t.Helper()
|
|
|
|
e, err := parser.NewParser(strings.NewReader(exprStr)).ParseExpr()
|
|
assert.NoError(t, err)
|
|
res, err := e.Eval(env)
|
|
if fails {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
require.Equal(t, want, res)
|
|
}
|
|
}
|
|
|
|
func TestString(t *testing.T) {
|
|
var operands = []string{
|
|
`10.4`,
|
|
"true",
|
|
"500",
|
|
`foo.bar[1]`,
|
|
`'hello'`,
|
|
`[1, 2, 'foo']`,
|
|
`{a: 'foo', b: 10}`,
|
|
"pk()",
|
|
"CAST(10 AS integer)",
|
|
}
|
|
|
|
var operators = []string{
|
|
"=", ">", ">=", "<", "<=",
|
|
"+", "-", "*", "/", "%", "&", "|", "^",
|
|
"AND", "OR",
|
|
}
|
|
|
|
testFn := func(s string, want string) {
|
|
t.Helper()
|
|
e, err := parser.NewParser(strings.NewReader(s)).ParseExpr()
|
|
assert.NoError(t, err)
|
|
require.Equal(t, want, fmt.Sprintf("%v", e))
|
|
}
|
|
|
|
for _, op := range operands {
|
|
testFn(op, op)
|
|
}
|
|
|
|
for _, op := range operators {
|
|
want := fmt.Sprintf("10.4 %s foo.bar[1]", op)
|
|
testFn(want, want)
|
|
}
|
|
}
|