2020-08-06 20:58:47 +03:00
|
|
|
package libsq_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/sqlz"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestQuerySQL_Smoke is a smoke test of testh.QuerySQL.
|
|
|
|
func TestQuerySQL_Smoke(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
wantActorFieldTypes := []reflect.Type{
|
2023-05-27 16:57:07 +03:00
|
|
|
sqlz.RTypeInt64,
|
|
|
|
sqlz.RTypeString,
|
|
|
|
sqlz.RTypeString,
|
|
|
|
sqlz.RTypeTime,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
handle string
|
|
|
|
fieldTypes []reflect.Type
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
handle: sakila.SL3,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.My,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.Pg,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
handle: sakila.MS,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
|
|
|
},
|
|
|
|
{
|
2023-04-09 17:44:27 +03:00
|
|
|
handle: sakila.CSVActor,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
2020-08-06 20:58:47 +03:00
|
|
|
},
|
|
|
|
{
|
2020-11-25 08:12:15 +03:00
|
|
|
handle: sakila.XLSX,
|
|
|
|
fieldTypes: wantActorFieldTypes,
|
2020-08-06 20:58:47 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.handle, func(t *testing.T) {
|
2022-12-17 05:09:49 +03:00
|
|
|
tutil.SkipShort(t, tc.handle == sakila.XLSX)
|
2020-08-06 20:58:47 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-04-19 17:08:26 +03:00
|
|
|
th := testh.New(t, testh.OptLongOpen())
|
2020-08-06 20:58:47 +03:00
|
|
|
src := th.Source(tc.handle)
|
|
|
|
|
|
|
|
tblName := sakila.TblActor
|
|
|
|
if th.IsMonotable(src) {
|
|
|
|
tblName = source.MonotableName
|
|
|
|
}
|
|
|
|
|
|
|
|
sink, err := th.QuerySQL(src, "SELECT * FROM "+tblName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sakila.TblActorCount, len(sink.Recs))
|
|
|
|
require.Equal(t, len(tc.fieldTypes), len(sink.Recs[0]))
|
|
|
|
for i := range sink.Recs[0] {
|
2023-04-09 17:44:27 +03:00
|
|
|
require.Equal(t,
|
|
|
|
tc.fieldTypes[i].String(),
|
|
|
|
reflect.TypeOf(sink.Recs[0][i]).String(),
|
|
|
|
"expected field[%d] {%s} to have type %s but got %s",
|
|
|
|
i,
|
|
|
|
sink.RecMeta[i].Name(),
|
|
|
|
tc.fieldTypes[i].String(),
|
|
|
|
reflect.TypeOf(sink.Recs[0][i]).String(),
|
|
|
|
)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 17:18:56 +03:00
|
|
|
func TestQuerySQL_Count(t *testing.T) { //nolint:tparallel
|
2020-08-09 17:40:46 +03:00
|
|
|
testCases := sakila.SQLAll()
|
2020-08-06 20:58:47 +03:00
|
|
|
for _, handle := range testCases {
|
|
|
|
handle := handle
|
2020-08-09 17:40:46 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
t.Run(handle, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
th := testh.New(t)
|
|
|
|
src := th.Source(handle)
|
|
|
|
|
|
|
|
sink, err := th.QuerySQL(src, "SELECT * FROM "+sakila.TblActor)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sakila.TblActorCount, len(sink.Recs))
|
|
|
|
|
|
|
|
sink, err = th.QuerySQL(src, "SELECT COUNT(*) FROM "+sakila.TblActor)
|
|
|
|
require.NoError(t, err)
|
2023-05-27 16:57:07 +03:00
|
|
|
count, ok := sink.Recs[0][0].(int64)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.True(t, ok)
|
2023-05-27 16:57:07 +03:00
|
|
|
require.Equal(t, int64(sakila.TblActorCount), count)
|
2020-08-06 20:58:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|