Refactored select tests

* Delete unecessary helpers
This commit is contained in:
Patrick O'brien 2016-07-14 14:12:26 +10:00
parent 4036786b6a
commit b7f8c132df
4 changed files with 14 additions and 60 deletions

View file

@ -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 ""
}
}

View file

@ -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"`,
},
}

View file

@ -148,6 +148,5 @@ var templateFunctions = template.FuncMap{
"columnNames": bdb.ColumnNames,
"columnDBTypes": bdb.ColumnDBTypes,
"toManyRelationships": bdb.ToManyRelationships,
"zeroValue": bdb.ZeroValue,
"defaultValues": bdb.DefaultValues,
}

View file

@ -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}}
}