sq/cli/cmd_tbl_test.go

105 lines
3.0 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package cli_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/stringz"
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/sakila"
)
func TestCmdTblCopy(t *testing.T) { //nolint:tparallel
for _, handle := range sakila.SQLAll() {
2020-08-06 20:58:47 +03:00
handle := handle
t.Run(handle, func(t *testing.T) {
t.Parallel()
th := testh.New(t)
src := th.Source(handle)
srcTblHandle := src.Handle + "." + sakila.TblActor
2020-08-13 06:22:53 +03:00
destTbl1 := stringz.UniqTableName(sakila.TblActor)
2020-08-06 20:58:47 +03:00
2023-04-22 16:37:07 +03:00
ru1 := newRun(th.Context, t, nil).add(*src)
err := ru1.Exec("tbl", "copy", "--data=false", srcTblHandle, src.Handle+"."+destTbl1)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
2020-08-13 06:22:53 +03:00
defer th.DropTable(src, destTbl1)
require.Equal(t, int64(0), th.RowCount(src, destTbl1),
"should not have copied any rows because --data=false")
2020-08-06 20:58:47 +03:00
2020-08-13 06:22:53 +03:00
// --data=true
2023-04-22 16:37:07 +03:00
ru2 := newRun(th.Context, t, nil).add(*src)
2020-08-13 06:22:53 +03:00
destTbl2 := stringz.UniqTableName(sakila.TblActor)
err = ru2.Exec("tbl", "copy", "--data=true", srcTblHandle, src.Handle+"."+destTbl2)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
2020-08-13 06:22:53 +03:00
defer th.DropTable(src, destTbl2)
require.Equal(t, int64(sakila.TblActorCount), th.RowCount(src, destTbl2),
"should have copied rows because --data=true")
2020-08-06 20:58:47 +03:00
})
}
}
func TestCmdTblDrop(t *testing.T) { //nolint:tparallel
for _, handle := range sakila.SQLAll() {
2020-08-06 20:58:47 +03:00
handle := handle
t.Run(handle, func(t *testing.T) {
t.Parallel()
th := testh.New(t)
src := th.Source(handle)
2020-08-13 06:22:53 +03:00
destTblName := th.CopyTable(false, src, sakila.TblActor, "", true)
needsDrop := true
defer func() {
if needsDrop {
th.DropTable(src, destTblName)
}
}()
2020-08-06 20:58:47 +03:00
tblMeta, err := th.Open(src).TableMetadata(th.Context, destTblName)
require.NoError(t, err) // verify that the table exists
require.Equal(t, destTblName, tblMeta.Name)
require.Equal(t, int64(sakila.TblActorCount), tblMeta.RowCount)
2023-04-22 16:37:07 +03:00
err = newRun(th.Context, t, nil).add(*src).Exec("tbl", "drop", src.Handle+"."+destTblName)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
2020-08-13 06:22:53 +03:00
needsDrop = false
2020-08-06 20:58:47 +03:00
tblMeta, err = th.Open(src).TableMetadata(th.Context, destTblName)
require.Error(t, err, "should get an error because the table was dropped")
require.Nil(t, tblMeta)
})
}
}
func TestCmdTblTruncate(t *testing.T) {
t.Parallel()
2020-08-06 20:58:47 +03:00
testCases := []string{sakila.MS, sakila.My, sakila.Pg9}
2020-08-13 06:22:53 +03:00
for _, handle := range testCases {
2020-08-06 20:58:47 +03:00
handle := handle
t.Run(handle, func(t *testing.T) {
t.Parallel()
th := testh.New(t)
src := th.Source(handle)
destTblName := th.CopyTable(true, src, sakila.TblActor, "", true)
2020-08-06 20:58:47 +03:00
tblMeta, err := th.Open(src).TableMetadata(th.Context, destTblName)
require.NoError(t, err) // verify that the table exists
require.Equal(t, destTblName, tblMeta.Name)
require.Equal(t, int64(sakila.TblActorCount), tblMeta.RowCount)
2023-04-22 16:37:07 +03:00
err = newRun(th.Context, t, nil).add(*src).Exec("tbl", "truncate", src.Handle+"."+destTblName)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
tblMeta, err = th.Open(src).TableMetadata(th.Context, destTblName)
require.NoError(t, err)
require.Equal(t, int64(0), tblMeta.RowCount)
})
}
}