Files
chaisql/internal/query/statement/delete_test.go
Jean Hadrien Chabran 4a6e68439a Refactor to handle errors with internal/errors (#432)
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
2021-08-22 11:47:54 +03:00

65 lines
2.3 KiB
Go

package statement_test
import (
"bytes"
"testing"
"github.com/genjidb/genji"
"github.com/genjidb/genji/internal/testutil"
"github.com/genjidb/genji/internal/testutil/assert"
"github.com/stretchr/testify/require"
)
func TestDeleteStmt(t *testing.T) {
tests := []struct {
name string
query string
fails bool
expected string
params []interface{}
}{
{"No cond", `DELETE FROM test`, false, "[]", nil},
{"With cond", "DELETE FROM test WHERE b = 'bar1'", false, `[{"d": "foo3", "b": "bar2", "e": "bar3", "n": 1}]`, nil},
{"With offset", "DELETE FROM test OFFSET 1", false, `[{"a":"foo1", "b":"bar1", "c":"baz1", "n": 3}]`, nil},
{"With order by then offset", "DELETE FROM test ORDER BY n OFFSET 1", false, `[{"d":"foo3", "b":"bar2", "e":"bar3", "n": 1}]`, nil},
{"With order by DESC then offset", "DELETE FROM test ORDER BY n DESC OFFSET 1", false, `[{"a": "foo1", "b": "bar1", "c": "baz1", "n": 3}]`, nil},
{"With limit", "DELETE FROM test ORDER BY n LIMIT 2", false, `[{"a":"foo1", "b":"bar1", "c":"baz1", "n": 3}]`, nil},
{"With order by then limit then offset", "DELETE FROM test ORDER BY n LIMIT 1 OFFSET 1", false, `[{"a": "foo1", "b": "bar1", "c": "baz1", "n": 3}, {"d": "foo3", "b": "bar2", "e": "bar3", "n": 1}]`, nil},
{"Table not found", "DELETE FROM foo WHERE b = 'bar1'", true, "[]", nil},
{"Read-only table", "DELETE FROM __genji_catalog", true, "[]", nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
db, err := genji.Open(":memory:")
assert.NoError(t, err)
defer db.Close()
err = db.Exec("CREATE TABLE test")
assert.NoError(t, err)
err = db.Exec("INSERT INTO test (a, b, c, n) VALUES ('foo1', 'bar1', 'baz1', 3)")
assert.NoError(t, err)
err = db.Exec("INSERT INTO test (a, b, n) VALUES ('foo2', 'bar1', 2)")
assert.NoError(t, err)
err = db.Exec("INSERT INTO test (d, b, e, n) VALUES ('foo3', 'bar2', 'bar3', 1)")
assert.NoError(t, err)
err = db.Exec(test.query, test.params...)
if test.fails {
assert.Error(t, err)
return
}
assert.NoError(t, err)
st, err := db.Query("SELECT * FROM test")
assert.NoError(t, err)
defer st.Close()
var buf bytes.Buffer
err = testutil.IteratorToJSONArray(&buf, st)
assert.NoError(t, err)
require.JSONEq(t, test.expected, buf.String())
})
}
}