github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/sqlboiler-sqlite3/driver/override/test/upsert.go.tpl (about)

     1  {{- $alias := .Aliases.Table .Table.Name}}
     2  func test{{$alias.UpPlural}}Upsert(t *testing.T) {
     3  	t.Parallel()
     4  	if len({{$alias.DownSingular}}AllColumns) == len({{$alias.DownSingular}}PrimaryKeyColumns) {
     5  		t.Skip("Skipping table with only primary key columns")
     6  	}
     7  
     8  	seed := randomize.NewSeed()
     9  	var err error
    10  	// Attempt the INSERT side of an UPSERT
    11  	o := {{$alias.UpSingular}}{}
    12  	if err = randomize.Struct(seed, &o, {{$alias.DownSingular}}DBTypes, true); err != nil {
    13  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    14  	}
    15  
    16  	{{if not .NoContext}}ctx := context.Background(){{end}}
    17  	tx := MustTx({{if .NoContext}}{{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}{{else}}boil.BeginTx(ctx, nil){{end}})
    18  	defer func() { _ = tx.Rollback() }()
    19  	if err = o.Upsert({{if not .NoContext}}ctx, {{end -}} tx, false, nil, boil.Infer(), boil.Infer()); err != nil {
    20  		t.Errorf("Unable to upsert {{$alias.UpSingular}}: %s", err)
    21  	}
    22  
    23  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	if count != 1 {
    28  		t.Error("want one record, got:", count)
    29  	}
    30  
    31  	// Attempt the UPDATE side of an UPSERT
    32  	if err = randomize.Struct(seed, &o, {{$alias.DownSingular}}DBTypes, false, {{$alias.DownSingular}}PrimaryKeyColumns...); err != nil {
    33  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    34  	}
    35  
    36  	if err = o.Upsert({{if not .NoContext}}ctx, {{end -}} tx, true, nil, boil.Infer(), boil.Infer()); err != nil {
    37  		t.Errorf("Unable to upsert {{$alias.UpSingular}}: %s", err)
    38  	}
    39  
    40  	count, err = {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    41  	if err != nil {
    42  		t.Error(err)
    43  	}
    44  	if count != 1 {
    45  		t.Error("want one record, got:", count)
    46  	}
    47  }
    48