github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/test/exists.go.tpl (about) 1 {{- $alias := .Aliases.Table .Table.Name}} 2 func test{{$alias.UpPlural}}Exists(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 {{$pkeyArgs := .Table.PKey.Columns | stringMap (aliasCols $alias) | prefixStringSlice (printf "%s." "o") | join ", " -}} 20 e, err := {{$alias.UpSingular}}Exists({{if not .NoContext}}ctx, {{end -}} tx, {{$pkeyArgs}}) 21 if err != nil { 22 t.Errorf("Unable to check if {{$alias.UpSingular}} exists: %s", err) 23 } 24 if !e { 25 t.Errorf("Expected {{$alias.UpSingular}}Exists to return true, but got false.") 26 } 27 }