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

     1  {{- $alias := .Aliases.Table .Table.Name -}}
     2  {{- $canSoftDelete := .Table.CanSoftDelete $.AutoColumns.Deleted -}}
     3  {{- $soft := and .AddSoftDeletes $canSoftDelete }}
     4  {{if $soft -}}
     5  func test{{$alias.UpPlural}}SoftDelete(t *testing.T) {
     6  	t.Parallel()
     7  
     8  	seed := randomize.NewSeed()
     9  	var err error
    10  	o := &{{$alias.UpSingular}}{}
    11  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
    12  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    13  	}
    14  
    15  	{{if not .NoContext}}ctx := context.Background(){{end}}
    16  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
    17  	defer func() { _ = tx.Rollback() }()
    18  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
    19  		t.Error(err)
    20  	}
    21  
    22  	{{if .NoRowsAffected -}}
    23  	if err = o.Delete({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
    24  		t.Error(err)
    25  	}
    26  
    27  	{{else -}}
    28  	if rowsAff, err := o.Delete({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
    29  		t.Error(err)
    30  	} else if rowsAff != 1 {
    31  		t.Error("should only have deleted one row, but affected:", rowsAff)
    32  	}
    33  
    34  	{{end -}}
    35  
    36  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    37  	if err != nil {
    38  		t.Error(err)
    39  	}
    40  
    41  	if count != 0 {
    42  		t.Error("want zero records, got:", count)
    43  	}
    44  }
    45  
    46  func test{{$alias.UpPlural}}QuerySoftDeleteAll(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	seed := randomize.NewSeed()
    50  	var err error
    51  	o := &{{$alias.UpSingular}}{}
    52  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
    53  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    54  	}
    55  
    56  	{{if not .NoContext}}ctx := context.Background(){{end}}
    57  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
    58  	defer func() { _ = tx.Rollback() }()
    59  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
    60  		t.Error(err)
    61  	}
    62  
    63  	{{if .NoRowsAffected -}}
    64  	if err = {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
    65  		t.Error(err)
    66  	}
    67  
    68  	{{else -}}
    69  	if rowsAff, err := {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
    70  		t.Error(err)
    71  	} else if rowsAff != 1 {
    72  		t.Error("should only have deleted one row, but affected:", rowsAff)
    73  	}
    74  
    75  	{{end -}}
    76  
    77  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
    78  	if err != nil {
    79  		t.Error(err)
    80  	}
    81  
    82  	if count != 0 {
    83  		t.Error("want zero records, got:", count)
    84  	}
    85  }
    86  
    87  func test{{$alias.UpPlural}}SliceSoftDeleteAll(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	seed := randomize.NewSeed()
    91  	var err error
    92  	o := &{{$alias.UpSingular}}{}
    93  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
    94  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
    95  	}
    96  
    97  	{{if not .NoContext}}ctx := context.Background(){{end}}
    98  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
    99  	defer func() { _ = tx.Rollback() }()
   100  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
   101  		t.Error(err)
   102  	}
   103  
   104  	slice := {{$alias.UpSingular}}Slice{{"{"}}o{{"}"}}
   105  
   106  	{{if .NoRowsAffected -}}
   107  	if err = slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
   108  		t.Error(err)
   109  	}
   110  
   111  	{{else -}}
   112  	if rowsAff, err := slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx, false); err != nil {
   113  		t.Error(err)
   114  	} else if rowsAff != 1 {
   115  		t.Error("should only have deleted one row, but affected:", rowsAff)
   116  	}
   117  
   118  	{{end -}}
   119  
   120  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
   121  	if err != nil {
   122  		t.Error(err)
   123  	}
   124  
   125  	if count != 0 {
   126  		t.Error("want zero records, got:", count)
   127  	}
   128  }
   129  
   130  {{end -}}
   131  
   132  func test{{$alias.UpPlural}}Delete(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	seed := randomize.NewSeed()
   136  	var err error
   137  	o := &{{$alias.UpSingular}}{}
   138  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
   139  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
   140  	}
   141  
   142  	{{if not .NoContext}}ctx := context.Background(){{end}}
   143  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
   144  	defer func() { _ = tx.Rollback() }()
   145  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
   146  		t.Error(err)
   147  	}
   148  
   149  	{{if .NoRowsAffected -}}
   150  	if err = o.Delete({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   151  		t.Error(err)
   152  	}
   153  
   154  	{{else -}}
   155  	if rowsAff, err := o.Delete({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   156  		t.Error(err)
   157  	} else if rowsAff != 1 {
   158  		t.Error("should only have deleted one row, but affected:", rowsAff)
   159  	}
   160  
   161  	{{end -}}
   162  
   163  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
   164  	if err != nil {
   165  		t.Error(err)
   166  	}
   167  
   168  	if count != 0 {
   169  		t.Error("want zero records, got:", count)
   170  	}
   171  }
   172  
   173  func test{{$alias.UpPlural}}QueryDeleteAll(t *testing.T) {
   174  	t.Parallel()
   175  
   176  	seed := randomize.NewSeed()
   177  	var err error
   178  	o := &{{$alias.UpSingular}}{}
   179  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
   180  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
   181  	}
   182  
   183  	{{if not .NoContext}}ctx := context.Background(){{end}}
   184  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
   185  	defer func() { _ = tx.Rollback() }()
   186  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
   187  		t.Error(err)
   188  	}
   189  
   190  	{{if .NoRowsAffected -}}
   191  	if err = {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   192  		t.Error(err)
   193  	}
   194  
   195  	{{else -}}
   196  	if rowsAff, err := {{$alias.UpPlural}}().DeleteAll({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   197  		t.Error(err)
   198  	} else if rowsAff != 1 {
   199  		t.Error("should only have deleted one row, but affected:", rowsAff)
   200  	}
   201  
   202  	{{end -}}
   203  
   204  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
   205  	if err != nil {
   206  		t.Error(err)
   207  	}
   208  
   209  	if count != 0 {
   210  		t.Error("want zero records, got:", count)
   211  	}
   212  }
   213  
   214  func test{{$alias.UpPlural}}SliceDeleteAll(t *testing.T) {
   215  	t.Parallel()
   216  
   217  	seed := randomize.NewSeed()
   218  	var err error
   219  	o := &{{$alias.UpSingular}}{}
   220  	if err = randomize.Struct(seed, o, {{$alias.DownSingular}}DBTypes, true, {{$alias.DownSingular}}ColumnsWithDefault...); err != nil {
   221  		t.Errorf("Unable to randomize {{$alias.UpSingular}} struct: %s", err)
   222  	}
   223  
   224  	{{if not .NoContext}}ctx := context.Background(){{end}}
   225  	tx := MustTx({{if .NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}})
   226  	defer func() { _ = tx.Rollback() }()
   227  	if err = o.Insert({{if not .NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil {
   228  		t.Error(err)
   229  	}
   230  
   231  	slice := {{$alias.UpSingular}}Slice{{"{"}}o{{"}"}}
   232  
   233  	{{if .NoRowsAffected -}}
   234  	if err = slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   235  		t.Error(err)
   236  	}
   237  
   238  	{{else -}}
   239  	if rowsAff, err := slice.DeleteAll({{if not .NoContext}}ctx, {{end -}} tx {{- if $soft}}, true{{end}}); err != nil {
   240  		t.Error(err)
   241  	} else if rowsAff != 1 {
   242  		t.Error("should only have deleted one row, but affected:", rowsAff)
   243  	}
   244  
   245  	{{end -}}
   246  
   247  	count, err := {{$alias.UpPlural}}().Count({{if not .NoContext}}ctx, {{end -}} tx)
   248  	if err != nil {
   249  		t.Error(err)
   250  	}
   251  
   252  	if count != 0 {
   253  		t.Error("want zero records, got:", count)
   254  	}
   255  }