2020-08-06 20:58:47 +03:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2023-04-22 16:37:07 +03:00
|
|
|
"context"
|
2020-08-06 20:58:47 +03:00
|
|
|
"encoding/json"
|
2023-06-22 08:48:58 +03:00
|
|
|
"fmt"
|
2020-08-06 20:58:47 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
"github.com/samber/lo"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
"github.com/neilotoole/sq/cli/output/format"
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/testrun"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/ioz"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgt"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/drivertype"
|
2024-01-25 09:29:55 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/location"
|
2023-11-21 00:42:38 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source/metadata"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/proj"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/testh/tu"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
// TestCmdInspect_json_yaml tests "sq inspect" for
|
|
|
|
// the JSON and YAML formats.
|
2024-01-15 04:45:34 +03:00
|
|
|
func TestCmdInspect_json_yaml(t *testing.T) { //nolint:tparallel
|
|
|
|
tu.SkipShort(t, true)
|
2023-07-08 18:21:27 +03:00
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
possibleTbls := append(sakila.AllTbls(), source.MonotableName)
|
2023-06-22 08:48:58 +03:00
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
wantTbls []string
|
|
|
|
}{
|
|
|
|
{sakila.CSVActor, []string{source.MonotableName}},
|
|
|
|
{sakila.TSVActor, []string{source.MonotableName}},
|
|
|
|
{sakila.XLSX, sakila.AllTbls()},
|
|
|
|
{sakila.SL3, sakila.AllTbls()},
|
|
|
|
{sakila.Pg, lo.Without(sakila.AllTbls(), sakila.TblFilmText)}, // pg doesn't have film_text
|
|
|
|
{sakila.My, sakila.AllTbls()},
|
|
|
|
{sakila.MS, sakila.AllTbls()},
|
|
|
|
}
|
|
|
|
|
|
|
|
testFormats := []struct {
|
|
|
|
format format.Format
|
|
|
|
unmarshalFn func(data []byte, v any) error
|
|
|
|
}{
|
|
|
|
{format.JSON, json.Unmarshal},
|
|
|
|
{format.YAML, ioz.UnmarshallYAML},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tf := range testFormats {
|
|
|
|
tf := tf
|
|
|
|
t.Run(tf.format.String(), func(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
t.Parallel()
|
|
|
|
tu.SkipWindowsIf(t, tc.handle == sakila.XLSX, "XLSX too slow on windows workflow")
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(tc.handle)
|
2021-02-26 09:16:01 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
tr := testrun.New(th.Context, t, nil).Hush().Add(*src)
|
|
|
|
err := tr.Exec("inspect", fmt.Sprintf("--%s", tf.format))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
srcMeta := &metadata.Source{}
|
2023-06-22 08:48:58 +03:00
|
|
|
require.NoError(t, tf.unmarshalFn(tr.Out.Bytes(), srcMeta))
|
|
|
|
require.Equal(t, src.Type, srcMeta.Driver)
|
|
|
|
require.Equal(t, src.Handle, srcMeta.Handle)
|
2024-01-25 09:29:55 +03:00
|
|
|
require.Equal(t, location.Redact(src.Location), srcMeta.Location)
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
gotTableNames := srcMeta.TableNames()
|
2023-08-16 18:09:50 +03:00
|
|
|
gotTableNames = lo.Intersect(gotTableNames, possibleTbls)
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
for _, wantTblName := range tc.wantTbls {
|
2024-01-25 09:29:55 +03:00
|
|
|
if src.Type == drivertype.Pg && wantTblName == sakila.TblFilmText {
|
2023-06-22 08:48:58 +03:00
|
|
|
// Postgres sakila DB doesn't have film_text for some reason
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
require.Contains(t, gotTableNames, wantTblName)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("inspect_table", func(t *testing.T) {
|
|
|
|
for _, tblName := range gotTableNames {
|
|
|
|
tblName := tblName
|
|
|
|
t.Run(tblName, func(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
tu.SkipShort(t, true)
|
|
|
|
tr2 := testrun.New(lg.NewContext(th.Context, lgt.New(t)), t, tr)
|
2023-06-22 08:48:58 +03:00
|
|
|
err := tr2.Exec("inspect", "."+tblName, fmt.Sprintf("--%s", tf.format))
|
|
|
|
require.NoError(t, err)
|
2023-11-21 00:42:38 +03:00
|
|
|
tblMeta := &metadata.Table{}
|
2023-06-22 08:48:58 +03:00
|
|
|
require.NoError(t, tf.unmarshalFn(tr2.Out.Bytes(), tblMeta))
|
|
|
|
require.Equal(t, tblName, tblMeta.Name)
|
|
|
|
require.True(t, len(tblMeta.Columns) > 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inspect_overview", func(t *testing.T) {
|
|
|
|
t.Logf("Test: sq inspect @src --overview")
|
2024-01-15 04:45:34 +03:00
|
|
|
tr2 := testrun.New(lg.NewContext(th.Context, lgt.New(t)), t, tr)
|
2023-06-22 08:48:58 +03:00
|
|
|
err := tr2.Exec(
|
|
|
|
"inspect",
|
|
|
|
tc.handle,
|
|
|
|
fmt.Sprintf("--%s", flag.InspectOverview),
|
|
|
|
fmt.Sprintf("--%s", tf.format),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
srcMeta := &metadata.Source{}
|
2023-06-22 08:48:58 +03:00
|
|
|
require.NoError(t, tf.unmarshalFn(tr2.Out.Bytes(), srcMeta))
|
|
|
|
require.Equal(t, src.Type, srcMeta.Driver)
|
|
|
|
require.Equal(t, src.Handle, srcMeta.Handle)
|
|
|
|
require.Nil(t, srcMeta.Tables)
|
|
|
|
require.Zero(t, srcMeta.TableCount)
|
|
|
|
require.Zero(t, srcMeta.ViewCount)
|
2023-11-19 17:21:38 +03:00
|
|
|
require.NotEmpty(t, srcMeta.Name)
|
|
|
|
require.NotEmpty(t, srcMeta.Schema)
|
|
|
|
require.NotEmpty(t, srcMeta.FQName)
|
|
|
|
require.NotEmpty(t, srcMeta.DBDriver)
|
|
|
|
require.NotEmpty(t, srcMeta.DBProduct)
|
|
|
|
require.NotEmpty(t, srcMeta.DBVersion)
|
|
|
|
require.NotZero(t, srcMeta.Size)
|
2023-06-22 08:48:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inspect_dbprops", func(t *testing.T) {
|
|
|
|
t.Logf("Test: sq inspect @src --dbprops")
|
2024-01-15 04:45:34 +03:00
|
|
|
tr2 := testrun.New(lg.NewContext(th.Context, lgt.New(t)), t, tr)
|
2023-06-22 08:48:58 +03:00
|
|
|
err := tr2.Exec(
|
|
|
|
"inspect",
|
|
|
|
tc.handle,
|
|
|
|
fmt.Sprintf("--%s", flag.InspectDBProps),
|
|
|
|
fmt.Sprintf("--%s", tf.format),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
props := map[string]any{}
|
|
|
|
require.NoError(t, tf.unmarshalFn(tr2.Out.Bytes(), &props))
|
|
|
|
require.NotEmpty(t, props)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCmdInspect_text tests "sq inspect" for
|
|
|
|
// the text format.
|
|
|
|
func TestCmdInspect_text(t *testing.T) { //nolint:tparallel
|
2020-11-02 20:40:29 +03:00
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
wantTbls []string
|
|
|
|
}{
|
2023-06-22 08:48:58 +03:00
|
|
|
{sakila.CSVActor, []string{source.MonotableName}},
|
|
|
|
{sakila.TSVActor, []string{source.MonotableName}},
|
|
|
|
{sakila.XLSX, sakila.AllTbls()},
|
|
|
|
{sakila.SL3, sakila.AllTbls()},
|
|
|
|
{sakila.Pg, lo.Without(sakila.AllTbls(), sakila.TblFilmText)}, // pg doesn't have film_text
|
|
|
|
{sakila.My, sakila.AllTbls()},
|
|
|
|
{sakila.MS, sakila.AllTbls()},
|
2020-11-02 20:40:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
2021-02-26 09:16:01 +03:00
|
|
|
t.Parallel()
|
2020-11-02 20:40:29 +03:00
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
tu.SkipWindowsIf(t, tc.handle == sakila.XLSX, "XLSX too slow on windows workflow")
|
2023-06-22 08:48:58 +03:00
|
|
|
|
2020-11-02 20:40:29 +03:00
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(tc.handle)
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
tr := testrun.New(th.Context, t, nil).Hush().Add(*src)
|
|
|
|
err := tr.Exec("inspect", fmt.Sprintf("--%s", format.Text))
|
|
|
|
require.NoError(t, err)
|
2020-11-02 20:40:29 +03:00
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
output := tr.Out.String()
|
|
|
|
require.Contains(t, output, src.Type)
|
|
|
|
require.Contains(t, output, src.Handle)
|
2024-01-25 09:29:55 +03:00
|
|
|
require.Contains(t, output, location.Redact(src.Location))
|
2023-06-22 08:48:58 +03:00
|
|
|
|
|
|
|
for _, wantTblName := range tc.wantTbls {
|
2024-01-25 09:29:55 +03:00
|
|
|
if src.Type == drivertype.Pg && wantTblName == "film_text" {
|
2023-06-22 08:48:58 +03:00
|
|
|
// Postgres sakila DB doesn't have film_text for some reason
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
require.Contains(t, output, wantTblName)
|
2020-11-02 20:40:29 +03:00
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
t.Run("inspect_table", func(t *testing.T) {
|
|
|
|
for _, tblName := range tc.wantTbls {
|
|
|
|
tblName := tblName
|
|
|
|
t.Run(tblName, func(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
tu.SkipShort(t, true)
|
2023-06-22 08:48:58 +03:00
|
|
|
t.Logf("Test: sq inspect .tbl")
|
|
|
|
tr2 := testrun.New(th.Context, t, tr)
|
|
|
|
err := tr2.Exec("inspect", "."+tblName, fmt.Sprintf("--%s", format.Text))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
output := tr2.Out.String()
|
|
|
|
require.Contains(t, output, tblName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inspect_overview", func(t *testing.T) {
|
|
|
|
t.Logf("Test: sq inspect @src --overview")
|
|
|
|
tr2 := testrun.New(th.Context, t, tr)
|
|
|
|
err := tr2.Exec(
|
|
|
|
"inspect",
|
|
|
|
tc.handle,
|
|
|
|
fmt.Sprintf("--%s", flag.InspectOverview),
|
|
|
|
fmt.Sprintf("--%s", format.Text),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
output := tr2.Out.String()
|
|
|
|
require.Contains(t, output, src.Type)
|
|
|
|
require.Contains(t, output, src.Handle)
|
2024-01-25 09:29:55 +03:00
|
|
|
require.Contains(t, output, location.Redact(src.Location))
|
2023-06-22 08:48:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("inspect_dbprops", func(t *testing.T) {
|
|
|
|
t.Logf("Test: sq inspect @src --dbprops")
|
|
|
|
tr2 := testrun.New(th.Context, t, tr)
|
|
|
|
err := tr2.Exec(
|
|
|
|
"inspect",
|
|
|
|
tc.handle,
|
|
|
|
fmt.Sprintf("--%s", flag.InspectDBProps),
|
|
|
|
fmt.Sprintf("--%s", format.Text),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
output := tr2.Out.String()
|
|
|
|
require.NotEmpty(t, output)
|
|
|
|
})
|
2020-11-02 20:40:29 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
func TestCmdInspect_smoke(t *testing.T) {
|
2020-08-06 20:58:47 +03:00
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(sakila.SL3)
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
tr := testrun.New(th.Context, t, nil)
|
|
|
|
err := tr.Exec("inspect")
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Error(t, err, "should fail because no active src")
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
tr = testrun.New(th.Context, t, nil)
|
|
|
|
tr.Add(*src) // now have an active src
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
err = tr.Exec("inspect", "--json")
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err, "should pass because there is an active src")
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
md := &metadata.Source{}
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
2024-01-25 09:29:55 +03:00
|
|
|
require.Equal(t, drivertype.SQLite, md.Driver)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, sakila.SL3, md.Handle)
|
2023-01-01 06:17:44 +03:00
|
|
|
require.Equal(t, src.RedactedLocation(), md.Location)
|
2020-08-16 00:06:40 +03:00
|
|
|
require.Equal(t, sakila.AllTblsViews(), md.TableNames())
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// Try one more source for good measure
|
2023-05-19 17:24:18 +03:00
|
|
|
tr = testrun.New(th.Context, t, nil)
|
2020-08-06 20:58:47 +03:00
|
|
|
src = th.Source(sakila.CSVActor)
|
2023-05-19 17:24:18 +03:00
|
|
|
tr.Add(*src)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
err = tr.Exec("inspect", "--json", src.Handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
md = &metadata.Source{}
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
2024-01-25 09:29:55 +03:00
|
|
|
require.Equal(t, drivertype.CSV, md.Driver)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, sakila.CSVActor, md.Handle)
|
|
|
|
require.Equal(t, src.Location, md.Location)
|
|
|
|
require.Equal(t, []string{source.MonotableName}, md.TableNames())
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
func TestCmdInspect_stdin(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
testCases := []struct {
|
|
|
|
fpath string
|
|
|
|
wantErr bool
|
2023-11-21 00:42:38 +03:00
|
|
|
wantType drivertype.Type
|
2020-08-06 20:58:47 +03:00
|
|
|
wantTbls []string
|
|
|
|
}{
|
2023-06-22 08:48:58 +03:00
|
|
|
{
|
|
|
|
fpath: proj.Abs(sakila.PathCSVActor),
|
2024-01-25 09:29:55 +03:00
|
|
|
wantType: drivertype.CSV,
|
2023-06-22 08:48:58 +03:00
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fpath: proj.Abs(sakila.PathTSVActor),
|
2024-01-25 09:29:55 +03:00
|
|
|
wantType: drivertype.TSV,
|
2023-06-22 08:48:58 +03:00
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
t.Run(tu.Name(tc.fpath), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-04-22 16:37:07 +03:00
|
|
|
ctx := context.Background()
|
2020-08-09 00:23:30 +03:00
|
|
|
f, err := os.Open(tc.fpath) // No need to close f
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
2020-08-09 00:23:30 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
tr := testrun.New(ctx, t, nil)
|
|
|
|
tr.Run.Stdin = f
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
err = tr.Exec("inspect", "--json")
|
2020-08-06 20:58:47 +03:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err, "should read from stdin")
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
md := &metadata.Source{}
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
|
|
require.Equal(t, tc.wantType, md.Driver)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, source.StdinHandle, md.Handle)
|
|
|
|
require.Equal(t, source.StdinHandle, md.Location)
|
|
|
|
require.Equal(t, tc.wantTbls, md.TableNames())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 00:42:38 +03:00
|
|
|
|
|
|
|
func TestCmdInspect_mode_schemata(t *testing.T) {
|
|
|
|
active := lo.ToPtr(true)
|
|
|
|
|
|
|
|
type schema struct {
|
|
|
|
Name string `json:"schema" yaml:"schema"`
|
|
|
|
Catalog string `json:"catalog" yaml:"catalog"`
|
|
|
|
Owner string `json:"owner,omitempty" yaml:"owner,omitempty"`
|
|
|
|
Active *bool `json:"active" yaml:"active"`
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
wantSchemata []schema
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
handle: sakila.SL3,
|
|
|
|
wantSchemata: []schema{
|
|
|
|
{Name: "main", Catalog: "default", Active: active},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.Pg,
|
|
|
|
wantSchemata: []schema{
|
|
|
|
{Name: "information_schema", Catalog: "sakila", Owner: "sakila"},
|
|
|
|
{Name: "pg_catalog", Catalog: "sakila", Owner: "sakila"},
|
|
|
|
{Name: "public", Catalog: "sakila", Owner: "sakila", Active: active},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.MS,
|
|
|
|
wantSchemata: []schema{
|
|
|
|
{Name: "INFORMATION_SCHEMA", Catalog: "sakila", Owner: "INFORMATION_SCHEMA"},
|
|
|
|
{Name: "dbo", Catalog: "sakila", Owner: "dbo", Active: active},
|
|
|
|
{Name: "sys", Catalog: "sakila", Owner: "sys"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.My,
|
|
|
|
wantSchemata: []schema{
|
|
|
|
{Name: "information_schema", Catalog: "def", Owner: ""},
|
|
|
|
{Name: "mysql", Catalog: "def", Owner: ""},
|
|
|
|
{Name: "sakila", Catalog: "def", Owner: "", Active: active},
|
|
|
|
{Name: "sys", Catalog: "def", Owner: ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fm := range []format.Format{format.JSON, format.YAML, format.Text} {
|
|
|
|
fm := fm
|
|
|
|
t.Run(fm.String(), func(t *testing.T) {
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(tc.handle)
|
|
|
|
|
|
|
|
tr := testrun.New(th.Context, t, nil).Hush().Add(*src)
|
|
|
|
err := tr.Exec("inspect", "--"+flag.InspectSchemata, "--"+fm.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
var gotSchemata []schema
|
|
|
|
|
|
|
|
switch fm { //nolint:exhaustive
|
|
|
|
case format.JSON:
|
|
|
|
tr.Bind(&gotSchemata)
|
|
|
|
case format.YAML:
|
|
|
|
tr.BindYAML(&gotSchemata)
|
|
|
|
case format.Text:
|
|
|
|
t.Logf("\n%s", tr.OutString())
|
|
|
|
// Return early because we can't be bothered to parse text output
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range tc.wantSchemata {
|
|
|
|
require.Contains(t, gotSchemata, s, "wantSchemata[%d]", i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmdInspect_mode_catalogs(t *testing.T) {
|
|
|
|
active := lo.ToPtr(true)
|
|
|
|
type catalog struct {
|
|
|
|
Catalog string `json:"catalog" yaml:"catalog"`
|
|
|
|
Active *bool `json:"active,omitempty" yaml:"active,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
wantCatalogs []catalog
|
|
|
|
}{
|
|
|
|
// Note that SQLite doesn't support catalogs
|
|
|
|
{
|
|
|
|
handle: sakila.Pg,
|
|
|
|
wantCatalogs: []catalog{
|
|
|
|
{Catalog: "postgres"},
|
|
|
|
{Catalog: "sakila", Active: active},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.MS,
|
|
|
|
wantCatalogs: []catalog{
|
|
|
|
{Catalog: "master"},
|
|
|
|
{Catalog: "model"},
|
|
|
|
{Catalog: "msdb"},
|
|
|
|
{Catalog: "sakila", Active: active},
|
|
|
|
{Catalog: "tempdb"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.My,
|
|
|
|
wantCatalogs: []catalog{
|
|
|
|
{Catalog: "def", Active: active},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fm := range []format.Format{format.JSON, format.YAML, format.Text} {
|
|
|
|
fm := fm
|
|
|
|
t.Run(fm.String(), func(t *testing.T) {
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(tc.handle)
|
|
|
|
|
|
|
|
tr := testrun.New(th.Context, t, nil).Hush().Add(*src)
|
|
|
|
err := tr.Exec("inspect", "--"+flag.InspectCatalogs, "--"+fm.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
var gotCatalogs []catalog
|
|
|
|
|
|
|
|
switch fm { //nolint:exhaustive
|
|
|
|
case format.JSON:
|
|
|
|
tr.Bind(&gotCatalogs)
|
|
|
|
case format.YAML:
|
|
|
|
tr.BindYAML(&gotCatalogs)
|
|
|
|
case format.Text:
|
|
|
|
t.Logf("\n%s", tr.OutString())
|
|
|
|
// Return early because we can't be bothered to parse text output
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range tc.wantCatalogs {
|
|
|
|
require.Contains(t, gotCatalogs, c, "wantCatalogs[%d]", i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|