public void UpdateManyAsync_should_throw_a_WriteException_when_an_error_occurs(bool upsert) { var filter = BsonDocument.Parse("{a:1}"); var update = BsonDocument.Parse("{$set:{a:1}}"); var expectedRequest = new UpdateRequest(UpdateType.Update, filter, update) { CorrelationId = 0, IsUpsert = upsert, IsMulti = true }; var exception = new MongoBulkWriteOperationException( _connectionId, new BulkWriteOperationResult.Acknowledged( requestCount: 1, matchedCount: 1, deletedCount: 0, insertedCount: 0, modifiedCount: 0, processedRequests: new[] { expectedRequest }, upserts: new List <BulkWriteOperationUpsert>()), new[] { new BulkWriteOperationError(0, 1, "blah", new BsonDocument()) }, null, new List <WriteRequest>()); _operationExecutor.EnqueueException <BulkWriteOperationResult>(exception); Action act = () => _subject.UpdateManyAsync( filter, update, new UpdateOptions { IsUpsert = upsert }, CancellationToken.None).GetAwaiter().GetResult(); act.ShouldThrow <MongoWriteException>(); }
public void InsertOneAsync_should_throw_a_WriteException_when_an_error_occurs() { var document = BsonDocument.Parse("{_id:1,a:1}"); var expectedRequest = new InsertRequest(document) { CorrelationId = 0 }; var exception = new MongoBulkWriteOperationException( _connectionId, new BulkWriteOperationResult.Acknowledged( requestCount: 1, matchedCount: 0, deletedCount: 0, insertedCount: 0, modifiedCount: 0, processedRequests: new[] { expectedRequest }, upserts: new List <BulkWriteOperationUpsert>()), new[] { new BulkWriteOperationError(0, 1, "blah", new BsonDocument()) }, null, new List <WriteRequest>()); _operationExecutor.EnqueueException <BulkWriteOperationResult>(exception); Action act = () => _subject.InsertOneAsync( document, CancellationToken.None).GetAwaiter().GetResult(); act.ShouldThrow <MongoWriteException>(); }
public void FromCore_should_convert_from_core_exception_with_a_write_concern_error_when_original_models_do_not_exist() { var exception = new MongoBulkWriteOperationException( _connectionId, result: new BulkWriteOperationResult.Acknowledged( requestCount: 1, matchedCount: 1, deletedCount: 0, insertedCount: 0, modifiedCount: 0, processedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("b", 1))) { CorrelationId = 1 } }, upserts: new List <BulkWriteOperationUpsert>()), writeErrors: new[] { new BulkWriteOperationError(10, 1, "blah", new BsonDocument("a", 1)) }, writeConcernError: new BulkWriteConcernError(11, "funny", new BsonDocument("c", 1)), unprocessedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("a", 1))) { CorrelationId = 0 } }); var mapped = MongoBulkWriteException <BsonDocument> .FromCore(exception); mapped.Result.ProcessedRequests.Count.Should().Be(1); mapped.Result.ProcessedRequests[0].Should().BeOfType <InsertOneModel <BsonDocument> >(); ((InsertOneModel <BsonDocument>)mapped.Result.ProcessedRequests[0]).Document.Should().Be("{b:1}"); mapped.WriteConcernError.Should().NotBeNull(); mapped.WriteErrors.Count.Should().Be(1); mapped.WriteErrors[0].Should().NotBeNull(); mapped.UnprocessedRequests.Count.Should().Be(1); mapped.UnprocessedRequests[0].Should().BeOfType <InsertOneModel <BsonDocument> >(); ((InsertOneModel <BsonDocument>)mapped.UnprocessedRequests[0]).Document.Should().Be("{a:1}"); }
public void DeleteManyAsync_should_throw_a_WriteException_when_an_error_occurs() { var filter = new BsonDocument("a", 1); var expectedRequest = new DeleteRequest(filter) { CorrelationId = 0, Limit = 0 }; var exception = new MongoBulkWriteOperationException( _connectionId, new BulkWriteOperationResult.Acknowledged( requestCount: 1, matchedCount: 1, deletedCount: 0, insertedCount: 0, modifiedCount: 0, processedRequests: new[] { expectedRequest }, upserts: new List <BulkWriteOperationUpsert>()), new[] { new BulkWriteOperationError(10, 1, "blah", new BsonDocument()) }, null, new List <WriteRequest>()); _operationExecutor.EnqueueException <BulkWriteOperationResult>(exception); Action act = () => _subject.DeleteManyAsync( filter, CancellationToken.None).GetAwaiter().GetResult(); act.ShouldThrow <MongoWriteException>(); }
public void FromCore_should_convert_from_core_exception_with_a_write_concern_error_when_original_models_exists() { var exception = new MongoBulkWriteOperationException( __connectionId, result: new BulkWriteOperationResult.Acknowledged( requestCount: 1, matchedCount: 1, deletedCount: 0, insertedCount: 0, modifiedCount: 0, processedRequests: new[] { new InsertRequest(new BsonDocument("b", 1)) { CorrelationId = 1 } }, upserts: new List <BulkWriteOperationUpsert>()), writeErrors: new[] { new BulkWriteOperationError(10, 1, "blah", new BsonDocument("a", 1)) }, writeConcernError: new BulkWriteConcernError(11, "47", "funny", new BsonDocument("c", 1), new[] { "RetryableWriteError" }), unprocessedRequests: new[] { new InsertRequest(new BsonDocument("a", 1)) { CorrelationId = 0 } }); var models = new[] { new InsertOneModel <BsonDocument>(new BsonDocument("a", 1)), new InsertOneModel <BsonDocument>(new BsonDocument("b", 1)) }; var mapped = MongoBulkWriteException <BsonDocument> .FromCore(exception, models); mapped.ErrorLabels.Should().BeEquivalentTo(exception.ErrorLabels); mapped.Result.ProcessedRequests.Count.Should().Be(1); mapped.Result.ProcessedRequests[0].Should().BeSameAs(models[1]); mapped.WriteConcernError.Should().NotBeNull(); mapped.WriteConcernError.ErrorLabels.Should().BeEquivalentTo(exception.WriteConcernError.ErrorLabels); mapped.WriteErrors.Count.Should().Be(1); mapped.WriteErrors[0].Should().NotBeNull(); mapped.UnprocessedRequests.Count.Should().Be(1); mapped.UnprocessedRequests[0].Should().BeSameAs(models[0]); }