From 3e10868a2975e88d0a09a09aaff4740a859acc7a Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Mon, 10 Aug 2020 09:16:44 -0600 Subject: [PATCH] Switched pkg sakila "const" slices to functions (#57) * replaced sakila source slices with funcs * missed some pkg sakila slices to refactor --- cli/cmd_inspect_test.go | 2 +- cli/cmd_slq_test.go | 2 +- cli/cmd_sql_test.go | 2 +- cli/output/adapter_test.go | 4 +-- drivers/csv/csv_test.go | 2 +- drivers/mysql/mysql_test.go | 2 +- drivers/postgres/postgres_test.go | 4 +-- drivers/sqlite3/metadata_test.go | 6 ++--- drivers/sqlserver/sqlserver_test.go | 2 +- drivers/xlsx/xlsx_test.go | 4 +-- libsq/driver/driver_test.go | 6 ++--- testh/sakila/sakila.go | 42 +++++++++++++++++++++-------- testh/testh_test.go | 4 +-- 13 files changed, 51 insertions(+), 31 deletions(-) diff --git a/cli/cmd_inspect_test.go b/cli/cmd_inspect_test.go index 8686b613..da8839c9 100644 --- a/cli/cmd_inspect_test.go +++ b/cli/cmd_inspect_test.go @@ -34,7 +34,7 @@ func TestCmdInspect(t *testing.T) { require.Equal(t, sqlite3.Type, md.SourceType) require.Equal(t, sakila.SL3, md.Handle) require.Equal(t, src.Location, md.Location) - require.Equal(t, sakila.AllTbls, md.TableNames()) + require.Equal(t, sakila.AllTbls(), md.TableNames()) // Try one more source for good measure ru = newRun(t) diff --git a/cli/cmd_slq_test.go b/cli/cmd_slq_test.go index 665f96d4..4094fd4a 100644 --- a/cli/cmd_slq_test.go +++ b/cli/cmd_slq_test.go @@ -50,7 +50,7 @@ func TestCmdSLQ_Insert(t *testing.T) { } insertTo := fmt.Sprintf("%s.%s", destSrc.Handle, actualDestTbl) - cols := stringz.PrefixSlice(sakila.TblActorCols, ".") + cols := stringz.PrefixSlice(sakila.TblActorCols(), ".") query := fmt.Sprintf("%s.%s | %s", originSrc.Handle, srcTbl, strings.Join(cols, ", ")) err := ru.exec("slq", "--insert="+insertTo, query) diff --git a/cli/cmd_sql_test.go b/cli/cmd_sql_test.go index 085c4861..43cb1fa9 100644 --- a/cli/cmd_sql_test.go +++ b/cli/cmd_sql_test.go @@ -49,7 +49,7 @@ func TestCmdSQL_Insert(t *testing.T) { } insertTo := fmt.Sprintf("%s.%s", destSrc.Handle, actualDestTbl) - query := fmt.Sprintf("SELECT %s FROM %s", strings.Join(sakila.TblActorCols, ", "), originTbl) + query := fmt.Sprintf("SELECT %s FROM %s", strings.Join(sakila.TblActorCols(), ", "), originTbl) err := ru.exec("sql", "--insert="+insertTo, query) require.NoError(t, err) diff --git a/cli/output/adapter_test.go b/cli/output/adapter_test.go index c45c95bf..6ce12537 100644 --- a/cli/output/adapter_test.go +++ b/cli/output/adapter_test.go @@ -31,13 +31,13 @@ func TestRecordWriterAdapter(t *testing.T) { handle: sakila.SL3, sqlQuery: "SELECT * FROM actor", wantRowCount: sakila.TblActorCount, - wantColCount: len(sakila.TblActorCols), + wantColCount: len(sakila.TblActorCols()), }, { handle: testsrc.CSVPersonBig, sqlQuery: "SELECT * FROM data", wantRowCount: 10000, - wantColCount: len(sakila.TblActorCols), + wantColCount: len(sakila.TblActorCols()), }, } diff --git a/drivers/csv/csv_test.go b/drivers/csv/csv_test.go index 5ceb3622..6fba7e50 100644 --- a/drivers/csv/csv_test.go +++ b/drivers/csv/csv_test.go @@ -24,7 +24,7 @@ func TestSmoke(t *testing.T) { sink, err := th.QuerySQL(src, "SELECT * FROM data") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) }) } diff --git a/drivers/mysql/mysql_test.go b/drivers/mysql/mysql_test.go index e6c7a80c..07a95e8c 100644 --- a/drivers/mysql/mysql_test.go +++ b/drivers/mysql/mysql_test.go @@ -23,7 +23,7 @@ func TestSmoke(t *testing.T) { th, src, _, _ := testh.NewWith(t, handle) sink, err := th.QuerySQL(src, "SELECT * FROM actor") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) }) } diff --git a/drivers/postgres/postgres_test.go b/drivers/postgres/postgres_test.go index df192f8a..59a368ba 100644 --- a/drivers/postgres/postgres_test.go +++ b/drivers/postgres/postgres_test.go @@ -29,7 +29,7 @@ func TestSmoke(t *testing.T) { th, src, _, _ := testh.NewWith(t, handle) sink, err := th.QuerySQL(src, "SELECT * FROM actor") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) }) } @@ -135,7 +135,7 @@ func TestGetTableColumnNames(t *testing.T) { colNames, err := postgres.GetTableColumnNames(th.Context, th.Log, th.Open(src).DB(), sakila.TblActor) require.NoError(t, err) - require.Equal(t, sakila.TblActorCols, colNames) + require.Equal(t, sakila.TblActorCols(), colNames) }) } } diff --git a/drivers/sqlite3/metadata_test.go b/drivers/sqlite3/metadata_test.go index 5530f698..cef1339e 100644 --- a/drivers/sqlite3/metadata_test.go +++ b/drivers/sqlite3/metadata_test.go @@ -73,7 +73,7 @@ func TestRecordMetadata(t *testing.T) { { tbl: sakila.TblActor, rowCount: sakila.TblActorCount, - colNames: sakila.TblActorCols, + colNames: sakila.TblActorCols(), colKinds: []sqlz.Kind{sqlz.KindInt, sqlz.KindText, sqlz.KindText, sqlz.KindDatetime}, scanTypes: []reflect.Type{sqlz.RTypeNullInt64, sqlz.RTypeNullString, sqlz.RTypeNullString, sqlz.RTypeNullTime}, colsMeta: []*source.ColMetadata{ @@ -86,7 +86,7 @@ func TestRecordMetadata(t *testing.T) { { tbl: sakila.TblFilmActor, rowCount: sakila.TblFilmActorCount, - colNames: sakila.TblFilmActorCols, + colNames: sakila.TblFilmActorCols(), colKinds: []sqlz.Kind{sqlz.KindInt, sqlz.KindInt, sqlz.KindDatetime}, scanTypes: []reflect.Type{sqlz.RTypeNullInt64, sqlz.RTypeNullInt64, sqlz.RTypeNullTime}, colsMeta: []*source.ColMetadata{ @@ -98,7 +98,7 @@ func TestRecordMetadata(t *testing.T) { { tbl: sakila.TblPayment, rowCount: sakila.TblPaymentCount, - colNames: sakila.TblPaymentCols, + colNames: sakila.TblPaymentCols(), colKinds: []sqlz.Kind{sqlz.KindInt, sqlz.KindInt, sqlz.KindInt, sqlz.KindInt, sqlz.KindDecimal, sqlz.KindDatetime, sqlz.KindDatetime}, scanTypes: []reflect.Type{sqlz.RTypeNullInt64, sqlz.RTypeNullInt64, sqlz.RTypeNullInt64, sqlz.RTypeNullInt64, sqlz.RTypeNullString, sqlz.RTypeNullTime, sqlz.RTypeNullTime}, colsMeta: []*source.ColMetadata{ diff --git a/drivers/sqlserver/sqlserver_test.go b/drivers/sqlserver/sqlserver_test.go index cda7a0e5..dce2df20 100644 --- a/drivers/sqlserver/sqlserver_test.go +++ b/drivers/sqlserver/sqlserver_test.go @@ -27,7 +27,7 @@ func TestSmoke(t *testing.T) { th, src, _, _ := testh.NewWith(t, handle) sink, err := th.QuerySQL(src, "SELECT * FROM actor") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) }) } diff --git a/drivers/xlsx/xlsx_test.go b/drivers/xlsx/xlsx_test.go index 2d82f893..91377bab 100644 --- a/drivers/xlsx/xlsx_test.go +++ b/drivers/xlsx/xlsx_test.go @@ -21,7 +21,7 @@ func Test_Smoke_Subset(t *testing.T) { sink, err := th.QuerySQL(src, "SELECT * FROM actor") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) } @@ -34,7 +34,7 @@ func Test_Smoke_Full(t *testing.T) { sink, err := th.QuerySQL(src, "SELECT * FROM actor") require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(sink.RecMeta)) + require.Equal(t, len(sakila.TblActorCols()), len(sink.RecMeta)) require.Equal(t, sakila.TblActorCount, len(sink.Recs)) } diff --git a/libsq/driver/driver_test.go b/libsq/driver/driver_test.go index 99087bda..86da0dd5 100644 --- a/libsq/driver/driver_test.go +++ b/libsq/driver/driver_test.go @@ -148,13 +148,13 @@ func TestDriver_TableColumnTypes(t *testing.T) { // as if the query was (SELECT * FROM actualTblName) colTypes, err := drvr.TableColumnTypes(th.Context, db, actualTblName, nil) require.NoError(t, err) - require.Equal(t, len(sakila.TblActorCols), len(colTypes)) + require.Equal(t, len(sakila.TblActorCols()), len(colTypes)) for i := range colTypes { - require.Equal(t, sakila.TblActorCols[i], colTypes[i].Name()) + require.Equal(t, sakila.TblActorCols()[i], colTypes[i].Name()) } // Try again, but requesting specific col names - wantColNames := []string{sakila.TblActorCols[0], sakila.TblActorCols[2]} + wantColNames := []string{sakila.TblActorCols()[0], sakila.TblActorCols()[2]} colTypes, err = drvr.TableColumnTypes(th.Context, db, actualTblName, wantColNames) require.NoError(t, err) require.Equal(t, len(wantColNames), len(colTypes)) diff --git a/testh/sakila/sakila.go b/testh/sakila/sakila.go index 1003b9c6..d7c394e1 100644 --- a/testh/sakila/sakila.go +++ b/testh/sakila/sakila.go @@ -87,18 +87,38 @@ const ( ) // Facts regarding the sakila database. -var ( - TblActorCols = []string{"actor_id", "first_name", "last_name", "last_update"} - TblActorColKinds = []sqlz.Kind{sqlz.KindInt, sqlz.KindText, sqlz.KindText, sqlz.KindDatetime} - TblFilmActorCols = []string{"actor_id", "film_id", "last_update"} - TblPaymentCols = []string{"payment_id", "customer_id", "staff_id", "rental_id", "amount", "payment_date", "last_update"} - AllTbls = []string{"actor", "address", "category", "city", "country", "customer", "film", "film_actor", "film_category", "film_text", "inventory", "language", "payment", "rental", "staff", "store"} - // AllTblsExceptFilmText exists because our current postgres image is different - // from the others in that it doesn't have the film_text table. - // FIXME: delete AllTblsExceptFilmText when postgres image is updated to include film_text. - AllTblsExceptFilmText = []string{"actor", "address", "category", "city", "country", "customer", "film", "film_actor", "film_category", "inventory", "language", "payment", "rental", "staff", "store"} -) +// TblActorCols returns table "actor" column names. +func TblActorCols() []string { + return []string{"actor_id", "first_name", "last_name", "last_update"} +} + +// TblActorColKinds returns the kinds of the columns from table actor. +func TblActorColKinds() []sqlz.Kind { + return []sqlz.Kind{sqlz.KindInt, sqlz.KindText, sqlz.KindText, sqlz.KindDatetime} +} + +// TblFilmActorCols returns table "film" column names. +func TblFilmActorCols() []string { + return []string{"actor_id", "film_id", "last_update"} +} + +// TblPaymentCols returns table "payment" column names. +func TblPaymentCols() []string { + return []string{"payment_id", "customer_id", "staff_id", "rental_id", "amount", "payment_date", "last_update"} +} + +// AllTbls returns all table names. +func AllTbls() []string { + return []string{"actor", "address", "category", "city", "country", "customer", "film", "film_actor", "film_category", "film_text", "inventory", "language", "payment", "rental", "staff", "store"} +} + +// AllTblsExceptFilmText exists because our current postgres image is different +// from the others in that it doesn't have the film_text table. +func AllTblsExceptFilmText() []string { + // TODO: delete AllTblsExceptFilmText when postgres image is updated to include film_text. + return []string{"actor", "address", "category", "city", "country", "customer", "film", "film_actor", "film_category", "inventory", "language", "payment", "rental", "staff", "store"} +} // URLs for sakila resources. const ( diff --git a/testh/testh_test.go b/testh/testh_test.go index 35dae4c1..17dd303b 100644 --- a/testh/testh_test.go +++ b/testh/testh_test.go @@ -115,7 +115,7 @@ func TestCopyRecords(t *testing.T) { func TestRecordsFromTbl(t *testing.T) { recMeta1, recs1 := testh.RecordsFromTbl(t, sakila.SL3, sakila.TblActor) - require.Equal(t, sakila.TblActorColKinds, recMeta1.Kinds()) + require.Equal(t, sakila.TblActorColKinds(), recMeta1.Kinds()) recs1[0][0] = t.Name() @@ -149,7 +149,7 @@ func TestHelper_Files(t *testing.T) { r, err := fs.NewReader(gctx, src) require.NoError(t, err) - defer func() {require.NoError(t, r.Close())}() + defer func() { require.NoError(t, r.Close()) }() b, err := ioutil.ReadAll(r) require.NoError(t, err)