Refactored select tests
* Delete unecessary helpers
This commit is contained in:
parent
4036786b6a
commit
b7f8c132df
4 changed files with 14 additions and 60 deletions
|
@ -155,26 +155,3 @@ func DefaultValues(columns []Column) []string {
|
||||||
|
|
||||||
return dVals
|
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 ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -173,6 +173,7 @@ var defaultTestTemplateImports = imports{
|
||||||
`"gopkg.in/nullbio/null.v4"`,
|
`"gopkg.in/nullbio/null.v4"`,
|
||||||
`"github.com/nullbio/sqlboiler/boil"`,
|
`"github.com/nullbio/sqlboiler/boil"`,
|
||||||
`"github.com/nullbio/sqlboiler/boil/qm"`,
|
`"github.com/nullbio/sqlboiler/boil/qm"`,
|
||||||
|
`"github.com/nullbio/sqlboiler/strmangle"`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,6 +148,5 @@ var templateFunctions = template.FuncMap{
|
||||||
"columnNames": bdb.ColumnNames,
|
"columnNames": bdb.ColumnNames,
|
||||||
"columnDBTypes": bdb.ColumnDBTypes,
|
"columnDBTypes": bdb.ColumnDBTypes,
|
||||||
"toManyRelationships": bdb.ToManyRelationships,
|
"toManyRelationships": bdb.ToManyRelationships,
|
||||||
"zeroValue": bdb.ZeroValue,
|
|
||||||
"defaultValues": bdb.DefaultValues,
|
"defaultValues": bdb.DefaultValues,
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
|
||||||
|
|
||||||
nullTime := null.NewTime(time.Time{}, true)
|
nullTime := null.NewTime(time.Time{}, true)
|
||||||
_ = nullTime
|
_ = nullTime
|
||||||
|
|
||||||
{{$varNamePlural}}DeleteAllRows(t)
|
{{$varNamePlural}}DeleteAllRows(t)
|
||||||
|
|
||||||
o := make({{$varNameSingular}}Slice, 3)
|
o := make({{$varNameSingular}}Slice, 3)
|
||||||
|
@ -36,15 +36,6 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
|
||||||
{{$varNameSingular}}CompareVals(o[i], j[i], 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)
|
{{$varNamePlural}}DeleteAllRows(t)
|
||||||
|
|
||||||
item := &{{$tableNameSingular}}{}
|
item := &{{$tableNameSingular}}{}
|
||||||
|
@ -78,32 +69,18 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
|
||||||
|
|
||||||
{{with .Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false}}
|
{{with .Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false}}
|
||||||
// Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values.
|
// Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values.
|
||||||
{{range .}}
|
regularCols := []string{{"{"}}{{. | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
|
||||||
{{- $tc := titleCase .Name -}}
|
|
||||||
{{- $zv := zeroValue . -}}
|
for _, c := range regularCols {
|
||||||
{{$ty := trimPrefix "null." .Type}}
|
rv := reflect.Indirect(reflect.ValueOf(item))
|
||||||
{{if and (ne $ty "[]byte") .Nullable}}
|
field := rv.FieldByName(strmangle.TitleCase(c))
|
||||||
if item.{{$tc}}.Valid == true {
|
|
||||||
t.Errorf("Expected the nullable column {{$tc}} of {{$tableNameSingular}} to be invalid (null).")
|
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}}
|
{{end}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue