sqlboiler/templates_test/finishers.tpl

140 lines
4 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Bind(t *testing.T) {
var err error
o := {{$tableNameSingular}}{}
2016-07-14 07:09:05 +02:00
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
2016-08-01 07:10:10 +02:00
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
j := {{$tableNameSingular}}{}
2016-08-01 07:10:10 +02:00
err = {{$tableNamePlural}}G(qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
if err != nil {
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
}
{{$varNameSingular}}CompareVals(&o, &j, t)
// insert 3 rows, attempt to bind into slice
{{$varNamePlural}}DeleteAllRows(t)
y := make({{$tableNameSingular}}Slice, 3)
2016-07-14 07:09:05 +02:00
if err = boil.RandomizeSlice(&y, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(y); i++ {
2016-08-01 07:10:10 +02:00
err = y[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", y[i], err)
}
}
k := {{$tableNameSingular}}Slice{}
2016-08-01 07:10:10 +02:00
err = {{$tableNamePlural}}G().Bind(&k)
if err != nil {
t.Errorf("Unable to call Bind on {{$tableNameSingular}} slice of objects: %s", err)
}
if len(k) != 3 {
t.Errorf("Expected 3 results, got %d", len(k))
}
for i := 0; i < len(y); i++ {
{{$varNameSingular}}CompareVals(y[i], k[i], t)
}
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}One(t *testing.T) {
var err error
o := {{$tableNameSingular}}{}
2016-07-14 07:09:05 +02:00
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
2016-08-01 07:10:10 +02:00
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
2016-08-01 07:10:10 +02:00
j, err := {{$tableNamePlural}}G().One()
if err != nil {
t.Errorf("Unable to fetch One {{$tableNameSingular}} result:\n#%v\nErr: %s", j, err)
}
{{$varNameSingular}}CompareVals(&o, j, t)
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}All(t *testing.T) {
var err error
o := make({{$tableNameSingular}}Slice, 3)
2016-07-14 07:09:05 +02:00
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(o); i++ {
2016-08-01 07:10:10 +02:00
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
2016-08-01 07:10:10 +02:00
j, err := {{$tableNamePlural}}G().All()
if err != nil {
t.Errorf("Unable to fetch All {{$tableNameSingular}} results: %s", err)
}
if len(j) != 3 {
t.Errorf("Expected 3 results, got %d", len(j))
}
for i := 0; i < len(o); i++ {
{{$varNameSingular}}CompareVals(o[i], j[i], t)
}
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}Count(t *testing.T) {
var err error
o := make({{$tableNameSingular}}Slice, 3)
2016-07-14 07:09:05 +02:00
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
2016-06-14 15:15:32 +02:00
// insert random columns to test Count
for i := 0; i < len(o); i++ {
2016-08-01 07:10:10 +02:00
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
2016-06-14 15:15:32 +02:00
2016-08-01 07:10:10 +02:00
c, err := {{$tableNamePlural}}G().Count()
2016-06-14 15:15:32 +02:00
if err != nil {
t.Errorf("Unable to count query {{$tableNameSingular}}: %s", err)
}
if c != 3 {
t.Errorf("Expected 3 results from count {{$tableNameSingular}}, got %d", c)
}
{{$varNamePlural}}DeleteAllRows(t)
}