public void CreateOperation_should_return_expected_result(
            [Values(false, true)]
            bool isCommandSupported)
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var writeConcern = new WriteConcern(1);
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                WriteConcern = writeConcern
            };
            var serverVersion = Feature.CreateIndexesCommand.SupportedOrNotSupportedVersion(isCommandSupported);

            var result = subject.CreateOperation(serverVersion);

            if (isCommandSupported)
            {
                result.Should().BeOfType<CreateIndexesUsingCommandOperation>();
                var operation = (CreateIndexesUsingCommandOperation)result;
                operation.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
                operation.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
                operation.Requests.Should().Equal(requests);
                operation.WriteConcern.Should().BeSameAs(writeConcern);
            }
            else
            {
                result.Should().BeOfType<CreateIndexesUsingInsertOperation>();
                var operation = (CreateIndexesUsingInsertOperation)result;
                operation.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
                operation.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
                operation.Requests.Should().Equal(requests);
            }
        }
        public void CollectionNamespace_get_should_return_expected_value()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = subject.CollectionNamespace;

            result.Should().BeSameAs(_collectionNamespace);
        }
        public void constructor_should_initialize_subject()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
            subject.Requests.Should().ContainInOrder(requests);
            subject.WriteConcern.Should().Be(WriteConcern.Acknowledged);
        }
Exemple #4
0
 // private methods
 private async Task CreateChunksCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var collectionNamespace = GetChunksCollectionNamespace();
     var requests            = new[] { new CreateIndexRequest(new BsonDocument {
             { "files_id", 1 }, { "n", 1 }
         })
                                       {
                                           Unique = true
                                       } };
     var messageEncoderSettings = GetMessageEncoderSettings();
     var operation = new CreateIndexesOperation(collectionNamespace, requests, messageEncoderSettings);
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
        public void CreateCommand_should_return_expected_result_when_creating_one_index()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);
            var expectedResult = new BsonDocument
            {
                { "createIndexes", _collectionNamespace.CollectionName },
                { "indexes", new BsonArray { requests[0].CreateIndexDocument() } }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
        public void Execute_should_work_when_creating_one_index(
            [Values(false, true)]
            bool async)
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

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

            var indexes = ListIndexes(async);
            indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_1" });
        }
        public void Execute_should_work_when_background_is_true(
            [Values(false, true)]
            bool async)
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { Background = true } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

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

            var indexes = ListIndexes(async);
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["background"].ToBoolean().Should().BeTrue();
        }
Exemple #8
0
            public async override Task <IEnumerable <string> > CreateManyAsync(IEnumerable <CreateIndexModel <TDocument> > models, CancellationToken cancellationToken = default(CancellationToken))
            {
                Ensure.IsNotNull(models, nameof(models));

                var requests = models.Select(m =>
                {
                    var options      = m.Options ?? new CreateIndexOptions <TDocument>();
                    var keysDocument = m.Keys.Render(_collection._documentSerializer, _collection._settings.SerializerRegistry);
                    var renderedPartialFilterExpression = options.PartialFilterExpression == null ? null : options.PartialFilterExpression.Render(_collection._documentSerializer, _collection._settings.SerializerRegistry);

                    return(new CreateIndexRequest(keysDocument)
                    {
                        Name = options.Name,
                        Background = options.Background,
                        Bits = options.Bits,
                        BucketSize = options.BucketSize,
                        DefaultLanguage = options.DefaultLanguage,
                        ExpireAfter = options.ExpireAfter,
                        LanguageOverride = options.LanguageOverride,
                        Max = options.Max,
                        Min = options.Min,
                        PartialFilterExpression = renderedPartialFilterExpression,
                        Sparse = options.Sparse,
                        SphereIndexVersion = options.SphereIndexVersion,
                        StorageEngine = options.StorageEngine,
                        TextIndexVersion = options.TextIndexVersion,
                        Unique = options.Unique,
                        Version = options.Version,
                        Weights = options.Weights
                    });
                });

                var operation = new CreateIndexesOperation(_collection._collectionNamespace, requests, _collection._messageEncoderSettings);
                await _collection.ExecuteWriteOperationAsync(operation, cancellationToken).ConfigureAwait(false);

                return(requests.Select(x => x.GetIndexName()));
            }
        public void Execute_should_work_when_expireAfter_has_value(
            [Values(false, true)]
            bool async)
        {
            var expireAfterSeconds = 1.5;
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { ExpireAfter = TimeSpan.FromSeconds(expireAfterSeconds) } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

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

            var indexes = ListIndexes(async);
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["expireAfterSeconds"].ToDouble().Should().Be(expireAfterSeconds);
        }
        public void Execute_should_work_when_partialFilterExpression_is_has_value(
            [Values(false, true)]
            bool async)
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { PartialFilterExpression = new BsonDocument("x", new BsonDocument("$gt", 0)) } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

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

            var indexes = ListIndexes(async);
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["partialFilterExpression"].AsBsonDocument.Should().Be(requests[0].PartialFilterExpression);
        }
 // private methods
 private void EnsureIndexExists()
 {
     DropCollection();
     var keys = new BsonDocument("x", 1);
     var requests = new[] { new CreateIndexRequest(keys) };
     var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);
     ExecuteOperation(createIndexOperation);
 }
        public void Execute_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
            };

            Action action = () => ExecuteOperation(subject, async);
            var ex = action.ShouldThrow<MongoBulkWriteOperationException>().Subject.Single();

            var result = ex.Result;
            result.DeletedCount.Should().Be(0);
            result.InsertedCount.Should().Be(1);
            if (result.IsModifiedCountAvailable)
            {
                result.ModifiedCount.Should().Be(0);
            }
            result.MatchedCount.Should().Be(0);
            result.ProcessedRequests.Should().HaveCount(2);
            result.RequestCount.Should().Be(5);
            result.Upserts.Should().BeEmpty();

            var list = ReadAllFromCollection(async);
            list.Should().HaveCount(1);
        }
        private void CreateIndexAndInsertData(CollectionNamespace collectionNamespace)
        {
            var createIndexRequests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var createIndexOperation = new CreateIndexesOperation(collectionNamespace, createIndexRequests, _messageEncoderSettings);
            ExecuteOperation(createIndexOperation);

            var insertRequests = new[] { new InsertRequest(new BsonDocument("x", 1)) };
            var insertOperation = new BulkInsertOperation(collectionNamespace, insertRequests, _messageEncoderSettings);
            ExecuteOperation(insertOperation);
        }
        public async Task ExecuteAsync_with_an_error_in_the_first_batch_and_ordered_is_true()
        {
            var keys = new BsonDocument("x", 1);
            var createIndexRequests = new[] { new CreateIndexRequest(keys) { Unique = true } };
            var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, createIndexRequests, _messageEncoderSettings);
            await ExecuteOperationAsync(createIndexOperation);

            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
            };

            var ex = await CatchAsync<MongoBulkWriteOperationException>(() => ExecuteOperationAsync(subject));

            var result = ex.Result;
            result.DeletedCount.Should().Be(0);
            result.InsertedCount.Should().Be(1);
            if (result.IsModifiedCountAvailable)
            {
                result.ModifiedCount.Should().Be(0);
            }
            result.MatchedCount.Should().Be(0);
            result.ProcessedRequests.Should().HaveCount(2);
            result.RequestCount.Should().Be(5);
            result.Upserts.Should().BeEmpty();

            var list = await ReadAllFromCollectionAsync();
            list.Should().HaveCount(1);
        }
        public void WriteConcern_get_and_set_should_work()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);
            var value = WriteConcern.WMajority;

            subject.WriteConcern = value;
            var result = subject.WriteConcern;

            result.Should().BeSameAs(value);
        }
        public void WriteConcern_set_should_throw_when_value_is_null()
        {
            var subject = new CreateIndexesOperation(_collectionNamespace, Enumerable.Empty<CreateIndexRequest>(), _messageEncoderSettings);

            var exception = Record.Exception(() => { subject.WriteConcern = null; });

            var argumentNullException = exception.Should().BeOfType<ArgumentNullException>().Subject;
            argumentNullException.ParamName.Should().Be("value");
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            var keys = new BsonDocument("x", 1);
            var requests = new[] { new CreateIndexRequest(keys) };
            var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);
            ExecuteOperation(createIndexOperation, async);
            var indexName = "x_1";
            var subject = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result["ok"].ToBoolean().Should().BeTrue();
        }
        public async Task ExecuteAsync_should_return_expected_result()
        {
            using (var binding = CoreTestConfiguration.GetReadWriteBinding())
            {
                var keys = new BsonDocument("x", 1);
                var requests = new[] { new CreateIndexRequest(keys) };
                var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);
                await createIndexOperation.ExecuteAsync(binding, CancellationToken.None);

                var indexName = "x_1";
                var subject = new DropIndexOperation(_collectionNamespace, indexName, _messageEncoderSettings);

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

                result["ok"].ToBoolean().Should().BeTrue();
            }
        }
        public async Task ExecuteAsync_should_work_when_background_is_true()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { Background = true } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = await ExecuteOperationAsync(subject);

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

            var listIndexesOperation = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
            var cursor = await ExecuteOperationAsync(listIndexesOperation);
            var indexes = await cursor.ToListAsync();
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["background"].ToBoolean().Should().BeTrue();
        }
        public async Task ExecuteAsync_should_work_when_creating_two_indexes()
        {
            var requests = new[]
            {
                new CreateIndexRequest(new BsonDocument("x", 1)),
                new CreateIndexRequest(new BsonDocument("y", 1))
            };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = await ExecuteOperationAsync(subject);

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

            var listIndexesOperation = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
            var cursor = await ExecuteOperationAsync(listIndexesOperation);
            var indexes = await cursor.ToListAsync();
            indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_1", "y_1" });
        }
        public async Task ExecuteAsync_should_work_when_expireAfter_has_value()
        {
            var expireAfterSeconds = 1.5;
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { ExpireAfter = TimeSpan.FromSeconds(expireAfterSeconds) } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            var result = await ExecuteOperationAsync(subject);

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

            var listIndexesOperation = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
            var cursor = await ExecuteOperationAsync(listIndexesOperation);
            var indexes = await cursor.ToListAsync();
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["expireAfterSeconds"].ToDouble().Should().Be(expireAfterSeconds);
        }
        public void WriteConcern_set_should_throw_when_value_is_null()
        {
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            Action action = () => { subject.WriteConcern = null; };

            action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("value");
        }
        public void WriteConcern_get_and_set_should_work(
            [Values(1, 2)]
            int w)
        {
            var subject = new CreateIndexesOperation(_collectionNamespace, Enumerable.Empty<CreateIndexRequest>(), _messageEncoderSettings);
            var value = new WriteConcern(w);

            subject.WriteConcern = value;
            var result = subject.WriteConcern;

            result.Should().BeSameAs(value);
        }
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var collectionNamespace = GetFilesCollectionNamespace();
     var requests = new[] { new CreateIndexRequest(new BsonDocument { { "filename", 1 }, { "uploadDate", 1 } }) };
     var messageEncoderSettings = GetMessageEncoderSettings();
     var operation = new CreateIndexesOperation(collectionNamespace, requests, messageEncoderSettings);
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
        public void Execute_unacknowledged_with_an_error_in_the_first_batch_and_ordered_is_true(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            DropCollection();
            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 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
            };

            var result = ExecuteOperation(subject);
            result.ProcessedRequests.Should().HaveCount(5);
            result.RequestCount.Should().Be(5);

            var list = ReadAllFromCollection(async);
            list.Should().HaveCount(1);
        }
        public void Execute_should_work_when_Collation_has_value(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Collation);
            DropCollection();
            var collation = new Collation(locale);
            var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { Collation = collation } };
            var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings);

            ExecuteOperation(subject, async);

            var indexes = ListIndexes();
            var index = indexes.Single(i => i["name"].AsString == "x_1");
            index["collation"]["locale"].AsString.Should().Be(locale);
        }