PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/MongoDB.Driver.Core.Tests/MongoExceptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 166 lines | 129 code | 23 blank | 14 comment | 3 complexity | 29aa40ca38c9d0f2daf1731b982299e3 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.Linq;
  19. #if NET452
  20. using System.Runtime.Serialization.Formatters.Binary;
  21. #endif
  22. using FluentAssertions;
  23. using MongoDB.Bson.TestHelpers.XunitExtensions;
  24. using Xunit;
  25. namespace MongoDB.Driver
  26. {
  27. public class MongoExceptionTests
  28. {
  29. private Exception _innerException = new Exception("inner");
  30. private string _message = "message";
  31. [Fact]
  32. public void constructor_should_initialize_subject()
  33. {
  34. var subject = new MongoException(_message);
  35. subject.Message.Should().BeSameAs(_message);
  36. subject.InnerException.Should().BeNull();
  37. subject.ErrorLabels.Should().HaveCount(0);
  38. }
  39. [Fact]
  40. public void constructor_with_innerException_should_initialize_subject()
  41. {
  42. var subject = new MongoException(_message, _innerException);
  43. subject.Message.Should().BeSameAs(_message);
  44. subject.InnerException.Should().BeSameAs(_innerException);
  45. subject.ErrorLabels.Should().HaveCount(0);
  46. }
  47. [Theory]
  48. [InlineData(new object[] { new string[0] })]
  49. [InlineData(new object[] { new[] { "one" } })]
  50. [InlineData(new object[] { new[] { "one", "two" } })]
  51. [InlineData(new object[] { new[] { "one", "two", "three" } })]
  52. public void ErrorLabels_should_return_expected_result(string[] errorLabels)
  53. {
  54. var subject = new MongoException(_message);
  55. foreach (var errorLabel in errorLabels)
  56. {
  57. subject.AddErrorLabel(errorLabel);
  58. }
  59. var result = subject.ErrorLabels;
  60. result.Should().Equal(errorLabels);
  61. }
  62. [Theory]
  63. [ParameterAttributeData]
  64. public void AddErrorLabels_should_have_expected_result(
  65. [Values(0, 1, 2, 3)] int existingCount)
  66. {
  67. var subject = new MongoException(_message);
  68. for (var i = 0; i < existingCount; i++)
  69. {
  70. var errorLabel = $"label{i}";
  71. subject.AddErrorLabel(errorLabel);
  72. }
  73. var existingErrorLabels = new List<string>(subject.ErrorLabels);
  74. var newErrorLabel = "x";
  75. subject.AddErrorLabel(newErrorLabel);
  76. subject.ErrorLabels.Should().Equal(existingErrorLabels.Concat(new[] { newErrorLabel }));
  77. }
  78. [Fact]
  79. public void AddErrorLabels_should_not_add_duplicates()
  80. {
  81. var subject = new MongoException(_message);
  82. subject.AddErrorLabel("one");
  83. subject.AddErrorLabel("one");
  84. subject.ErrorLabels.Should().HaveCount(1);
  85. }
  86. [Theory]
  87. [ParameterAttributeData]
  88. public void HasErrorLabel_should_have_expected_result(
  89. [Values(0, 1, 2, 3)] int existingCount)
  90. {
  91. var subject = new MongoException(_message);
  92. for (var i = 0; i < existingCount; i++)
  93. {
  94. var errorLabel = $"label{i}";
  95. subject.AddErrorLabel(errorLabel);
  96. }
  97. foreach (var errorLabel in subject.ErrorLabels)
  98. {
  99. subject.HasErrorLabel(errorLabel).Should().BeTrue();
  100. }
  101. subject.HasErrorLabel("x").Should().BeFalse();
  102. }
  103. [Theory]
  104. [InlineData(new string[0], "x")]
  105. [InlineData(new[] { "one" }, "one")]
  106. [InlineData(new[] { "one" }, "x")]
  107. [InlineData(new[] { "one", "two" }, "one")]
  108. [InlineData(new[] { "one", "two" }, "two")]
  109. [InlineData(new[] { "one", "two" }, "x")]
  110. [InlineData(new[] { "one", "two", "three" }, "one")]
  111. [InlineData(new[] { "one", "two", "three" }, "two")]
  112. [InlineData(new[] { "one", "two", "three" }, "three")]
  113. [InlineData(new[] { "one", "two", "three" }, "x")]
  114. public void RemoveErrorLabels_should_have_expected_result(string[] errorLabels, string removeErrorLabel)
  115. {
  116. var subject = new MongoException(_message);
  117. foreach (var errorLabel in errorLabels)
  118. {
  119. subject.AddErrorLabel(errorLabel);
  120. }
  121. subject.RemoveErrorLabel(removeErrorLabel);
  122. subject.ErrorLabels.Should().Equal(errorLabels.Where(x => x != removeErrorLabel));
  123. }
  124. #if NET452
  125. [Fact]
  126. public void Serialization_should_work()
  127. {
  128. var subject = new MongoException(_message, _innerException);
  129. subject.AddErrorLabel("one");
  130. var formatter = new BinaryFormatter();
  131. using (var stream = new MemoryStream())
  132. {
  133. formatter.Serialize(stream, subject);
  134. stream.Position = 0;
  135. var rehydrated = (MongoException)formatter.Deserialize(stream);
  136. rehydrated.Message.Should().Be(subject.Message);
  137. rehydrated.InnerException.Message.Should().Be(subject.InnerException.Message); // Exception does not override Equals
  138. rehydrated.ErrorLabels.Should().Equal(subject.ErrorLabels);
  139. }
  140. }
  141. #endif
  142. }
  143. }