PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/BulkWriteResultTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 130 lines | 100 code | 16 blank | 14 comment | 2 complexity | 66f2df28b079490e7be514f23170cefb MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Driver.Core.Operations;
  23. using Xunit;
  24. namespace MongoDB.Driver.Tests
  25. {
  26. public class BulkWriteResultTests
  27. {
  28. [Theory]
  29. [InlineData(null)]
  30. [InlineData((long)5)]
  31. public void Should_convert_from_core_acknowledged_result_when_original_models_exists(long? modifiedCount)
  32. {
  33. var core = new BulkWriteOperationResult.Acknowledged(
  34. requestCount: 1,
  35. matchedCount: 2,
  36. deletedCount: 3,
  37. insertedCount: 4,
  38. modifiedCount: modifiedCount,
  39. processedRequests: new[] { new InsertRequest(new BsonDocument("b", 1)) },
  40. upserts: new List<BulkWriteOperationUpsert>());
  41. var models = new[]
  42. {
  43. new InsertOneModel<BsonDocument>(new BsonDocument("a", 1))
  44. };
  45. var mapped = BulkWriteResult<BsonDocument>.FromCore(core, models);
  46. mapped.ProcessedRequests[0].Should().BeSameAs(models[0]);
  47. mapped.IsAcknowledged.Should().BeTrue();
  48. mapped.RequestCount.Should().Be(core.RequestCount);
  49. mapped.MatchedCount.Should().Be(core.MatchedCount);
  50. mapped.DeletedCount.Should().Be(core.DeletedCount);
  51. mapped.InsertedCount.Should().Be(core.InsertedCount);
  52. mapped.IsModifiedCountAvailable.Should().Be(core.IsModifiedCountAvailable);
  53. if (mapped.IsModifiedCountAvailable)
  54. {
  55. mapped.ModifiedCount.Should().Be(core.ModifiedCount);
  56. }
  57. mapped.Upserts.Count.Should().Be(core.Upserts.Count);
  58. }
  59. [Fact]
  60. public void Should_convert_from_core_unacknowledged_result_when_original_models_exists()
  61. {
  62. var core = new BulkWriteOperationResult.Unacknowledged(
  63. requestCount: 1,
  64. processedRequests: new[] { new InsertRequest(new BsonDocument("b", 1)) });
  65. var models = new[]
  66. {
  67. new InsertOneModel<BsonDocument>(new BsonDocument("a", 1))
  68. };
  69. var mapped = BulkWriteResult<BsonDocument>.FromCore(core, models);
  70. mapped.ProcessedRequests[0].Should().BeSameAs(models[0]);
  71. mapped.IsAcknowledged.Should().BeFalse();
  72. mapped.RequestCount.Should().Be(core.RequestCount);
  73. }
  74. [Theory]
  75. [InlineData(null)]
  76. [InlineData((long)5)]
  77. public void Should_convert_from_core_acknowledged_result_when_original_models_do_not_exist(long? modifiedCount)
  78. {
  79. var core = new BulkWriteOperationResult.Acknowledged(
  80. requestCount: 1,
  81. matchedCount: 2,
  82. deletedCount: 3,
  83. insertedCount: 4,
  84. modifiedCount: modifiedCount,
  85. processedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("b", 1))) },
  86. upserts: new List<BulkWriteOperationUpsert>());
  87. var mapped = BulkWriteResult<BsonDocument>.FromCore(core);
  88. mapped.ProcessedRequests[0].Should().BeOfType<InsertOneModel<BsonDocument>>();
  89. mapped.IsAcknowledged.Should().BeTrue();
  90. mapped.RequestCount.Should().Be(core.RequestCount);
  91. mapped.MatchedCount.Should().Be(core.MatchedCount);
  92. mapped.DeletedCount.Should().Be(core.DeletedCount);
  93. mapped.InsertedCount.Should().Be(core.InsertedCount);
  94. mapped.IsModifiedCountAvailable.Should().Be(core.IsModifiedCountAvailable);
  95. if (mapped.IsModifiedCountAvailable)
  96. {
  97. mapped.ModifiedCount.Should().Be(core.ModifiedCount);
  98. }
  99. mapped.Upserts.Count.Should().Be(core.Upserts.Count);
  100. }
  101. [Fact]
  102. public void Should_convert_from_core_unacknowledged_result_when_original_models_does_not_exist()
  103. {
  104. var core = new BulkWriteOperationResult.Unacknowledged(
  105. requestCount: 1,
  106. processedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("b", 1))) });
  107. var mapped = BulkWriteResult<BsonDocument>.FromCore(core);
  108. mapped.ProcessedRequests[0].Should().BeOfType<InsertOneModel<BsonDocument>>();
  109. ((InsertOneModel<BsonDocument>)mapped.ProcessedRequests[0]).Document.Should().Be("{b:1}");
  110. mapped.IsAcknowledged.Should().BeFalse();
  111. mapped.RequestCount.Should().Be(core.RequestCount);
  112. }
  113. }
  114. }