public void Execute_should_create_collection_when_MaxSize_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxSize = 10000L;
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped  = true,
                MaxSize = maxSize
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats["capped"].ToBoolean().Should().BeTrue();
                // TODO: not sure how to verify that the maxSize took effect
            }
        }
Example #2
0
        public void Execute_should_create_collection_when_Capped_is_set(
            [Values(false, true)]
            bool capped,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            DropCollection();
            var maxSize = capped ? 10000L : (long?)null;
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped  = capped,
                MaxSize = maxSize
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetCollectionInfo(binding);
            }

            // in older versions of the server options might not be present if capped is false
            if (capped || info.Contains("options"))
            {
                info["options"].AsBsonDocument.GetValue("capped", false).ToBoolean().Should().Be(capped);
            }
        }
Example #3
0
 protected async Task <TResult> ExecuteOperationAsync <TResult>(IWriteOperation <TResult> operation)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding())
     {
         return(await operation.ExecuteAsync(binding, CancellationToken.None));
     }
 }
Example #4
0
        public void Execute_should_create_collection_when_IndexOptionDefaults_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.IndexOptionsDefaults);
            DropCollection();
            var indexOptionDefaults = new BsonDocument
            {
                { "storageEngine", new BsonDocument("mmapv1", new BsonDocument()) }
            };
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                IndexOptionDefaults = indexOptionDefaults
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetCollectionInfo(binding);
            }

            info["options"]["indexOptionDefaults"].Should().Be(indexOptionDefaults);
        }
        public void Execute_should_create_collection_when_Capped_is_set(
            [Values(false, true)]
            bool capped,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxSize = capped ? (long?)10000 : null;
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped  = capped,
                MaxSize = maxSize
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats.GetValue("capped", false).ToBoolean().Should().Be(capped);
            }
        }
Example #6
0
        public void Execute_should_create_collection_when_StorageEngine_is_set(
            [Values("abc", "def")]
            string metadata,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().StorageEngine("wiredTiger");
            DropCollection();
            var storageEngine = new BsonDocument
            {
                { "wiredTiger", new BsonDocument("configString", "app_metadata=" + metadata) }
            };
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                StorageEngine = storageEngine
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetCollectionInfo(binding);
            }

            info["options"]["storageEngine"].Should().Be(storageEngine);
        }
 // private methods
 private BsonValue ExecuteOperation(EvalOperation operation, bool async)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding())
     {
         return(ExecuteOperation(operation, binding, async));
     }
 }
        public void Execute_should_create_collection_when_MaxDocuments_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var maxDocuments = 123L;
            var subject      = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped       = true,
                MaxSize      = 10000L,
                MaxDocuments = maxDocuments
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var stats = GetCollectionStats(binding, async);
                stats["ns"].ToString().Should().Be(_collectionNamespace.FullName);
                stats["capped"].ToBoolean().Should().BeTrue();
                stats["max"].ToInt64().Should().Be(maxDocuments);
            }
        }
        public void Execute_should_create_collection_when_AutoIndexId_is_set(
            [Values(false, true)]
            bool autoIndexId,
            [Values(false, true)]
            bool async)
        {
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                AutoIndexId = autoIndexId
            };

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();

                var listIndexesOperation = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
                List <BsonDocument> indexes;
                if (async)
                {
                    var cursor = listIndexesOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
                    indexes = cursor.ToListAsync().GetAwaiter().GetResult();
                }
                else
                {
                    var cursor = listIndexesOperation.Execute(binding, CancellationToken.None);
                    indexes = cursor.ToList();
                }

                indexes.Count.Should().Be(autoIndexId ? 1 : 0);
            }
        }
Example #10
0
        public void Execute_should_create_view_when_WriteConcern_is_set(
            [Values("a", "b")]
            string viewName,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Views);
            DropView(viewName);
            var subject = new CreateViewOperation(_databaseNamespace, viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings)
            {
                WriteConcern = new WriteConcern(1)
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetViewInfo(binding, viewName);
            }

            var options = info["options"].AsBsonDocument;

            options["viewOn"].AsString.Should().Be(_collectionNamespace.CollectionName);
            options["pipeline"].AsBsonArray.Cast <BsonDocument>().Should().Equal(_pipeline);
        }
Example #11
0
        public void Execute_should_create_view_when_Collation_is_set(
            [Values(null, "en_US")]
            string locale,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Views).VersionGreaterThanOrEqualTo("3.3.13");
            DropView(_viewName);
            var collation = locale == null ? null : new Collation(locale);
            var subject   = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings)
            {
                Collation = collation
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetViewInfo(binding, _viewName);
            }

            var options = info["options"].AsBsonDocument;

            options["viewOn"].AsString.Should().Be(_collectionNamespace.CollectionName);
            options["pipeline"].AsBsonArray.Cast <BsonDocument>().Should().Equal(_pipeline);
            if (collation == null)
            {
                options.Contains("collation").Should().BeFalse();
            }
            else
            {
                options["collation"].AsBsonDocument.Should().Equals(collation.ToBsonDocument());
            }
        }
Example #12
0
 protected TResult ExecuteOperation <TResult>(IWriteOperation <TResult> operation)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding())
     {
         return(operation.Execute(binding, CancellationToken.None));
     }
 }
Example #13
0
        public void Execute_should_create_collection_when_Validator_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.DocumentValidation);
            DropCollection();
            var validator = new BsonDocument("_id", new BsonDocument("$exists", true));
            var subject   = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Validator        = validator,
                ValidationLevel  = DocumentValidationLevel.Strict,
                ValidationAction = DocumentValidationAction.Error
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetCollectionInfo(binding);
            }

            info["options"]["validator"].Should().Be(validator);
            info["options"]["validationLevel"].AsString.Should().Be("strict");
            info["options"]["validationAction"].AsString.Should().Be("error");
        }
Example #14
0
        public void Execute_should_create_collection_when_MaxDocuments_is_set(
            [Values(1L, 2L)]
            long maxDocuments,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            DropCollection();
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Capped       = true,
                MaxSize      = 10000L,
                MaxDocuments = maxDocuments
            };

            BsonDocument info;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
            {
                ExecuteOperation(subject, binding, async);
                info = GetCollectionInfo(binding);
            }

            info["options"]["max"].ToInt64().Should().Be(maxDocuments);
        }
 protected TResult ExecuteOperation <TResult>(IWriteOperation <TResult> operation)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding(_session.Fork()))
         using (var bindingHandle = new ReadWriteBindingHandle(binding))
         {
             return(operation.Execute(bindingHandle, CancellationToken.None));
         }
 }
        // helper methods
        private void DropCollection()
        {
            var operation = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
            }
        }
        public async Task ExecuteAsync_should_not_throw_when_collection_does_not_exist()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var indexName = "x_1";
                var subject   = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

                await subject.ExecuteAsync(binding, CancellationToken.None); // should not throw
            }
        }
        public async Task ExecuteAsync_should_not_throw_when_collection_does_not_exist()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                var dropCollectionOperation = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                await dropCollectionOperation.ExecuteAsync(binding, CancellationToken.None);

                await subject.ExecuteAsync(binding, CancellationToken.None); // this will throw if we have a problem...
            }
        }
Example #19
0
        public void Execute_should_not_throw_when_collection_does_not_exist(
            [Values(false, true)]
            bool async)
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var indexName = "x_1";
                var subject   = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

                ExecuteOperation(subject, async); // should not throw
            }
        }
 public void Execute_should_not_throw_when_collection_does_not_exist(
     [Values(false, true)]
     bool async)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding())
     {
         var subject = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
         var dropCollectionOperation = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
         ExecuteOperation(dropCollectionOperation, binding, async);
         ExecuteOperation(subject, binding, async); // this will throw if we have a problem...
     }
 }
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new RenameCollectionOperation(_collectionNamespace, _newCollectionNamespace, _messageEncoderSettings);
                EnsureCollectionExists(binding, _collectionNamespace);
                EnsureCollectionDoesNotExist(binding, _newCollectionNamespace);

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                result["ok"].ToBoolean().Should().BeTrue();
            }
        }
Example #22
0
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                EnsureDatabaseExists(binding);
                var subject = new DropDatabaseOperation(_databaseNamespace, _messageEncoderSettings);

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                result["ok"].ToBoolean().Should().BeTrue();
                result["dropped"].ToString().Should().Be(_databaseNamespace.DatabaseName);
            }
        }
        public void Execute_unacknowledged_with_an_error_in_the_first_batch_and_ordered_is_true(
            [Values(false, true)]
            bool async)
        {
            var keys = new BsonDocument("x", 1);
            var createIndexRequests = new[] { new CreateIndexRequest(keys)
                                              {
                                                  Unique = true
                                              } };
            var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, createIndexRequests, _messageEncoderSettings);

            ExecuteOperation(createIndexOperation, async);

            var requests = new[]
            {
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),                                                  // will fail
                new InsertRequest(new BsonDocument {
                    { "_id", 3 }
                }),
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),                                                  // will fail
                new InsertRequest(new BsonDocument {
                    { "_id", 5 }
                }),
            };

            var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                IsOrdered    = true,
                WriteConcern = WriteConcern.Unacknowledged
            };

            using (var readWriteBinding = CoreTestConfiguration.GetReadWriteBinding())
                using (var channelSource = readWriteBinding.GetWriteChannelSource(CancellationToken.None))
                    using (var channel = channelSource.GetChannel(CancellationToken.None))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                        {
                            var result = ExecuteOperation(subject, channelBinding, async);
                            result.ProcessedRequests.Should().HaveCount(5);
                            result.RequestCount.Should().Be(5);

                            var list = ReadAllFromCollection(channelBinding);
                            list.Should().HaveCount(1);
                        }
        }
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new DropCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                var createCollectionOperation = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
                await createCollectionOperation.ExecuteAsync(binding, CancellationToken.None);

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                result["ok"].ToBoolean().Should().BeTrue();
                result["ns"].ToString().Should().Be(_collectionNamespace.FullName);
            }
        }
Example #25
0
        public async Task ExecuteAsync_should_return_expected_result_when_collection_does_not_exist()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
                await DropCollectionAsync(binding);

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                var list = await result.ToListAsync();

                list.Count.Should().Be(0);
            }
        }
Example #26
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                EnsureDatabaseExists(binding);
                var subject = new DropDatabaseOperation(_databaseNamespace, _messageEncoderSettings);

                var result = ExecuteOperation(subject, binding, async);

                result["ok"].ToBoolean().Should().BeTrue();
            }
        }
Example #27
0
        public async Task ExecuteAsync_should_return_expected_result()
        {
            var function = "return 1";
            var subject  = new EvalOperation(_databaseNamespace, function, _messageEncoderSettings);

            BsonValue result;

            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                result = await subject.ExecuteAsync(binding, CancellationToken.None);
            }

            result.Should().Be(1.0);
        }
Example #28
0
 private TResult ExecuteOperation <TResult>(IWriteOperation <TResult> operation, bool async)
 {
     using (var binding = CoreTestConfiguration.GetReadWriteBinding())
     {
         if (async)
         {
             return(operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult());
         }
         else
         {
             return(operation.Execute(binding, CancellationToken.None));
         }
     }
 }
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new ListDatabasesOperation(_messageEncoderSettings);
                EnsureDatabaseExists(binding);

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                var list = await result.ToListAsync();

                list.Should().Contain(x => x["name"] == _databaseNamespace.DatabaseName);
            }
        }
Example #30
0
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var subject = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
                await EnsureCollectionExistsAsync(binding);

                var expectedNames = new[] { "_id_" };

                var result = await subject.ExecuteAsync(binding, CancellationToken.None);

                var list = await result.ToListAsync();

                list.Select(index => index["name"].AsString).Should().BeEquivalentTo(expectedNames);
            }
        }