public void Execute_should_send_session_id_when_supported( [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.CreateIndexesCommand); DropCollection(); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); VerifySessionIdWasSentWhenSupported(subject, "createIndexes", async); }
public void CommitQuorum_get_and_set_should_work( [Values(null, 1, 2)] int?w) { var subject = new CreateIndexesOperation(_collectionNamespace, Enumerable.Empty <CreateIndexRequest>(), _messageEncoderSettings); var value = w.HasValue ? CreateIndexCommitQuorum.Create(w.Value) : null; subject.CommitQuorum = value; var result = subject.CommitQuorum; result.Should().BeSameAs(value); }
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 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 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().Equal(requests); subject.WriteConcern.Should().BeSameAs(WriteConcern.Acknowledged); }
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 void MaxTime_get_and_set_should_work( [Values(null, -10000, 0, 1, 42, 9000, 10000, 10001)] int?maxTimeTicks) { var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); var value = maxTimeTicks == null ? (TimeSpan?)null : TimeSpan.FromTicks(maxTimeTicks.Value); subject.MaxTime = value; var result = subject.MaxTime; result.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); }
public void MaxTime_set_should_throw_when_value_is_invalid( [Values(-10001, -9999, -42, -1)] long maxTimeTicks) { var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); var value = TimeSpan.FromTicks(maxTimeTicks); var exception = Record.Exception(() => subject.MaxTime = value); var e = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject; e.ParamName.Should().Be("value"); }
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_work_when_creating_one_index() { var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 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 indexes = await ExecuteOperationAsync(listIndexesOperation); indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_1" }); }
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 CreateCommand_should_throw_when_commitQuorum_is_specified_and_not_supported() { var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var commitQuorum = CreateIndexCommitQuorum.Create(1); var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings) { CommitQuorum = commitQuorum }; var session = OperationTestHelper.CreateSession(); var connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.CreateIndexCommitQuorum.LastNotSupportedWireVersion); var exception = Record.Exception(() => subject.CreateCommand(session, connectionDescription)); exception.Should().BeOfType <NotSupportedException>(); }
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_throw_when_a_write_concern_error_occurs( [Values(false, true)] bool async) { RequireServer.Check().ClusterType(ClusterType.ReplicaSet); DropCollection(); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings) { WriteConcern = new WriteConcern(9) }; var exception = Record.Exception(() => ExecuteOperation(subject, async)); exception.Should().BeOfType <MongoWriteConcernException>(); }
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 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 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 void Execute_should_throw_when_maxTime_is_exceeded( [Values(false, true)] bool async) { RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings) { MaxTime = TimeSpan.FromSeconds(9001) }; using (var failPoint = FailPoint.ConfigureAlwaysOn(_cluster, _session, FailPointName.MaxTimeAlwaysTimeout)) { var exception = Record.Exception(() => ExecuteOperation(subject, failPoint.Binding, async)); exception.Should().BeOfType <MongoExecutionTimeoutException>(); } }
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 indexes = (await ExecuteOperationAsync(listIndexesOperation)).ToList(); var index = indexes.Single(i => i["name"].AsString == "x_1"); index["background"].ToBoolean().Should().BeTrue(); }
public void Execute_should_work_when_creating_two_indexes( [Values(false, true)] bool async) { RequireServer.Check(); DropCollection(); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)), new CreateIndexRequest(new BsonDocument("y", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); ExecuteOperation(subject, async); var indexes = ListIndexes(); indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_1", "y_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(); }
public void Execute_should_work_when_unique_is_true( [Values(false, true)] bool async) { RequireServer.Check(); DropCollection(); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) { Unique = true } }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); ExecuteOperation(subject, async); var indexes = ListIndexes(); var index = indexes.Single(i => i["name"].AsString == "x_1"); index["unique"].ToBoolean().Should().BeTrue(); }
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 indexes = (await ExecuteOperationAsync(listIndexesOperation)).ToList(); 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); }
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_commitQuorum_is_specified( [Values(1, "majority", "votingMembers")] object commitQuorumCase, [Values(false, true)] bool async) { RequireServer.Check().ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded).Supports(Feature.CreateIndexCommitQuorum); DropCollection(); var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; CreateIndexCommitQuorum commitQuorum; if (commitQuorumCase is int w) { commitQuorum = CreateIndexCommitQuorum.Create(w); } else if (commitQuorumCase is string mode) { switch (mode) { case "majority": commitQuorum = CreateIndexCommitQuorum.Majority; break; case "votingMembers": commitQuorum = CreateIndexCommitQuorum.VotingMembers; break; default: commitQuorum = CreateIndexCommitQuorum.Create(mode); break; } } else { throw new ArgumentException($"Invalid commitQuorumCase: {commitQuorumCase}.", nameof(commitQuorumCase)); } var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings) { CommitQuorum = commitQuorum }; ExecuteOperation(subject, async); var indexes = ListIndexes(); indexes.Select(index => index["name"].AsString).Should().BeEquivalentTo(new[] { "_id_", "x_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); }
public void CreateCommand_should_return_expected_result_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 session = OperationTestHelper.CreateSession(); var connectionDescription = OperationTestHelper.CreateConnectionDescription(); var result = subject.CreateCommand(session, connectionDescription); var expectedResult = new BsonDocument { { "createIndexes", _collectionNamespace.CollectionName }, { "indexes", new BsonArray { requests[0].CreateIndexDocument(), requests[1].CreateIndexDocument() } } }; result.Should().Be(expectedResult); }
public void CreateCommand_should_return_expected_result_when_MaxTime_is_Set(long maxTimeTicks, int expectedMaxTimeMS) { var requests = new[] { new CreateIndexRequest(new BsonDocument("x", 1)) }; var subject = new CreateIndexesOperation(_collectionNamespace, requests, _messageEncoderSettings); subject.MaxTime = TimeSpan.FromTicks(maxTimeTicks); var session = OperationTestHelper.CreateSession(); var connectionDescription = OperationTestHelper.CreateConnectionDescription(); var result = subject.CreateCommand(session, connectionDescription); var expectedResult = new BsonDocument { { "createIndexes", _collectionNamespace.CollectionName }, { "indexes", new BsonArray { requests[0].CreateIndexDocument() } }, { "maxTimeMS", expectedMaxTimeMS }, }; result.Should().Be(expectedResult); result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32); }