Skip to content

Commit 21d7a89

Browse files
Merge pull request #45 from elcengine/test/increase-coverage
test: increased coverage
2 parents a6c5dbe + 358c7ce commit 21d7a89

7 files changed

Lines changed: 91 additions & 10 deletions

core/model_plugins.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package elemental
22

3-
import "github.com/elcengine/elemental/plugins/filterquery"
3+
import (
4+
"github.com/elcengine/elemental/plugins/filterquery"
5+
)
46

57
// QS allows you to construct an Elemental query directly from a request's query string.
68
//
@@ -23,7 +25,9 @@ func (m Model[T]) QSR(result fq.Result) Model[T] {
2325
m = m.Find(result.Filters)
2426
}
2527
if len(result.Include) > 0 {
26-
m = m.Populate(result.Include)
28+
for _, field := range result.Include {
29+
m = m.Populate(field)
30+
}
2731
}
2832
if len(result.SecondaryFilters) > 0 {
2933
m = m.Find(result.SecondaryFilters)

core/model_utils.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,21 @@ func (m Model[T]) findMatchStage() bson.M {
122122
}
123123

124124
func (m Model[T]) parseDocument(doc any) primitive.M {
125-
if reflect.TypeOf(doc).Kind() == reflect.Ptr {
125+
docType := reflect.TypeOf(doc).Kind()
126+
if docType == reflect.Ptr {
126127
doc = reflect.ValueOf(doc).Elem().Interface()
127128
}
128-
if reflect.TypeOf(doc).Kind() == reflect.Map {
129+
if docType == reflect.Map {
129130
return utils.Cast[primitive.M](doc)
130131
}
131-
result := utils.ToBSONDoc(doc)
132-
for k, v := range *result {
133-
if !reflect.ValueOf(v).IsValid() || reflect.ValueOf(v).IsZero() {
134-
delete(*result, k)
132+
result := *utils.ToBSONDoc(doc)
133+
for k, v := range result {
134+
fieldValue := reflect.ValueOf(v)
135+
if !fieldValue.IsValid() || fieldValue.IsZero() {
136+
delete(result, k)
135137
}
136138
}
137-
return *result
139+
return result
138140
}
139141

140142
func parseUpdateOptions[T any, O any](m Model[T], opts []*O) []*O {

tests/core_delete_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package tests
22

33
import (
4+
"errors"
45
"testing"
56

67
"go.mongodb.org/mongo-driver/bson/primitive"
78

89
"github.com/elcengine/elemental/tests/fixtures/mocks"
910
ts "github.com/elcengine/elemental/tests/fixtures/setup"
11+
"github.com/google/uuid"
1012
. "github.com/smartystreets/goconvey/convey"
1113
)
1214

@@ -46,6 +48,21 @@ func TestCoreDelete(t *testing.T) {
4648
UserModel.Delete(user).Exec()
4749
So(UserModel.FindByID(user.ID).Exec(), ShouldBeNil)
4850
})
51+
Convey("Delete a user using fail with", func() {
52+
user := UserModel.FindOne().ExecT()
53+
So(user.Name, ShouldEqual, mocks.Caranthir.Name)
54+
55+
So(func() {
56+
UserModel.SetConnection(uuid.NewString()).DeleteByID(user.ID).OrFail().Exec()
57+
}, ShouldPanicWith, errors.New("no results found matching the given query"))
58+
59+
Convey("With custom error", func() {
60+
err := errors.New("some custom error")
61+
So(func() {
62+
UserModel.SetConnection(uuid.NewString()).DeleteByID(user.ID).OrFail(err).Exec()
63+
}, ShouldPanicWith, err)
64+
})
65+
})
4966
Convey("Delete a user by ID", func() {
5067
user := UserModel.FindOne().ExecT()
5168
So(user.Name, ShouldEqual, mocks.Caranthir.Name)

tests/core_middleware_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ func TestCoreMiddleware(t *testing.T) {
3636
return true
3737
})
3838

39+
CastleModel.PostSave(func(castle Castle) bool {
40+
invokedHooks["postSaveSecond"] = true
41+
return false
42+
})
43+
44+
CastleModel.PostSave(func(castle Castle) bool {
45+
invokedHooks["postSaveThird"] = true
46+
return true
47+
})
48+
3949
CastleModel.PreUpdateOne(func(doc any) bool {
4050
invokedHooks["preUpdateOne"] = true
4151
return true
@@ -140,6 +150,8 @@ func TestCoreMiddleware(t *testing.T) {
140150
Convey("Post hooks", t, func() {
141151
Convey("Save", func() {
142152
So(invokedHooks["postSave"], ShouldBeTrue)
153+
So(invokedHooks["postSaveSecond"], ShouldBeTrue)
154+
So(invokedHooks["postSaveThird"], ShouldBeFalse)
143155
})
144156
Convey("UpdateOne", func() {
145157
So(invokedHooks["postUpdateOne"], ShouldBeTrue)

tests/core_soft_delete_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func TestCoreSoftDelete(t *testing.T) {
8888
}
8989

9090
So(UserModel.CountDocuments().Exec(), ShouldEqual, 0)
91+
92+
So(UserModel.Distinct("name").Exec(), ShouldBeEmpty)
9193
})
9294
})
9395
}

tests/core_update_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ func TestCoreUpdate(t *testing.T) {
5656
}).Upsert().Exec()
5757
So(UserModel.Where("name", "Triss").Exec(), ShouldHaveLength, 1)
5858
})
59+
Convey("Update user with upsert within options", func() {
60+
UserModel.UpdateOne(&primitive.M{"name": "Letho"}, User{
61+
Name: "Letho",
62+
}, &options.UpdateOptions{
63+
Upsert: lo.ToPtr(true),
64+
}).Exec()
65+
So(UserModel.Where("name", "Letho").Exec(), ShouldHaveLength, 1)
66+
})
67+
Convey("Update a user with a pointer document", func() {
68+
user := User{
69+
Name: "Foltest",
70+
Age: 50,
71+
}
72+
UserModel.Create(user).Exec()
73+
UserModel.UpdateOne(&primitive.M{"name": user.Name}, &User{
74+
Age: 51,
75+
}).Exec()
76+
So(UserModel.FindOne().Where("name", user.Name).ExecT().Age, ShouldEqual, 51)
77+
})
5978
Convey("Update user by ID", func() {
6079
user := UserModel.FindOne().Where("name", "Triss").ExecT()
6180
UserModel.UpdateByID(user.ID, User{
@@ -103,7 +122,7 @@ func TestCoreUpdate(t *testing.T) {
103122
Weapons: []string{"Dagger"},
104123
}).Exec()
105124
users := UserModel.Where("weapons", "Dagger").ExecTT()
106-
So(users, ShouldHaveLength, len(mocks.Users)+1)
125+
So(users, ShouldHaveLength, len(mocks.Users)+3)
107126
})
108127
Convey("Increment age of a user", func() {
109128
UserModel.Where("name", mocks.Vesemir.Name).Inc("age", 1).Exec()

tests/plugin_filter_query_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,30 @@ func TestPluginFilterQuery(t *testing.T) {
253253
So(result.Docs[0].Name, ShouldEqual, mocks.Ciri.Name)
254254
So(result.Docs[1].Name, ShouldEqual, mocks.Geralt.Name)
255255
})
256+
Convey("When an include is present in query string", func() {
257+
MonsterModel := MonsterModel.SetDatabase(t.Name())
258+
KingdomModel := KingdomModel.SetDatabase(t.Name())
259+
BestiaryModel := BestiaryModel.SetDatabase(t.Name())
260+
261+
monster := MonsterModel.Create(Monster{
262+
Name: "Katakan",
263+
Category: "Vampire",
264+
}).ExecT()
265+
266+
kingdom := KingdomModel.Create(Kingdom{
267+
Name: "Nilfgaard",
268+
}).ExecT()
269+
270+
BestiaryModel.Create(Bestiary{
271+
Monster: monster,
272+
Kingdom: kingdom,
273+
}).Exec()
274+
275+
bestiaries := BestiaryModel.QS("include=monster,kingdom").ExecTT()
276+
So(bestiaries, ShouldHaveLength, 1)
277+
So(bestiaries[0].Monster.Name, ShouldEqual, monster.Name)
278+
So(bestiaries[0].Monster.Category, ShouldEqual, monster.Category)
279+
So(bestiaries[0].Kingdom.Name, ShouldEqual, kingdom.Name)
280+
})
256281
})
257282
}

0 commit comments

Comments
 (0)