mirror of
https://github.com/chaisql/chai.git
synced 2025-10-05 07:36:56 +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
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package parser_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/genjidb/genji/document"
|
|
"github.com/genjidb/genji/internal/query/statement"
|
|
"github.com/genjidb/genji/internal/sql/parser"
|
|
"github.com/genjidb/genji/internal/stream"
|
|
"github.com/genjidb/genji/internal/testutil"
|
|
"github.com/genjidb/genji/internal/testutil/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestParserUpdate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
s string
|
|
expected *stream.Stream
|
|
errored bool
|
|
}{
|
|
{"SET/No cond", "UPDATE test SET a = 1",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Set(document.Path(testutil.ParsePath(t, "a")), testutil.IntegerValue(1))).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"SET/With cond", "UPDATE test SET a = 1, b = 2 WHERE age = 10",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Filter(parser.MustParseExpr("age = 10"))).
|
|
Pipe(stream.Set(document.Path(testutil.ParsePath(t, "a")), testutil.IntegerValue(1))).
|
|
Pipe(stream.Set(document.Path(testutil.ParsePath(t, "b")), parser.MustParseExpr("2"))).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"SET/No cond path with backquotes", "UPDATE test SET ` some \"path\" ` = 1",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Set(document.Path(testutil.ParsePath(t, "` some \"path\" `")), testutil.IntegerValue(1))).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"SET/No cond nested path", "UPDATE test SET a.b = 1",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Set(document.Path(testutil.ParsePath(t, "a.b")), testutil.IntegerValue(1))).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"UNSET/No cond", "UPDATE test UNSET a",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Unset("a")).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"UNSET/With cond", "UPDATE test UNSET a, b WHERE age = 10",
|
|
stream.New(stream.SeqScan("test")).
|
|
Pipe(stream.Filter(parser.MustParseExpr("age = 10"))).
|
|
Pipe(stream.Unset("a")).
|
|
Pipe(stream.Unset("b")).
|
|
Pipe(stream.TableReplace("test")),
|
|
false,
|
|
},
|
|
{"Trailing comma", "UPDATE test SET a = 1, WHERE age = 10", nil, true},
|
|
{"No SET", "UPDATE test WHERE age = 10", nil, true},
|
|
{"No pair", "UPDATE test SET WHERE age = 10", nil, true},
|
|
{"query.Field only", "UPDATE test SET a WHERE age = 10", nil, true},
|
|
{"No value", "UPDATE test SET a = WHERE age = 10", nil, true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
q, err := parser.ParseQuery(test.s)
|
|
if test.errored {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
require.Len(t, q.Statements, 1)
|
|
stmt := q.Statements[0].(*statement.StreamStmt)
|
|
require.False(t, stmt.IsReadOnly())
|
|
require.EqualValues(t, test.expected, q.Statements[0].(*statement.StreamStmt).Stream)
|
|
})
|
|
}
|
|
}
|