-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb_test.go
192 lines (147 loc) · 4.05 KB
/
mongodb_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mds
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"encoding/json"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type (
Person struct {
Name string
Phone string
}
PersonModel struct {
*Collection
Name string `json:"name"`
Phone string `json:"phone"`
ID bson.ObjectId `json:"_id" bson:"_id"`
}
)
func (p *PersonModel) One() {
defer p.Close()
name := "Ale"
p.Find(bson.M{"name": name}).One(&p)
}
func NewPersonalModel(c *Collection) *PersonModel {
model := &PersonModel{
&Collection{c.Collection, c.Session},
"",
"",
bson.NewObjectId(),
}
return model
}
var DATABASE string = "Mds_Test"
var DATABASE_1 string = "Mds_Test1"
var COLLECTION string = "people"
func TestMongoDB(t *testing.T) {
ds := &MongoDB{
Use: true,
Dn: "GoTest",
Type: MONGODB,
DialInfo: &mgo.DialInfo{
Addrs: []string{"localhost"},
Database: DATABASE,
},
}
Convey("MongoDB operation", t, func() {
Convey("Connect()", func() {
// default session
session, err := ds.GetSession(false)
So(session, ShouldEqual, nil)
So(err, ShouldNotEqual, nil)
err = ds.Connect()
So(err, ShouldEqual, nil)
So(ds.Connected, ShouldEqual, true)
err = ds.Connect()
So(err, ShouldNotEqual, true)
})
Convey("GetDatabase() (default session)", func() {
db, err := ds.GetDataBase("", false)
So(db.Name, ShouldEqual, DATABASE)
So(err, ShouldEqual, nil)
db, err = ds.GetDataBase(DATABASE_1, false)
So(db.Name, ShouldEqual, DATABASE_1)
So(err, ShouldEqual, nil)
})
Convey("GetDatabase() (make session)", func() {
db, err := ds.GetDataBase("", true)
So(db.Name, ShouldEqual, DATABASE)
So(err, ShouldEqual, nil)
defer db.Session.Close()
db, err = ds.GetDataBase(DATABASE_1, true)
So(db.Name, ShouldEqual, DATABASE_1)
So(err, ShouldEqual, nil)
defer db.Session.Close()
})
Convey("GetCollection (default session)", func() {
// Original Session
col, err := ds.GetCollection(COLLECTION, false)
So(col.Session, ShouldEqual, ds.Session)
So(err, ShouldEqual, nil)
})
Convey("GetCollection (make session)", func() {
// Original Session
col, err := ds.GetCollection(COLLECTION, true)
So(col.Session, ShouldNotEqual, ds.Session)
So(err, ShouldEqual, nil)
defer col.Session.Close()
})
Convey("GetCollectionWithoutErr (default session, not exit)", func() {
// Original Session
col := ds.GetCollectionWithoutErr(COLLECTION, false, false)
So(col.Session, ShouldEqual, ds.Session)
})
Convey("GetCollectionWithoutErr (make session, not exit)", func() {
// Original Session
col := ds.GetCollectionWithoutErr(COLLECTION, true, false)
So(col.Session, ShouldNotEqual, ds.Session)
defer col.Session.Close()
})
Convey("GetCollectionWithoutErr (default session, exit)", func() {
// Original Session
col := ds.GetCollectionWithoutErr(COLLECTION, false, true)
So(col.Session, ShouldEqual, ds.Session)
})
Convey("GetCollectionWithoutErr (make session, exit)", func() {
// Original Session
col := ds.GetCollectionWithoutErr(COLLECTION, true, true)
So(col.Session, ShouldNotEqual, ds.Session)
defer col.Session.Close()
})
Convey("Query", func() {
name := "Ale"
phone := "+55 53 8116 9639"
var err error
c, _ := ds.GetCollection(COLLECTION, false)
err = c.Insert(&Person{
Name: name,
Phone: phone,
},
&Person{"Cla", "+55 53 8402 8510"},
)
So(err, ShouldEqual, nil)
result := Person{}
err = c.Find(bson.M{"name": name}).One(&result)
So(err, ShouldEqual, nil)
So(result.Name, ShouldEqual, name)
So(result.Phone, ShouldEqual, phone)
})
Convey("Model", func() {
c, _ := ds.GetCollection(COLLECTION, true)
model := NewPersonalModel(c)
model.One()
So(model.Phone, ShouldEqual, "+55 53 8116 9639")
_, err := json.Marshal(model)
So(err, ShouldBeNil)
})
Convey("MongoDB.String()", func() {
So(ds.String(), ShouldNotEqual, "")
})
Convey("Close original session", func() {
ds.Session.Close()
So(ds.Session, ShouldNotEqual, true)
})
})
}