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-08-14 10:58:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
|
|
|
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-08-14 11:11:32 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
2016-07-15 12:14:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
tx, err := boil.Begin()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-07-15 12:14:47 +02:00
|
|
|
}
|
2016-08-14 08:38:41 +02:00
|
|
|
defer tx.Rollback()
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if err = {{$varNameSingular}}.Insert(tx); err != nil {
|
|
|
|
t.Error(err)
|
2016-07-15 12:14:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
2016-07-15 12:14:47 +02:00
|
|
|
if err != nil {
|
2016-08-11 14:26:49 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if err = boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-14 11:11:32 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if err = {{$varNameSingular}}.Update(tx); err != nil {
|
|
|
|
t.Error(err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
2016-08-14 08:38:41 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
func Test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
2016-08-14 10:58:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
|
|
|
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-08-14 11:11:32 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
2016-08-14 08:38:41 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
tx, err := boil.Begin()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
2016-08-14 08:38:41 +02:00
|
|
|
defer tx.Rollback()
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if err = {{$varNameSingular}}.Insert(tx); err != nil {
|
|
|
|
t.Error(err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
2016-08-11 10:23:47 +02:00
|
|
|
if err != nil {
|
2016-08-14 08:38:41 +02:00
|
|
|
t.Error(err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
if err = boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-14 11:11:32 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
// Remove Primary keys and unique columns from what we plan to update
|
|
|
|
fields := strmangle.SetComplement(
|
|
|
|
{{$varNameSingular}}Columns,
|
2016-08-14 08:50:40 +02:00
|
|
|
{{$varNameSingular}}PrimaryKeyColumns,
|
2016-08-14 08:38:41 +02:00
|
|
|
)
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
|
|
|
|
updateMap := M{}
|
|
|
|
for _, col := range fields {
|
|
|
|
updateMap[col] = value.FieldByName(strmangle.TitleCase(col)).Interface()
|
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
slice := {{$tableNameSingular}}Slice{{"{"}}{{$varNameSingular}}{{"}"}}
|
|
|
|
if err = slice.UpdateAll(tx, updateMap); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
}
|