2020-08-06 20:58:47 +03:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-22 10:37:00 +03:00
|
|
|
"context"
|
2020-08-06 20:58:47 +03:00
|
|
|
"fmt"
|
|
|
|
"image/gif"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
2020-08-23 22:00:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlmodel"
|
2020-08-23 13:42:15 +03:00
|
|
|
"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/fixt"
|
|
|
|
"github.com/neilotoole/sq/testh/proj"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSmoke(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// Execute a bunch of smoke test cases.
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
a []string
|
|
|
|
// errBecause, if non-empty, indicates an error is expected.
|
|
|
|
errBecause string
|
|
|
|
}{
|
2021-02-22 10:37:00 +03:00
|
|
|
{a: []string{"ls"}},
|
|
|
|
{a: []string{"ls", "-v"}},
|
|
|
|
{a: []string{"ls", "--help"}},
|
|
|
|
{a: []string{"inspect"}, errBecause: "no active data source"},
|
|
|
|
{a: []string{"inspect", "--help"}},
|
|
|
|
{a: []string{"version"}},
|
|
|
|
{a: []string{"--version"}},
|
|
|
|
{a: []string{"help"}},
|
|
|
|
{a: []string{"--help"}},
|
2021-02-27 17:44:08 +03:00
|
|
|
{a: []string{"ping", "--all"}},
|
2021-02-22 10:37:00 +03:00
|
|
|
{a: []string{"ping", "--help"}},
|
|
|
|
{a: []string{"ping"}, errBecause: "no active data source"},
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(strings.Join(tc.a, "_"), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
rc, out, errOut := newTestRunCtx(t, nil)
|
2021-02-22 10:37:00 +03:00
|
|
|
err := cli.ExecuteWith(context.Background(), rc, tc.a)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// We log sq's output before doing assert, because it reads
|
|
|
|
// better in testing's output that way.
|
|
|
|
if out.Len() > 0 {
|
|
|
|
t.Log(strings.TrimSuffix(out.String(), "\n"))
|
|
|
|
}
|
|
|
|
if errOut.Len() > 0 {
|
|
|
|
t.Log(strings.TrimSuffix(errOut.String(), "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.errBecause != "" {
|
|
|
|
assert.Error(t, err, tc.errBecause)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, tc.errBecause)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTblTestBytes(t *testing.T) {
|
|
|
|
th, src, _, _ := testh.NewWith(t, sakila.Pg9)
|
2020-08-16 00:06:40 +03:00
|
|
|
th.DiffDB(src)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
tblDef := sqlmodel.NewTableDef(
|
|
|
|
stringz.UniqTableName("test_bytes"),
|
|
|
|
[]string{"col_name", "col_bytes"},
|
2020-08-23 13:42:15 +03:00
|
|
|
[]kind.Kind{kind.Text, kind.Bytes},
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fBytes := proj.ReadFile(fixt.GopherPath)
|
2022-12-17 02:34:33 +03:00
|
|
|
data := []any{fixt.GopherFilename, fBytes}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
require.Equal(t, int64(1), th.CreateTable(true, src, tblDef, data))
|
2023-04-08 23:58:10 +03:00
|
|
|
th.DropTable(src, tblDef.Name)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestOutputRaw verifies that the raw output format works.
|
|
|
|
// We're particularly concerned that bytes output is correct.
|
|
|
|
func TestOutputRaw(t *testing.T) {
|
2022-12-18 10:18:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-08-09 17:40:46 +03:00
|
|
|
for _, handle := range sakila.SQLAll() {
|
2020-08-06 20:58:47 +03:00
|
|
|
handle := handle
|
|
|
|
|
|
|
|
t.Run(handle, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
wantBytes := proj.ReadFile(fixt.GopherPath)
|
|
|
|
require.Equal(t, fixt.GopherSize, len(wantBytes))
|
|
|
|
_, err := gif.Decode(bytes.NewReader(wantBytes))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tblDef := sqlmodel.NewTableDef(
|
|
|
|
stringz.UniqTableName("test_bytes"),
|
|
|
|
[]string{"col_name", "col_bytes"},
|
2020-08-23 13:42:15 +03:00
|
|
|
[]kind.Kind{kind.Text, kind.Bytes},
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
th, src, _, _ := testh.NewWith(t, handle)
|
|
|
|
|
|
|
|
// Create the table and insert data
|
2022-12-17 02:34:33 +03:00
|
|
|
insertRow := []any{fixt.GopherFilename, wantBytes}
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, int64(1), th.CreateTable(true, src, tblDef, insertRow))
|
2023-04-08 23:58:10 +03:00
|
|
|
defer th.DropTable(src, tblDef.Name)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// 1. Query and check that libsq is returning bytes correctly.
|
|
|
|
query := fmt.Sprintf("SELECT col_bytes FROM %s WHERE col_name = '%s'",
|
|
|
|
tblDef.Name, fixt.GopherFilename)
|
|
|
|
sink, err := th.QuerySQL(src, query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(sink.Recs))
|
2020-08-23 13:42:15 +03:00
|
|
|
require.Equal(t, kind.Bytes, sink.RecMeta[0].Kind())
|
2020-08-06 20:58:47 +03:00
|
|
|
dbBytes := *(sink.Recs[0][0].(*[]byte))
|
|
|
|
require.Equal(t, fixt.GopherSize, len(dbBytes))
|
|
|
|
require.Equal(t, wantBytes, dbBytes)
|
|
|
|
|
|
|
|
// 1. Now that we've verified libsq, we'll test cli. First
|
|
|
|
// using using --output=/path/to/file
|
2022-12-18 02:11:33 +03:00
|
|
|
tmpDir, err := os.MkdirTemp("", "")
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
outputPath := filepath.Join(tmpDir, "gopher.gif")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(outputPath)
|
|
|
|
})
|
|
|
|
|
2023-03-19 09:18:54 +03:00
|
|
|
ru := newRun(t, nil).add(*src).hush()
|
2023-01-01 06:17:44 +03:00
|
|
|
err = ru.Exec("sql", "--raw", "--output="+outputPath, query)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-18 02:11:33 +03:00
|
|
|
outputBytes, err := os.ReadFile(outputPath)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, fixt.GopherSize, len(outputBytes))
|
|
|
|
_, err = gif.Decode(bytes.NewReader(outputBytes))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// 2. Now test that stdout also gets the same data
|
2023-03-19 09:18:54 +03:00
|
|
|
ru = newRun(t, nil).add(*src)
|
2023-01-01 06:17:44 +03:00
|
|
|
err = ru.Exec("sql", "--raw", query)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, wantBytes, ru.out.Bytes())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|