PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/MongoWriteExceptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 113 lines | 87 code | 12 blank | 14 comment | 1 complexity | 634514599b9964896229f7825ac3adbc MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-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.IO;
  18. using System.Net;
  19. #if NET452
  20. using System.Runtime.Serialization.Formatters.Binary;
  21. #endif
  22. using FluentAssertions;
  23. using MongoDB.Bson;
  24. using MongoDB.Bson.TestHelpers.EqualityComparers;
  25. using MongoDB.Driver.Core.Clusters;
  26. using MongoDB.Driver.Core.Connections;
  27. using MongoDB.Driver.Core.Servers;
  28. using Xunit;
  29. namespace MongoDB.Driver.Tests
  30. {
  31. public class MongoWriteExceptionTests
  32. {
  33. private static ConnectionId __connectionId;
  34. private static Exception __innerException;
  35. private static WriteConcernError __writeConcernError;
  36. private static WriteError __writeError;
  37. private static bool __oneTimeSetupHasRun = false;
  38. private static object __oneTimeSetupLock = new object();
  39. public MongoWriteExceptionTests()
  40. {
  41. lock (__oneTimeSetupLock)
  42. {
  43. __oneTimeSetupHasRun = __oneTimeSetupHasRun || OneTimeSetup();
  44. }
  45. }
  46. public bool OneTimeSetup()
  47. {
  48. __connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2);
  49. __innerException = new Exception("inner");
  50. __writeConcernError = new WriteConcernError(1, null, "writeConcernError", new BsonDocument("details", "writeConcernError"));
  51. __writeError = new WriteError(ServerErrorCategory.Uncategorized, 1, "writeError", new BsonDocument("details", "writeError"));
  52. return true;
  53. }
  54. [Fact]
  55. public void constructor_should_initialize_subject()
  56. {
  57. var subject = new MongoWriteException(__connectionId, __writeError, __writeConcernError, __innerException);
  58. subject.ConnectionId.Should().Be(__connectionId);
  59. subject.InnerException.Should().Be(__innerException);
  60. subject.Message.Should().Be("A write operation resulted in an error." + Environment.NewLine + " writeError" + Environment.NewLine + " writeConcernError");
  61. subject.WriteConcernError.Should().Be(__writeConcernError);
  62. subject.WriteError.Should().Be(__writeError);
  63. }
  64. [Fact]
  65. public void FromBulkWriteException_should_return_expected_result()
  66. {
  67. var processedRequests = new[] { new InsertOneModel<BsonDocument>(new BsonDocument("_id", 1)) };
  68. var upserts = new List<BulkWriteUpsert>();
  69. var bulkWriteResult = new BulkWriteResult<BsonDocument>.Acknowledged(1, 1, 0, 0, 0, processedRequests, upserts);
  70. var writeErrors = new[] { new BulkWriteError(1, ServerErrorCategory.Uncategorized, 2, "message", new BsonDocument("details", 1)) };
  71. var writeConcernError = new WriteConcernError(1, null, "message", new BsonDocument("details", 1));
  72. var unprocessedRequests = new List<WriteModel<BsonDocument>>();
  73. var bulkWriteException = new MongoBulkWriteException<BsonDocument>(__connectionId, bulkWriteResult, writeErrors, writeConcernError, unprocessedRequests);
  74. var result = MongoWriteException.FromBulkWriteException(bulkWriteException);
  75. result.ConnectionId.Should().Be(__connectionId);
  76. result.InnerException.Should().BeSameAs(bulkWriteException);
  77. result.Message.Should().Be("A write operation resulted in an error." + Environment.NewLine + " message" + Environment.NewLine + " message");
  78. result.WriteConcernError.Should().Be(writeConcernError);
  79. result.WriteError.Should().Be(writeErrors[0]);
  80. }
  81. #if NET452
  82. [Fact]
  83. public void Serialization_should_work()
  84. {
  85. var subject = new MongoWriteException(__connectionId, __writeError, __writeConcernError, __innerException);
  86. var formatter = new BinaryFormatter();
  87. using (var stream = new MemoryStream())
  88. {
  89. formatter.Serialize(stream, subject);
  90. stream.Position = 0;
  91. var rehydrated = (MongoWriteException)formatter.Deserialize(stream);
  92. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  93. rehydrated.InnerException.Message.Should().Be(subject.InnerException.Message); // Exception does not override Equals
  94. rehydrated.Message.Should().Be(subject.Message);
  95. rehydrated.WriteConcernError.Should().BeUsing(subject.WriteConcernError, EqualityComparerRegistry.Default);
  96. rehydrated.WriteError.Should().BeUsing(subject.WriteError, EqualityComparerRegistry.Default);
  97. }
  98. }
  99. #endif
  100. }
  101. }