github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/test/relationship_to_many_setops.go.tpl (about) 1 {{- if .Table.IsJoinTable -}} 2 {{- else -}} 3 {{- $table := .Table -}} 4 {{- range $rel := .Table.ToManyRelationships -}} 5 {{- $ltable := $.Aliases.Table $rel.Table -}} 6 {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} 7 {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} 8 {{- $usesPrimitives := usesPrimitives $.Tables $rel.Table $rel.Column $rel.ForeignTable $rel.ForeignColumn -}} 9 {{- $colField := $ltable.Column $rel.Column -}} 10 {{- $fcolField := $ftable.Column $rel.ForeignColumn }} 11 func test{{$ltable.UpSingular}}ToManyAddOp{{$relAlias.Local}}(t *testing.T) { 12 var err error 13 14 {{if not $.NoContext}}ctx := context.Background(){{end}} 15 tx := MustTx({{if $.NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}) 16 defer func() { _ = tx.Rollback() }() 17 18 var a {{$ltable.UpSingular}} 19 var b, c, d, e {{$ftable.UpSingular}} 20 21 seed := randomize.NewSeed() 22 if err = randomize.Struct(seed, &a, {{$ltable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ltable.DownSingular}}PrimaryKeyColumns, {{$ltable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 23 t.Fatal(err) 24 } 25 foreigners := []*{{$ftable.UpSingular}}{&b, &c, &d, &e} 26 for _, x := range foreigners { 27 if err = randomize.Struct(seed, x, {{$ftable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ftable.DownSingular}}PrimaryKeyColumns, {{$ftable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 28 t.Fatal(err) 29 } 30 } 31 32 if err := a.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 33 t.Fatal(err) 34 } 35 if err = b.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 36 t.Fatal(err) 37 } 38 if err = c.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 39 t.Fatal(err) 40 } 41 42 foreignersSplitByInsertion := [][]*{{$ftable.UpSingular}}{ 43 {&b, &c}, 44 {&d, &e}, 45 } 46 47 for i, x := range foreignersSplitByInsertion { 48 err = a.Add{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} tx, i != 0, x...) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 first := x[0] 54 second := x[1] 55 {{- if .ToJoinTable}} 56 57 if first.R.{{$relAlias.Foreign}}[0] != &a { 58 t.Error("relationship was not added properly to the slice") 59 } 60 if second.R.{{$relAlias.Foreign}}[0] != &a { 61 t.Error("relationship was not added properly to the slice") 62 } 63 {{- else}} 64 65 {{if $usesPrimitives -}} 66 if a.{{$colField}} != first.{{$fcolField}} { 67 t.Error("foreign key was wrong value", a.{{$colField}}, first.{{$fcolField}}) 68 } 69 if a.{{$colField}} != second.{{$fcolField}} { 70 t.Error("foreign key was wrong value", a.{{$colField}}, second.{{$fcolField}}) 71 } 72 {{else -}} 73 if !queries.Equal(a.{{$colField}}, first.{{$fcolField}}) { 74 t.Error("foreign key was wrong value", a.{{$colField}}, first.{{$fcolField}}) 75 } 76 if !queries.Equal(a.{{$colField}}, second.{{$fcolField}}) { 77 t.Error("foreign key was wrong value", a.{{$colField}}, second.{{$fcolField}}) 78 } 79 {{- end}} 80 81 if first.R.{{$relAlias.Foreign}} != &a { 82 t.Error("relationship was not added properly to the foreign slice") 83 } 84 if second.R.{{$relAlias.Foreign}} != &a { 85 t.Error("relationship was not added properly to the foreign slice") 86 } 87 {{- end}} 88 89 if a.R.{{$relAlias.Local}}[i*2] != first { 90 t.Error("relationship struct slice not set to correct value") 91 } 92 if a.R.{{$relAlias.Local}}[i*2+1] != second { 93 t.Error("relationship struct slice not set to correct value") 94 } 95 96 count, err := a.{{$relAlias.Local}}().Count({{if not $.NoContext}}ctx, {{end -}} tx) 97 if err != nil { 98 t.Fatal(err) 99 } 100 if want := int64((i+1)*2); count != want { 101 t.Error("want", want, "got", count) 102 } 103 } 104 } 105 {{- if (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} 106 107 func test{{$ltable.UpSingular}}ToManySetOp{{$relAlias.Local}}(t *testing.T) { 108 var err error 109 110 {{if not $.NoContext}}ctx := context.Background(){{end}} 111 tx := MustTx({{if $.NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}) 112 defer func() { _ = tx.Rollback() }() 113 114 var a {{$ltable.UpSingular}} 115 var b, c, d, e {{$ftable.UpSingular}} 116 117 seed := randomize.NewSeed() 118 if err = randomize.Struct(seed, &a, {{$ltable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ltable.DownSingular}}PrimaryKeyColumns, {{$ltable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 119 t.Fatal(err) 120 } 121 foreigners := []*{{$ftable.UpSingular}}{&b, &c, &d, &e} 122 for _, x := range foreigners { 123 if err = randomize.Struct(seed, x, {{$ftable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ftable.DownSingular}}PrimaryKeyColumns, {{$ftable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 124 t.Fatal(err) 125 } 126 } 127 128 if err = a.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 129 t.Fatal(err) 130 } 131 if err = b.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 132 t.Fatal(err) 133 } 134 if err = c.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 135 t.Fatal(err) 136 } 137 138 err = a.Set{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} tx, false, &b, &c) 139 if err != nil { 140 t.Fatal(err) 141 } 142 143 count, err := a.{{$relAlias.Local}}().Count({{if not $.NoContext}}ctx, {{end -}} tx) 144 if err != nil { 145 t.Fatal(err) 146 } 147 if count != 2 { 148 t.Error("count was wrong:", count) 149 } 150 151 err = a.Set{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} tx, true, &d, &e) 152 if err != nil { 153 t.Fatal(err) 154 } 155 156 count, err = a.{{$relAlias.Local}}().Count({{if not $.NoContext}}ctx, {{end -}} tx) 157 if err != nil { 158 t.Fatal(err) 159 } 160 if count != 2 { 161 t.Error("count was wrong:", count) 162 } 163 164 {{- if .ToJoinTable}} 165 166 // The following checks cannot be implemented since we have no handle 167 // to these when we call Set(). Leaving them here as wishful thinking 168 // and to let people know there's dragons. 169 // 170 // if len(b.R.{{$relAlias.Foreign}}) != 0 { 171 // t.Error("relationship was not removed properly from the slice") 172 // } 173 // if len(c.R.{{$relAlias.Foreign}}) != 0 { 174 // t.Error("relationship was not removed properly from the slice") 175 // } 176 if d.R.{{$relAlias.Foreign}}[0] != &a { 177 t.Error("relationship was not added properly to the slice") 178 } 179 if e.R.{{$relAlias.Foreign}}[0] != &a { 180 t.Error("relationship was not added properly to the slice") 181 } 182 {{- else}} 183 184 if !queries.IsValuerNil(b.{{$fcolField}}) { 185 t.Error("want b's foreign key value to be nil") 186 } 187 if !queries.IsValuerNil(c.{{$fcolField}}) { 188 t.Error("want c's foreign key value to be nil") 189 } 190 {{if $usesPrimitives -}} 191 if a.{{$colField}} != d.{{$fcolField}} { 192 t.Error("foreign key was wrong value", a.{{$colField}}, d.{{$fcolField}}) 193 } 194 if a.{{$colField}} != e.{{$fcolField}} { 195 t.Error("foreign key was wrong value", a.{{$colField}}, e.{{$fcolField}}) 196 } 197 {{else -}} 198 if !queries.Equal(a.{{$colField}}, d.{{$fcolField}}) { 199 t.Error("foreign key was wrong value", a.{{$colField}}, d.{{$fcolField}}) 200 } 201 if !queries.Equal(a.{{$colField}}, e.{{$fcolField}}) { 202 t.Error("foreign key was wrong value", a.{{$colField}}, e.{{$fcolField}}) 203 } 204 {{- end}} 205 206 if b.R.{{$relAlias.Foreign}} != nil { 207 t.Error("relationship was not removed properly from the foreign struct") 208 } 209 if c.R.{{$relAlias.Foreign}} != nil { 210 t.Error("relationship was not removed properly from the foreign struct") 211 } 212 if d.R.{{$relAlias.Foreign}} != &a { 213 t.Error("relationship was not added properly to the foreign struct") 214 } 215 if e.R.{{$relAlias.Foreign}} != &a { 216 t.Error("relationship was not added properly to the foreign struct") 217 } 218 {{- end}} 219 220 if a.R.{{$relAlias.Local}}[0] != &d { 221 t.Error("relationship struct slice not set to correct value") 222 } 223 if a.R.{{$relAlias.Local}}[1] != &e { 224 t.Error("relationship struct slice not set to correct value") 225 } 226 } 227 228 func test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}(t *testing.T) { 229 var err error 230 231 {{if not $.NoContext}}ctx := context.Background(){{end}} 232 tx := MustTx({{if $.NoContext}}boil.Begin(){{else}}boil.BeginTx(ctx, nil){{end}}) 233 defer func() { _ = tx.Rollback() }() 234 235 var a {{$ltable.UpSingular}} 236 var b, c, d, e {{$ftable.UpSingular}} 237 238 seed := randomize.NewSeed() 239 if err = randomize.Struct(seed, &a, {{$ltable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ltable.DownSingular}}PrimaryKeyColumns, {{$ltable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 240 t.Fatal(err) 241 } 242 foreigners := []*{{$ftable.UpSingular}}{&b, &c, &d, &e} 243 for _, x := range foreigners { 244 if err = randomize.Struct(seed, x, {{$ftable.DownSingular}}DBTypes, false, strmangle.SetComplement({{$ftable.DownSingular}}PrimaryKeyColumns, {{$ftable.DownSingular}}ColumnsWithoutDefault)...); err != nil { 245 t.Fatal(err) 246 } 247 } 248 249 if err := a.Insert({{if not $.NoContext}}ctx, {{end -}} tx, boil.Infer()); err != nil { 250 t.Fatal(err) 251 } 252 253 err = a.Add{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} tx, true, foreigners...) 254 if err != nil { 255 t.Fatal(err) 256 } 257 258 count, err := a.{{$relAlias.Local}}().Count({{if not $.NoContext}}ctx, {{end -}} tx) 259 if err != nil { 260 t.Fatal(err) 261 } 262 if count != 4 { 263 t.Error("count was wrong:", count) 264 } 265 266 err = a.Remove{{$relAlias.Local}}({{if not $.NoContext}}ctx, {{end -}} tx, foreigners[:2]...) 267 if err != nil { 268 t.Fatal(err) 269 } 270 271 count, err = a.{{$relAlias.Local}}().Count({{if not $.NoContext}}ctx, {{end -}} tx) 272 if err != nil { 273 t.Fatal(err) 274 } 275 if count != 2 { 276 t.Error("count was wrong:", count) 277 } 278 279 {{- if .ToJoinTable}} 280 281 if len(b.R.{{$relAlias.Foreign}}) != 0 { 282 t.Error("relationship was not removed properly from the slice") 283 } 284 if len(c.R.{{$relAlias.Foreign}}) != 0 { 285 t.Error("relationship was not removed properly from the slice") 286 } 287 if d.R.{{$relAlias.Foreign}}[0] != &a { 288 t.Error("relationship was not added properly to the foreign struct") 289 } 290 if e.R.{{$relAlias.Foreign}}[0] != &a { 291 t.Error("relationship was not added properly to the foreign struct") 292 } 293 {{- else}} 294 295 if !queries.IsValuerNil(b.{{$fcolField}}) { 296 t.Error("want b's foreign key value to be nil") 297 } 298 if !queries.IsValuerNil(c.{{$fcolField}}) { 299 t.Error("want c's foreign key value to be nil") 300 } 301 302 if b.R.{{$relAlias.Foreign}} != nil { 303 t.Error("relationship was not removed properly from the foreign struct") 304 } 305 if c.R.{{$relAlias.Foreign}} != nil { 306 t.Error("relationship was not removed properly from the foreign struct") 307 } 308 if d.R.{{$relAlias.Foreign}} != &a { 309 t.Error("relationship to a should have been preserved") 310 } 311 if e.R.{{$relAlias.Foreign}} != &a { 312 t.Error("relationship to a should have been preserved") 313 } 314 {{- end}} 315 316 if len(a.R.{{$relAlias.Local}}) != 2 { 317 t.Error("should have preserved two relationships") 318 } 319 320 // Removal doesn't do a stable deletion for performance so we have to flip the order 321 if a.R.{{$relAlias.Local}}[1] != &d { 322 t.Error("relationship to d should have been preserved") 323 } 324 if a.R.{{$relAlias.Local}}[0] != &e { 325 t.Error("relationship to e should have been preserved") 326 } 327 } 328 {{end -}} 329 {{- end -}}{{- /* range relationships */ -}} 330 {{- end -}}{{- /* outer if join table */ -}}