From aa167002700f7f0735b63193f9e7ad08eb6a06c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Thu, 12 Sep 2024 17:38:15 -0400 Subject: [PATCH] add test cases for chaining Query methods --- model_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/model_test.go b/model_test.go index 702bb4c..fb77ce0 100644 --- a/model_test.go +++ b/model_test.go @@ -107,6 +107,51 @@ func TestModel_PopulateMulti(t *testing.T) { } } +func TestModel_PopulateChained_Multi(t *testing.T) { + initTest() + bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) + saveDoc(t, bandDoc) + mauthor := Create(author).(*user) + saveDoc(t, mauthor) + iti_multi.Author = mauthor + createAndSave(t, &iti_multi) + smodel := Create(story{}).(*story) + query, err := smodel.Find(bson.M{}, options.Find()) + assert.Equal(t, nil, err) + final := CreateSlice(story{}) + query.Populate("Author").Populate("Chapters.Bands").Exec(&final) + assert.Greater(t, len(final), 0) + for _, s := range final { + assert.NotZero(t, s.Chapters[0].Bands[0].Name) + } +} + +func TestPopulate_Chained(t *testing.T) { + initTest() + + bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) + storyDoc := Create(iti_single).(*story) + mauthor := Create(author).(*user) + saveDoc(t, mauthor) + saveDoc(t, bandDoc) + storyDoc.Author = mauthor + saveDoc(t, storyDoc) + assert.Greater(t, storyDoc.ID, int64(0)) + + smodel := Create(story{}).(*story) + q, err := smodel.FindByID(storyDoc.ID) + assert.Equal(t, nil, err) + assert.NotPanics(t, func() { + foundDoc := &story{} + q.Populate("Author").Populate("Chapters.Bands").Exec(foundDoc) + j, _ := q.JSON() + fmt.Printf("%s\n", j) + }) + for _, c := range storyDoc.Chapters { + assert.NotZero(t, c.Bands[0].Name) + } +} + func TestModel_Append(t *testing.T) { initTest() bandDoc := Create(metallica).(*band)