Ejemplo n.º 1
0
        public void TestSetValidator()
        {
            var options  = CollectionOptions.SetValidator(new QueryDocument("_id", new BsonDocument("$exists", true)));
            var expected = "{ \"validator\" : { \"_id\" : { \"$exists\" : true } } }";

            Assert.AreEqual(expected, options.ToJson());
        }
Ejemplo n.º 2
0
        public void TestCreateCollectionSetValidator()
        {
            var collection = _database.GetCollection("testvalidation");

            collection.Drop();
            Assert.IsFalse(collection.Exists());
            var options = CollectionOptions
                          .SetValidator(new QueryDocument("_id", new BsonDocument("$exists", true)))
                          .SetValidationAction(DocumentValidationAction.Error)
                          .SetValidationLevel(DocumentValidationLevel.Strict);

            _database.CreateCollection(collection.Name, options);

            var commandResult  = _database.RunCommand("listCollections");
            var collectionInfo = commandResult.Response["cursor"]["firstBatch"].AsBsonArray.Where(c => c["name"] == collection.Name).Single();

            Assert.That(collectionInfo["options"]["validator"], Is.EqualTo(new BsonDocument("_id", new BsonDocument("$exists", true))));
            Assert.That(collectionInfo["options"]["validationAction"].AsString, Is.EqualTo("error"));
            Assert.That(collectionInfo["options"]["validationLevel"].AsString, Is.EqualTo("strict"));
        }