MCPcopy
hub / github.com/go-gorm/gorm / TestNilPointerSlice2

Function TestNilPointerSlice2

tests/preload_suits_test.go:1350–1391  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1348}
1349
1350func TestNilPointerSlice2(t *testing.T) {
1351 type (
1352 Level4 struct {
1353 ID uint
1354 }
1355 Level3 struct {
1356 ID uint
1357 Level4ID sql.NullInt64 `sql:"index"`
1358 Level4 *Level4
1359 }
1360 Level2 struct {
1361 ID uint
1362 Level3s []*Level3 `gorm:"many2many:level2_level3s"`
1363 }
1364 Level1 struct {
1365 ID uint
1366 Level2ID sql.NullInt64 `sql:"index"`
1367 Level2 *Level2
1368 }
1369 )
1370
1371 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})
1372
1373 if err := DB.AutoMigrate(new(Level4), new(Level3), new(Level2), new(Level1)); err != nil {
1374 t.Error(err)
1375 }
1376
1377 want := new(Level1)
1378 if err := DB.Save(want).Error; err != nil {
1379 t.Error(err)
1380 }
1381
1382 got := new(Level1)
1383 err := DB.Preload("Level2.Level3s.Level4").Last(&got).Error
1384 if err != nil {
1385 t.Error(err)
1386 }
1387
1388 if !reflect.DeepEqual(got, want) {
1389 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
1390 }
1391}
1392
1393func TestPrefixedPreloadDuplication(t *testing.T) {
1394 type (

Callers

nothing calls this directly

Calls 8

toJSONStringFunction · 0.85
SaveMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
ErrorMethod · 0.65
LastMethod · 0.65
PreloadMethod · 0.65

Tested by

no test coverage detected