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