Skip to content

Commit

Permalink
Add tests for getUUIDForNS
Browse files Browse the repository at this point in the history
mtest mock is used for mocking MongoClient.
  • Loading branch information
boris-ilijic committed Dec 20, 2024
1 parent 7f65c29 commit 8d9407d
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pbm/oplog/db_test.go
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")
}
})
}

0 comments on commit 8d9407d

Please sign in to comment.