sqlboiler/templates_test/all.tpl

65 lines
2 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}}(t *testing.T) {
var err error
o := make({{$tableNameSingular}}Slice, 2)
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-10 04:09:48 +02:00
}
// insert two random objects 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)
}
}
// Delete all rows to give a clean slate
2016-08-01 07:10:10 +02:00
err = {{$tableNamePlural}}G().DeleteAll()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
}
// Check number of rows in table to ensure DeleteAll was successful
var c int64
2016-08-01 07:10:10 +02:00
c, err = {{$tableNamePlural}}G().Count()
if c != 0 {
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
}
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-10 04:09:48 +02:00
}
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)
}
}
// Ensure Count is valid
2016-08-01 07:10:10 +02:00
c, err = {{$tableNamePlural}}G().Count()
if c != 3 {
t.Errorf("Expected {{.Table.Name}} table to have 3 rows, but got %d", c)
}
// Attempt to retrieve all objects
2016-08-01 07:10:10 +02:00
res, err := {{$tableNamePlural}}G().All()
if err != nil {
t.Errorf("Unable to retrieve all {{$tableNamePlural}}, err: %s", err)
}
if len(res) != 3 {
t.Errorf("Expected 3 {{$tableNameSingular}} rows, got %d", len(res))
}
{{$varNamePlural}}DeleteAllRows(t)
}