github.com/rjgonzale/pop/v5@v5.1.3-dev/preload_associations_test.go (about) 1 package pop 2 3 import ( 4 "testing" 5 6 "github.com/gobuffalo/nulls" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func Test_New_Implementation_For_Nplus1(t *testing.T) { 11 if PDB == nil { 12 t.Skip("skipping integration tests") 13 } 14 transaction(func(tx *Connection) { 15 a := require.New(t) 16 for _, name := range []string{"Mark", "Joe", "Jane"} { 17 user := User{Name: nulls.NewString(name)} 18 a.NoError(tx.Create(&user)) 19 20 book := Book{UserID: nulls.NewInt(user.ID)} 21 a.NoError(tx.Create(&book)) 22 23 writer := Writer{Name: "Larry", BookID: book.ID} 24 a.NoError(tx.Create(&writer)) 25 26 if name == "Mark" { 27 song := Song{UserID: user.ID} 28 a.NoError(tx.Create(&song)) 29 30 address := Address{Street: "Pop"} 31 a.NoError(tx.Create(&address)) 32 33 home := UsersAddress{UserID: user.ID, AddressID: address.ID} 34 a.NoError(tx.Create(&home)) 35 } 36 } 37 38 users := []User{} 39 a.NoError(tx.All(&users)) 40 41 // FILL THE HAS-MANY and HAS_ONE 42 a.NoError(preload(tx, &users)) 43 44 a.Len(users[0].Books, 1) 45 a.Len(users[1].Books, 1) 46 a.Len(users[2].Books, 1) 47 a.Equal(users[0].FavoriteSong.UserID, users[0].ID) 48 a.Len(users[0].Houses, 1) 49 50 book := Book{} 51 a.NoError(tx.First(&book)) 52 a.NoError(preload(tx, &book)) 53 a.Len(book.Writers, 1) 54 a.Equal("Larry", book.Writers[0].Name) 55 a.Equal("Mark", book.User.Name.String) 56 }) 57 } 58 59 func Test_New_Implementation_For_Nplus1_With_UUID(t *testing.T) { 60 if PDB == nil { 61 t.Skip("skipping integration tests") 62 } 63 transaction(func(tx *Connection) { 64 a := require.New(t) 65 66 courses := []Course{} 67 for i := 0; i < 3; i++ { 68 course := Course{} 69 a.NoError(tx.Create(&course)) 70 courses = append(courses, course) 71 if i == 0 { 72 a.NoError(tx.Create(&CourseCode{ 73 CourseID: course.ID, 74 })) 75 } 76 } 77 78 courseCodes := []CourseCode{} 79 a.NoError(tx.All(&courseCodes)) 80 a.Len(courseCodes, 1) 81 82 // FILL THE HAS-MANY and HAS_ONE 83 a.NoError(preload(tx, &courseCodes)) 84 a.Equal(courses[0].ID, courseCodes[0].Course.ID) 85 86 student := Student{} 87 a.NoError(tx.Create(&student)) 88 89 parent := Parent{} 90 a.NoError(tx.Create(&parent)) 91 92 a.NoError(tx.RawQuery("insert into parents_students(parent_id, student_id) values(?,?)", parent.ID.String(), student.ID.String()).Exec()) 93 94 parents := []Parent{} 95 a.NoError(tx.All(&parents)) 96 97 a.NoError(preload(tx, &parents)) 98 a.Len(parents, 1) 99 a.Len(parents[0].Students, 1) 100 a.Equal(student.ID, parents[0].Students[0].ID) 101 }) 102 } 103 104 func Test_New_Implementation_For_Nplus1_Single(t *testing.T) { 105 if PDB == nil { 106 t.Skip("skipping integration tests") 107 } 108 transaction(func(tx *Connection) { 109 a := require.New(t) 110 for _, name := range []string{"Mark", "Joe", "Jane"} { 111 user := User{Name: nulls.NewString(name)} 112 a.NoError(tx.Create(&user)) 113 114 book := Book{UserID: nulls.NewInt(user.ID)} 115 a.NoError(tx.Create(&book)) 116 117 writer := Writer{Name: "Larry", BookID: book.ID} 118 a.NoError(tx.Create(&writer)) 119 120 if name == "Mark" { 121 song := Song{UserID: user.ID} 122 a.NoError(tx.Create(&song)) 123 124 address := Address{Street: "Pop"} 125 a.NoError(tx.Create(&address)) 126 127 home := UsersAddress{UserID: user.ID, AddressID: address.ID} 128 a.NoError(tx.Create(&home)) 129 } 130 } 131 132 users := []User{} 133 a.NoError(tx.All(&users)) 134 135 // FILL THE HAS-MANY and HAS_ONE 136 a.NoError(preload(tx, &users, "Books")) 137 138 a.Len(users[0].Books, 1) 139 a.Len(users[1].Books, 1) 140 a.Len(users[2].Books, 1) 141 a.Zero(users[0].FavoriteSong.UserID) 142 a.Len(users[0].Houses, 0) 143 }) 144 } 145 146 func Test_New_Implementation_For_Nplus1_Nested(t *testing.T) { 147 if PDB == nil { 148 t.Skip("skipping integration tests") 149 } 150 transaction(func(tx *Connection) { 151 a := require.New(t) 152 var song Song 153 for _, name := range []string{"Mark", "Joe", "Jane"} { 154 user := User{Name: nulls.NewString(name)} 155 a.NoError(tx.Create(&user)) 156 157 book := Book{UserID: nulls.NewInt(user.ID)} 158 a.NoError(tx.Create(&book)) 159 160 if name == "Mark" { 161 song = Song{UserID: user.ID} 162 a.NoError(tx.Create(&song)) 163 164 address := Address{Street: "Pop"} 165 a.NoError(tx.Create(&address)) 166 167 home := UsersAddress{UserID: user.ID, AddressID: address.ID} 168 a.NoError(tx.Create(&home)) 169 } 170 } 171 172 SetEagerMode(EagerPreload) 173 users := []User{} 174 a.NoError(tx.Eager("Houses", "Books", "Books.User.FavoriteSong").All(&users)) 175 a.Len(users[0].Books, 1) 176 a.Len(users[1].Books, 1) 177 a.Len(users[2].Books, 1) 178 a.Len(users[0].Houses, 1) 179 180 a.Equal(users[0].ID, users[0].Books[0].User.ID) 181 a.Equal(song.ID, users[0].Books[0].User.FavoriteSong.ID) 182 SetEagerMode(EagerDefault) 183 }) 184 } 185 186 func Test_New_Implementation_For_Nplus1_BelongsTo_Not_Underscore(t *testing.T) { 187 if PDB == nil { 188 t.Skip("skipping integration tests") 189 } 190 transaction(func(tx *Connection) { 191 a := require.New(t) 192 user := User{Name: nulls.NewString("Mark")} 193 a.NoError(tx.Create(&user)) 194 195 taxi := Taxi{UserID: nulls.NewInt(user.ID)} 196 a.NoError(tx.Create(&taxi)) 197 198 SetEagerMode(EagerPreload) 199 taxis := []Taxi{} 200 a.NoError(tx.EagerPreload().All(&taxis)) 201 a.Len(taxis, 1) 202 a.Equal("Mark", taxis[0].Driver.Name.String) 203 SetEagerMode(EagerDefault) 204 }) 205 }