From b7f8c132df56407dc3384172c87d884ec77992e8 Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Thu, 14 Jul 2016 14:12:26 +1000 Subject: [PATCH] Refactored select tests * Delete unecessary helpers --- bdb/column.go | 23 ------------------ imports.go | 1 + templates.go | 1 - templates_test/insert.tpl | 49 +++++++++++---------------------------- 4 files changed, 14 insertions(+), 60 deletions(-) diff --git a/bdb/column.go b/bdb/column.go index 0352c2f..11bb56c 100644 --- a/bdb/column.go +++ b/bdb/column.go @@ -155,26 +155,3 @@ func DefaultValues(columns []Column) []string { return dVals } - -// ZeroValue returns the zero value string of the column type -func ZeroValue(column Column) string { - switch column.Type { - case "null.Uint", "null.Uint8", "null.Uint16", "null.Uint32", "null.Uint64", - "null.Int", "null.Int8", "null.Int16", "null.Int32", "null.Int64", - "uint", "uint8", "uint16", "uint32", "uint64", - "int", "int8", "int16", "int32", "int64": - return `0` - case "null.Float32", "null.Float64", "float32", "float64": - return `0.0` - case "null.String", "string": - return `""` - case "null.Bool", "bool": - return `false` - case "null.Time", "time.Time": - return `time.Time{}` - case "[]byte": - return `[]byte(nil)` - default: - return "" - } -} diff --git a/imports.go b/imports.go index 1473998..0f1bf7f 100644 --- a/imports.go +++ b/imports.go @@ -173,6 +173,7 @@ var defaultTestTemplateImports = imports{ `"gopkg.in/nullbio/null.v4"`, `"github.com/nullbio/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil/qm"`, + `"github.com/nullbio/sqlboiler/strmangle"`, }, } diff --git a/templates.go b/templates.go index 347e53c..5ebe770 100644 --- a/templates.go +++ b/templates.go @@ -148,6 +148,5 @@ var templateFunctions = template.FuncMap{ "columnNames": bdb.ColumnNames, "columnDBTypes": bdb.ColumnDBTypes, "toManyRelationships": bdb.ToManyRelationships, - "zeroValue": bdb.ZeroValue, "defaultValues": bdb.DefaultValues, } diff --git a/templates_test/insert.tpl b/templates_test/insert.tpl index b2835e4..d150c15 100644 --- a/templates_test/insert.tpl +++ b/templates_test/insert.tpl @@ -15,7 +15,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { nullTime := null.NewTime(time.Time{}, true) _ = nullTime - + {{$varNamePlural}}DeleteAllRows(t) o := make({{$varNameSingular}}Slice, 3) @@ -36,15 +36,6 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { {{$varNameSingular}}CompareVals(o[i], j[i], t) } - /** - * Edge case test for: - * No includes specified, all zero values. - * - * Expected result: - * Columns with default values set to their default values. - * Columns without default values set to their zero value. - */ - {{$varNamePlural}}DeleteAllRows(t) item := &{{$tableNameSingular}}{} @@ -78,32 +69,18 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { {{with .Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false}} // Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values. - {{range .}} - {{- $tc := titleCase .Name -}} - {{- $zv := zeroValue . -}} - {{$ty := trimPrefix "null." .Type}} - {{if and (ne $ty "[]byte") .Nullable}} - if item.{{$tc}}.Valid == true { - t.Errorf("Expected the nullable column {{$tc}} of {{$tableNameSingular}} to be invalid (null).") + regularCols := []string{{"{"}}{{. | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}} + + for _, c := range regularCols { + rv := reflect.Indirect(reflect.ValueOf(item)) + field := rv.FieldByName(strmangle.TitleCase(c)) + + zv := reflect.Zero(field.Type()).Interface() + fv := field.Interface() + + if !reflect.DeepEqual(zv, fv) { + t.Errorf("Expected column %s to be zero value, got: %v, wanted: %v", c, fv, zv) + } } - {{if eq .Type "null.Time"}} - if item.{{$tc}}.{{$ty}}.String() != emptyTime { - {{else}} - if item.{{$tc}}.{{$ty}} != {{$zv}} { - {{- end -}} - t.Errorf("Expected the nullable column {{$tc}} of {{$tableNameSingular}} to be a zero-value (null):\n%#v\n%v\n\n", item.{{$tc}}.{{$ty}}, {{$zv}}) - } - {{else}} - {{if eq .Type "[]byte"}} - if string(item.{{$tc}}) != string({{$zv}}) { - {{else if eq .Type "time.Time"}} - if item.{{$tc}}.String() != emptyTime { - {{else}} - if item.{{$tc}} != {{$zv}} { - {{- end -}} - t.Errorf("Expected the column {{$tc}} of {{$tableNameSingular}} to be a zero-value (null):\n%#v\n%v\n\n", item.{{$tc}}, {{$zv}}) - } - {{- end}} - {{end}} {{end}} }