2020-08-06 20:58:47 +03:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2020-11-02 20:40:29 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/xlsx"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/proj"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCmdInspect(t *testing.T) {
|
2021-02-26 09:16:01 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-11-02 20:40:29 +03:00
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
wantErr bool
|
|
|
|
wantType source.Type
|
|
|
|
wantTbls []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
handle: sakila.CSVActor,
|
|
|
|
wantType: csv.TypeCSV,
|
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.TSVActor,
|
|
|
|
wantType: csv.TypeTSV,
|
|
|
|
wantTbls: []string{source.MonotableName},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.XLSX,
|
|
|
|
wantType: xlsx.Type,
|
|
|
|
wantTbls: sakila.AllTbls(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.SL3,
|
|
|
|
wantType: sqlite3.Type,
|
|
|
|
wantTbls: sakila.AllTblsViews(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(tc.handle)
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
ru := newRun(t, nil).add(*src)
|
2020-11-02 20:40:29 +03:00
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
err := ru.Exec("inspect", "--json")
|
2020-11-02 20:40:29 +03:00
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
md := &source.Metadata{}
|
|
|
|
require.NoError(t, json.Unmarshal(ru.out.Bytes(), md))
|
|
|
|
require.Equal(t, tc.wantType, md.SourceType)
|
|
|
|
require.Equal(t, src.Handle, md.Handle)
|
|
|
|
require.Equal(t, src.Location, md.Location)
|
|
|
|
require.Equal(t, tc.wantTbls, md.TableNames())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmdInspectSmoke(t *testing.T) {
|
2020-08-06 20:58:47 +03:00
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(sakila.SL3)
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
ru := newRun(t, nil)
|
2023-01-01 06:17:44 +03:00
|
|
|
err := ru.Exec("inspect")
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Error(t, err, "should fail because no active src")
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
ru = newRun(t, nil)
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.add(*src) // now have an active src
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
err = ru.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{}
|
|
|
|
require.NoError(t, json.Unmarshal(ru.out.Bytes(), md))
|
|
|
|
require.Equal(t, sqlite3.Type, md.SourceType)
|
|
|
|
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-03-19 09:18:54 +03:00
|
|
|
ru = newRun(t, nil)
|
2020-08-06 20:58:47 +03:00
|
|
|
src = th.Source(sakila.CSVActor)
|
|
|
|
ru.add(*src)
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
err = ru.Exec("inspect", "--json", src.Handle)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
md = &source.Metadata{}
|
|
|
|
require.NoError(t, json.Unmarshal(ru.out.Bytes(), md))
|
|
|
|
require.Equal(t, csv.TypeCSV, md.SourceType)
|
|
|
|
require.Equal(t, sakila.CSVActor, md.Handle)
|
|
|
|
require.Equal(t, src.Location, md.Location)
|
|
|
|
require.Equal(t, []string{source.MonotableName}, md.TableNames())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmdInspect_Stdin(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
fpath string
|
|
|
|
wantErr bool
|
|
|
|
wantType source.Type
|
|
|
|
wantTbls []string
|
|
|
|
}{
|
|
|
|
{fpath: proj.Abs(sakila.PathCSVActor), wantType: csv.TypeCSV, wantTbls: []string{source.MonotableName}},
|
|
|
|
{fpath: proj.Abs(sakila.PathTSVActor), wantType: csv.TypeTSV, wantTbls: []string{source.MonotableName}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
2022-12-17 05:09:49 +03:00
|
|
|
t.Run(tutil.Name(tc.fpath), func(t *testing.T) {
|
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-03-19 09:18:54 +03:00
|
|
|
ru := newRun(t, nil)
|
2020-08-06 20:58:47 +03:00
|
|
|
ru.rc.Stdin = f
|
|
|
|
|
2023-01-01 06:17:44 +03:00
|
|
|
err = ru.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{}
|
|
|
|
require.NoError(t, json.Unmarshal(ru.out.Bytes(), md))
|
|
|
|
require.Equal(t, tc.wantType, md.SourceType)
|
|
|
|
require.Equal(t, source.StdinHandle, md.Handle)
|
|
|
|
require.Equal(t, source.StdinHandle, md.Location)
|
|
|
|
require.Equal(t, tc.wantTbls, md.TableNames())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|