mirror of
https://github.com/neilotoole/sq.git
synced 2024-12-28 10:44:29 +03:00
1ea24dac4a
* "sq diff" initial implementation * Refactor "cli" pkg.
158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/neilotoole/sq/cli/testrun"
|
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/neilotoole/sq/drivers/csv"
|
|
"github.com/neilotoole/sq/drivers/sqlite3"
|
|
"github.com/neilotoole/sq/drivers/xlsx"
|
|
"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) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
handle string
|
|
wantErr bool
|
|
wantType source.DriverType
|
|
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) {
|
|
t.Parallel()
|
|
|
|
th := testh.New(t)
|
|
src := th.Source(tc.handle)
|
|
|
|
tr := testrun.New(th.Context, t, nil).Add(*src)
|
|
|
|
err := tr.Exec("inspect", "--json")
|
|
if tc.wantErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
md := &source.Metadata{}
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
require.Equal(t, tc.wantType, md.Driver)
|
|
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) {
|
|
th := testh.New(t)
|
|
src := th.Source(sakila.SL3)
|
|
|
|
tr := testrun.New(th.Context, t, nil)
|
|
err := tr.Exec("inspect")
|
|
require.Error(t, err, "should fail because no active src")
|
|
|
|
tr = testrun.New(th.Context, t, nil)
|
|
tr.Add(*src) // now have an active src
|
|
|
|
err = tr.Exec("inspect", "--json")
|
|
require.NoError(t, err, "should pass because there is an active src")
|
|
|
|
md := &source.Metadata{}
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
require.Equal(t, sqlite3.Type, md.Driver)
|
|
require.Equal(t, sakila.SL3, md.Handle)
|
|
require.Equal(t, src.RedactedLocation(), md.Location)
|
|
require.Equal(t, sakila.AllTblsViews(), md.TableNames())
|
|
|
|
// Try one more source for good measure
|
|
tr = testrun.New(th.Context, t, nil)
|
|
src = th.Source(sakila.CSVActor)
|
|
tr.Add(*src)
|
|
|
|
err = tr.Exec("inspect", "--json", src.Handle)
|
|
require.NoError(t, err)
|
|
|
|
md = &source.Metadata{}
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
require.Equal(t, csv.TypeCSV, md.Driver)
|
|
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.DriverType
|
|
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
|
|
|
|
t.Run(tutil.Name(tc.fpath), func(t *testing.T) {
|
|
ctx := context.Background()
|
|
f, err := os.Open(tc.fpath) // No need to close f
|
|
require.NoError(t, err)
|
|
|
|
tr := testrun.New(ctx, t, nil)
|
|
tr.Run.Stdin = f
|
|
|
|
err = tr.Exec("inspect", "--json")
|
|
if tc.wantErr {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err, "should read from stdin")
|
|
|
|
md := &source.Metadata{}
|
|
require.NoError(t, json.Unmarshal(tr.Out.Bytes(), md))
|
|
require.Equal(t, tc.wantType, md.Driver)
|
|
require.Equal(t, source.StdinHandle, md.Handle)
|
|
require.Equal(t, source.StdinHandle, md.Location)
|
|
require.Equal(t, tc.wantTbls, md.TableNames())
|
|
})
|
|
}
|
|
}
|