sqlboiler/templates_test/find.tpl
Patrick O'brien 1508d85cb6 Finish Bind test, renamed querymods package
* Rename querymods qs package to qm
* Refactor comparing values in template tests to remove redundancy
2016-06-15 00:01:28 +10:00

46 lines
1.7 KiB
Smarty

{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func Test{{$tableNamePlural}}Find(t *testing.T) {
var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3)
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, 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}})
{{$varNameSingular}}CompareVals(o[i], j[i], t)
}
{{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}}
}