2023-04-01 12:48:24 +03:00
|
|
|
package libsq_test
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
import (
|
2023-08-12 21:54:14 +03:00
|
|
|
"slices"
|
2023-04-01 11:38:32 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-06-17 07:54:25 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-17 07:54:25 +03:00
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
"github.com/neilotoole/sq/libsq"
|
2023-04-01 11:38:32 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
2023-04-01 11:38:32 +03:00
|
|
|
)
|
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
// driverMap is a map of source.DriverType to a string.
|
|
|
|
// It is used to specify a string for a specific driver.
|
|
|
|
type driverMap map[source.DriverType]string
|
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
// queryTestCase is used to test libsq's rendering of SLQ into SQL.
|
|
|
|
// It is probably the most important test struct in the codebase.
|
|
|
|
type queryTestCase struct {
|
|
|
|
// name is the test name
|
|
|
|
name string
|
|
|
|
|
|
|
|
// skip indicates the test should be skipped. Useful for test cases
|
2023-07-03 18:34:19 +03:00
|
|
|
// that we want to implement in the future.
|
2023-04-01 11:38:32 +03:00
|
|
|
skip bool
|
|
|
|
|
|
|
|
// in is the SLQ input. The "@sakila" handle is replaced
|
|
|
|
// with the source's actual handle before an individual
|
|
|
|
// test cases is executed.
|
|
|
|
in string
|
|
|
|
|
|
|
|
// args contains args for the "--args a b" mechanism. Typically empty.
|
|
|
|
args map[string]string
|
|
|
|
|
|
|
|
// wantErr indicates that an error is expected
|
|
|
|
wantErr bool
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// wantSQL is the desired SQL. If empty, the returned SQL is
|
|
|
|
// not tested (but is still executed).
|
2023-04-01 11:38:32 +03:00
|
|
|
wantSQL string
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
// beforeRun is called just before the query is executed. It's an
|
|
|
|
// opportunity to modify the query context before execution.
|
|
|
|
beforeRun func(tc queryTestCase, th *testh.Helper, qc *libsq.QueryContext)
|
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
// override allows an alternative "wantSQL" for a specific driver type.
|
|
|
|
// For example, MySQL uses backtick as the quote char, so it needs
|
|
|
|
// a separate wantSQL string.
|
2023-06-18 09:05:09 +03:00
|
|
|
override driverMap
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
// onlyFor indicates that this test should only run on sources of
|
|
|
|
// the specified types. When empty, the test is executed on all types.
|
2023-04-22 06:36:32 +03:00
|
|
|
onlyFor []source.DriverType
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
// skipExec indicates that the resulting query should not be executed.
|
|
|
|
// Some SLQ inputs we wantSQL to test don't actually have corresponding
|
|
|
|
// data in the Sakila datasets.
|
|
|
|
skipExec bool
|
|
|
|
|
2023-06-17 07:54:25 +03:00
|
|
|
// wantRecCount is the number of expected records from actually executing
|
2023-04-01 11:38:32 +03:00
|
|
|
// the query. This is N/A if skipExec is true.
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount int
|
|
|
|
|
|
|
|
// sinkTest, if non-nil, is executed against the sink returned
|
|
|
|
// from the query execution.
|
|
|
|
sinkFns []SinkTestFunc
|
2023-07-03 18:34:19 +03:00
|
|
|
|
|
|
|
// repeatReplace, when non-empty, instructs the test runner to repeat
|
|
|
|
// the test, replacing in the input string all occurrences of the first
|
|
|
|
// slice element with each subsequent element. For example, given:
|
|
|
|
//
|
|
|
|
// in: ".actor | join(.address, .address_id):
|
|
|
|
// repeatReplace: []string{"join", "inner_join"}
|
|
|
|
//
|
|
|
|
// The test will run once using "join" (the original query) and then another
|
|
|
|
// time with ".actor | inner_join(.address, .address_id)".
|
|
|
|
//
|
|
|
|
// Thus the field must be empty, or have at least two elements.
|
|
|
|
repeatReplace []string
|
2023-04-01 11:38:32 +03:00
|
|
|
}
|
|
|
|
|
2023-06-17 07:54:25 +03:00
|
|
|
// SinkTestFunc is a function that tests a sink.
|
|
|
|
type SinkTestFunc func(t testing.TB, sink *testh.RecordSink)
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// execQueryTestCase is called by test functions to execute
|
|
|
|
// a queryTestCase.
|
2023-04-01 11:38:32 +03:00
|
|
|
func execQueryTestCase(t *testing.T, tc queryTestCase) {
|
|
|
|
if tc.skip {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
t.Helper()
|
2023-06-17 07:54:25 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
switch len(tc.repeatReplace) {
|
|
|
|
case 0:
|
|
|
|
doExecQueryTestCase(t, tc)
|
|
|
|
return
|
|
|
|
case 1:
|
|
|
|
t.Fatalf("queryTestCase.repeatReplace must be empty or have at least two elements")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
subTests := make([]queryTestCase, len(tc.repeatReplace))
|
|
|
|
for i := range tc.repeatReplace {
|
|
|
|
subTests[i] = tc
|
2023-11-19 03:05:48 +03:00
|
|
|
subTests[i].name = tutil.Name(tc.repeatReplace[i])
|
2023-07-03 18:34:19 +03:00
|
|
|
if i == 0 {
|
|
|
|
// No need for replacement on first item, it's the original.
|
|
|
|
continue
|
|
|
|
}
|
2023-06-17 07:54:25 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
subTests[i].in = strings.ReplaceAll(
|
|
|
|
subTests[i].in,
|
|
|
|
tc.repeatReplace[0],
|
|
|
|
tc.repeatReplace[i],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, st := range subTests {
|
|
|
|
st := st
|
|
|
|
t.Run(st.name, func(t *testing.T) {
|
|
|
|
doExecQueryTestCase(t, st)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doExecQueryTestCase is called by execQueryTestCase to
|
|
|
|
// execute a queryTestCase. This function should not be called
|
|
|
|
// directly by test functions. The query is executed for each
|
|
|
|
// of the sources in sakila.SQLLatest. To do so, the first
|
|
|
|
// occurrence of the string "@sakila." is replaced with the
|
|
|
|
// actual handle of each source. E.g:
|
|
|
|
//
|
|
|
|
// "@sakila | .actor" --> "@sakila_pg12 | .actor"
|
|
|
|
func doExecQueryTestCase(t *testing.T, tc queryTestCase) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
coll := testh.New(t).NewCollection(sakila.SQLLatest()...)
|
2023-06-17 07:54:25 +03:00
|
|
|
for _, src := range coll.Sources() {
|
2023-04-01 11:38:32 +03:00
|
|
|
src := src
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
if len(tc.onlyFor) > 0 && !slices.Contains(tc.onlyFor, src.Type) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
t.Run(string(src.Type), func(t *testing.T) {
|
2023-04-07 11:00:49 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
in := strings.Replace(tc.in, "@sakila", src.Handle, 1)
|
2023-07-03 18:34:19 +03:00
|
|
|
t.Logf("QUERY:\n\n%s\n\n", in)
|
2023-04-01 11:38:32 +03:00
|
|
|
want := tc.wantSQL
|
|
|
|
if overrideWant, ok := tc.override[src.Type]; ok {
|
|
|
|
want = overrideWant
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
_, err := coll.SetActive(src.Handle, false)
|
2023-04-01 11:38:32 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
th := testh.New(t)
|
2023-11-19 05:21:14 +03:00
|
|
|
pools := th.Pools()
|
2023-04-01 11:38:32 +03:00
|
|
|
|
2023-04-01 12:48:24 +03:00
|
|
|
qc := &libsq.QueryContext{
|
2023-11-19 05:21:14 +03:00
|
|
|
Collection: coll,
|
|
|
|
PoolOpener: pools,
|
|
|
|
JoinPoolOpener: pools,
|
|
|
|
ScratchPoolOpener: pools,
|
|
|
|
Args: tc.args,
|
2023-04-01 12:48:24 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:48 +03:00
|
|
|
if tc.beforeRun != nil {
|
|
|
|
tc.beforeRun(tc, th, qc)
|
|
|
|
}
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
gotSQL, gotErr := libsq.SLQ2SQL(th.Context, qc, in)
|
2023-04-01 11:38:32 +03:00
|
|
|
if tc.wantErr {
|
2023-07-03 18:34:19 +03:00
|
|
|
assert.Error(t, gotErr)
|
|
|
|
t.Logf("ERROR: %v", gotErr)
|
2023-04-01 11:38:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
t.Logf("SQL:\n\n%s\n\n", gotSQL)
|
2023-04-01 11:38:32 +03:00
|
|
|
require.NoError(t, gotErr)
|
2023-07-03 18:34:19 +03:00
|
|
|
|
|
|
|
if want != "" {
|
|
|
|
require.Equal(t, want, gotSQL)
|
|
|
|
}
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
if tc.skipExec {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
sink, err := th.QuerySLQ(in, tc.args)
|
2023-04-01 11:38:32 +03:00
|
|
|
require.NoError(t, err)
|
2023-06-17 07:54:25 +03:00
|
|
|
require.Equal(t, tc.wantRecCount, len(sink.Recs))
|
|
|
|
|
|
|
|
for i := range tc.sinkFns {
|
|
|
|
tc.sinkFns[i](t, sink)
|
|
|
|
}
|
2023-04-01 11:38:32 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 07:54:25 +03:00
|
|
|
|
|
|
|
// assertSinkColValue returns a SinkTestFunc that asserts that
|
|
|
|
// the column colIndex of each record matches val.
|
|
|
|
func assertSinkColValue(colIndex int, val any) SinkTestFunc {
|
|
|
|
return func(t testing.TB, sink *testh.RecordSink) {
|
|
|
|
for rowi, rec := range sink.Recs {
|
|
|
|
assert.Equal(t, val, rec[colIndex], "record[%d:%d] (%s)", rowi, colIndex, sink.RecMeta[colIndex].Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertSinkColValue returns a SinkTestFunc that asserts that
|
|
|
|
// the name of column colIndex matches name.
|
2023-06-25 19:29:24 +03:00
|
|
|
func assertSinkColName(colIndex int, name string) SinkTestFunc { //nolint:unparam
|
2023-06-17 07:54:25 +03:00
|
|
|
return func(t testing.TB, sink *testh.RecordSink) {
|
|
|
|
assert.Equal(t, name, sink.RecMeta[colIndex].Name(), "column %d", colIndex)
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 18:34:19 +03:00
|
|
|
|
2023-07-27 07:19:11 +03:00
|
|
|
// assertSinkColMungedNames returns a SinkTestFunc that matches col names.
|
|
|
|
func assertSinkColMungedNames(names ...string) SinkTestFunc {
|
2023-07-03 18:34:19 +03:00
|
|
|
return func(t testing.TB, sink *testh.RecordSink) {
|
2023-07-27 07:19:11 +03:00
|
|
|
gotNames := sink.RecMeta.MungedNames()
|
2023-07-03 18:34:19 +03:00
|
|
|
assert.Equal(t, names, gotNames)
|
|
|
|
}
|
|
|
|
}
|