sqlboiler/templates_test/upsert.tpl

47 lines
1.5 KiB
Smarty
Raw Normal View History

{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func test{{$tableNamePlural}}Upsert(t *testing.T) {
t.Parallel()
2016-08-18 09:06:28 +02:00
seed := randomize.NewSeed()
var err error
// Attempt the INSERT side of an UPSERT
{{$varNameSingular}} := {{$tableNameSingular}}{}
2016-08-18 09:06:28 +02:00
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()
2016-08-14 10:58:36 +02:00
if err = {{$varNameSingular}}.Upsert(tx, false, nil, nil); err != nil {
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
}
2016-08-14 10:58:36 +02:00
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
if count != 1 {
t.Error("want one record, got:", count)
}
// 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 {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
2016-08-14 10:58:36 +02:00
if err = {{$varNameSingular}}.Upsert(tx, true, nil, nil); err != nil {
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
}
2016-08-14 10:58:36 +02:00
count, err = {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
2016-08-14 10:58:36 +02:00
if count != 1 {
t.Error("want one record, got:", count)
}
}