2020-08-06 20:58:47 +03:00
|
|
|
package csv_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/timez"
|
|
|
|
|
2023-04-10 04:29:13 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSmoke(t *testing.T) {
|
2022-12-18 10:18:35 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
testCases := []string{sakila.CSVActor, sakila.TSVActor, sakila.CSVActorHTTP}
|
|
|
|
|
|
|
|
for _, handle := range testCases {
|
|
|
|
handle := handle
|
|
|
|
|
|
|
|
t.Run(handle, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(sakila.CSVActor)
|
|
|
|
|
|
|
|
sink, err := th.QuerySQL(src, "SELECT * FROM data")
|
|
|
|
require.NoError(t, err)
|
2020-08-10 18:16:44 +03:00
|
|
|
require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta))
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Equal(t, sakila.TblActorCount, len(sink.Recs))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuerySQL_Count(t *testing.T) {
|
2022-12-18 10:18:35 +03:00
|
|
|
t.Parallel()
|
2020-08-12 21:24:01 +03:00
|
|
|
|
2022-12-18 10:18:35 +03:00
|
|
|
testCases := []string{sakila.CSVActor, sakila.TSVActor}
|
2020-08-06 20:58:47 +03:00
|
|
|
for _, handle := range testCases {
|
|
|
|
handle := handle
|
|
|
|
|
|
|
|
t.Run(handle, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(handle)
|
|
|
|
|
|
|
|
sink, err := th.QuerySQL(src, "SELECT * FROM data")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sakila.TblActorCount, len(sink.Recs))
|
|
|
|
|
|
|
|
sink, err = th.QuerySQL(src, "SELECT COUNT(*) FROM data")
|
|
|
|
require.NoError(t, err)
|
2023-04-10 04:29:13 +03:00
|
|
|
require.EqualValues(t, lo.ToPtr[int64](sakila.TblActorCount), sink.Result())
|
2020-08-06 20:58:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 04:29:13 +03:00
|
|
|
|
|
|
|
func TestEmptyAsNull(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
sink, err := th.QuerySLQ(sakila.CSVAddress+`| .data | .[0:1]`, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(sink.Recs))
|
|
|
|
|
|
|
|
require.Equal(t, stringz.Strings(sakila.TblAddressColKinds()), stringz.Strings(sink.RecMeta.Kinds()))
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
ts, err := timez.ParseTimestampUTC("2020-02-15T06:59:28Z")
|
2023-04-10 04:29:13 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rec0 := sink.Recs[0]
|
|
|
|
want := []any{
|
|
|
|
lo.ToPtr[int64](1),
|
|
|
|
lo.ToPtr("47 MySakila Drive"),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
lo.ToPtr[int64](300),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
lo.ToPtr(ts),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range want {
|
|
|
|
require.EqualValues(t, want[i], rec0[i], "field [%d]", i)
|
|
|
|
}
|
|
|
|
}
|