99 lines
3.1 KiB
Smarty
99 lines
3.1 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 := randomize.NewSeed()
|
|
var err error
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
|
if err = randomize.Struct(seed, {{$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 = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
}
|
|
|
|
// If table only contains primary key columns, we need to pass
|
|
// them into a whitelist to get a valid test result,
|
|
// otherwise the Update method will error because it will not be able to
|
|
// generate a whitelist (due to it excluding primary key columns).
|
|
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
|
|
if err = {{$varNameSingular}}.Update(tx, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
|
t.Error(err)
|
|
}
|
|
} else {
|
|
if err = {{$varNameSingular}}.Update(tx); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
seed := randomize.NewSeed()
|
|
var err error
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
|
if err = randomize.Struct(seed, {{$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 = randomize.Struct(seed, {{$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
|
|
var fields []string
|
|
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
|
|
fields = {{$varNameSingular}}Columns
|
|
} else {
|
|
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)
|
|
}
|
|
}
|