-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomgo_test.go
85 lines (70 loc) · 1.63 KB
/
omgo_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package omgo
import (
"testing"
"time"
"github.com/yoer/omgo"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
mg_collection = "omgo_test_collection"
mg_instance = "mg_instance"
)
var (
mg_user = ""
mg_pass = ""
mg_host = "127.0.0.1:27017"
mg_database = ""
)
func init() {
omgo.AppendSession(mg_instance, &MgoDBCfg{
User: mg_user,
Pass: mg_pass,
Host: mg_host,
DB: mg_database,
})
}
// golang structure in mongoDB
type DongoData struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:`
Date bson.MongoTimestamp `bson:"date"`
}
func TestInsert(t *testing.T) {
if err := omgo.RunMgFun(mg_instance, mg_collection, func(c *mgo.Collection) error {
return c.Insert(&DongoData{
Id: bson.NewObjectId(),
Name: "yoer",
Date: bson.MongoTimestamp(time.Now().UnixNano()),
})
}); nil != err {
t.Error(err.Error())
}
}
func TestUpdate(t *testing.T) {
if err := omgo.RunMgFun(mg_instance, mg_collection, func(c *mgo.Collection) error {
return c.Update(bson.M{"name": "yoer"}, bson.M{"name": "yoer", "date": time.Now().UnixNano()})
}); nil != err {
t.Error(err.Error())
}
}
func TestFind(t *testing.T) {
find := &DongoData{}
if err := omgo.RunMgFun(mg_instance, mg_collection, func(c *mgo.Collection) error {
return c.Find(bson.M{"name": "yoer"}).One(find)
}); nil != err {
t.Error(err.Error())
}
t.Log(find)
}
func TestCount(t *testing.T) {
count := 0
if err := omgo.RunMgFun(mg_instance, mg_collection, func(c *mgo.Collection) error {
rs, err := c.Find(bson.M{"name": "yoer"}).Count()
count = rs
return err
}); nil != err {
t.Error(err.Error())
}
t.Log(count)
}