-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfind_test.go
64 lines (55 loc) · 1.68 KB
/
find_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
package mongo_go_driver_mock
import (
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)
func TestFindOne(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("success", func(mt *mtest.T) {
userCollection = mt.Coll
expectedUser := user{
ID: primitive.NewObjectID(),
Name: "john",
Email: "[email protected]",
}
mt.AddMockResponses(mtest.CreateCursorResponse(1, "foo.bar", mtest.FirstBatch, bson.D{
{"_id", expectedUser.ID},
{"name", expectedUser.Name},
{"email", expectedUser.Email},
}))
userResponse, err := getFromID(expectedUser.ID)
assert.Nil(t, err)
assert.Equal(t, &expectedUser, userResponse)
})
}
func TestFind(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
defer mt.Close()
mt.Run("success", func(mt *mtest.T) {
userCollection = mt.Coll
id1 := primitive.NewObjectID()
id2 := primitive.NewObjectID()
first := mtest.CreateCursorResponse(1, "foo.bar", mtest.FirstBatch, bson.D{
{"_id", id1},
{"name", "john"},
{"email", "[email protected]"},
})
second := mtest.CreateCursorResponse(1, "foo.bar", mtest.NextBatch, bson.D{
{"_id", id2},
{"name", "john"},
{"email", "[email protected]"},
})
killCursors := mtest.CreateCursorResponse(0, "foo.bar", mtest.NextBatch)
mt.AddMockResponses(first, second, killCursors)
users, err := find("john")
assert.Nil(t, err)
assert.Equal(t, []user{
{ID: id1, Name: "john", Email: "[email protected]"},
{ID: id2, Name: "john", Email: "[email protected]"},
}, users)
})
}