2024-09-01 16:17:48 -04:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-14 02:09:38 -04:00
|
|
|
is := iti_single()
|
|
|
|
doc := Create(is).(*story)
|
|
|
|
assert.Equal(t, is.Title, doc.Title)
|
|
|
|
assert.Equal(t, is.Chapters[0].Summary, doc.Chapters[0].Summary)
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSave(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
storyDoc := Create(iti_multi()).(*story)
|
2024-09-01 16:17:48 -04:00
|
|
|
lauthor := Create(author).(*user)
|
|
|
|
storyDoc.Author = lauthor
|
|
|
|
assert.Equal(t, storyDoc.Id(), int64(0))
|
|
|
|
assert.Equal(t, lauthor.ID, storyDoc.Author.ID)
|
|
|
|
aerr := lauthor.Save()
|
|
|
|
assert.Equal(t, nil, aerr)
|
|
|
|
serr := storyDoc.Save()
|
|
|
|
assert.Equal(t, nil, serr)
|
|
|
|
assert.Less(t, int64(0), storyDoc.ID)
|
|
|
|
assert.Less(t, int64(0), lauthor.ID)
|
2024-09-04 19:43:12 -04:00
|
|
|
for _, c := range storyDoc.Chapters {
|
|
|
|
assert.NotZero(t, c.ChapterID)
|
|
|
|
}
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPopulate(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
2024-09-12 18:29:12 -04:00
|
|
|
bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band)
|
|
|
|
storyDoc := Create(iti_single()).(*story)
|
2024-09-03 00:14:12 -04:00
|
|
|
mauthor := Create(author).(*user)
|
|
|
|
saveDoc(t, mauthor)
|
|
|
|
saveDoc(t, bandDoc)
|
|
|
|
storyDoc.Author = mauthor
|
|
|
|
saveDoc(t, storyDoc)
|
2024-09-01 16:17:48 -04:00
|
|
|
assert.Greater(t, storyDoc.ID, int64(0))
|
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-01 16:17:48 -04:00
|
|
|
q, err := smodel.FindByID(storyDoc.ID)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
foundDoc := &story{}
|
|
|
|
q.Populate("Author", "Chapters.Bands").Exec(foundDoc)
|
|
|
|
j, _ := q.JSON()
|
|
|
|
fmt.Printf("%s\n", j)
|
|
|
|
})
|
2024-09-04 19:43:12 -04:00
|
|
|
for _, c := range storyDoc.Chapters {
|
|
|
|
assert.NotZero(t, c.Bands[0].Name)
|
|
|
|
}
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-03 00:14:12 -04:00
|
|
|
nb := Create(metallica).(*band)
|
|
|
|
saveDoc(t, nb)
|
2024-09-01 16:17:48 -04:00
|
|
|
nb.Locked = true
|
2024-09-03 00:14:12 -04:00
|
|
|
saveDoc(t, nb)
|
2024-09-01 16:17:48 -04:00
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
foundM := ModelRegistry["band"]
|
2024-09-01 16:17:48 -04:00
|
|
|
q, err := foundM.FindByID(int64(1))
|
2024-09-03 00:14:12 -04:00
|
|
|
assert.Equal(t, nil, err)
|
2024-09-01 16:17:48 -04:00
|
|
|
found := &band{}
|
|
|
|
q.Exec(found)
|
|
|
|
assert.Equal(t, int64(1), found.ID)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, true, found.Locked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModel_FindAll(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
im := iti_multi()
|
|
|
|
createAndSave(t, &im)
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-05 14:29:35 -04:00
|
|
|
query, err := smodel.Find(bson.M{}, options.Find())
|
2024-09-01 16:17:48 -04:00
|
|
|
assert.Equal(t, nil, err)
|
2024-09-05 17:53:04 -04:00
|
|
|
final := CreateSlice(story{})
|
2024-09-01 16:17:48 -04:00
|
|
|
query.Exec(&final)
|
|
|
|
assert.Greater(t, len(final), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModel_PopulateMulti(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band)
|
2024-09-03 00:14:12 -04:00
|
|
|
saveDoc(t, bandDoc)
|
2024-09-05 17:53:04 -04:00
|
|
|
mauthor := Create(author).(*user)
|
|
|
|
saveDoc(t, mauthor)
|
2024-09-12 18:29:12 -04:00
|
|
|
im := iti_multi()
|
|
|
|
im.Author = mauthor
|
|
|
|
createAndSave(t, &im)
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-05 14:29:35 -04:00
|
|
|
query, err := smodel.Find(bson.M{}, options.Find())
|
2024-09-01 16:17:48 -04:00
|
|
|
assert.Equal(t, nil, err)
|
2024-09-05 17:53:04 -04:00
|
|
|
final := CreateSlice(story{})
|
2024-09-01 16:17:48 -04:00
|
|
|
query.Populate("Author", "Chapters.Bands").Exec(&final)
|
|
|
|
assert.Greater(t, len(final), 0)
|
2024-09-03 00:14:12 -04:00
|
|
|
for _, s := range final {
|
|
|
|
assert.NotZero(t, s.Chapters[0].Bands[0].Name)
|
|
|
|
}
|
2024-09-01 16:17:48 -04:00
|
|
|
}
|
2024-09-02 19:32:39 -04:00
|
|
|
|
2024-09-12 17:38:15 -04:00
|
|
|
func TestModel_PopulateChained_Multi(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
im := iti_multi()
|
|
|
|
bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band)
|
2024-09-12 17:38:15 -04:00
|
|
|
saveDoc(t, bandDoc)
|
|
|
|
mauthor := Create(author).(*user)
|
|
|
|
saveDoc(t, mauthor)
|
2024-09-12 18:29:12 -04:00
|
|
|
im.Author = mauthor
|
|
|
|
createAndSave(t, &im)
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-12 17:38:15 -04:00
|
|
|
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()
|
|
|
|
|
2024-09-12 18:29:12 -04:00
|
|
|
bandDoc := Create(iti_single().Chapters[0].Bands[0]).(*band)
|
|
|
|
storyDoc := Create(iti_single()).(*story)
|
2024-09-12 17:38:15 -04:00
|
|
|
mauthor := Create(author).(*user)
|
|
|
|
saveDoc(t, mauthor)
|
|
|
|
saveDoc(t, bandDoc)
|
|
|
|
storyDoc.Author = mauthor
|
|
|
|
saveDoc(t, storyDoc)
|
|
|
|
assert.Greater(t, storyDoc.ID, int64(0))
|
|
|
|
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-12 17:38:15 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 19:32:39 -04:00
|
|
|
func TestModel_Append(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-03 00:14:12 -04:00
|
|
|
bandDoc := Create(metallica).(*band)
|
|
|
|
saveDoc(t, bandDoc)
|
2024-09-14 02:09:38 -04:00
|
|
|
bmodel := ModelRegistry["band"]
|
2024-09-02 19:32:39 -04:00
|
|
|
query, err := bmodel.FindByID(int64(1))
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
fin := &band{}
|
|
|
|
query.Exec(fin)
|
|
|
|
assert.Greater(t, fin.ID, int64(0))
|
2024-09-14 02:09:38 -04:00
|
|
|
err = fin.Append("Characters", "Robert Trujillo")
|
2024-09-02 19:32:39 -04:00
|
|
|
assert.Equal(t, nil, err)
|
2024-09-14 02:09:38 -04:00
|
|
|
saveDoc(t, fin)
|
2024-09-02 19:32:39 -04:00
|
|
|
fin = &band{}
|
|
|
|
query, _ = bmodel.FindByID(int64(1))
|
|
|
|
query.Exec(fin)
|
|
|
|
assert.Greater(t, len(fin.Characters), 4)
|
2024-09-03 00:14:12 -04:00
|
|
|
}
|
2024-09-02 19:32:39 -04:00
|
|
|
|
2024-09-03 00:14:12 -04:00
|
|
|
func TestModel_Delete(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
bandDoc := Create(metallica).(*band)
|
|
|
|
saveDoc(t, bandDoc)
|
|
|
|
err := bandDoc.Delete()
|
|
|
|
assert.Nil(t, err)
|
2024-09-02 19:32:39 -04:00
|
|
|
}
|
2024-09-04 19:43:12 -04:00
|
|
|
|
|
|
|
func TestModel_Pull(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
storyDoc := Create(iti_multi()).(*story)
|
2024-09-14 02:09:38 -04:00
|
|
|
smodel := ModelRegistry["story"]
|
2024-09-04 19:43:12 -04:00
|
|
|
saveDoc(t, storyDoc)
|
|
|
|
err := storyDoc.Pull("Chapters", storyDoc.Chapters[4])
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, storyDoc.ID)
|
|
|
|
saveDoc(t, storyDoc)
|
|
|
|
fin := &story{}
|
|
|
|
query, err := smodel.FindByID(storyDoc.ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
query.Exec(fin)
|
|
|
|
assert.Equal(t, 4, len(fin.Chapters))
|
|
|
|
}
|
2024-09-05 13:57:35 -04:00
|
|
|
|
|
|
|
func TestModel_Swap(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
is := iti_single()
|
|
|
|
is.Author = &author
|
|
|
|
storyDoc := Create(iti_single()).(*story)
|
2024-09-05 17:53:04 -04:00
|
|
|
saveDoc(t, storyDoc)
|
2024-09-12 17:20:17 -04:00
|
|
|
storyDoc.Chapters[0].Bands = append(storyDoc.Chapters[0].Bands, bodom)
|
2024-09-05 13:57:35 -04:00
|
|
|
assert.Equal(t, 2, len(storyDoc.Chapters[0].Bands))
|
|
|
|
err := storyDoc.Swap("Chapters[0].Bands", 0, 1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
c := storyDoc.Chapters[0].Bands
|
2024-09-12 17:20:17 -04:00
|
|
|
assert.Equal(t, bodom.ID, c[0].ID)
|
|
|
|
assert.Equal(t, diamondHead.ID, c[1].ID)
|
2024-09-05 13:57:35 -04:00
|
|
|
saveDoc(t, storyDoc)
|
|
|
|
}
|
2024-09-12 18:10:03 -04:00
|
|
|
|
|
|
|
func TestModel_GridFSLoad(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
ModelRegistry.Model(somethingWithNestedChapters{})
|
2024-09-14 02:09:38 -04:00
|
|
|
model := ModelRegistry["somethingWithNestedChapters"]
|
2024-09-12 18:10:03 -04:00
|
|
|
thingDoc := Create(doSomethingWithNested()).(*somethingWithNestedChapters)
|
|
|
|
found := &somethingWithNestedChapters{}
|
|
|
|
|
|
|
|
saveDoc(t, thingDoc)
|
|
|
|
assert.NotZero(t, thingDoc.ID)
|
|
|
|
fq, err := model.FindByID(thingDoc.ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fq.LoadFile("NestedText", "Chapters.Text").Exec(found)
|
|
|
|
assert.NotZero(t, found.NestedText)
|
|
|
|
assert.NotZero(t, len(found.Chapters))
|
|
|
|
for _, c := range found.Chapters {
|
|
|
|
assert.NotZero(t, c.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 18:29:12 -04:00
|
|
|
func TestModel_GridFSLoad_Chained(t *testing.T) {
|
2024-09-12 18:10:03 -04:00
|
|
|
initTest()
|
2024-09-12 18:29:12 -04:00
|
|
|
ModelRegistry.Model(somethingWithNestedChapters{})
|
2024-09-14 02:09:38 -04:00
|
|
|
model := ModelRegistry["somethingWithNestedChapters"]
|
2024-09-12 18:29:12 -04:00
|
|
|
thingDoc := Create(doSomethingWithNested()).(*somethingWithNestedChapters)
|
|
|
|
found := &somethingWithNestedChapters{}
|
|
|
|
|
2024-09-12 18:10:03 -04:00
|
|
|
saveDoc(t, thingDoc)
|
|
|
|
assert.NotZero(t, thingDoc.ID)
|
|
|
|
fq, err := model.FindByID(thingDoc.ID)
|
|
|
|
assert.Nil(t, err)
|
2024-09-12 18:29:12 -04:00
|
|
|
fq.LoadFile("NestedText").LoadFile("Chapters.Text").Exec(found)
|
|
|
|
assert.NotZero(t, found.NestedText)
|
2024-09-12 18:10:03 -04:00
|
|
|
assert.NotZero(t, len(found.Chapters))
|
|
|
|
for _, c := range found.Chapters {
|
|
|
|
assert.NotZero(t, c.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 18:29:12 -04:00
|
|
|
func TestModel_GridFSLoad_Complex(t *testing.T) {
|
2024-09-12 18:10:03 -04:00
|
|
|
initTest()
|
2024-09-14 02:09:38 -04:00
|
|
|
model := ModelRegistry["story"]
|
2024-09-12 18:29:12 -04:00
|
|
|
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
|
2024-09-12 18:10:03 -04:00
|
|
|
saveDoc(t, thingDoc)
|
|
|
|
assert.NotZero(t, thingDoc.ID)
|
|
|
|
fq, err := model.FindByID(thingDoc.ID)
|
|
|
|
assert.Nil(t, err)
|
2024-09-12 18:29:12 -04:00
|
|
|
fq.Populate("Author", "Chapters.Bands").LoadFile("Chapters.Text").Exec(found)
|
2024-09-12 18:10:03 -04:00
|
|
|
assert.NotZero(t, len(found.Chapters))
|
|
|
|
for _, c := range found.Chapters {
|
|
|
|
assert.NotZero(t, c.Text)
|
2024-09-12 18:29:12 -04:00
|
|
|
assert.NotZero(t, c.Bands[0].Name)
|
2024-09-12 18:10:03 -04:00
|
|
|
}
|
2024-09-12 18:29:12 -04:00
|
|
|
j, _ := fq.JSON()
|
|
|
|
fmt.Printf("%s\n", j)
|
2024-09-12 18:10:03 -04:00
|
|
|
}
|