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"
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
|
|
|
|
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"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2022-12-23 21:09:06 +03:00
|
|
|
_ "github.com/ryboe/q" // keep the q lib around
|
2020-10-20 18:05:43 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/csv"
|
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/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
|
|
|
|
2023-04-09 17:44:27 +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
|
2023-04-09 17:44:27 +03:00
|
|
|
if stringz.Val(v0) != nil {
|
2020-08-06 20:58:47 +03:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2022-12-18 11:35:59 +03:00
|
|
|
v1 := want
|
2022-12-17 02:34:33 +03:00
|
|
|
var v1a any = want
|
2022-12-18 11:35:59 +03:00
|
|
|
v2 := &v1
|
2022-12-17 02:34:33 +03:00
|
|
|
var v3 any = &v1
|
2022-12-18 11:35:59 +03:00
|
|
|
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 {
|
2023-04-09 17:44:27 +03:00
|
|
|
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"}
|
2023-04-09 17:44:27 +03:00
|
|
|
require.Equal(t, slice, stringz.Val(slice))
|
|
|
|
require.Equal(t, slice, stringz.Val(&slice))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
b := true
|
2023-04-09 17:44:27 +03:00
|
|
|
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"}
|
2023-04-09 17:44:27 +03:00
|
|
|
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
|
2023-04-09 17:44:27 +03:00
|
|
|
require.Nil(t, stringz.Val(c))
|
2020-08-06 20:58:47 +03:00
|
|
|
c = make(chan int, 10)
|
2023-04-09 17:44:27 +03:00
|
|
|
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) {
|
2022-12-18 11:35:59 +03:00
|
|
|
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
|
|
|
|
2023-05-22 18:08:14 +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])
|
|
|
|
|
2023-04-09 17:44:27 +03:00
|
|
|
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)
|
2020-08-10 18:16:44 +03:00
|
|
|
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()
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
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)
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
g, _ := errgroup.WithContext(th.Context)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
g.Go(func() error {
|
2022-12-18 10:18:35 +03:00
|
|
|
r, fErr := fs.Open(src)
|
|
|
|
require.NoError(t, fErr)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-08-10 18:16:44 +03:00
|
|
|
defer func() { require.NoError(t, r.Close()) }()
|
2020-08-09 00:23:30 +03:00
|
|
|
|
2022-12-18 10:18:35 +03:00
|
|
|
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 {
|
2022-12-17 05:09:49 +03:00
|
|
|
got := tutil.Name(tc.a...)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, tc.want, got)
|
|
|
|
}
|
|
|
|
}
|