github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/test/update.go.tpl (about) 1 {{- $alias := .Aliases.Table .Table.Name}} 2 func test{{$alias.UpPlural}}Update(t *testing.T) { 3 t.Parallel() 4 5 if 0 == len({{$alias.DownSingular}}PrimaryKeyColumns) { 6 t.Skip("Skipping table with no primary key columns") 7 } 8 if len({{$alias.DownSingular}}AllColumns) == len({{$alias.DownSingular}}PrimaryKeyColumns) { 9 t.Skip("Skipping table with only primary key columns") 10 } 11 12 seed := randomize.NewSeed() 13 var err error 14 o := &{{$alias.UpSingular}}{} 15 if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil { 16 t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err) 17 } 18 19 {{if not .NoContext}}ctx := context.Background(){{end}} 20 tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}) 21 defer func() { _ = tx.Rollback() }() 22 if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 23 t.Error(err) 24 } 25 26 count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx) 27 if err != nil { 28 t.Error(err) 29 } 30 31 if count != 1 { 32 t.Error("want one record, got:", count) 33 } 34 35 if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}PrimaryKeyColumns...); err != nil { 36 t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err) 37 } 38 39 {{if .NoRowsAffected -}} 40 if err = o.Update({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 41 t.Error(err) 42 } 43 {{else -}} 44 if rowsAff, err := o.Update({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 45 t.Error(err) 46 } else if rowsAff != 1 { 47 t.Error("should only affect one row but affected", rowsAff) 48 } 49 {{end -}} 50 } 51 52 func test{{$alias.UpPlural}}SliceUpdateAll(t *testing.T) { 53 t.Parallel() 54 55 if len({{$alias.DownSingular}}AllColumns) == len({{$alias.DownSingular}}PrimaryKeyColumns) { 56 t.Skip("Skipping table with only primary key columns") 57 } 58 59 seed := randomize.NewSeed() 60 var err error 61 o := &{{$alias.UpSingular}}{} 62 if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil { 63 t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err) 64 } 65 66 {{if not .NoContext}}ctx := context.Background(){{end}} 67 tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}) 68 defer func() { _ = tx.Rollback() }() 69 if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 70 t.Error(err) 71 } 72 73 count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx) 74 if err != nil { 75 t.Error(err) 76 } 77 78 if count != 1 { 79 t.Error("want one record, got:", count) 80 } 81 82 if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}PrimaryKeyColumns...); err != nil { 83 t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err) 84 } 85 86 // Remove Primary keys and unique columns from what we plan to update 87 var fields []string 88 if strmangle.StringSliceMatch({{$alias.DownSingular}}AllColumns, {{$alias.DownSingular}}PrimaryKeyColumns) { 89 fields = {{$alias.DownSingular}}AllColumns 90 } else { 91 fields = strmangle.SetComplement( 92 {{$alias.DownSingular}}AllColumns, 93 {{$alias.DownSingular}}PrimaryKeyColumns, 94 ) 95 {{- if filterColumnsByAuto true .Table.Columns }} 96 fields = strmangle.SetComplement(fields, {{$alias.DownSingular}}GeneratedColumns) 97 {{- end}} 98 } 99 100 value := reflect.Indirect(reflect.ValueOf(o)) 101 typ := reflect.TypeOf(o).Elem() 102 n := typ.NumField() 103 104 updateMap := M{} 105 for _, col := range fields { 106 for i := 0; i < n; i++ { 107 f := typ.Field(i) 108 if f.Tag.Get("boil") == col { 109 updateMap[col] = value.Field(i).Interface() 110 } 111 } 112 } 113 114 slice := {{$alias.UpSingular}}Slice{{"{"}}o{{"}"}} 115 {{if .NoRowsAffected -}} 116 if err = slice.UpdateAll({{if not .NoContext}}ctx, {{end -}} tx, updateMap); err != nil { 117 t.Error(err) 118 } 119 {{else -}} 120 if rowsAff, err := slice.UpdateAll({{if not .NoContext}}ctx, {{end -}} tx, updateMap); err != nil { 121 t.Error(err) 122 } else if rowsAff != 1 { 123 t.Error("wanted one record updated but got", rowsAff) 124 } 125 {{end -}} 126 }