diamond-orm/model_test.go
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 4297071b3b
restructuring
- use pointers where possible when using reflection
- add a `CreateSlice` func, for storing slices of Models created by Find and FindPaged queries
- modify tests to use `CreateSlice`
2024-09-05 17:53:04 -04:00

167 lines
4.2 KiB
Go

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()
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)
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)
for _, c := range storyDoc.Chapters {
assert.NotZero(t, c.ChapterID)
}
}
func TestPopulate(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", "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 TestUpdate(t *testing.T) {
initTest()
nb := Create(metallica).(*band)
saveDoc(t, nb)
nb.Locked = true
saveDoc(t, nb)
foundM := Create(band{}).(*band)
q, err := foundM.FindByID(int64(1))
assert.Equal(t, nil, err)
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()
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.Exec(&final)
assert.Greater(t, len(final), 0)
}
func TestModel_PopulateMulti(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", "Chapters.Bands").Exec(&final)
assert.Greater(t, len(final), 0)
for _, s := range final {
assert.NotZero(t, s.Chapters[0].Bands[0].Name)
}
}
func TestModel_Append(t *testing.T) {
initTest()
bandDoc := Create(metallica).(*band)
saveDoc(t, bandDoc)
bmodel := Create(band{}).(*band)
query, err := bmodel.FindByID(int64(1))
assert.Equal(t, nil, err)
fin := &band{}
query.Exec(fin)
assert.Greater(t, fin.ID, int64(0))
err = bmodel.Append("Characters", "Robert Trujillo")
assert.Equal(t, nil, err)
saveDoc(t, bmodel)
fin = &band{}
query, _ = bmodel.FindByID(int64(1))
query.Exec(fin)
assert.Greater(t, len(fin.Characters), 4)
}
func TestModel_Delete(t *testing.T) {
initTest()
bandDoc := Create(metallica).(*band)
saveDoc(t, bandDoc)
err := bandDoc.Delete()
assert.Nil(t, err)
}
func TestModel_Pull(t *testing.T) {
initTest()
storyDoc := Create(iti_multi).(*story)
smodel := Create(story{}).(*story)
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))
}
func TestModel_Swap(t *testing.T) {
initTest()
iti_single.Author = &author
storyDoc := Create(iti_single).(*story)
saveDoc(t, storyDoc)
storyDoc.Chapters[0].Bands = append(storyDoc.Chapters[0].Bands, metallica)
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
assert.Equal(t, metallica.ID, c[0].ID)
assert.Equal(t, dh.ID, c[1].ID)
saveDoc(t, storyDoc)
}