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()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPopulate(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2024-09-03 00:14:12 -04:00
|
|
|
assert.NotZero(t, storyDoc.Chapters[0].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
|
|
|
|
|
|
|
foundM := Create(band{}).(*band)
|
|
|
|
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-03 00:14:12 -04:00
|
|
|
createAndSave(t, &iti_multi)
|
2024-09-01 16:17:48 -04:00
|
|
|
smodel := Create(story{}).(*story)
|
|
|
|
query, err := smodel.FindAll(bson.M{}, options.Find())
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
final := make([]story, 0)
|
|
|
|
query.Exec(&final)
|
|
|
|
assert.Greater(t, len(final), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModel_PopulateMulti(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-03 00:14:12 -04:00
|
|
|
bandDoc := Create(iti_single.Chapters[0].Bands[0]).(*band)
|
|
|
|
saveDoc(t, bandDoc)
|
|
|
|
createAndSave(t, &iti_multi)
|
2024-09-01 16:17:48 -04:00
|
|
|
smodel := Create(story{}).(*story)
|
|
|
|
query, err := smodel.FindAll(bson.M{}, options.Find())
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
final := make([]story, 0)
|
|
|
|
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
|
|
|
|
|
|
|
func TestModel_Append(t *testing.T) {
|
|
|
|
initTest()
|
2024-09-03 00:14:12 -04:00
|
|
|
bandDoc := Create(metallica).(*band)
|
|
|
|
saveDoc(t, bandDoc)
|
2024-09-02 19:32:39 -04:00
|
|
|
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)
|
2024-09-03 00:14:12 -04:00
|
|
|
saveDoc(t, bmodel)
|
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
|
|
|
}
|