-
Notifications
You must be signed in to change notification settings - Fork 5
/
hooks_test.go
51 lines (38 loc) · 1.22 KB
/
hooks_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
package colt
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type testDoc struct {
mock.Mock `bson:"-"`
DocWithTimestamps `bson:",inline"`
Title string `bson:"title" json:"title"`
}
func (t *testDoc) BeforeInsert() error {
t.DocWithTimestamps.BeforeInsert()
args := t.Called()
return args.Error(0)
}
func (t *testDoc) BeforeUpdate() error {
t.DocWithTimestamps.BeforeUpdate()
args := t.Called()
return args.Error(0)
}
var mockDb = Database{}
func TestBeforeInsertHook(t *testing.T) {
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
doc := testDoc{Title: "Test"}
doc.On("BeforeInsert").Return(nil)
_, insertErr := GetCollection[*testDoc](&mockDb, "testdocs").Insert(&doc)
assert.Nil(t, insertErr)
doc.AssertExpectations(t)
}
func TestBeforeUpdateHook(t *testing.T) {
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
doc := testDoc{Title: "Test"}
doc.On("BeforeUpdate").Return(nil)
updateErr := GetCollection[*testDoc](&mockDb, "testdocs").UpdateById(doc.ID, &doc)
assert.Nil(t, updateErr)
doc.AssertExpectations(t)
}