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-08-15 05:14:39 +02:00
|
|
|
t.Parallel()
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-18 09:06:28 +02:00
|
|
|
seed := randomize.NewSeed()
|
2016-08-15 05:14:39 +02:00
|
|
|
var err error
|
2016-08-05 04:59:40 +02:00
|
|
|
// Attempt the INSERT side of an UPSERT
|
2016-08-15 05:14:39 +02:00
|
|
|
{{$varNameSingular}} := {{$tableNameSingular}}{}
|
2016-08-18 09:06:28 +02:00
|
|
|
if err = randomize.Struct(seed, &{{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-08-05 04:59:40 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-15 05:14:39 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
2016-08-14 06:21:26 +02:00
|
|
|
defer tx.Rollback()
|
2016-08-14 10:58:36 +02:00
|
|
|
if err = {{$varNameSingular}}.Upsert(tx, false, nil, nil); err != nil {
|
2016-08-05 04:59:40 +02:00
|
|
|
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-14 10:58:36 +02:00
|
|
|
count, err := {{$tableNamePlural}}(tx).Count()
|
2016-08-05 04:59:40 +02:00
|
|
|
if err != nil {
|
2016-08-11 14:26:49 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-14 10:58:36 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-05 04:59:40 +02:00
|
|
|
// Attempt the UPDATE side of an UPSERT
|
2016-08-18 09:06:28 +02:00
|
|
|
if err = randomize.Struct(seed, &{{$varNameSingular}}, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-05 04:59:40 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-14 10:58:36 +02:00
|
|
|
if err = {{$varNameSingular}}.Upsert(tx, true, nil, nil); err != nil {
|
2016-08-05 04:59:40 +02:00
|
|
|
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
|
2016-08-14 10:58:36 +02:00
|
|
|
count, err = {{$tableNamePlural}}(tx).Count()
|
2016-08-05 04:59:40 +02:00
|
|
|
if err != nil {
|
2016-08-11 14:26:49 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-14 10:58:36 +02:00
|
|
|
if count != 1 {
|
|
|
|
t.Error("want one record, got:", count)
|
|
|
|
}
|
2016-08-04 16:20:17 +02:00
|
|
|
}
|