2016-07-06 08:02:35 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
|
|
|
|
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
|
|
|
func Test{{$tableNamePlural}}Update(t *testing.T) {
|
2016-07-15 12:14:47 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
item := {{$tableNameSingular}}{}
|
2016-08-11 14:26:49 +02:00
|
|
|
boil.RandomizeValidatedStruct(&item, {{$varNameSingular}}ValidatedColumns, {{$varNameSingular}}DBTypes)
|
2016-08-01 07:10:10 +02:00
|
|
|
if err = item.InsertG(); err != nil {
|
2016-07-15 12:14:47 +02:00
|
|
|
t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:03:34 +02:00
|
|
|
blacklistCols := strmangle.SetMerge({{$varNameSingular}}AutoIncrementColumns, {{$varNameSingular}}PrimaryKeyColumns)
|
2016-07-15 12:14:47 +02:00
|
|
|
if err = boil.RandomizeStruct(&item, {{$varNameSingular}}DBTypes, false, blacklistCols...); err != nil {
|
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:03:34 +02:00
|
|
|
whitelist := strmangle.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}AutoIncrementColumns)
|
2016-08-01 07:10:10 +02:00
|
|
|
if err = item.UpdateG(whitelist...); err != nil {
|
2016-07-15 12:14:47 +02:00
|
|
|
t.Errorf("Unable to update {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var j *{{$tableNameSingular}}
|
2016-08-01 07:10:10 +02:00
|
|
|
j, err = {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "item." | join ", "}})
|
2016-07-15 12:14:47 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to find {{$tableNameSingular}} row: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-08-11 14:26:49 +02:00
|
|
|
err = {{$varNameSingular}}CompareVals(&item, j, true); if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
|
|
|
{{$varNamePlural}}DeleteAllRows(t)
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
|
|
|
func Test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// insert random columns to test UpdateAll
|
|
|
|
o := make({{$tableNameSingular}}Slice, 3)
|
|
|
|
j := make({{$tableNameSingular}}Slice, 3)
|
|
|
|
|
|
|
|
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, false); err != nil {
|
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
if err = o[i].InsertG(); err != nil {
|
|
|
|
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := M{}
|
|
|
|
|
|
|
|
tmp := {{$tableNameSingular}}{}
|
2016-08-14 01:03:34 +02:00
|
|
|
blacklist := strmangle.SetMerge({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}UniqueColumns)
|
2016-08-11 14:26:49 +02:00
|
|
|
if err = boil.RandomizeStruct(&tmp, {{$varNameSingular}}DBTypes, false, blacklist...); err != nil {
|
2016-08-11 10:23:47 +02:00
|
|
|
t.Errorf("Unable to randomize struct {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the columns and column values from the randomized struct
|
|
|
|
tmpVal := reflect.Indirect(reflect.ValueOf(tmp))
|
2016-08-14 01:03:34 +02:00
|
|
|
nonBlacklist := strmangle.SetComplement({{$varNameSingular}}Columns, blacklist)
|
2016-08-11 14:26:49 +02:00
|
|
|
for _, col := range nonBlacklist {
|
2016-08-11 10:23:47 +02:00
|
|
|
vals[col] = tmpVal.FieldByName(strmangle.TitleCase(col)).Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = o.UpdateAllG(vals)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to update all for {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
j[i], err = {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[i]." | join ", "}})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to find {{$tableNameSingular}} row: %s", err)
|
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
|
|
|
err = {{$varNameSingular}}CompareVals(j[i], &tmp, true, blacklist...)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 14:26:49 +02:00
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
// Ensure Find found the correct primary key ID's
|
|
|
|
orig := boil.GetStructValues(o[i], {{$varNameSingular}}PrimaryKeyColumns...)
|
|
|
|
new := boil.GetStructValues(j[i], {{$varNameSingular}}PrimaryKeyColumns...)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(orig, new) {
|
|
|
|
t.Errorf("object %d): primary keys do not match:\n\n%#v\n%#v", i, orig, new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 10:23:47 +02:00
|
|
|
{{$varNamePlural}}DeleteAllRows(t)
|
|
|
|
}
|