-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mtest mock is used for mocking MongoClient.
- Loading branch information
1 parent
7f65c29
commit 8d9407d
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package oplog | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo/integration/mtest" | ||
) | ||
|
||
func TestGetUUIDForNS(t *testing.T) { | ||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) | ||
|
||
mt.Run("successful response from db", func(mt *mtest.T) { | ||
expectedUUID := primitive.Binary{Subtype: 0xFF, Data: []byte{0x01, 0x02, 0x03}} | ||
listCollRes := bson.D{ | ||
{"name", "c1"}, | ||
{"type", "collection"}, | ||
{"info", bson.D{ | ||
{"readOnly", false}, | ||
{"uuid", expectedUUID}, | ||
}}} | ||
mt.AddMockResponses(mtest.CreateCursorResponse(1, "mydb.c1", mtest.FirstBatch, listCollRes)) | ||
|
||
db := newMDB(mt.Client) | ||
uuid, err := db.getUUIDForNS(context.Background(), "mydb.c1") | ||
if err != nil { | ||
t.Errorf("got err=%v", err) | ||
} | ||
primitive.NewObjectID() | ||
|
||
if !uuid.Equal(expectedUUID) { | ||
t.Errorf("wrong uuid for ns: expected=%v, got=%v", expectedUUID, uuid) | ||
} | ||
t.Log(uuid) | ||
}) | ||
|
||
mt.Run("failed response from db", func(mt *mtest.T) { | ||
errRes := mtest.CreateCommandErrorResponse(mtest.CommandError{ | ||
Code: 11601, | ||
Name: "error", | ||
Message: "querying list collections", | ||
}) | ||
mt.AddMockResponses(errRes) | ||
db := newMDB(mt.Client) | ||
_, err := db.getUUIDForNS(context.Background(), "mydb.c1") | ||
if err == nil { | ||
t.Error("expected to get error from getUUIDForNS") | ||
} | ||
if !strings.Contains(err.Error(), "list collections") { | ||
t.Error("wrong err") | ||
} | ||
}) | ||
} |