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"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/output/format"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/ioz"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/drivers/postgres"
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/testrun"
|
|
|
|
|
2022-12-18 10:18:35 +03:00
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/drivers/csv"
|
|
|
|
"github.com/neilotoole/sq/drivers/sqlite3"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/proj"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
)
|
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
// TestCmdInspect_json_yaml tests "sq inspect" for
|
|
|
|
// the JSON and YAML formats.
|
|
|
|
func TestCmdInspect_json_yaml(t *testing.T) {
|
2023-07-08 18:21:27 +03:00
|
|
|
tutil.SkipShort(t, true)
|
|
|
|
|
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) {
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
|
|
|
tutil.SkipWindowsIf(t, tc.handle == sakila.XLSX, "XLSX too slow on windows workflow")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
srcMeta := &source.Metadata{}
|
|
|
|
require.NoError(t, tf.unmarshalFn(tr.Out.Bytes(), srcMeta))
|
|
|
|
require.Equal(t, src.Type, srcMeta.Driver)
|
|
|
|
require.Equal(t, src.Handle, srcMeta.Handle)
|
|
|
|
require.Equal(t, source.RedactLocation(src.Location), srcMeta.Location)
|
|
|
|
|
|
|
|
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 {
|
2023-08-16 18:09:50 +03:00
|
|
|
if src.Type == postgres.Type && 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) {
|
|
|
|
tutil.SkipShort(t, true)
|
|
|
|
tr2 := testrun.New(th.Context, t, tr)
|
|
|
|
err := tr2.Exec("inspect", "."+tblName, fmt.Sprintf("--%s", tf.format))
|
|
|
|
require.NoError(t, err)
|
|
|
|
tblMeta := &source.TableMetadata{}
|
|
|
|
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")
|
|
|
|
tr2 := testrun.New(th.Context, t, tr)
|
|
|
|
err := tr2.Exec(
|
|
|
|
"inspect",
|
|
|
|
tc.handle,
|
|
|
|
fmt.Sprintf("--%s", flag.InspectOverview),
|
|
|
|
fmt.Sprintf("--%s", tf.format),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
srcMeta := &source.Metadata{}
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
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", 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
|
|
|
|
2023-06-22 08:48:58 +03:00
|
|
|
tutil.SkipWindowsIf(t, tc.handle == sakila.XLSX, "XLSX too slow on windows workflow")
|
|
|
|
|
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)
|
|
|
|
require.Contains(t, output, source.RedactLocation(src.Location))
|
|
|
|
|
|
|
|
for _, wantTblName := range tc.wantTbls {
|
|
|
|
if src.Type == postgres.Type && wantTblName == "film_text" {
|
|
|
|
// 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) {
|
|
|
|
tutil.SkipShort(t, true)
|
|
|
|
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)
|
|
|
|
require.Contains(t, output, source.RedactLocation(src.Location))
|
|
|
|
})
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
md := &source.Metadata{}
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
|
|
require.Equal(t, sqlite3.Type, 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)
|
|
|
|
|
|
|
|
md = &source.Metadata{}
|
2023-05-19 17:24:18 +03:00
|
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
|
|
require.Equal(t, csv.TypeCSV, 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) {
|
2020-08-06 20:58:47 +03:00
|
|
|
testCases := []struct {
|
|
|
|
fpath string
|
|
|
|
wantErr bool
|
2023-04-22 06:36:32 +03:00
|
|
|
wantType source.DriverType
|
2020-08-06 20:58:47 +03:00
|
|
|
wantTbls []string
|
|
|
|
}{
|
2023-06-22 08:48:58 +03:00
|
|
|
{
|
|
|
|
fpath: proj.Abs(sakila.PathCSVActor),
|
|
|
|
wantType: csv.TypeCSV,
|
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fpath: proj.Abs(sakila.PathTSVActor),
|
|
|
|
wantType: csv.TypeTSV,
|
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
2022-12-17 05:09:49 +03:00
|
|
|
t.Run(tutil.Name(tc.fpath), func(t *testing.T) {
|
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")
|
|
|
|
|
|
|
|
md := &source.Metadata{}
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|