Files
chaisql/internal/query/statement/alter_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

50 lines
1.3 KiB
Go

package statement_test
import (
"testing"
"github.com/genjidb/genji"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/errors"
"github.com/genjidb/genji/internal/testutil/assert"
errs "github.com/genjidb/genji/errors"
"github.com/stretchr/testify/require"
)
func TestAlterTable(t *testing.T) {
db, err := genji.Open(":memory:")
assert.NoError(t, err)
defer db.Close()
err = db.Exec("CREATE TABLE foo")
assert.NoError(t, err)
// Insert some data into foo
err = db.Exec(`INSERT INTO foo VALUES {name: "John Doe", age: 99}`)
assert.NoError(t, err)
// Renaming the table to the same name should fail.
err = db.Exec("ALTER TABLE foo RENAME TO foo")
assert.ErrorIs(t, err, errs.AlreadyExistsError{Name: "foo"})
err = db.Exec("ALTER TABLE foo RENAME TO bar")
assert.NoError(t, err)
// Selecting from the old name should fail.
err = db.Exec("SELECT * FROM foo")
if !errors.Is(err, errs.NotFoundError{}) {
assert.ErrorIs(t, err, errs.NotFoundError{Name: "foo"})
}
d, err := db.QueryDocument("SELECT * FROM bar")
assert.NoError(t, err)
data, err := document.MarshalJSON(d)
assert.NoError(t, err)
require.JSONEq(t, `{"name": "John Doe", "age": 99}`, string(data))
// Renaming a read-only table should fail
err = db.Exec("ALTER TABLE __genji_catalog RENAME TO bar")
assert.Error(t, err)
}