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 -}}
|
2016-08-15 11:36:38 +02:00
|
|
|
func test{{$tableNamePlural}}Update(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Parallel()
|
2016-08-14 10:58:36 +02:00
|
|
|
|
2016-09-20 10:05:33 +02:00
|
|
|
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
|
|
|
|
t.Skip("Skipping table with only primary key columns")
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
seed := randomize.NewSeed()
|
|
|
|
var err error
|
|
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
2017-03-15 13:37:04 +01:00
|
|
|
if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
if err = {{$varNameSingular}}.Insert(tx); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-07-15 12:14:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-19 09:03:29 +02:00
|
|
|
if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-20 10:05:33 +02:00
|
|
|
if err = {{$varNameSingular}}.Update(tx); err != nil {
|
|
|
|
t.Error(err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-14 08:38:41 +02:00
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-08-15 11:36:38 +02:00
|
|
|
func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Parallel()
|
2016-08-14 10:58:36 +02:00
|
|
|
|
2016-09-20 10:05:33 +02:00
|
|
|
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
|
|
|
|
t.Skip("Skipping table with only primary key columns")
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
seed := randomize.NewSeed()
|
|
|
|
var err error
|
|
|
|
{{$varNameSingular}} := &{{$tableNameSingular}}{}
|
2017-03-15 13:37:04 +01:00
|
|
|
if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
if err = {{$varNameSingular}}.Insert(tx); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err = randomize.Struct(seed, {{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-11 10:23:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
// 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,
|
|
|
|
)
|
2017-03-19 10:44:15 +01:00
|
|
|
{{- if eq .DriverName "mssql"}}
|
|
|
|
fields = strmangle.SetComplement(
|
|
|
|
fields,
|
|
|
|
{{$varNameSingular}}ColumnsWithAuto,
|
|
|
|
)
|
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-08-14 08:38:41 +02:00
|
|
|
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
|
2016-09-14 10:08:30 +02:00
|
|
|
updateMap := M{}
|
|
|
|
for _, col := range fields {
|
|
|
|
updateMap[col] = value.FieldByName(strmangle.TitleCase(col)).Interface()
|
|
|
|
}
|
2016-08-11 14:26:49 +02:00
|
|
|
|
2016-09-14 10:08:30 +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
|
|
|
}
|