diff --git a/model_test.go b/model_test.go index cd0bf26..c5d179d 100644 --- a/model_test.go +++ b/model_test.go @@ -11,14 +11,14 @@ import ( func TestNew(t *testing.T) { initTest() - doc := Create(&iti_single).(*story) - assert.Equal(t, iti_single.Title, doc.Title) - assert.Equal(t, iti_single.Chapters[0].Summary, doc.Chapters[0].Summary) + doc := Create(iti_single()).(*story) + assert.Equal(t, iti_single().Title, doc.Title) + assert.Equal(t, iti_single().Chapters[0].Summary, doc.Chapters[0].Summary) } func TestSave(t *testing.T) { initTest() - storyDoc := Create(iti_multi).(*story) + storyDoc := Create(iti_multi()).(*story) lauthor := Create(author).(*user) storyDoc.Author = lauthor assert.Equal(t, storyDoc.Id(), int64(0)) @@ -37,8 +37,8 @@ func TestSave(t *testing.T) { func TestPopulate(t *testing.T) { initTest() - bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) - storyDoc := Create(iti_single).(*story) + bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band) + storyDoc := Create(iti_single()).(*story) mauthor := Create(author).(*user) saveDoc(t, mauthor) saveDoc(t, bandDoc) @@ -79,7 +79,8 @@ func TestUpdate(t *testing.T) { func TestModel_FindAll(t *testing.T) { initTest() - createAndSave(t, &iti_multi) + im := iti_multi() + createAndSave(t, &im) smodel := Create(story{}).(*story) query, err := smodel.Find(bson.M{}, options.Find()) assert.Equal(t, nil, err) @@ -90,12 +91,13 @@ func TestModel_FindAll(t *testing.T) { func TestModel_PopulateMulti(t *testing.T) { initTest() - bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) + 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) + im := iti_multi() + im.Author = mauthor + createAndSave(t, &im) smodel := Create(story{}).(*story) query, err := smodel.Find(bson.M{}, options.Find()) assert.Equal(t, nil, err) @@ -109,12 +111,13 @@ func TestModel_PopulateMulti(t *testing.T) { func TestModel_PopulateChained_Multi(t *testing.T) { initTest() - bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) + im := iti_multi() + 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) + im.Author = mauthor + createAndSave(t, &im) smodel := Create(story{}).(*story) query, err := smodel.Find(bson.M{}, options.Find()) assert.Equal(t, nil, err) @@ -129,8 +132,8 @@ func TestModel_PopulateChained_Multi(t *testing.T) { func TestPopulate_Chained(t *testing.T) { initTest() - bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) - storyDoc := Create(iti_single).(*story) + bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band) + storyDoc := Create(iti_single()).(*story) mauthor := Create(author).(*user) saveDoc(t, mauthor) saveDoc(t, bandDoc) @@ -181,7 +184,7 @@ func TestModel_Delete(t *testing.T) { func TestModel_Pull(t *testing.T) { initTest() - storyDoc := Create(iti_multi).(*story) + storyDoc := Create(iti_multi()).(*story) smodel := Create(story{}).(*story) saveDoc(t, storyDoc) err := storyDoc.Pull("Chapters", storyDoc.Chapters[4]) @@ -197,8 +200,9 @@ func TestModel_Pull(t *testing.T) { func TestModel_Swap(t *testing.T) { initTest() - iti_single.Author = &author - storyDoc := Create(iti_single).(*story) + is := iti_single() + is.Author = &author + storyDoc := Create(iti_single()).(*story) saveDoc(t, storyDoc) storyDoc.Chapters[0].Bands = append(storyDoc.Chapters[0].Bands, bodom) assert.Equal(t, 2, len(storyDoc.Chapters[0].Bands)) @@ -229,30 +233,6 @@ func TestModel_GridFSLoad(t *testing.T) { } } -func TestModel_GridFSLoad_Complex(t *testing.T) { - initTest() - model := Create(story{}).(*story) - bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band) - thingDoc := Create(iti_multi).(*story) - mauthor := Create(author).(*user) - found := &story{} - saveDoc(t, mauthor) - saveDoc(t, bandDoc) - thingDoc.Author = mauthor - saveDoc(t, thingDoc) - assert.NotZero(t, thingDoc.ID) - fq, err := model.FindByID(thingDoc.ID) - assert.Nil(t, err) - fq.Populate("Author", "Chapters.Bands").LoadFile("Chapters.Text").Exec(found) - assert.NotZero(t, len(found.Chapters)) - for _, c := range found.Chapters { - assert.NotZero(t, c.Text) - assert.NotZero(t, c.Bands[0].Name) - } - j, _ := fq.JSON() - fmt.Printf("%s\n", j) -} - func TestModel_GridFSLoad_Chained(t *testing.T) { initTest() ModelRegistry.Model(somethingWithNestedChapters{}) @@ -271,3 +251,27 @@ func TestModel_GridFSLoad_Chained(t *testing.T) { assert.NotZero(t, c.Text) } } + +func TestModel_GridFSLoad_Complex(t *testing.T) { + initTest() + model := Create(story{}).(*story) + bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band) + thingDoc := Create(iti_multi()).(*story) + mauthor := Create(author).(*user) + found := &story{} + saveDoc(t, bandDoc) + saveDoc(t, mauthor) + thingDoc.Author = mauthor + saveDoc(t, thingDoc) + assert.NotZero(t, thingDoc.ID) + fq, err := model.FindByID(thingDoc.ID) + assert.Nil(t, err) + fq.Populate("Author", "Chapters.Bands").LoadFile("Chapters.Text").Exec(found) + assert.NotZero(t, len(found.Chapters)) + for _, c := range found.Chapters { + assert.NotZero(t, c.Text) + assert.NotZero(t, c.Bands[0].Name) + } + j, _ := fq.JSON() + fmt.Printf("%s\n", j) +} diff --git a/testing.go b/testing.go index a20da77..2569e5a 100644 --- a/testing.go +++ b/testing.go @@ -163,21 +163,24 @@ func doSomethingWithNested() somethingWithNestedChapters { } return swnc } - -var iti_single = story{ - Title: "title", - Completed: true, - Chapters: genChaps(true), +func iti_single() story { + return story{ + Title: "title", + Completed: true, + Chapters: genChaps(true), + } } -var iti_multi = story{ - Title: "Brian Tatler Fucked and Abused Sean Harris", - Completed: false, - Chapters: genChaps(false), +func iti_multi() story { + return story{ + Title: "Brian Tatler Fucked and Abused Sean Harris", + Completed: false, + Chapters: genChaps(false), + } } func iti_blank() story { - t := iti_single + t := iti_single() t.Chapters = make([]chapter, 0) return t }