sq/testh/testh_test.go

185 lines
4.0 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package testh_test
import (
2022-12-18 02:11:33 +03:00
"io"
2020-08-06 20:58:47 +03:00
"testing"
"time"
"github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/testh/tutil"
2020-08-06 20:58:47 +03:00
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
_ "github.com/ryboe/q" // keep the q lib around
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/drivers/csv"
"github.com/neilotoole/sq/libsq/core/stringz"
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 TestVal(t *testing.T) {
want := "hello"
2022-12-17 02:34:33 +03:00
var got any
2020-08-06 20:58:47 +03:00
if stringz.Val(nil) != nil {
2020-08-06 20:58:47 +03:00
t.FailNow()
}
2022-12-17 02:34:33 +03:00
var v0 any
if stringz.Val(v0) != nil {
2020-08-06 20:58:47 +03:00
t.FailNow()
}
v1 := want
2022-12-17 02:34:33 +03:00
var v1a any = want
v2 := &v1
2022-12-17 02:34:33 +03:00
var v3 any = &v1
v4 := &v2
v5 := &v4
2020-08-06 20:58:47 +03:00
2022-12-17 02:34:33 +03:00
vals := []any{v1, v1a, v2, v3, v4, v5}
2020-08-06 20:58:47 +03:00
for _, val := range vals {
got = stringz.Val(val)
2020-08-06 20:58:47 +03:00
if got != want {
t.Errorf("expected %T(%v) but got %T(%v)", want, want, got, got)
}
}
slice := []string{"a", "b"}
require.Equal(t, slice, stringz.Val(slice))
require.Equal(t, slice, stringz.Val(&slice))
2020-08-06 20:58:47 +03:00
b := true
require.Equal(t, b, stringz.Val(b))
require.Equal(t, b, stringz.Val(&b))
2020-08-06 20:58:47 +03:00
type structT struct {
f string
}
st1 := structT{f: "hello"}
require.Equal(t, st1, stringz.Val(st1))
require.Equal(t, st1, stringz.Val(&st1))
2020-08-06 20:58:47 +03:00
var c chan int
require.Nil(t, stringz.Val(c))
2020-08-06 20:58:47 +03:00
c = make(chan int, 10)
require.Equal(t, c, stringz.Val(c))
require.Equal(t, c, stringz.Val(&c))
2020-08-06 20:58:47 +03:00
}
func TestCopyRecords(t *testing.T) {
v1, v2, v3, v4, v5, v6 := int64(1), float64(1.1), true, "hello", []byte("hello"), time.Unix(0, 0)
2020-08-06 20:58:47 +03:00
testCases := map[string][]record.Record{
2020-08-06 20:58:47 +03:00
"nil": nil,
"empty": {},
"vals": {
{nil, &v1, &v2, &v3, &v4, &v5, &v6},
// {nil, &v1, &v2, &v3, &v4, &v5, &v6},
},
}
for name, recs := range testCases {
name, recs := name, recs
t.Run(name, func(t *testing.T) {
recs2 := testh.CopyRecords(recs)
require.True(t, len(recs) == len(recs2))
if recs == nil {
require.True(t, recs2 == nil)
return
}
for i := range recs {
require.True(t, len(recs[i]) == len(recs2[i]))
for j := range recs[i] {
if recs[i][j] == nil {
require.True(t, recs2[i][j] == nil)
continue
}
require.False(t, recs[i][j] == recs2[i][j],
"pointer values should not be equal: %#v --> %#v", recs[i][j], recs2[i][j])
val1, val2 := stringz.Val(recs[i][j]), stringz.Val(recs2[i][j])
2020-08-06 20:58:47 +03:00
require.Equal(t, val1, val2,
"dereferenced values should be equal: %#v --> %#v", val1, val2)
}
}
})
}
}
func TestRecordsFromTbl(t *testing.T) {
recMeta1, recs1 := testh.RecordsFromTbl(t, sakila.SL3, sakila.TblActor)
require.Equal(t, sakila.TblActorColKinds(), recMeta1.Kinds())
2020-08-06 20:58:47 +03:00
recs1[0][0] = t.Name()
recMeta2, recs2 := testh.RecordsFromTbl(t, sakila.SL3, sakila.TblActor)
require.False(t, &recMeta1 == &recMeta2, "should be distinct copies")
require.False(t, &recs1 == &recs2, "should be distinct copies")
require.NotEqual(t, recs1[0][0], recs2[0][0], "recs2 should not have the mutated value from recs1")
}
func TestHelper_Files(t *testing.T) {
fpath := "drivers/csv/testdata/person.csv"
wantBytes := proj.ReadFile(fpath)
src := &source.Source{
Handle: "@test_" + stringz.Uniq8(),
Type: csv.TypeCSV,
Location: proj.Abs(fpath),
}
th := testh.New(t)
fs := th.Files()
typ, err := fs.DriverType(th.Context, src.Location)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
require.Equal(t, src.Type, typ)
g, _ := errgroup.WithContext(th.Context)
2020-08-06 20:58:47 +03:00
for i := 0; i < 1000; i++ {
g.Go(func() error {
r, fErr := fs.Open(src)
require.NoError(t, fErr)
2020-08-06 20:58:47 +03:00
defer func() { require.NoError(t, r.Close()) }()
b, fErr := io.ReadAll(r)
require.NoError(t, fErr)
2020-08-06 20:58:47 +03:00
require.Equal(t, wantBytes, b)
return nil
})
}
err = g.Wait()
require.NoError(t, err)
}
func TestTName(t *testing.T) {
testCases := []struct {
2022-12-17 02:34:33 +03:00
a []any
2020-08-06 20:58:47 +03:00
want string
}{
2022-12-17 02:34:33 +03:00
{a: []any{}, want: "empty"},
{a: []any{"test", 1}, want: "test_1"},
{a: []any{"/file/path/name"}, want: "_file_path_name"},
2020-08-06 20:58:47 +03:00
}
for _, tc := range testCases {
got := tutil.Name(tc.a...)
2020-08-06 20:58:47 +03:00
require.Equal(t, tc.want, got)
}
}