Finish Find template tests

* Add reflect import to template tests
This commit is contained in:
Patrick O'brien 2016-06-10 18:53:15 +10:00
parent fb34fa6dab
commit 8cb58511a0
3 changed files with 39 additions and 4 deletions

View file

@ -89,6 +89,7 @@ var sqlBoilerSinglesImports = map[string]imports{
var sqlBoilerTestImports = imports{
standard: importList{
`"testing"`,
`"reflect"`,
},
thirdparty: importList{
`"github.com/nullbio/sqlboiler/boil"`,

View file

@ -8,7 +8,8 @@ func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.C
}
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
var {{$varNameSingular}} *{{$tableNameSingular}}
{{$varNameSingular}} := &{{$tableNameSingular}}{}
mods := []qs.QueryMod{
qs.Select(selectCols...),
qs.Table("{{.Table.Name}}"),
@ -17,7 +18,7 @@ func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.
q := NewQueryX(exec, mods...)
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers(&{{$varNameSingular}}, selectCols...)...)
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers({{$varNameSingular}}, selectCols...)...)
if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)

View file

@ -3,7 +3,6 @@
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func Test{{$tableNamePlural}}Find(t *testing.T) {
var err error
@ -13,5 +12,39 @@ func Test{{$tableNamePlural}}Find(t *testing.T) {
if err = boil.RandomizeSlice(&o); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
for i := 0; i < len(o); i++ {
if err = o[i].Insert(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
j := make({{$varNameSingular}}Slice, 0, 3)
// Perform all Find queries and assign result objects to slice for comparison
for i := 0; i < len(j); i++ {
j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}})
// Compare saved objects from earlier to the found objects
if !reflect.DeepEqual(j[i], o[i]) {
t.Errorf("Expected j[%d] to match o[%d], got:\n\nj: #%v\n\no:#%v\n\n", i, i, j[i], o[i])
}
}
{{if hasPrimaryKey .Table.PKey}}
f, err := {{$tableNameSingular}}Find({{titleCaseCommaList "o[0]." .Table.PKey.Columns}}, {{$varNameSingular}}PrimaryKeyColumns...)
{{range $key, $value := .Table.PKey.Columns}}
if o[0].{{titleCase $value}} != f.{{titleCase $value}} {
t.Errorf("Expected primary key values to match, {{titleCase $value}} did not match")
}
{{end}}
colsWithoutPrimKeys := boil.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns)
fRef := reflect.ValueOf(f).Elem()
for _, v := range colsWithoutPrimKeys {
val := fRef.FieldByName(v)
if val.IsValid() {
t.Errorf("Expected all other columns to be zero value, but column %s was %#v", v, val.Interface())
}
}
{{end}}
}