|
| 1 | +package oplog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.mongodb.org/mongo-driver/bson" |
| 9 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 10 | + "go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetUUIDForNS(t *testing.T) { |
| 14 | + mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) |
| 15 | + |
| 16 | + mt.Run("successful response from db", func(mt *mtest.T) { |
| 17 | + expectedUUID := primitive.Binary{Subtype: 0xFF, Data: []byte{0x01, 0x02, 0x03}} |
| 18 | + listCollRes := bson.D{ |
| 19 | + {"name", "c1"}, |
| 20 | + {"type", "collection"}, |
| 21 | + {"info", bson.D{ |
| 22 | + {"readOnly", false}, |
| 23 | + {"uuid", expectedUUID}, |
| 24 | + }}, |
| 25 | + } |
| 26 | + mt.AddMockResponses(mtest.CreateCursorResponse(1, "mydb.c1", mtest.FirstBatch, listCollRes)) |
| 27 | + |
| 28 | + db := newMDB(mt.Client) |
| 29 | + uuid, err := db.getUUIDForNS(context.Background(), "mydb.c1") |
| 30 | + if err != nil { |
| 31 | + t.Errorf("got err=%v", err) |
| 32 | + } |
| 33 | + primitive.NewObjectID() |
| 34 | + |
| 35 | + if !uuid.Equal(expectedUUID) { |
| 36 | + t.Errorf("wrong uuid for ns: expected=%v, got=%v", expectedUUID, uuid) |
| 37 | + } |
| 38 | + t.Log(uuid) |
| 39 | + }) |
| 40 | + |
| 41 | + mt.Run("failed response from db", func(mt *mtest.T) { |
| 42 | + errRes := mtest.CreateCommandErrorResponse(mtest.CommandError{ |
| 43 | + Code: 11601, |
| 44 | + Name: "error", |
| 45 | + Message: "querying list collections", |
| 46 | + }) |
| 47 | + mt.AddMockResponses(errRes) |
| 48 | + db := newMDB(mt.Client) |
| 49 | + _, err := db.getUUIDForNS(context.Background(), "mydb.c1") |
| 50 | + if err == nil { |
| 51 | + t.Error("expected to get error from getUUIDForNS") |
| 52 | + } |
| 53 | + if !strings.Contains(err.Error(), "list collections") { |
| 54 | + t.Error("wrong err") |
| 55 | + } |
| 56 | + }) |
| 57 | +} |
0 commit comments