sq/cli/cmd_ping_test.go

78 lines
2.0 KiB
Go
Raw Normal View History

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/csv"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/cli/testrun"
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/libsq/source"
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/sakila"
)
func TestCmdPing(t *testing.T) {
t.Parallel()
2023-04-22 16:37:07 +03:00
ctx := context.Background()
2020-08-06 20:58:47 +03:00
err := testrun.New(ctx, t, nil).Exec("ping")
2020-08-06 20:58:47 +03:00
require.Error(t, err, "no active data source")
err = testrun.New(ctx, t, nil).Exec("ping", "invalid_handle")
2020-08-06 20:58:47 +03:00
require.Error(t, err)
err = testrun.New(ctx, t, nil).Exec("ping", "@not_a_handle")
2020-08-06 20:58:47 +03:00
require.Error(t, err)
var tr *testrun.TestRun
2020-08-06 20:58:47 +03:00
th := testh.New(t)
src1, src2 := th.Source(sakila.CSVActor), th.Source(sakila.CSVActorNoHeader)
tr = testrun.New(ctx, t, nil).Add(*src1)
err = tr.Exec("ping", "--csv", src1.Handle)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
checkPingOutputCSV(t, tr, *src1)
2020-08-06 20:58:47 +03:00
tr = testrun.New(ctx, t, nil).Add(*src2)
err = tr.Exec("ping", "--csv", src2.Handle)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
checkPingOutputCSV(t, tr, *src2)
2020-08-06 20:58:47 +03:00
tr = testrun.New(ctx, t, nil).Add(*src1, *src2)
err = tr.Exec("ping", "--csv", src1.Handle, src2.Handle)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
checkPingOutputCSV(t, tr, *src1, *src2)
2020-08-06 20:58:47 +03:00
}
// checkPintOutputCSV reads CSV records from h.out, and verifies
// that there's an appropriate record for each of srcs.
func checkPingOutputCSV(t *testing.T, h *testrun.TestRun, srcs ...source.Source) {
2024-01-27 16:43:17 +03:00
t.Helper()
recs, err := csv.NewReader(h.Out).ReadAll()
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
require.Equal(t, len(srcs), len(recs))
if len(srcs) > 0 {
require.Equal(t, 3, len(recs[0]), "each ping record should have 3 fields, but got %d fields", len(recs[0]))
}
handles := make(map[string]bool)
for _, src := range srcs {
handles[src.Handle] = true
}
for i := 0; i < len(recs); i++ {
recHandle := recs[i][0]
require.True(t, handles[recHandle], "should have handle %s in map", recHandle)
2020-08-06 20:58:47 +03:00
_, err = time.ParseDuration(recs[i][1])
2020-08-06 20:58:47 +03:00
require.NoError(t, err, "should be a valid duration value")
require.Equal(t, "pong", recs[i][2], "error field should be empty")
2020-08-06 20:58:47 +03:00
}
}