-
Notifications
You must be signed in to change notification settings - Fork 60
/
testhelpers_test.go
53 lines (42 loc) · 1.07 KB
/
testhelpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package mgm_test
import (
"github.com/kamva/mgm/v3"
"github.com/kamva/mgm/v3/internal/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"testing"
)
func setupDefConnection() {
util.PanicErr(
mgm.SetDefaultConfig(nil, "models", options.Client().ApplyURI("mongodb://root:12345@localhost:27017")),
)
}
func resetCollection() {
_, err := mgm.Coll(&Doc{}).DeleteMany(mgm.Ctx(), bson.M{})
_, err2 := mgm.Coll(&Person{}).DeleteMany(mgm.Ctx(), bson.M{})
util.PanicErr(err)
util.PanicErr(err2)
}
func seed() {
docs := []interface{}{
NewDoc("Ali", 24),
NewDoc("Mehran", 24),
NewDoc("Reza", 26),
NewDoc("Omid", 27),
}
_, err := mgm.Coll(&Doc{}).InsertMany(mgm.Ctx(), docs)
util.PanicErr(err)
}
func findDoc(t *testing.T) *Doc {
found := &Doc{}
util.AssertErrIsNil(t, mgm.Coll(found).FindOne(mgm.Ctx(), bson.M{}).Decode(found))
return found
}
type Doc struct {
mgm.DefaultModel `bson:",inline"`
Name string `bson:"name"`
Age int `bson:"age"`
}
func NewDoc(name string, age int) *Doc {
return &Doc{Name: name, Age: age}
}