2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
2016-06-08 07:45:34 +02:00
|
|
|
{{- $dbName := singular .Table.Name -}}
|
2016-06-20 07:22:50 +02:00
|
|
|
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
|
|
|
|
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-06-08 07:45:34 +02:00
|
|
|
func {{$varNamePlural}}DeleteAllRows(t *testing.T) {
|
|
|
|
// Delete all rows to give a clean slate
|
|
|
|
err := {{$tableNamePlural}}().DeleteAll()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 07:38:58 +02:00
|
|
|
func Test{{$tableNamePlural}}QueryDeleteAll(t *testing.T) {
|
2016-06-08 07:45:34 +02:00
|
|
|
var err error
|
2016-06-10 06:40:02 +02:00
|
|
|
var c int64
|
2016-06-08 07:45:34 +02:00
|
|
|
|
|
|
|
// Start from a clean slate
|
|
|
|
{{$varNamePlural}}DeleteAllRows(t)
|
|
|
|
|
2016-06-10 06:40:02 +02:00
|
|
|
// Check number of rows in table to ensure DeleteAll was successful
|
|
|
|
c, err = {{$tableNamePlural}}().Count()
|
2016-06-08 07:45:34 +02:00
|
|
|
|
2016-06-10 06:40:02 +02:00
|
|
|
if c != 0 {
|
|
|
|
t.Errorf("Expected 0 rows after ObjDeleteAllRows() call, but got %d rows", c)
|
|
|
|
}
|
2016-06-08 07:45:34 +02:00
|
|
|
|
2016-06-10 06:40:02 +02:00
|
|
|
o := make({{$varNameSingular}}Slice, 3)
|
2016-07-14 07:09:05 +02:00
|
|
|
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-06-10 07:44:20 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
2016-06-10 06:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert random columns to test DeleteAll
|
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
err = o[i].Insert()
|
2016-06-08 07:45:34 +02:00
|
|
|
if err != nil {
|
2016-06-10 06:40:02 +02:00
|
|
|
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 04:09:48 +02:00
|
|
|
// Test DeleteAll() query function
|
2016-06-08 07:45:34 +02:00
|
|
|
err = {{$tableNamePlural}}().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
|
|
|
|
c, err = {{$tableNamePlural}}().Count()
|
|
|
|
|
|
|
|
if c != 0 {
|
2016-06-10 06:40:02 +02:00
|
|
|
t.Errorf("Expected 0 rows after Obj().DeleteAll() call, but got %d rows", c)
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|
2016-06-10 07:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test{{$tableNamePlural}}SliceDeleteAll(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var c int64
|
2016-06-08 07:45:34 +02:00
|
|
|
|
2016-06-10 04:09:48 +02:00
|
|
|
// insert random columns to test DeleteAll
|
2016-06-10 07:38:58 +02:00
|
|
|
o := make({{$varNameSingular}}Slice, 3)
|
2016-07-14 07:09:05 +02:00
|
|
|
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-06-10 07:44:20 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
2016-06-10 06:40:02 +02:00
|
|
|
}
|
2016-06-10 04:09:48 +02:00
|
|
|
|
2016-06-10 06:40:02 +02:00
|
|
|
for i := 0; i < len(o); i++ {
|
2016-06-10 04:09:48 +02:00
|
|
|
err = o[i].Insert()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test DeleteAll slice function
|
2016-06-10 07:20:26 +02:00
|
|
|
if err = o.DeleteAll(); err != nil {
|
|
|
|
t.Errorf("Unable to objSlice.DeleteAll(): %s", err)
|
|
|
|
}
|
2016-06-10 04:09:48 +02:00
|
|
|
|
|
|
|
// Check number of rows in table to ensure DeleteAll was successful
|
|
|
|
c, err = {{$tableNamePlural}}().Count()
|
|
|
|
|
|
|
|
if c != 0 {
|
2016-06-10 06:40:02 +02:00
|
|
|
t.Errorf("Expected 0 rows after objSlice.DeleteAll() call, but got %d rows", c)
|
2016-06-10 04:09:48 +02:00
|
|
|
}
|
2016-06-10 07:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test{{$tableNamePlural}}Delete(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var c int64
|
2016-06-10 04:09:48 +02:00
|
|
|
|
2016-06-08 07:45:34 +02:00
|
|
|
// insert random columns to test Delete
|
2016-06-10 07:38:58 +02:00
|
|
|
o := make({{$varNameSingular}}Slice, 3)
|
2016-07-14 07:09:05 +02:00
|
|
|
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-06-10 07:44:20 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
2016-06-10 06:40:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:45:34 +02:00
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
err = o[i].Insert()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 06:40:02 +02:00
|
|
|
o[0].Delete()
|
2016-06-10 04:09:48 +02:00
|
|
|
|
|
|
|
// Check number of rows in table to ensure DeleteAll was successful
|
|
|
|
c, err = {{$tableNamePlural}}().Count()
|
|
|
|
|
|
|
|
if c != 2 {
|
2016-06-10 06:40:02 +02:00
|
|
|
t.Errorf("Expected 2 rows after obj.Delete() call, but got %d rows", c)
|
2016-06-10 04:09:48 +02:00
|
|
|
}
|
2016-06-10 07:38:58 +02:00
|
|
|
|
|
|
|
o[1].Delete()
|
|
|
|
o[2].Delete()
|
|
|
|
|
|
|
|
// Check number of rows in table to ensure Delete worked for all rows
|
|
|
|
c, err = {{$tableNamePlural}}().Count()
|
|
|
|
|
|
|
|
if c != 0 {
|
|
|
|
t.Errorf("Expected 0 rows after all obj.Delete() calls, but got %d rows", c)
|
|
|
|
}
|
2016-06-08 07:45:34 +02:00
|
|
|
}
|