2017-03-13 10:55:26 +01:00
|
|
|
{{- if ne .DriverName "mssql" -}}
|
2016-08-04 16:20:17 +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}}Upsert(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Parallel()
|
2016-08-04 16:20:17 +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
|
|
|
|
// Attempt the INSERT side of an UPSERT
|
|
|
|
{{$varNameSingular}} := {{$tableNameSingular}}{}
|
|
|
|
if err = randomize.Struct(seed, &{{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
|
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
if err = {{$varNameSingular}}.Upsert(tx, {{if eq .DriverName "postgres"}}false, nil, {{end}}nil); err != nil {
|
|
|
|
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
// Attempt the UPDATE side of an UPSERT
|
|
|
|
if err = randomize.Struct(seed, &{{$varNameSingular}}, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err = {{$varNameSingular}}.Upsert(tx, {{if eq .DriverName "postgres"}}true, nil, {{end}}nil); err != nil {
|
|
|
|
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
count, err = {{$tableNamePlural}}(tx).Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|
2017-03-16 09:01:49 +01:00
|
|
|
{{- end}}
|