How to use ToJSONString method of got Package

Best Got code snippet using got.ToJSONString

preload_suits_test.go

Source:preload_suits_test.go Github

copy

Full Screen

1package tests_test2import (3 "database/sql"4 "encoding/json"5 "reflect"6 "sort"7 "sync/atomic"8 "testing"9 "gorm.io/gorm"10)11func toJSONString(v interface{}) []byte {12 r, _ := json.Marshal(v)13 return r14}15func TestNestedPreload1(t *testing.T) {16 type (17 Level1 struct {18 ID uint19 Value string20 Level2ID uint21 }22 Level2 struct {23 ID uint24 Level1 Level125 Level3ID uint26 }27 Level3 struct {28 ID uint29 Name string30 Level2 Level231 }32 )33 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})34 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {35 t.Error(err)36 }37 want := Level3{Level2: Level2{Level1: Level1{Value: "value"}}}38 if err := DB.Create(&want).Error; err != nil {39 t.Error(err)40 }41 var got Level342 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {43 t.Error(err)44 }45 if !reflect.DeepEqual(got, want) {46 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))47 }48 if err := DB.Preload("Level2").Preload("Level2.Level1").First(&got, "name = ?", "not_found").Error; err != gorm.ErrRecordNotFound {49 t.Error(err)50 }51}52func TestNestedPreload2(t *testing.T) {53 type (54 Level1 struct {55 ID uint56 Value string57 Level2ID uint58 }59 Level2 struct {60 ID uint61 Level1s []*Level162 Level3ID uint63 }64 Level3 struct {65 ID uint66 Name string67 Level2s []Level268 }69 )70 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})71 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {72 t.Error(err)73 }74 want := Level3{75 Level2s: []Level2{76 {77 Level1s: []*Level1{78 {Value: "value1"},79 {Value: "value2"},80 },81 },82 {83 Level1s: []*Level1{84 {Value: "value3"},85 },86 },87 },88 }89 if err := DB.Create(&want).Error; err != nil {90 t.Error(err)91 }92 var got Level393 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {94 t.Error(err)95 }96 if !reflect.DeepEqual(got, want) {97 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))98 }99}100func TestNestedPreload3(t *testing.T) {101 type (102 Level1 struct {103 ID uint104 Value string105 Level2ID uint106 }107 Level2 struct {108 ID uint109 Level1 Level1110 Level3ID uint111 }112 Level3 struct {113 Name string114 ID uint115 Level2s []Level2116 }117 )118 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})119 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {120 t.Error(err)121 }122 want := Level3{123 Level2s: []Level2{124 {Level1: Level1{Value: "value1"}},125 {Level1: Level1{Value: "value2"}},126 },127 }128 if err := DB.Create(&want).Error; err != nil {129 t.Error(err)130 }131 var got Level3132 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {133 t.Error(err)134 }135 if !reflect.DeepEqual(got, want) {136 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))137 }138}139func TestNestedPreload4(t *testing.T) {140 type (141 Level1 struct {142 ID uint143 Value string144 Level2ID uint145 }146 Level2 struct {147 ID uint148 Level1s []Level1149 Level3ID uint150 }151 Level3 struct {152 ID uint153 Name string154 Level2 Level2155 }156 )157 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})158 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {159 t.Error(err)160 }161 want := Level3{162 Level2: Level2{163 Level1s: []Level1{164 {Value: "value1"},165 {Value: "value2"},166 },167 },168 }169 if err := DB.Create(&want).Error; err != nil {170 t.Error(err)171 }172 var got Level3173 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {174 t.Error(err)175 }176 if !reflect.DeepEqual(got, want) {177 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))178 }179}180// Slice: []Level3181func TestNestedPreload5(t *testing.T) {182 type (183 Level1 struct {184 ID uint185 Value string186 Level2ID uint187 }188 Level2 struct {189 ID uint190 Level1 Level1191 Level3ID uint192 }193 Level3 struct {194 ID uint195 Name string196 Level2 Level2197 }198 )199 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})200 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {201 t.Error(err)202 }203 want := make([]Level3, 2)204 want[0] = Level3{Level2: Level2{Level1: Level1{Value: "value"}}}205 if err := DB.Create(&want[0]).Error; err != nil {206 t.Error(err)207 }208 want[1] = Level3{Level2: Level2{Level1: Level1{Value: "value2"}}}209 if err := DB.Create(&want[1]).Error; err != nil {210 t.Error(err)211 }212 var got []Level3213 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {214 t.Error(err)215 }216 if !reflect.DeepEqual(got, want) {217 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))218 }219}220func TestNestedPreload6(t *testing.T) {221 type (222 Level1 struct {223 ID uint224 Value string225 Level2ID uint226 }227 Level2 struct {228 ID uint229 Level1s []Level1230 Level3ID uint231 }232 Level3 struct {233 ID uint234 Name string235 Level2s []Level2236 }237 )238 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})239 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {240 t.Error(err)241 }242 want := make([]Level3, 2)243 want[0] = Level3{244 Level2s: []Level2{245 {246 Level1s: []Level1{247 {Value: "value1"},248 {Value: "value2"},249 },250 },251 {252 Level1s: []Level1{253 {Value: "value3"},254 },255 },256 },257 }258 if err := DB.Create(&want[0]).Error; err != nil {259 t.Error(err)260 }261 want[1] = Level3{262 Level2s: []Level2{263 {264 Level1s: []Level1{265 {Value: "value3"},266 {Value: "value4"},267 },268 },269 {270 Level1s: []Level1{271 {Value: "value5"},272 },273 },274 },275 }276 if err := DB.Create(&want[1]).Error; err != nil {277 t.Error(err)278 }279 var got []Level3280 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {281 t.Error(err)282 }283 if !reflect.DeepEqual(got, want) {284 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))285 }286}287func TestNestedPreload7(t *testing.T) {288 type (289 Level1 struct {290 ID uint291 Value string292 Level2ID uint293 }294 Level2 struct {295 ID uint296 Level1 Level1297 Level3ID uint298 }299 Level3 struct {300 ID uint301 Name string302 Level2s []Level2303 }304 )305 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})306 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {307 t.Error(err)308 }309 want := make([]Level3, 2)310 want[0] = Level3{311 Level2s: []Level2{312 {Level1: Level1{Value: "value1"}},313 {Level1: Level1{Value: "value2"}},314 },315 }316 if err := DB.Create(&want[0]).Error; err != nil {317 t.Error(err)318 }319 want[1] = Level3{320 Level2s: []Level2{321 {Level1: Level1{Value: "value3"}},322 {Level1: Level1{Value: "value4"}},323 },324 }325 if err := DB.Create(&want[1]).Error; err != nil {326 t.Error(err)327 }328 var got []Level3329 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {330 t.Error(err)331 }332 if !reflect.DeepEqual(got, want) {333 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))334 }335}336func TestNestedPreload8(t *testing.T) {337 type (338 Level1 struct {339 ID uint340 Value string341 Level2ID uint342 }343 Level2 struct {344 ID uint345 Level1s []Level1346 Level3ID uint347 }348 Level3 struct {349 ID uint350 Name string351 Level2 Level2352 }353 )354 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})355 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {356 t.Error(err)357 }358 want := make([]Level3, 2)359 want[0] = Level3{360 Level2: Level2{361 Level1s: []Level1{362 {Value: "value1"},363 {Value: "value2"},364 },365 },366 }367 if err := DB.Create(&want[0]).Error; err != nil {368 t.Error(err)369 }370 want[1] = Level3{371 Level2: Level2{372 Level1s: []Level1{373 {Value: "value3"},374 {Value: "value4"},375 },376 },377 }378 if err := DB.Create(&want[1]).Error; err != nil {379 t.Error(err)380 }381 var got []Level3382 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {383 t.Error(err)384 }385 if !reflect.DeepEqual(got, want) {386 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))387 }388}389func TestNestedPreload9(t *testing.T) {390 type (391 Level0 struct {392 ID uint393 Value string394 Level1ID uint395 }396 Level1 struct {397 ID uint398 Value string399 Level2ID *uint400 Level2_1ID *uint401 Level0s []Level0 `json:",omitempty"`402 }403 Level2 struct {404 ID uint405 Level1s []Level1406 Level3ID uint407 }408 Level2_1 struct {409 ID uint410 Level1s []Level1 `json:",omitempty"`411 Level3ID uint412 }413 Level3 struct {414 ID uint415 Name string416 Level2 Level2417 Level2_1 Level2_1418 }419 )420 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level2_1{}, &Level0{})421 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}, &Level2_1{}, &Level0{}); err != nil {422 t.Error(err)423 }424 want := make([]Level3, 2)425 want[0] = Level3{426 Level2: Level2{427 Level1s: []Level1{428 {Value: "value1"},429 {Value: "value2"},430 },431 },432 Level2_1: Level2_1{433 Level1s: []Level1{434 {435 Value: "value1-1",436 Level0s: []Level0{{Value: "Level0-1"}},437 },438 {439 Value: "value2-2",440 Level0s: []Level0{{Value: "Level0-2"}},441 },442 },443 },444 }445 if err := DB.Create(&want[0]).Error; err != nil {446 t.Error(err)447 }448 want[1] = Level3{449 Level2: Level2{450 Level1s: []Level1{451 {Value: "value3"},452 {Value: "value4"},453 },454 },455 Level2_1: Level2_1{456 Level1s: []Level1{457 {458 Value: "value3-3",459 Level0s: []Level0{},460 },461 {462 Value: "value4-4",463 Level0s: []Level0{},464 },465 },466 },467 }468 if err := DB.Create(&want[1]).Error; err != nil {469 t.Error(err)470 }471 var got []Level3472 if err := DB.Preload("Level2").Preload("Level2.Level1s").Preload("Level2_1").Preload("Level2_1.Level1s").Preload("Level2_1.Level1s.Level0s").Find(&got).Error; err != nil {473 t.Error(err)474 }475 if string(toJSONString(got)) != string(toJSONString(want)) {476 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))477 }478}479type LevelA1 struct {480 ID uint481 Value string482}483type LevelA2 struct {484 ID uint485 Value string486 LevelA3s []*LevelA3 `json:",omitempty"`487}488type LevelA3 struct {489 ID uint490 Value string491 LevelA1ID sql.NullInt64492 LevelA1 *LevelA1493 LevelA2ID sql.NullInt64494 LevelA2 *LevelA2495}496func TestNestedPreload10(t *testing.T) {497 DB.Migrator().DropTable(&LevelA3{}, &LevelA2{}, &LevelA1{})498 if err := DB.AutoMigrate(&LevelA1{}, &LevelA2{}, &LevelA3{}); err != nil {499 t.Error(err)500 }501 levelA1 := &LevelA1{Value: "foo"}502 if err := DB.Save(levelA1).Error; err != nil {503 t.Error(err)504 }505 want := []*LevelA2{506 {507 Value: "bar",508 LevelA3s: []*LevelA3{509 {510 Value: "qux",511 LevelA1: levelA1,512 },513 },514 },515 {516 Value: "bar 2",517 LevelA3s: []*LevelA3{},518 },519 }520 for _, levelA2 := range want {521 if err := DB.Save(levelA2).Error; err != nil {522 t.Error(err)523 }524 }525 var got []*LevelA2526 if err := DB.Preload("LevelA3s.LevelA1").Find(&got).Error; err != nil {527 t.Error(err)528 }529 if !reflect.DeepEqual(toJSONString(got), toJSONString(want)) {530 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))531 }532}533type LevelB1 struct {534 ID uint535 Value string536 LevelB3s []*LevelB3537}538type LevelB2 struct {539 ID uint540 Value string541}542type LevelB3 struct {543 ID uint544 Value string545 LevelB1ID sql.NullInt64546 LevelB1 *LevelB1547 LevelB2s []*LevelB2 `gorm:"many2many:levelb1_levelb3_levelb2s" json:",omitempty"`548}549func TestNestedPreload11(t *testing.T) {550 DB.Migrator().DropTable(&LevelB3{}, &LevelB2{}, &LevelB1{})551 if err := DB.AutoMigrate(&LevelB1{}, &LevelB2{}, &LevelB3{}); err != nil {552 t.Error(err)553 }554 levelB1 := &LevelB1{Value: "foo"}555 if err := DB.Create(levelB1).Error; err != nil {556 t.Error(err)557 }558 levelB3 := &LevelB3{559 Value: "bar",560 LevelB1ID: sql.NullInt64{Valid: true, Int64: int64(levelB1.ID)},561 LevelB2s: []*LevelB2{},562 }563 if err := DB.Create(levelB3).Error; err != nil {564 t.Error(err)565 }566 levelB1.LevelB3s = []*LevelB3{levelB3}567 want := []*LevelB1{levelB1}568 var got []*LevelB1569 if err := DB.Preload("LevelB3s.LevelB2s").Find(&got).Error; err != nil {570 t.Error(err)571 }572 if !reflect.DeepEqual(toJSONString(got), toJSONString(want)) {573 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))574 }575}576type LevelC1 struct {577 ID uint578 Value string579 LevelC2ID uint580}581type LevelC2 struct {582 ID uint583 Value string584 LevelC1 LevelC1585}586type LevelC3 struct {587 ID uint588 Value string589 LevelC2ID uint590 LevelC2 LevelC2591}592func TestNestedPreload12(t *testing.T) {593 DB.Migrator().DropTable(&LevelC3{}, &LevelC2{}, &LevelC1{})594 if err := DB.AutoMigrate(&LevelC1{}, &LevelC2{}, &LevelC3{}); err != nil {595 t.Error(err)596 }597 level2 := LevelC2{598 Value: "c2",599 LevelC1: LevelC1{600 Value: "c1",601 },602 }603 DB.Create(&level2)604 want := []LevelC3{605 {606 Value: "c3-1",607 LevelC2: level2,608 }, {609 Value: "c3-2",610 LevelC2: level2,611 },612 }613 for i := range want {614 if err := DB.Create(&want[i]).Error; err != nil {615 t.Error(err)616 }617 }618 var got []LevelC3619 if err := DB.Preload("LevelC2").Preload("LevelC2.LevelC1").Find(&got).Error; err != nil {620 t.Error(err)621 }622 if !reflect.DeepEqual(got, want) {623 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))624 }625}626func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {627 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {628 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")629 }630 type (631 Level1 struct {632 ID uint `gorm:"primary_key;"`633 LanguageCode string `gorm:"primary_key"`634 Value string635 }636 Level2 struct {637 ID uint `gorm:"primary_key;"`638 LanguageCode string `gorm:"primary_key"`639 Value string640 Level1s []Level1 `gorm:"many2many:levels;"`641 }642 )643 DB.Migrator().DropTable(&Level2{}, &Level1{})644 DB.Migrator().DropTable("levels")645 if err := DB.AutoMigrate(&Level2{}, &Level1{}); err != nil {646 t.Error(err)647 }648 want := Level2{Value: "Bob", LanguageCode: "ru", Level1s: []Level1{649 {Value: "ru", LanguageCode: "ru"},650 {Value: "en", LanguageCode: "en"},651 }}652 if err := DB.Save(&want).Error; err != nil {653 t.Error(err)654 }655 want2 := Level2{Value: "Tom", LanguageCode: "zh", Level1s: []Level1{656 {Value: "zh", LanguageCode: "zh"},657 {Value: "de", LanguageCode: "de"},658 }}659 if err := DB.Save(&want2).Error; err != nil {660 t.Error(err)661 }662 var got Level2663 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {664 t.Error(err)665 }666 if !reflect.DeepEqual(got, want) {667 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))668 }669 var got2 Level2670 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {671 t.Error(err)672 }673 if !reflect.DeepEqual(got2, want2) {674 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))675 }676 var got3 []Level2677 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {678 t.Error(err)679 }680 if !reflect.DeepEqual(got3, []Level2{got, got2}) {681 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))682 }683 var got4 []Level2684 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {685 t.Error(err)686 }687 var ruLevel1 Level1688 var zhLevel1 Level1689 DB.First(&ruLevel1, "value = ?", "ru")690 DB.First(&zhLevel1, "value = ?", "zh")691 got.Level1s = []Level1{ruLevel1}692 got2.Level1s = []Level1{zhLevel1}693 if !reflect.DeepEqual(got4, []Level2{got, got2}) {694 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))695 }696 if err := DB.Preload("Level1s").Find(&got4, "value IN (?)", []string{"non-existing"}).Error; err != nil {697 t.Error(err)698 }699}700func TestManyToManyPreloadForNestedPointer(t *testing.T) {701 type (702 Level1 struct {703 ID uint704 Value string705 }706 Level2 struct {707 ID uint708 Value string709 Level1s []*Level1 `gorm:"many2many:levels;"`710 }711 Level3 struct {712 ID uint713 Value string714 Level2ID sql.NullInt64715 Level2 *Level2716 }717 )718 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})719 DB.Migrator().DropTable("levels")720 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {721 t.Error(err)722 }723 want := Level3{724 Value: "Bob",725 Level2: &Level2{726 Value: "Foo",727 Level1s: []*Level1{728 {Value: "ru"},729 {Value: "en"},730 },731 },732 }733 if err := DB.Save(&want).Error; err != nil {734 t.Error(err)735 }736 want2 := Level3{737 Value: "Tom",738 Level2: &Level2{739 Value: "Bar",740 Level1s: []*Level1{741 {Value: "zh"},742 {Value: "de"},743 },744 },745 }746 if err := DB.Save(&want2).Error; err != nil {747 t.Error(err)748 }749 var got Level3750 if err := DB.Preload("Level2.Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {751 t.Error(err)752 }753 if !reflect.DeepEqual(got, want) {754 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))755 }756 var got2 Level3757 if err := DB.Preload("Level2.Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {758 t.Error(err)759 }760 if !reflect.DeepEqual(got2, want2) {761 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))762 }763 var got3 []Level3764 if err := DB.Preload("Level2.Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {765 t.Error(err)766 }767 if !reflect.DeepEqual(got3, []Level3{got, got2}) {768 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level3{got, got2}))769 }770 var got4 []Level3771 if err := DB.Preload("Level2.Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {772 t.Error(err)773 }774 var got5 Level3775 DB.Preload("Level2.Level1s").Find(&got5, "value = ?", "bogus")776 var ruLevel1 Level1777 var zhLevel1 Level1778 DB.First(&ruLevel1, "value = ?", "ru")779 DB.First(&zhLevel1, "value = ?", "zh")780 got.Level2.Level1s = []*Level1{&ruLevel1}781 got2.Level2.Level1s = []*Level1{&zhLevel1}782 if !reflect.DeepEqual(got4, []Level3{got, got2}) {783 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level3{got, got2}))784 }785}786func TestNestedManyToManyPreload(t *testing.T) {787 type (788 Level1 struct {789 ID uint790 Value string791 }792 Level2 struct {793 ID uint794 Value string795 Level1s []*Level1 `gorm:"many2many:level1_level2;"`796 }797 Level3 struct {798 ID uint799 Value string800 Level2s []Level2 `gorm:"many2many:level2_level3;"`801 }802 )803 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, "level1_level2", "level2_level3")804 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {805 t.Error(err)806 }807 want := Level3{808 Value: "Level3",809 Level2s: []Level2{810 {811 Value: "Bob",812 Level1s: []*Level1{813 {Value: "ru"},814 {Value: "en"},815 },816 }, {817 Value: "Tom",818 Level1s: []*Level1{819 {Value: "zh"},820 {Value: "de"},821 },822 },823 },824 }825 if err := DB.Save(&want).Error; err != nil {826 t.Error(err)827 }828 var got Level3829 if err := DB.Preload("Level2s").Preload("Level2s.Level1s").Find(&got, "value = ?", "Level3").Error; err != nil {830 t.Error(err)831 }832 if !reflect.DeepEqual(got, want) {833 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))834 }835 if err := DB.Preload("Level2s.Level1s").First(&got, "value = ?", "not_found").Error; err != gorm.ErrRecordNotFound {836 t.Error(err)837 }838}839func TestNestedManyToManyPreload2(t *testing.T) {840 type (841 Level1 struct {842 ID uint843 Value string844 }845 Level2 struct {846 ID uint847 Value string848 Level1s []*Level1 `gorm:"many2many:level1_level2;"`849 }850 Level3 struct {851 ID uint852 Value string853 Level2ID sql.NullInt64854 Level2 *Level2855 }856 )857 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})858 DB.Migrator().DropTable("level1_level2")859 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {860 t.Error(err)861 }862 want := Level3{863 Value: "Level3",864 Level2: &Level2{865 Value: "Bob",866 Level1s: []*Level1{867 {Value: "ru"},868 {Value: "en"},869 },870 },871 }872 if err := DB.Save(&want).Error; err != nil {873 t.Error(err)874 }875 var got Level3876 if err := DB.Preload("Level2.Level1s").Find(&got, "value = ?", "Level3").Error; err != nil {877 t.Error(err)878 }879 if !reflect.DeepEqual(got, want) {880 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))881 }882 if err := DB.Preload("Level2.Level1s").First(&got, "value = ?", "not_found").Error; err != gorm.ErrRecordNotFound {883 t.Error(err)884 }885}886func TestNestedManyToManyPreload3(t *testing.T) {887 type (888 Level1 struct {889 ID uint890 Value string891 }892 Level2 struct {893 ID uint894 Value string895 Level1s []*Level1 `gorm:"many2many:level1_level2;"`896 }897 Level3 struct {898 ID uint899 Value string900 Level2ID sql.NullInt64901 Level2 *Level2902 }903 )904 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, "level1_level2")905 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {906 t.Error(err)907 }908 level1Zh := &Level1{Value: "zh"}909 level1Ru := &Level1{Value: "ru"}910 level1En := &Level1{Value: "en"}911 level21 := &Level2{912 Value: "Level2-1",913 Level1s: []*Level1{level1Zh, level1Ru},914 }915 level22 := &Level2{916 Value: "Level2-2",917 Level1s: []*Level1{level1Zh, level1En},918 }919 wants := []*Level3{920 {921 Value: "Level3-1",922 Level2: level21,923 },924 {925 Value: "Level3-2",926 Level2: level22,927 },928 {929 Value: "Level3-3",930 Level2: level21,931 },932 }933 for _, want := range wants {934 if err := DB.Save(want).Error; err != nil {935 t.Error(err)936 }937 }938 var gots []*Level3939 if err := DB.Preload("Level2.Level1s", func(db *gorm.DB) *gorm.DB {940 return db.Order("level1.id ASC")941 }).Find(&gots).Error; err != nil {942 t.Error(err)943 }944 if !reflect.DeepEqual(gots, wants) {945 t.Errorf("got %s; want %s", toJSONString(gots), toJSONString(wants))946 }947}948func TestNestedManyToManyPreload3ForStruct(t *testing.T) {949 type (950 Level1 struct {951 ID uint952 Value string953 }954 Level2 struct {955 ID uint956 Value string957 Level1s []Level1 `gorm:"many2many:level1_level2;"`958 }959 Level3 struct {960 ID uint961 Value string962 Level2ID sql.NullInt64963 Level2 Level2964 }965 )966 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})967 DB.Migrator().DropTable("level1_level2")968 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {969 t.Error(err)970 }971 level1Zh := Level1{Value: "zh"}972 level1Ru := Level1{Value: "ru"}973 level1En := Level1{Value: "en"}974 level21 := Level2{975 Value: "Level2-1",976 Level1s: []Level1{level1Zh, level1Ru},977 }978 level22 := Level2{979 Value: "Level2-2",980 Level1s: []Level1{level1Zh, level1En},981 }982 wants := []*Level3{983 {984 Value: "Level3-1",985 Level2: level21,986 },987 {988 Value: "Level3-2",989 Level2: level22,990 },991 {992 Value: "Level3-3",993 Level2: level21,994 },995 }996 for _, want := range wants {997 if err := DB.Save(want).Error; err != nil {998 t.Error(err)999 }1000 }1001 var gots []*Level31002 if err := DB.Preload("Level2.Level1s", func(db *gorm.DB) *gorm.DB {1003 return db.Order("level1.id ASC")1004 }).Find(&gots).Error; err != nil {1005 t.Error(err)1006 }1007 if !reflect.DeepEqual(gots, wants) {1008 t.Errorf("got %s; want %s", toJSONString(gots), toJSONString(wants))1009 }1010}1011func TestNestedManyToManyPreload4(t *testing.T) {1012 type (1013 Level4 struct {1014 ID uint1015 Value string1016 Level3ID uint1017 }1018 Level3 struct {1019 ID uint1020 Value string1021 Level4s []*Level41022 }1023 Level2 struct {1024 ID uint1025 Value string1026 Level3s []*Level3 `gorm:"many2many:level2_level3;"`1027 }1028 Level1 struct {1029 ID uint1030 Value string1031 Level2s []*Level2 `gorm:"many2many:level1_level2;"`1032 }1033 )1034 DB.Migrator().DropTable("level1_level2", "level2_level3")1035 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})1036 dummy := Level1{1037 Value: "Level1",1038 Level2s: []*Level2{{1039 Value: "Level2",1040 Level3s: []*Level3{{1041 Value: "Level3",1042 Level4s: []*Level4{{1043 Value: "Level4",1044 }},1045 }},1046 }},1047 }1048 if err := DB.AutoMigrate(&Level4{}, &Level3{}, &Level2{}, &Level1{}); err != nil {1049 t.Error(err)1050 }1051 if err := DB.Save(&dummy).Error; err != nil {1052 t.Error(err)1053 }1054 var level1 Level11055 if err := DB.Preload("Level2s").Preload("Level2s.Level3s").Preload("Level2s.Level3s.Level4s").First(&level1).Error; err != nil {1056 t.Error(err)1057 }1058}1059func TestManyToManyPreloadForPointer(t *testing.T) {1060 type (1061 Level1 struct {1062 ID uint1063 Value string1064 }1065 Level2 struct {1066 ID uint1067 Value string1068 Level1s []*Level1 `gorm:"many2many:levels;"`1069 }1070 )1071 DB.Migrator().DropTable("levels", &Level2{}, &Level1{})1072 if err := DB.AutoMigrate(&Level2{}, &Level1{}); err != nil {1073 t.Error(err)1074 }1075 want := Level2{Value: "Bob", Level1s: []*Level1{1076 {Value: "ru"},1077 {Value: "en"},1078 }}1079 if err := DB.Save(&want).Error; err != nil {1080 t.Error(err)1081 }1082 want2 := Level2{Value: "Tom", Level1s: []*Level1{1083 {Value: "zh"},1084 {Value: "de"},1085 }}1086 if err := DB.Save(&want2).Error; err != nil {1087 t.Error(err)1088 }1089 var got Level21090 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {1091 t.Error(err)1092 }1093 if !reflect.DeepEqual(got, want) {1094 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))1095 }1096 var got2 Level21097 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {1098 t.Error(err)1099 }1100 if !reflect.DeepEqual(got2, want2) {1101 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))1102 }1103 var got3 []Level21104 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {1105 t.Error(err)1106 }1107 if !reflect.DeepEqual(got3, []Level2{got, got2}) {1108 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))1109 }1110 var got4 []Level21111 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {1112 t.Error(err)1113 }1114 var got5 Level21115 DB.Preload("Level1s").First(&got5, "value = ?", "bogus")1116 var ruLevel1 Level11117 var zhLevel1 Level11118 DB.First(&ruLevel1, "value = ?", "ru")1119 DB.First(&zhLevel1, "value = ?", "zh")1120 got.Level1s = []*Level1{&ruLevel1}1121 got2.Level1s = []*Level1{&zhLevel1}1122 if !reflect.DeepEqual(got4, []Level2{got, got2}) {1123 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))1124 }1125}1126func TestNilPointerSlice(t *testing.T) {1127 type (1128 Level3 struct {1129 ID uint1130 Value string1131 }1132 Level2 struct {1133 ID uint1134 Value string1135 Level3ID uint1136 Level3 *Level31137 }1138 Level1 struct {1139 ID uint1140 Value string1141 Level2ID *uint1142 Level2 *Level21143 }1144 )1145 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})1146 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {1147 t.Error(err)1148 }1149 want := Level1{1150 Value: "Bob",1151 Level2: &Level2{1152 Value: "en",1153 Level3: &Level3{1154 Value: "native",1155 },1156 },1157 }1158 if err := DB.Save(&want).Error; err != nil {1159 t.Error(err)1160 }1161 want2 := Level1{1162 Value: "Tom",1163 Level2: nil,1164 }1165 if err := DB.Save(&want2).Error; err != nil {1166 t.Fatalf("Got error %v", err)1167 }1168 var got []Level11169 if err := DB.Preload("Level2").Preload("Level2.Level3").Find(&got).Error; err != nil {1170 t.Error(err)1171 }1172 if len(got) != 2 {1173 t.Errorf("got %v items, expected 2", len(got))1174 }1175 if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) {1176 t.Errorf("got %s; want array containing %s", toJSONString(got), toJSONString(want))1177 }1178 if !reflect.DeepEqual(got[0], want2) && !reflect.DeepEqual(got[1], want2) {1179 t.Errorf("got %s; want array containing %s", toJSONString(got), toJSONString(want2))1180 }1181}1182func TestNilPointerSlice2(t *testing.T) {1183 type (1184 Level4 struct {1185 ID uint1186 }1187 Level3 struct {1188 ID uint1189 Level4ID sql.NullInt64 `sql:"index"`1190 Level4 *Level41191 }1192 Level2 struct {1193 ID uint1194 Level3s []*Level3 `gorm:"many2many:level2_level3s"`1195 }1196 Level1 struct {1197 ID uint1198 Level2ID sql.NullInt64 `sql:"index"`1199 Level2 *Level21200 }1201 )1202 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})1203 if err := DB.AutoMigrate(new(Level4), new(Level3), new(Level2), new(Level1)); err != nil {1204 t.Error(err)1205 }1206 want := new(Level1)1207 if err := DB.Save(want).Error; err != nil {1208 t.Error(err)1209 }1210 got := new(Level1)1211 err := DB.Preload("Level2.Level3s.Level4").Last(&got).Error1212 if err != nil {1213 t.Error(err)1214 }1215 if !reflect.DeepEqual(got, want) {1216 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))1217 }1218}1219func TestPrefixedPreloadDuplication(t *testing.T) {1220 type (1221 Level4 struct {1222 ID uint1223 Name string1224 Level3ID uint1225 }1226 Level3 struct {1227 ID uint1228 Name string1229 Level4s []*Level4 `json:",omitempty"`1230 }1231 Level2 struct {1232 ID uint1233 Name string1234 Level3ID sql.NullInt64 `sql:"index"`1235 Level3 *Level31236 }1237 Level1 struct {1238 ID uint1239 Name string1240 Level2ID sql.NullInt64 `sql:"index"`1241 Level2 *Level21242 }1243 )1244 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})1245 if err := DB.AutoMigrate(new(Level3), new(Level4), new(Level2), new(Level1)); err != nil {1246 t.Error(err)1247 }1248 lvl := &Level3{}1249 if err := DB.Save(lvl).Error; err != nil {1250 t.Error(err)1251 }1252 sublvl1 := &Level4{Level3ID: lvl.ID}1253 if err := DB.Save(sublvl1).Error; err != nil {1254 t.Error(err)1255 }1256 sublvl2 := &Level4{Level3ID: lvl.ID}1257 if err := DB.Save(sublvl2).Error; err != nil {1258 t.Error(err)1259 }1260 lvl.Level4s = []*Level4{sublvl1, sublvl2}1261 want1 := Level1{1262 Level2: &Level2{1263 Level3: lvl,1264 },1265 }1266 if err := DB.Save(&want1).Error; err != nil {1267 t.Error(err)1268 }1269 want2 := Level1{1270 Level2: &Level2{1271 Level3: lvl,1272 },1273 }1274 if err := DB.Save(&want2).Error; err != nil {1275 t.Error(err)1276 }1277 want := []Level1{want1, want2}1278 var got []Level11279 err := DB.Preload("Level2.Level3.Level4s").Find(&got).Error1280 if err != nil {1281 t.Error(err)1282 }1283 for _, level1 := range append(got, want...) {1284 sort.Slice(level1.Level2.Level3.Level4s, func(i, j int) bool {1285 return level1.Level2.Level3.Level4s[i].ID > level1.Level2.Level3.Level4s[j].ID1286 })1287 }1288 if !reflect.DeepEqual(got, want) {1289 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))1290 }1291}1292func TestPreloadManyToManyCallbacks(t *testing.T) {1293 type (1294 Level2 struct {1295 ID uint1296 Name string1297 }1298 Level1 struct {1299 ID uint1300 Name string1301 Level2s []Level2 `gorm:"many2many:level1_level2s"`1302 }1303 )1304 DB.Migrator().DropTable("level1_level2s", &Level2{}, &Level1{})1305 if err := DB.AutoMigrate(new(Level1), new(Level2)); err != nil {1306 t.Error(err)1307 }1308 lvl := Level1{1309 Name: "l1",1310 Level2s: []Level2{1311 {Name: "l2-1"}, {Name: "l2-2"},1312 },1313 }1314 DB.Save(&lvl)1315 var called int641316 DB.Callback().Query().After("gorm:query").Register("TestPreloadManyToManyCallbacks", func(_ *gorm.DB) {1317 atomic.AddInt64(&called, 1)1318 })1319 DB.Preload("Level2s").First(&Level1{}, "id = ?", lvl.ID)1320 if called != 3 {1321 t.Errorf("Wanted callback to be called 3 times but got %d", called)1322 }1323}...

Full Screen

Full Screen

preload_test.go

Source:preload_test.go Github

copy

Full Screen

1package gorm_test2import (3 "encoding/json"4 "os"5 "reflect"6 "testing"7)8func getPreloadUser(name string) *User {9 return getPreparedUser(name, "Preload")10}11func checkUserHasPreloadData(user User, t *testing.T) {12 u := getPreloadUser(user.Name)13 if user.BillingAddress.Address1 != u.BillingAddress.Address1 {14 t.Error("Failed to preload user's BillingAddress")15 }16 if user.ShippingAddress.Address1 != u.ShippingAddress.Address1 {17 t.Error("Failed to preload user's ShippingAddress")18 }19 if user.CreditCard.Number != u.CreditCard.Number {20 t.Error("Failed to preload user's CreditCard")21 }22 if user.Company.Name != u.Company.Name {23 t.Error("Failed to preload user's Company")24 }25 if len(user.Emails) != len(u.Emails) {26 t.Error("Failed to preload user's Emails")27 } else {28 var found int29 for _, e1 := range u.Emails {30 for _, e2 := range user.Emails {31 if e1.Email == e2.Email {32 found++33 break34 }35 }36 }37 if found != len(u.Emails) {38 t.Error("Failed to preload user's email details")39 }40 }41}42func TestPreload(t *testing.T) {43 user1 := getPreloadUser("user1")44 DB.Save(user1)45 preloadDB := DB.Where("role = ?", "Preload").Preload("BillingAddress").Preload("ShippingAddress").46 Preload("CreditCard").Preload("Emails").Preload("Company")47 var user User48 preloadDB.Find(&user)49 checkUserHasPreloadData(user, t)50 user2 := getPreloadUser("user2")51 DB.Save(user2)52 user3 := getPreloadUser("user3")53 DB.Save(user3)54 var users []User55 preloadDB.Find(&users)56 for _, user := range users {57 checkUserHasPreloadData(user, t)58 }59 var users2 []*User60 preloadDB.Find(&users2)61 for _, user := range users2 {62 checkUserHasPreloadData(*user, t)63 }64 var users3 []*User65 preloadDB.Preload("Emails", "email = ?", user3.Emails[0].Email).Find(&users3)66 for _, user := range users3 {67 if user.Name == user3.Name {68 if len(user.Emails) != 1 {69 t.Errorf("should only preload one emails for user3 when with condition")70 }71 } else if len(user.Emails) != 0 {72 t.Errorf("should not preload any emails for other users when with condition")73 }74 }75}76func TestNestedPreload1(t *testing.T) {77 type (78 Level1 struct {79 ID uint80 Value string81 Level2ID uint82 }83 Level2 struct {84 ID uint85 Level1 Level186 Level3ID uint87 }88 Level3 struct {89 ID uint90 Name string91 Level2 Level292 }93 )94 DB.DropTableIfExists(&Level3{})95 DB.DropTableIfExists(&Level2{})96 DB.DropTableIfExists(&Level1{})97 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {98 panic(err)99 }100 want := Level3{Level2: Level2{Level1: Level1{Value: "value"}}}101 if err := DB.Create(&want).Error; err != nil {102 panic(err)103 }104 var got Level3105 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {106 panic(err)107 }108 if !reflect.DeepEqual(got, want) {109 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))110 }111}112func TestNestedPreload2(t *testing.T) {113 type (114 Level1 struct {115 ID uint116 Value string117 Level2ID uint118 }119 Level2 struct {120 ID uint121 Level1s []*Level1122 Level3ID uint123 }124 Level3 struct {125 ID uint126 Name string127 Level2s []Level2128 }129 )130 DB.DropTableIfExists(&Level3{})131 DB.DropTableIfExists(&Level2{})132 DB.DropTableIfExists(&Level1{})133 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {134 panic(err)135 }136 want := Level3{137 Level2s: []Level2{138 {139 Level1s: []*Level1{140 &Level1{Value: "value1"},141 &Level1{Value: "value2"},142 },143 },144 {145 Level1s: []*Level1{146 &Level1{Value: "value3"},147 },148 },149 },150 }151 if err := DB.Create(&want).Error; err != nil {152 panic(err)153 }154 var got Level3155 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {156 panic(err)157 }158 if !reflect.DeepEqual(got, want) {159 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))160 }161}162func TestNestedPreload3(t *testing.T) {163 type (164 Level1 struct {165 ID uint166 Value string167 Level2ID uint168 }169 Level2 struct {170 ID uint171 Level1 Level1172 Level3ID uint173 }174 Level3 struct {175 Name string176 ID uint177 Level2s []Level2178 }179 )180 DB.DropTableIfExists(&Level3{})181 DB.DropTableIfExists(&Level2{})182 DB.DropTableIfExists(&Level1{})183 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {184 panic(err)185 }186 want := Level3{187 Level2s: []Level2{188 {Level1: Level1{Value: "value1"}},189 {Level1: Level1{Value: "value2"}},190 },191 }192 if err := DB.Create(&want).Error; err != nil {193 panic(err)194 }195 var got Level3196 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {197 panic(err)198 }199 if !reflect.DeepEqual(got, want) {200 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))201 }202}203func TestNestedPreload4(t *testing.T) {204 type (205 Level1 struct {206 ID uint207 Value string208 Level2ID uint209 }210 Level2 struct {211 ID uint212 Level1s []Level1213 Level3ID uint214 }215 Level3 struct {216 ID uint217 Name string218 Level2 Level2219 }220 )221 DB.DropTableIfExists(&Level3{})222 DB.DropTableIfExists(&Level2{})223 DB.DropTableIfExists(&Level1{})224 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {225 panic(err)226 }227 want := Level3{228 Level2: Level2{229 Level1s: []Level1{230 Level1{Value: "value1"},231 Level1{Value: "value2"},232 },233 },234 }235 if err := DB.Create(&want).Error; err != nil {236 panic(err)237 }238 var got Level3239 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {240 panic(err)241 }242 if !reflect.DeepEqual(got, want) {243 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))244 }245}246// Slice: []Level3247func TestNestedPreload5(t *testing.T) {248 type (249 Level1 struct {250 ID uint251 Value string252 Level2ID uint253 }254 Level2 struct {255 ID uint256 Level1 Level1257 Level3ID uint258 }259 Level3 struct {260 ID uint261 Name string262 Level2 Level2263 }264 )265 DB.DropTableIfExists(&Level3{})266 DB.DropTableIfExists(&Level2{})267 DB.DropTableIfExists(&Level1{})268 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {269 panic(err)270 }271 want := make([]Level3, 2)272 want[0] = Level3{Level2: Level2{Level1: Level1{Value: "value"}}}273 if err := DB.Create(&want[0]).Error; err != nil {274 panic(err)275 }276 want[1] = Level3{Level2: Level2{Level1: Level1{Value: "value2"}}}277 if err := DB.Create(&want[1]).Error; err != nil {278 panic(err)279 }280 var got []Level3281 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {282 panic(err)283 }284 if !reflect.DeepEqual(got, want) {285 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))286 }287}288func TestNestedPreload6(t *testing.T) {289 type (290 Level1 struct {291 ID uint292 Value string293 Level2ID uint294 }295 Level2 struct {296 ID uint297 Level1s []Level1298 Level3ID uint299 }300 Level3 struct {301 ID uint302 Name string303 Level2s []Level2304 }305 )306 DB.DropTableIfExists(&Level3{})307 DB.DropTableIfExists(&Level2{})308 DB.DropTableIfExists(&Level1{})309 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {310 panic(err)311 }312 want := make([]Level3, 2)313 want[0] = Level3{314 Level2s: []Level2{315 {316 Level1s: []Level1{317 {Value: "value1"},318 {Value: "value2"},319 },320 },321 {322 Level1s: []Level1{323 {Value: "value3"},324 },325 },326 },327 }328 if err := DB.Create(&want[0]).Error; err != nil {329 panic(err)330 }331 want[1] = Level3{332 Level2s: []Level2{333 {334 Level1s: []Level1{335 {Value: "value3"},336 {Value: "value4"},337 },338 },339 {340 Level1s: []Level1{341 {Value: "value5"},342 },343 },344 },345 }346 if err := DB.Create(&want[1]).Error; err != nil {347 panic(err)348 }349 var got []Level3350 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {351 panic(err)352 }353 if !reflect.DeepEqual(got, want) {354 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))355 }356}357func TestNestedPreload7(t *testing.T) {358 type (359 Level1 struct {360 ID uint361 Value string362 Level2ID uint363 }364 Level2 struct {365 ID uint366 Level1 Level1367 Level3ID uint368 }369 Level3 struct {370 ID uint371 Name string372 Level2s []Level2373 }374 )375 DB.DropTableIfExists(&Level3{})376 DB.DropTableIfExists(&Level2{})377 DB.DropTableIfExists(&Level1{})378 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {379 panic(err)380 }381 want := make([]Level3, 2)382 want[0] = Level3{383 Level2s: []Level2{384 {Level1: Level1{Value: "value1"}},385 {Level1: Level1{Value: "value2"}},386 },387 }388 if err := DB.Create(&want[0]).Error; err != nil {389 panic(err)390 }391 want[1] = Level3{392 Level2s: []Level2{393 {Level1: Level1{Value: "value3"}},394 {Level1: Level1{Value: "value4"}},395 },396 }397 if err := DB.Create(&want[1]).Error; err != nil {398 panic(err)399 }400 var got []Level3401 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {402 panic(err)403 }404 if !reflect.DeepEqual(got, want) {405 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))406 }407}408func TestNestedPreload8(t *testing.T) {409 type (410 Level1 struct {411 ID uint412 Value string413 Level2ID uint414 }415 Level2 struct {416 ID uint417 Level1s []Level1418 Level3ID uint419 }420 Level3 struct {421 ID uint422 Name string423 Level2 Level2424 }425 )426 DB.DropTableIfExists(&Level3{})427 DB.DropTableIfExists(&Level2{})428 DB.DropTableIfExists(&Level1{})429 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {430 panic(err)431 }432 want := make([]Level3, 2)433 want[0] = Level3{434 Level2: Level2{435 Level1s: []Level1{436 Level1{Value: "value1"},437 Level1{Value: "value2"},438 },439 },440 }441 if err := DB.Create(&want[0]).Error; err != nil {442 panic(err)443 }444 want[1] = Level3{445 Level2: Level2{446 Level1s: []Level1{447 Level1{Value: "value3"},448 Level1{Value: "value4"},449 },450 },451 }452 if err := DB.Create(&want[1]).Error; err != nil {453 panic(err)454 }455 var got []Level3456 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {457 panic(err)458 }459 if !reflect.DeepEqual(got, want) {460 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))461 }462}463func TestNestedPreload9(t *testing.T) {464 type (465 Level0 struct {466 ID uint467 Value string468 Level1ID uint469 }470 Level1 struct {471 ID uint472 Value string473 Level2ID uint474 Level2_1ID uint475 Level0s []Level0476 }477 Level2 struct {478 ID uint479 Level1s []Level1480 Level3ID uint481 }482 Level2_1 struct {483 ID uint484 Level1s []Level1485 Level3ID uint486 }487 Level3 struct {488 ID uint489 Name string490 Level2 Level2491 Level2_1 Level2_1492 }493 )494 DB.DropTableIfExists(&Level3{})495 DB.DropTableIfExists(&Level2{})496 DB.DropTableIfExists(&Level2_1{})497 DB.DropTableIfExists(&Level1{})498 DB.DropTableIfExists(&Level0{})499 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}, &Level2_1{}, &Level0{}).Error; err != nil {500 panic(err)501 }502 want := make([]Level3, 2)503 want[0] = Level3{504 Level2: Level2{505 Level1s: []Level1{506 Level1{Value: "value1"},507 Level1{Value: "value2"},508 },509 },510 Level2_1: Level2_1{511 Level1s: []Level1{512 Level1{513 Value: "value1-1",514 Level0s: []Level0{{Value: "Level0-1"}},515 },516 Level1{517 Value: "value2-2",518 Level0s: []Level0{{Value: "Level0-2"}},519 },520 },521 },522 }523 if err := DB.Create(&want[0]).Error; err != nil {524 panic(err)525 }526 want[1] = Level3{527 Level2: Level2{528 Level1s: []Level1{529 Level1{Value: "value3"},530 Level1{Value: "value4"},531 },532 },533 Level2_1: Level2_1{534 Level1s: []Level1{535 Level1{Value: "value3-3"},536 Level1{Value: "value4-4"},537 },538 },539 }540 if err := DB.Create(&want[1]).Error; err != nil {541 panic(err)542 }543 var got []Level3544 if err := DB.Preload("Level2").Preload("Level2.Level1s").Preload("Level2_1").Preload("Level2_1.Level1s").Preload("Level2_1.Level1s.Level0s").Find(&got).Error; err != nil {545 panic(err)546 }547 if !reflect.DeepEqual(got, want) {548 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))549 }550}551func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {552 if dialect := os.Getenv("GORM_DIALECT"); dialect == "" || dialect == "sqlite" {553 return554 }555 type (556 Level1 struct {557 ID uint `gorm:"primary_key;"`558 LanguageCode string `gorm:"primary_key"`559 Value string560 }561 Level2 struct {562 ID uint `gorm:"primary_key;"`563 LanguageCode string `gorm:"primary_key"`564 Value string565 Level1s []Level1 `gorm:"many2many:levels;"`566 }567 )568 DB.DropTableIfExists(&Level2{})569 DB.DropTableIfExists(&Level1{})570 DB.Table("levels").DropTableIfExists("levels")571 if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil {572 panic(err)573 }574 want := Level2{Value: "Bob", LanguageCode: "ru", Level1s: []Level1{575 {Value: "ru", LanguageCode: "ru"},576 {Value: "en", LanguageCode: "en"},577 }}578 if err := DB.Save(&want).Error; err != nil {579 panic(err)580 }581 want2 := Level2{Value: "Tom", LanguageCode: "zh", Level1s: []Level1{582 {Value: "zh", LanguageCode: "zh"},583 {Value: "de", LanguageCode: "de"},584 }}585 if err := DB.Save(&want2).Error; err != nil {586 panic(err)587 }588 var got Level2589 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {590 panic(err)591 }592 if !reflect.DeepEqual(got, want) {593 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))594 }595 var got2 Level2596 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {597 panic(err)598 }599 if !reflect.DeepEqual(got2, want2) {600 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))601 }602 var got3 []Level2603 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {604 panic(err)605 }606 if !reflect.DeepEqual(got3, []Level2{got, got2}) {607 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))608 }609 var got4 []Level2610 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {611 panic(err)612 }613 var ruLevel1 Level1614 var zhLevel1 Level1615 DB.First(&ruLevel1, "value = ?", "ru")616 DB.First(&zhLevel1, "value = ?", "zh")617 got.Level1s = []Level1{ruLevel1}618 got2.Level1s = []Level1{zhLevel1}619 if !reflect.DeepEqual(got4, []Level2{got, got2}) {620 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))621 }622}623func TestManyToManyPreloadForPointer(t *testing.T) {624 type (625 Level1 struct {626 ID uint `gorm:"primary_key;"`627 Value string628 }629 Level2 struct {630 ID uint `gorm:"primary_key;"`631 Value string632 Level1s []*Level1 `gorm:"many2many:levels;"`633 }634 )635 DB.DropTableIfExists(&Level2{})636 DB.DropTableIfExists(&Level1{})637 DB.Table("levels").DropTableIfExists("levels")638 if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil {639 panic(err)640 }641 want := Level2{Value: "Bob", Level1s: []*Level1{642 {Value: "ru"},643 {Value: "en"},644 }}645 if err := DB.Save(&want).Error; err != nil {646 panic(err)647 }648 want2 := Level2{Value: "Tom", Level1s: []*Level1{649 {Value: "zh"},650 {Value: "de"},651 }}652 if err := DB.Save(&want2).Error; err != nil {653 panic(err)654 }655 var got Level2656 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {657 panic(err)658 }659 if !reflect.DeepEqual(got, want) {660 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))661 }662 var got2 Level2663 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {664 panic(err)665 }666 if !reflect.DeepEqual(got2, want2) {667 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))668 }669 var got3 []Level2670 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {671 panic(err)672 }673 if !reflect.DeepEqual(got3, []Level2{got, got2}) {674 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))675 }676 var got4 []Level2677 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {678 panic(err)679 }680 var ruLevel1 Level1681 var zhLevel1 Level1682 DB.First(&ruLevel1, "value = ?", "ru")683 DB.First(&zhLevel1, "value = ?", "zh")684 got.Level1s = []*Level1{&ruLevel1}685 got2.Level1s = []*Level1{&zhLevel1}686 if !reflect.DeepEqual(got4, []Level2{got, got2}) {687 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))688 }689}690func TestNilPointerSlice(t *testing.T) {691 type (692 Level3 struct {693 ID uint `gorm:"primary_key;"`694 Value string695 }696 Level2 struct {697 ID uint `gorm:"primary_key;"`698 Value string699 Level3ID uint700 Level3 *Level3701 }702 Level1 struct {703 ID uint `gorm:"primary_key;"`704 Value string705 Level2ID uint706 Level2 *Level2707 }708 )709 DB.DropTableIfExists(&Level3{})710 DB.DropTableIfExists(&Level2{})711 DB.DropTableIfExists(&Level1{})712 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil {713 panic(err)714 }715 want := Level1{Value: "Bob", Level2: &Level2{716 Value: "en",717 Level3: &Level3{718 Value: "native",719 },720 }}721 if err := DB.Save(&want).Error; err != nil {722 panic(err)723 }724 want2 := Level1{Value: "Tom", Level2: nil}725 if err := DB.Save(&want2).Error; err != nil {726 panic(err)727 }728 var got []Level1729 if err := DB.Preload("Level2").Preload("Level2.Level3").Find(&got).Error; err != nil {730 panic(err)731 }732 if len(got) != 2 {733 t.Fatalf("got %v items, expected 2", len(got))734 }735 if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) {736 t.Errorf("got %s; want array containing %s", toJSONString(got), toJSONString(want))737 }738 if !reflect.DeepEqual(got[0], want2) && !reflect.DeepEqual(got[1], want2) {739 t.Errorf("got %s; want array containing %s", toJSONString(got), toJSONString(want2))740 }741}742func toJSONString(v interface{}) []byte {743 r, _ := json.MarshalIndent(v, "", " ")744 return r745}...

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 panic(err)7 }8 conf := loader.Config{Fset: fset, ParserMode: parser.ParseComments}9 conf.CreateFromFiles("main", f)10 prog, err := conf.Load()11 if err != nil {12 panic(err)13 }14 ssaprog := ssautil.CreateProgram(prog, 0)15 ssaprog.Build()16 got := &got{prog: prog, ssaprog: ssaprog}17 ssaprog.AddPackage(got)18 ssaprog.Build()19 fmt.Println(got.ToJSONString())20}21type got struct {22}23func (got) String() string {24}25func (got) Pkg() *types.Package {26}27func (got) Members() map[string]ssa.Member {28 return map[string]ssa.Member{29 "ToJSONString": &gotToJSONString{},30 }31}32type gotToJSONString struct {33}34func (gotToJSONString) String() string {35}36func (gotToJSONString) Name() string {37}

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))4}5import (6func main() {7 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))8}9import (10func main() {11 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))12}13import (14func main() {15 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))16}17import (18func main() {19 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))20}21import (22func main() {23 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))24}25import (26func main() {27 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))28}29import (30func main() {31 fmt.Println(stringutil.ToJSONString(map[string]interface{}{"key": "value"}))32}

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1func main() {2 var got = GoToJSONString()3 got.ToJSONString()4}5func main() {6 var got = GoToJSONString()7 got.ToJSONString()8}9func main() {10 var got = GoToJSONString()11 got.ToJSONString()12}13func main() {14 var got = GoToJSONString()15 got.ToJSONString()16}17func main() {18 var got = GoToJSONString()19 got.ToJSONString()20}21func main() {22 var got = GoToJSONString()23 got.ToJSONString()24}25func main() {26 var got = GoToJSONString()27 got.ToJSONString()28}29func main() {30 var got = GoToJSONString()31 got.ToJSONString()32}33func main() {34 var got = GoToJSONString()35 got.ToJSONString()36}37func main() {38 var got = GoToJSONString()39 got.ToJSONString()40}41func main() {42 var got = GoToJSONString()43 got.ToJSONString()44}45func main() {46 var got = GoToJSONString()47 got.ToJSONString()48}49func main() {50 var got = GoToJSONString()51 got.ToJSONString()52}

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var a map[string]interface{}4 a = make(map[string]interface{})5 var b []interface{}6 b = append(b, "one")7 b = append(b, "two")8 b = append(b, "three")9 fmt.Println(got.ToJSONString(a))10}11{"name":"anil","age":20,"married":true,"address":"xyz","numbers":["one","two","three"]}

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 got := got.New()4 m := make(map[string]string)5 jsonString := got.ToJSONString(m)6 fmt.Println(jsonString)7}8{"name":"John","age":"23"}9import (10func main() {11 got := got.New()12 type Person struct {13 }14 p := Person{Name: "John", Age: 23}15 jsonString := got.ToJSONString(p)16 fmt.Println(jsonString)17}18{"Name":"John","Age":23}19import (20func main() {21 got := got.New()22 s := []string{"John", "23"}23 jsonString := got.ToJSONString(s)24 fmt.Println(jsonString)25}26import (27func main() {28 got := got.New()29 type Person struct {30 }31 p := Person{Name: "John", Age: 23}

Full Screen

Full Screen

ToJSONString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := got.New()4 g.Set("name", "John")5 g.Set("age", 30)6 g.Set("cars", []string{"Ford", "BMW", "Fiat"})7 s := g.ToJSONString()8 fmt.Println(s)9}10{"name":"John","age":30,"cars":["Ford","BMW","Fiat"]}11import (12func main() {13 g := got.New()14 g.Set("name", "John")15 g.Set("age", 30)16 g.Set("cars", []string{"Ford", "BMW", "Fiat"})17 s, err := g.ToJSON()18 fmt.Println(s)19 fmt.Println(err)20}21{"name":"John","age":30,"cars":["Ford","BMW","Fiat"]}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful