PageRenderTime 49ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 138 lines | 103 code | 21 blank | 14 comment | 1 complexity | ce7f297c7778107926ac23e9ee0d476c 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.IO;
  17. using System.Net;
  18. #if NET452
  19. using System.Runtime.Serialization;
  20. using System.Runtime.Serialization.Formatters.Binary;
  21. #endif
  22. using FluentAssertions;
  23. using MongoDB.Bson;
  24. using MongoDB.Driver.Core.Clusters;
  25. using MongoDB.Driver.Core.Connections;
  26. using MongoDB.Driver.Core.Servers;
  27. using Xunit;
  28. namespace MongoDB.Driver
  29. {
  30. public class MongoCommandExceptionTests
  31. {
  32. private readonly BsonDocument _command = new BsonDocument("command", 1);
  33. private readonly BsonDocument _commandResult = new BsonDocument { { "code", 123 }, { "codeName", "abc" }, { "errmsg", "error message" }, { "ok", 0 } };
  34. private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
  35. private readonly string _message = "message";
  36. [Fact]
  37. public void Code_get_returns_expected_result()
  38. {
  39. var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);
  40. var result = subject.Code;
  41. result.Should().Be(123);
  42. }
  43. [Fact]
  44. public void CodeName_get_returns_expected_result()
  45. {
  46. var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);
  47. var result = subject.CodeName;
  48. result.Should().Be("abc");
  49. }
  50. [Fact]
  51. public void constructor_with_message_command_should_initialize_subject()
  52. {
  53. var subject = new MongoCommandException(_connectionId, _message, _command);
  54. subject.Command.Should().BeSameAs(_command);
  55. subject.ConnectionId.Should().BeSameAs(_connectionId);
  56. subject.InnerException.Should().BeNull();
  57. subject.Message.Should().BeSameAs(_message);
  58. subject.Result.Should().BeNull();
  59. }
  60. [Fact]
  61. public void constructor_with_message_command_result_should_initialize_subject()
  62. {
  63. var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);
  64. subject.Command.Should().BeSameAs(_command);
  65. subject.ConnectionId.Should().BeSameAs(_connectionId);
  66. subject.InnerException.Should().BeNull();
  67. subject.Message.Should().BeSameAs(_message);
  68. subject.Result.Should().BeSameAs(_commandResult);
  69. }
  70. [Theory]
  71. [InlineData(null, new string[0])]
  72. [InlineData("{ }", new string[0])]
  73. [InlineData("{ }", new string[0])]
  74. [InlineData("{ errorLabels : \"not an array\" }", new string[0])]
  75. [InlineData("{ errorLabels : [ ] }", new string[0])]
  76. [InlineData("{ errorLabels : [ 1 ] }", new string[0])]
  77. [InlineData("{ errorLabels : [ null ] }", new string[0])]
  78. [InlineData("{ errorLabels : [ \"one\" ] }", new[] { "one" })]
  79. [InlineData("{ errorLabels : [ 1, \"one\" ] }", new[] { "one" })]
  80. [InlineData("{ errorLabels : [ \"one\", 1 ] }", new[] { "one" })]
  81. [InlineData("{ errorLabels : [ \"one\", null, \"two\" ] }", new[] { "one", "two" })]
  82. public void constructor_should_add_error_labels(string jsonResult, string[] expectedErrorLabels)
  83. {
  84. var result = jsonResult == null ? null : BsonDocument.Parse(jsonResult);
  85. var subject = new MongoCommandException(_connectionId, _message, _command, result);
  86. subject.ErrorLabels.Should().Equal(expectedErrorLabels);
  87. }
  88. [Fact]
  89. public void ErrorMessage_get_returns_expected_result()
  90. {
  91. var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);
  92. var result = subject.ErrorMessage;
  93. result.Should().Be("error message");
  94. }
  95. #if NET452
  96. [Fact]
  97. public void Serialization_should_work()
  98. {
  99. var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);
  100. subject.AddErrorLabel("one");
  101. var formatter = new BinaryFormatter();
  102. using (var stream = new MemoryStream())
  103. {
  104. formatter.Serialize(stream, subject);
  105. stream.Position = 0;
  106. var rehydrated = (MongoCommandException)formatter.Deserialize(stream);
  107. rehydrated.ErrorLabels.Should().Equal(subject.ErrorLabels);
  108. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  109. rehydrated.Message.Should().Be(_message);
  110. rehydrated.InnerException.Should().BeNull();
  111. rehydrated.Command.Should().Be(_command);
  112. rehydrated.Result.Should().Be(_commandResult);
  113. }
  114. }
  115. #endif
  116. }
  117. }