From 7e17008f8de8caab85f18e1d58e257b2cc06d2c7 Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Fri, 15 Jul 2016 22:26:45 +1000 Subject: [PATCH] Finish insert tests * Add columnTypes helper --- bdb/column.go | 10 ++++++++++ templates.go | 1 + templates_test/select.tpl | 40 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/bdb/column.go b/bdb/column.go index 11bb56c..9c8ae0a 100644 --- a/bdb/column.go +++ b/bdb/column.go @@ -28,6 +28,16 @@ func ColumnNames(cols []Column) []string { return names } +// ColumnTypes of the columns. +func ColumnTypes(cols []Column) []string { + types := make([]string, len(cols)) + for i, c := range cols { + types[i] = c.Type + } + + return types +} + // ColumnDBTypes of the columns. func ColumnDBTypes(cols []Column) map[string]string { types := map[string]string{} diff --git a/templates.go b/templates.go index 607587b..eaa0681 100644 --- a/templates.go +++ b/templates.go @@ -150,6 +150,7 @@ var templateFunctions = template.FuncMap{ "sqlColDefinitions": bdb.SQLColDefinitions, "sqlColDefStrings": bdb.SQLColDefStrings, "columnNames": bdb.ColumnNames, + "columnTypes": bdb.ColumnTypes, "columnDBTypes": bdb.ColumnDBTypes, "toManyRelationships": bdb.ToManyRelationships, "defaultValues": bdb.DefaultValues, diff --git a/templates_test/select.tpl b/templates_test/select.tpl index d3e3cd5..ad89db5 100644 --- a/templates_test/select.tpl +++ b/templates_test/select.tpl @@ -4,5 +4,43 @@ {{- $varNamePlural := .Table.Name | plural | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}} func Test{{$tableNamePlural}}Select(t *testing.T) { - t.Skip("Test select not implemented") + // Only run this test if there are ample cols to test on + if len({{$varNameSingular}}AutoIncrementColumns) == 0 { + return + } + + var err error + + x := &struct{ + {{- $colNames := .Table.Columns | filterColumnsByAutoIncrement true | columnNames }} + {{ $colTypes := .Table.Columns | filterColumnsByAutoIncrement true | columnTypes }} + {{range $index, $element := $colNames}} + {{$element | titleCase}} {{index $colTypes $index}} + {{end}} + }{} + + item := {{$tableNameSingular}}{} + + blacklistCols := boil.SetMerge({{$varNameSingular}}AutoIncrementColumns, {{$varNameSingular}}PrimaryKeyColumns) + if err = boil.RandomizeStruct(&item, {{$varNameSingular}}DBTypes, false, blacklistCols...); err != nil { + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) + } + + if err = item.Insert(); err != nil { + t.Errorf("Unable to insert item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err) + } + + err = {{$tableNamePlural}}(qm.Select({{$varNameSingular}}AutoIncrementColumns...), qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "item." | join ", "}})).Bind(x) + if err != nil { + t.Errorf("Unable to select insert results with bind: %s", err) + } + + {{range $index, $element := $colNames }} + {{$e := titleCase $element}} + if item.{{$e}} != x.{{$e}} || x.{{$e}} == {{index $colTypes $index}}(0) { + t.Errorf("Expected item.{{$e}} to match x.{{$e}}, but got: %v, %v", item.{{$e}}, x.{{$e}}) + } + {{end}} + + {{$varNamePlural}}DeleteAllRows(t) }