sq/drivers/userdriver/userdriver_test.go

174 lines
3.4 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package userdriver_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/cli/config"
"github.com/neilotoole/sq/drivers/userdriver"
"github.com/neilotoole/sq/libsq/core/ioz"
"github.com/neilotoole/sq/libsq/core/stringz"
2020-08-06 20:58:47 +03:00
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/testsrc"
)
func TestDriver(t *testing.T) {
t.Parallel()
testCases := []struct {
handle string
tbl string
wantRecs int
}{
{handle: testsrc.PplUD, tbl: "person", wantRecs: 3},
{handle: testsrc.RSSNYTLocalUD, tbl: "item", wantRecs: 45},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.handle, func(t *testing.T) {
t.Parallel()
th := testh.New(t)
2020-08-06 20:58:47 +03:00
src := th.Source(tc.handle)
grip := th.Open(src)
2020-08-06 20:58:47 +03:00
srcMeta, err := grip.SourceMetadata(th.Context, false)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
require.True(t, stringz.InSlice(srcMeta.TableNames(), tc.tbl))
tblMeta, err := grip.TableMetadata(th.Context, tc.tbl)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
require.Equal(t, tc.tbl, tblMeta.Name)
sink, err := th.QuerySQL(src, nil, "SELECT * FROM "+tc.tbl)
2020-08-06 20:58:47 +03:00
require.NoError(t, err)
require.Equal(t, tc.wantRecs, len(sink.Recs))
})
}
}
func TestValidateDriverDef_KnownGood(t *testing.T) {
t.Parallel()
testCases := []string{testsrc.PathDriverDefPpl, testsrc.PathDriverDefRSS}
for _, defFile := range testCases {
defFile := defFile
t.Run(defFile, func(t *testing.T) {
t.Parallel()
defs := testh.DriverDefsFrom(t, defFile)
for _, def := range defs {
errs := userdriver.ValidateDriverDef(def)
require.Empty(t, errs)
}
})
}
}
func TestValidateDriverDef(t *testing.T) {
t.Parallel()
testCases := []struct {
title string
yml string
wantErrs int
}{
{
title: "driver name is missing, should return with 1 error",
yml: `user_drivers:
- driver:
2020-08-06 20:58:47 +03:00
genre: xml
title: People
selector: /people`,
wantErrs: 1,
},
{
title: "missing genre, title, selector and tables",
yml: `user_drivers:
- driver: ppl
genre:
title:
selector:
2020-08-06 20:58:47 +03:00
tables:`,
wantErrs: 4,
},
{
title: "table name, selector and cols are empty",
yml: `user_drivers:
- driver: ppl
genre: xml
title: People
selector: /people
tables:
- table:
selector:
2020-08-06 20:58:47 +03:00
cols:`,
wantErrs: 3,
},
{
title: "table selector is empty, col name is empty",
yml: `user_drivers:
- driver: ppl
genre: xml
title: People
selector: /people
tables:
- table: person
selector:
primary_key:
- first_name
cols:
- col:
2020-08-06 20:58:47 +03:00
kind: int
- col: first_name
selector: ./firstName
kind: text`,
wantErrs: 2,
},
{
title: "primary key is invalid",
yml: `user_drivers:
- driver: ppl
genre: xml
title: People
selector: /people
tables:
- table: person
selector: /people/person
primary_key:
- not_a_col_name
cols:
- col: person_id
kind: int`,
wantErrs: 1,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
defs := defsFromString(t, tc.yml)
require.Equal(t, 1, len(defs))
def := defs[0]
errs := userdriver.ValidateDriverDef(def)
require.Equal(t, tc.wantErrs, len(errs),
"wanted %d errs but got %d: %v", tc.wantErrs, len(errs), errs)
})
}
}
2024-01-27 16:43:17 +03:00
func defsFromString(t *testing.T, yml string) []*userdriver.DriverDef { //nolint:thelper
2020-08-06 20:58:47 +03:00
ext := &config.Ext{}
require.NoError(t, ioz.UnmarshallYAML([]byte(yml), ext))
2020-08-06 20:58:47 +03:00
return ext.UserDrivers
}