github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/test/insert.go.tpl (about)

     1  {{- $alias := .Aliases.Table .Table.Name}}
     2  func test{{$alias.UpPlural}}Insert(t *testing.T) {
     3  	t.Parallel()
     4  
     5  	seed := randomize.NewSeed()
     6  	var err error
     7  	o := &{{$alias.UpSingular}}{}
     8  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
     9  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    10  	}
    11  
    12  	{{if not .NoContext}}ctx := context.Background(){{end}}
    13  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
    14  	defer func() { _ = tx.Rollback() }()
    15  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
    16  		t.Error(err)
    17  	}
    18  
    19  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    20  	if err != nil {
    21  		t.Error(err)
    22  	}
    23  
    24  	if count != 1 {
    25  		t.Error("want one record, got:", count)
    26  	}
    27  }
    28  
    29  func test{{$alias.UpPlural}}InsertWhitelist(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	seed := randomize.NewSeed()
    33  	var err error
    34  	o := &{{$alias.UpSingular}}{}
    35  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true); err != nil {
    36  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    37  	}
    38  
    39  	{{if not .NoContext}}ctx := context.Background(){{end}}
    40  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
    41  	defer func() { _ = tx.Rollback() }()
    42  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Whitelist({{$alias.DownSingular}}ColumnsWithoutDefault...)); err != nil {
    43  		t.Error(err)
    44  	}
    45  
    46  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  
    51  	if count != 1 {
    52  		t.Error("want one record, got:", count)
    53  	}
    54  }