PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 73 lines | 52 code | 7 blank | 14 comment | 0 complexity | 9c9e9b1a840b6f0109280c993379aa18 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.IO;
  16. using System.Net;
  17. #if NET452
  18. using System.Runtime.Serialization.Formatters.Binary;
  19. #endif
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.TestHelpers.EqualityComparers;
  23. using MongoDB.Driver.Core.Clusters;
  24. using MongoDB.Driver.Core.Connections;
  25. using MongoDB.Driver.Core.Servers;
  26. using Xunit;
  27. namespace MongoDB.Driver
  28. {
  29. public class MongoDuplicateKeyExceptionTests
  30. {
  31. private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
  32. private readonly string _message = "message";
  33. private readonly WriteConcernResult _writeConcernResult = new WriteConcernResult(new BsonDocument("result", 1));
  34. [Fact]
  35. public void constructor_should_initialize_subject()
  36. {
  37. var subject = new MongoDuplicateKeyException(_connectionId, _message, _writeConcernResult);
  38. subject.Command.Should().BeNull();
  39. subject.ConnectionId.Should().BeSameAs(_connectionId);
  40. subject.InnerException.Should().BeNull();
  41. subject.Message.Should().BeSameAs(_message);
  42. subject.Result.Should().Be(_writeConcernResult.Response);
  43. subject.WriteConcernResult.Should().Be(_writeConcernResult);
  44. }
  45. #if NET452
  46. [Fact]
  47. public void Serialization_should_work()
  48. {
  49. var subject = new MongoDuplicateKeyException(_connectionId, _message, _writeConcernResult);
  50. var formatter = new BinaryFormatter();
  51. using (var stream = new MemoryStream())
  52. {
  53. formatter.Serialize(stream, subject);
  54. stream.Position = 0;
  55. var rehydrated = (MongoDuplicateKeyException)formatter.Deserialize(stream);
  56. rehydrated.Command.Should().BeNull();
  57. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  58. rehydrated.InnerException.Should().BeNull();
  59. rehydrated.Message.Should().Be(subject.Message);
  60. rehydrated.Result.Should().Be(subject.Result);
  61. rehydrated.WriteConcernResult.Should().BeUsing(subject.WriteConcernResult, EqualityComparerRegistry.Default);
  62. }
  63. }
  64. #endif
  65. }
  66. }