-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.mongo_test.go
92 lines (74 loc) · 1.98 KB
/
db.mongo_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
package mongodbhelper_test
import (
"errors"
"github.com/benacook/mongodbhelper/mock"
"github.com/golang/mock/gomock"
"testing"
)
func TestConnect(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockDoer := mock_mongodbhelper.NewMockMongoInterface(mockCtrl)
mdb := mockDoer
// Expect Do to be called once with 123 and "Hello GoMock" as parameters, and return nil from the mocked call.
mockDoer.EXPECT().Connect("mongodb://mongo:27017").Return(nil).Times(1)
mockDoer.EXPECT().Connect("").Return(errors.New(
"invalid host")).Times(1)
err := mdb.Connect("mongodb://mongo:27017")
if err != nil {
t.Fail()
}
err = mdb.Connect("")
if err == nil {
t.Fail()
}
}
type dummyRecord struct {
Name string
Age int
}
func TestInsertElement(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockDoer := mock_mongodbhelper.NewMockMongoInterface(mockCtrl)
mdb := mockDoer
dr := dummyRecord{"ben", 27}
// Expect Do to be called once with 123 and "Hello GoMock" as parameters, and return nil from the mocked call.
mockDoer.EXPECT().InsertElement(dr).Return(nil).Times(1)
mockDoer.EXPECT().InsertElement(nil).Return(errors.New("no element to insert")).
Times(1)
err := mdb.InsertElement(dr)
if err != nil {
t.Fail()
}
err = mdb.InsertElement(nil)
if err == nil {
t.Fail()
}
}
func TestGetLatest(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockDoer := mock_mongodbhelper.NewMockMongoInterface(mockCtrl)
mdb := mockDoer
dr := dummyRecord{"ben", 27}
// Expect Do to be called once with 123 and "Hello GoMock" as parameters, and return nil from the mocked call.
mockDoer.EXPECT().GetLatest().Return(dr, nil).Times(1)
mockDoer.EXPECT().GetLatest().Return(dummyRecord{},
errors.New("no element to insert")).
Times(1)
res, err := mdb.GetLatest()
if err != nil {
t.Fail()
}
if res != dr{
t.Fail()
}
res, err = mdb.GetLatest()
if err == nil {
t.Fail()
}
if res == dr{
t.Fail()
}
}