public void TestCreateIndexTimeToLive()
        {
            if (_server.BuildInfo.Version >= new Version(2, 2))
            {
                _collection.DropAllIndexes();
                Assert.AreEqual(1, _collection.GetIndexes().Count());

                var keys = IndexKeys.Ascending("ts");
                var options = IndexOptions.SetTimeToLive(TimeSpan.FromHours(1));
                var result = _collection.CreateIndex(keys, options);

                var expectedResult = new ExpectedWriteConcernResult();
                CheckExpectedResult(expectedResult, result);

                var indexes = _collection.GetIndexes();
                Assert.AreEqual("_id_", indexes[0].Name);
                Assert.AreEqual("ts_1", indexes[1].Name);
                Assert.AreEqual(TimeSpan.FromHours(1), indexes[1].TimeToLive);
            }
        }
 // private methods
 private void CheckExpectedResult(ExpectedWriteConcernResult expectedResult, WriteConcernResult result)
 {
     Assert.AreEqual(expectedResult.DocumentsAffected ?? 0, result.DocumentsAffected);
     Assert.AreEqual(expectedResult.HasLastErrorMessage ?? false, result.HasLastErrorMessage);
     if (expectedResult.LastErrorMessage != null)
     {
         Assert.AreEqual(expectedResult.LastErrorMessage, result.LastErrorMessage);
     }
     Assert.AreEqual(expectedResult.Upserted, result.Upserted);
     Assert.AreEqual(expectedResult.UpdatedExisting ?? false, result.UpdatedExisting);
 }
        public void TestCreateIndex()
        {
            var expectedIndexVersion = (_server.BuildInfo.Version >= new Version(2, 0, 0)) ? 1 : 0;

            _collection.Insert(new BsonDocument("x", 1));
            _collection.DropAllIndexes(); // doesn't drop the index on _id

            var indexes = _collection.GetIndexes().ToList();
            Assert.AreEqual(1, indexes.Count);
            Assert.AreEqual(false, indexes[0].DroppedDups);
            Assert.AreEqual(false, indexes[0].IsBackground);
            Assert.AreEqual(false, indexes[0].IsSparse);
            Assert.AreEqual(false, indexes[0].IsUnique);
            Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
            Assert.AreEqual("_id_", indexes[0].Name);
            Assert.AreEqual(_collection.FullName, indexes[0].Namespace);
            Assert.AreEqual(expectedIndexVersion, indexes[0].Version);

            _collection.DropAllIndexes();
            var result = _collection.CreateIndex("x");

            var expectedResult = new ExpectedWriteConcernResult();
            CheckExpectedResult(expectedResult, result);

            indexes = _collection.GetIndexes().OrderBy(x => x.Name).ToList();
            Assert.AreEqual(2, indexes.Count);
            Assert.AreEqual(false, indexes[0].DroppedDups);
            Assert.AreEqual(false, indexes[0].IsBackground);
            Assert.AreEqual(false, indexes[0].IsSparse);
            Assert.AreEqual(false, indexes[0].IsUnique);
            Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
            Assert.AreEqual("_id_", indexes[0].Name);
            Assert.AreEqual(_collection.FullName, indexes[0].Namespace);
            Assert.AreEqual(expectedIndexVersion, indexes[0].Version);
            Assert.AreEqual(false, indexes[1].DroppedDups);
            Assert.AreEqual(false, indexes[1].IsBackground);
            Assert.AreEqual(false, indexes[1].IsSparse);
            Assert.AreEqual(false, indexes[1].IsUnique);
            Assert.AreEqual(new BsonDocument("x", 1), indexes[1].Key);
            Assert.AreEqual("x_1", indexes[1].Name);
            Assert.AreEqual(_collection.FullName, indexes[1].Namespace);
            Assert.AreEqual(expectedIndexVersion, indexes[1].Version);

            _collection.DropAllIndexes();
            var options = IndexOptions.SetBackground(true).SetDropDups(true).SetSparse(true).SetUnique(true);
            result = _collection.CreateIndex(IndexKeys.Ascending("x").Descending("y"), options);

            expectedResult = new ExpectedWriteConcernResult();
            CheckExpectedResult(expectedResult, result);

            indexes = _collection.GetIndexes().OrderBy(x => x.Name).ToList();
            Assert.AreEqual(2, indexes.Count);
            Assert.AreEqual(false, indexes[0].DroppedDups);
            Assert.AreEqual(false, indexes[0].IsBackground);
            Assert.AreEqual(false, indexes[0].IsSparse);
            Assert.AreEqual(false, indexes[0].IsUnique);
            Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
            Assert.AreEqual("_id_", indexes[0].Name);
            Assert.AreEqual(_collection.FullName, indexes[0].Namespace);
            Assert.AreEqual(expectedIndexVersion, indexes[0].Version);
            Assert.AreEqual(true, indexes[1].DroppedDups);
            Assert.AreEqual(true, indexes[1].IsBackground);
            Assert.AreEqual(true, indexes[1].IsSparse);
            Assert.AreEqual(true, indexes[1].IsUnique);
            Assert.AreEqual(new BsonDocument { { "x", 1 }, { "y", -1 } }, indexes[1].Key);
            Assert.AreEqual("x_1_y_-1", indexes[1].Name);
            Assert.AreEqual(_collection.FullName, indexes[1].Namespace);
            Assert.AreEqual(expectedIndexVersion, indexes[1].Version);
        }
        public void TestUpsertExisting()
        {
            _collection.Drop();
            _collection.Insert(new BsonDocument("x", 1));
            var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2), UpdateFlags.Upsert);

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 1,
                UpdatedExisting = true
            };
            CheckExpectedResult(expectedResult, result);

            var document = _collection.FindOne();
            Assert.AreEqual(2, document["x"].AsInt32);
            Assert.AreEqual(1, _collection.Count());
        }
        public void TestUpsertInsert()
        {
            _collection.Drop();
            var id = new BsonObjectId(ObjectId.GenerateNewId());
            var result = _collection.Update(Query.EQ("_id", id), Update.Set("x", 2), UpdateFlags.Upsert);

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 1,
                Upserted = id
            };
            CheckExpectedResult(expectedResult, result);

            var document = _collection.FindOne();
            Assert.AreEqual(2, document["x"].AsInt32);
            Assert.AreEqual(1, _collection.Count());
        }
        public void TestRemoveNoMatchingDocument()
        {
            _collection.Drop();
            var result = _collection.Remove(Query.EQ("x", 1));

            var expectedResult = new ExpectedWriteConcernResult
            {
                UpdatedExisting = false
            };
            CheckExpectedResult(expectedResult, result);

            Assert.AreEqual(0, _collection.Count());
        }
        public void TestUpdateNoMatchingDocument()
        {
            _collection.Drop();
            var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2));

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 0
            };
            CheckExpectedResult(expectedResult, result);

            Assert.AreEqual(0, _collection.Count());
        }
        public void TestLenientWrite()
        {
            var settings = new MongoCollectionSettings { WriteEncoding = new UTF8Encoding(false, false) };
            var collection = _database.GetCollection(Configuration.TestCollection.Name, settings);

            var document = new BsonDocument("x", "\udc00"); // invalid lone low surrogate
            var result = collection.Save(document);

            var expectedResult = new ExpectedWriteConcernResult();
            CheckExpectedResult(expectedResult, result);

            var rehydrated = collection.FindOne(Query.EQ("_id", document["_id"]));
            Assert.AreEqual("\ufffd", rehydrated["x"].AsString);
        }
        public void TestRemove()
        {
            _collection.Drop();
            _collection.Insert(new BsonDocument("x", 1));
            var result = _collection.Remove(Query.EQ("x", 1));

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 1
            };
            CheckExpectedResult(expectedResult, result);

            Assert.AreEqual(0, _collection.Count());
        }
        public void TestInsertDuplicateKey()
        {
            var collection = _database.GetCollection("duplicatekeys");
            collection.Drop();

            var result = collection.Insert(new BsonDocument("_id", 1));
            var expectedResult = new ExpectedWriteConcernResult();
            CheckExpectedResult(expectedResult, result);

            var exception = Assert.Throws<MongoDuplicateKeyException>(() => { collection.Insert(new BsonDocument("_id", 1)); });
            result = exception.WriteConcernResult;
            expectedResult = new ExpectedWriteConcernResult
            {
                HasLastErrorMessage = true
            };
            CheckExpectedResult(expectedResult, result);
        }
        public void TestInsertBatchZeroDocuments()
        {
            if (_primary.BuildInfo.Version >= new Version(2, 5, 5))
            {
                _collection.Drop();
                var results = _collection.InsertBatch(new BsonDocument[0]);
                var expectedResult = new ExpectedWriteConcernResult();
                CheckExpectedResult(expectedResult, results.Single());

                Assert.AreEqual(0, _collection.Count());
            }
        }
        public void TestInsertBatchMultipleBatchesWriteConcernEnabledContinueOnErrorTrue()
        {
            var collectionName = Configuration.TestCollection.Name;
            var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Acknowledged };
            var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
            if (collection.Exists()) { collection.Drop(); }

            using (Configuration.TestDatabase.RequestStart())
            {
                var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;

                var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
                var documents = new BsonDocument[]
                {
                    // first sub-batch
                    new BsonDocument { { "_id", 1 }, { "filler", filler } },
                    new BsonDocument { { "_id", 2 }, { "filler", filler } },
                    // second sub-batch
                    new BsonDocument { { "_id", 3 }, { "filler", filler } },
                    new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
                    // third sub-batch
                    new BsonDocument { { "_id", 4 }, { "filler", filler } },
                    new BsonDocument { { "_id", 5 }, { "filler", filler } },
                };

                var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
                var exception = Assert.Throws<MongoDuplicateKeyException>(() => { collection.InsertBatch(documents, options); });
                var result = exception.WriteConcernResult;

                var expectedResult = new ExpectedWriteConcernResult()
                {
                    HasLastErrorMessage = true
                };
                CheckExpectedResult(expectedResult, result);

                var results = ((IEnumerable<WriteConcernResult>)exception.Data["results"]).ToArray();
                if (results.Length == 3)
                {
                    Assert.AreEqual(false, results[0].HasLastErrorMessage);
                    Assert.AreEqual(true, results[1].HasLastErrorMessage);
                    Assert.AreEqual(false, results[2].HasLastErrorMessage);
                }
                else
                {
                    // it the opcode was emulated there will just be one synthesized result
                    Assert.AreEqual(1, results.Length);
                    Assert.AreEqual(true, results[0].HasLastErrorMessage);
                }

                Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
                Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
                Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
                Assert.AreEqual(1, collection.Count(Query.EQ("_id", 4)));
                Assert.AreEqual(1, collection.Count(Query.EQ("_id", 5)));
            }
        }
        public void TestInsertBatchContinueOnError()
        {
            var collection = Configuration.TestCollection;
            collection.Drop();
            collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetUnique(true));

            var batch = new BsonDocument[]
            {
                new BsonDocument("x", 1),
                new BsonDocument("x", 1), // duplicate
                new BsonDocument("x", 2),
                new BsonDocument("x", 2), // duplicate
                new BsonDocument("x", 3),
                new BsonDocument("x", 3) // duplicate
            };

            // try the batch without ContinueOnError
            var exception = Assert.Throws<MongoDuplicateKeyException>(() => collection.InsertBatch(batch));
            var result = exception.WriteConcernResult;

            var expectedResult = new ExpectedWriteConcernResult
            {
                HasLastErrorMessage = true
            };
            CheckExpectedResult(expectedResult, result);

            Assert.AreEqual(1, collection.Count());
            Assert.AreEqual(1, collection.FindOne()["x"].AsInt32);

            // try the batch again with ContinueOnError
            if (_server.BuildInfo.Version >= new Version(2, 0, 0))
            {
                var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
                exception = Assert.Throws<MongoDuplicateKeyException>(() => collection.InsertBatch(batch, options));
                result = exception.WriteConcernResult;

                expectedResult = new ExpectedWriteConcernResult
                {
                    HasLastErrorMessage = true
                };
                CheckExpectedResult(expectedResult, result);

                Assert.AreEqual(3, collection.Count());
            }
        }
示例#14
0
        public void TestUpdate()
        {
            _collection.Drop();
            _collection.Insert(new BsonDocument("x", 1));
            var options = new MongoUpdateOptions { BypassDocumentValidation = true };

            var result = _collection.Update(Query.EQ("x", 1), Update.Set("x", 2), options);

            var expectedResult = new ExpectedWriteConcernResult
            {
                DocumentsAffected = 1,
                UpdatedExisting = true
            };
            CheckExpectedResult(expectedResult, result);

            var document = _collection.FindOne();
            Assert.Equal(2, document["x"].AsInt32);
            Assert.Equal(1, _collection.Count());
        }