sq/libsq/engine_test.go
Neil O'Toole b06b631e76
Bug #87: generated SQL should always quote table and column names in join statement (#89)
* BaseFragmentBuilder now quotes table and col names for joins

* Refactored libsq.engine so that the SQL generated from SLQ input can be tested

* Deleted dead code; additional comments
2021-03-07 23:27:35 -07:00

47 lines
1.0 KiB
Go

package libsq_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq"
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/sakila"
)
func TestSLQ2SQL(t *testing.T) {
testCases := []struct {
handles []string
slq string
wantSQL string
wantErr bool
}{
// Obviously we could use about 1,000 additional test cases.
{
handles: []string{sakila.SL3},
slq: `@sakila_sl3 | .actor, .film_actor | join(.film_actor.actor_id == .actor.actor_id)`,
wantSQL: `SELECT * FROM "actor" INNER JOIN "film_actor" ON "film_actor"."actor_id" = "actor"."actor_id"`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(testh.Name(tc.slq), func(t *testing.T) {
th := testh.New(t)
srcs := th.NewSourceSet(tc.handles...)
gotSQL, gotErr := libsq.EngineSLQ2SQL(th.Context, th.Log, th.Databases(), th.Databases(), srcs, tc.slq)
if tc.wantErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
require.Equal(t, tc.wantSQL, gotSQL)
})
}
}