PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 71 lines | 50 code | 7 blank | 14 comment | 0 complexity | bb4af5bf30d77fb79a3e8da47e0bb566 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.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.Driver.Core.Clusters;
  23. using MongoDB.Driver.Core.Connections;
  24. using MongoDB.Driver.Core.Servers;
  25. using Xunit;
  26. namespace MongoDB.Driver
  27. {
  28. public class MongoQueryExceptionTests
  29. {
  30. private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
  31. private readonly string _message = "message";
  32. private readonly BsonDocument _query = new BsonDocument("query", 1);
  33. private readonly BsonDocument _queryResult = new BsonDocument("result", 1);
  34. [Fact]
  35. public void constructor_should_initialize_subject()
  36. {
  37. var subject = new MongoQueryException(_connectionId, _message, _query, _queryResult);
  38. subject.ConnectionId.Should().BeSameAs(_connectionId);
  39. subject.InnerException.Should().BeNull();
  40. subject.Message.Should().Be(_message);
  41. subject.Query.Should().Be(_query);
  42. subject.QueryResult.Should().Be(_queryResult);
  43. }
  44. #if NET452
  45. [Fact]
  46. public void Serialization_should_work()
  47. {
  48. var subject = new MongoQueryException(_connectionId, _message, _query, _queryResult);
  49. var formatter = new BinaryFormatter();
  50. using (var stream = new MemoryStream())
  51. {
  52. formatter.Serialize(stream, subject);
  53. stream.Position = 0;
  54. var rehydrated = (MongoQueryException)formatter.Deserialize(stream);
  55. rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
  56. rehydrated.InnerException.Should().BeNull();
  57. rehydrated.Message.Should().Be(subject.Message);
  58. rehydrated.Query.Should().Be(subject.Query);
  59. rehydrated.QueryResult.Should().Be(subject.QueryResult);
  60. }
  61. }
  62. #endif
  63. }
  64. }