sqlboiler/templates_test/update.tpl
Patrick O'brien 76d4b84019 Convert all tests to go 1.7 Run format
* This fixes postgres deadlocks
2016-08-15 19:36:38 +10:00

84 lines
2.5 KiB
Smarty

{{- $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) {
t.Parallel()
seed := boil.NewSeed()
var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err = seed.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
tx := MustTx(boil.Begin())
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
if count != 1 {
t.Error("want one record, got:", count)
}
if err = seed.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = {{$varNameSingular}}.Update(tx); err != nil {
t.Error(err)
}
}
func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
t.Parallel()
seed := boil.NewSeed()
var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err = seed.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
tx := MustTx(boil.Begin())
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
if count != 1 {
t.Error("want one record, got:", count)
}
if err = seed.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
// Remove Primary keys and unique columns from what we plan to update
fields := strmangle.SetComplement(
{{$varNameSingular}}Columns,
{{$varNameSingular}}PrimaryKeyColumns,
)
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
updateMap := M{}
for _, col := range fields {
updateMap[col] = value.FieldByName(strmangle.TitleCase(col)).Interface()
}
slice := {{$tableNameSingular}}Slice{{"{"}}{{$varNameSingular}}{{"}"}}
if err = slice.UpdateAll(tx, updateMap); err != nil {
t.Error(err)
}
}