PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionDescriptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 144 lines | 104 code | 26 blank | 14 comment | 0 complexity | 7ac93b16446b12e2b787b0a2aadbb576 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 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.Net;
  17. using FluentAssertions;
  18. using MongoDB.Bson;
  19. using MongoDB.Driver.Core.Clusters;
  20. using MongoDB.Driver.Core.Connections;
  21. using MongoDB.Driver.Core.Servers;
  22. using NUnit.Framework;
  23. namespace MongoDB.Driver.Core.Connections
  24. {
  25. [TestFixture]
  26. public class ConnectionDescriptionTests
  27. {
  28. private static readonly BuildInfoResult __buildInfoResult = new BuildInfoResult(BsonDocument.Parse(
  29. "{ ok: 1, version: \"2.6.3\" }"
  30. ));
  31. private static readonly ConnectionId __connectionId = new ConnectionId(
  32. new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017)));
  33. private static readonly IsMasterResult __isMasterResult = new IsMasterResult(BsonDocument.Parse(
  34. "{ ok: 1, maxWriteBatchSize: 10, maxBsonObjectSize: 20, maxMessageSizeBytes: 30 }"
  35. ));
  36. [Test]
  37. public void Constructor_should_throw_an_ArgumentNullException_when_connectionId_is_null()
  38. {
  39. Action act = () => new ConnectionDescription(null, __isMasterResult, __buildInfoResult);
  40. act.ShouldThrow<ArgumentNullException>();
  41. }
  42. [Test]
  43. public void Constructor_should_throw_an_ArgumentNullException_when_isMasterResult_is_null()
  44. {
  45. Action act = () => new ConnectionDescription(__connectionId, null, __buildInfoResult);
  46. act.ShouldThrow<ArgumentNullException>();
  47. }
  48. [Test]
  49. public void Constructor_should_throw_an_ArgumentNullException_when_buildInfoResult_is_null()
  50. {
  51. Action act = () => new ConnectionDescription(__connectionId, __isMasterResult, null);
  52. act.ShouldThrow<ArgumentNullException>();
  53. }
  54. [Test]
  55. public void Equals_should_return_correct_results()
  56. {
  57. var connectionId1 = new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27018)), 10);
  58. var connectionId2 = new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27018)), 10);
  59. var isMasterResult1 = new IsMasterResult(new BsonDocument("x", 1));
  60. var isMasterResult2 = new IsMasterResult(new BsonDocument("x", 2));
  61. var buildInfoResult1 = new BuildInfoResult(new BsonDocument("version", "2.6.3"));
  62. var buildInfoResult2 = new BuildInfoResult(new BsonDocument("version", "2.4.10"));
  63. var subject1 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult1);
  64. var subject2 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult1);
  65. var subject3 = new ConnectionDescription(connectionId1, isMasterResult1, buildInfoResult2);
  66. var subject4 = new ConnectionDescription(connectionId1, isMasterResult2, buildInfoResult1);
  67. var subject5 = new ConnectionDescription(connectionId2, isMasterResult1, buildInfoResult1);
  68. subject1.Equals(subject2).Should().BeTrue();
  69. subject1.Equals(subject3).Should().BeFalse();
  70. subject1.Equals(subject4).Should().BeFalse();
  71. subject1.Equals(subject5).Should().BeFalse();
  72. }
  73. [Test]
  74. public void ConnectionId_should_return_ConnectionId()
  75. {
  76. var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
  77. subject.ConnectionId.Should().Be(__connectionId);
  78. }
  79. [Test]
  80. public void MaxBatchCount_should_return_isMasterResult_MaxBatchCount()
  81. {
  82. var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
  83. subject.MaxBatchCount.Should().Be(__isMasterResult.MaxBatchCount);
  84. }
  85. [Test]
  86. public void MaxDocumentSize_should_return_isMasterResult_MaxDocumentSize()
  87. {
  88. var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
  89. subject.MaxDocumentSize.Should().Be(__isMasterResult.MaxDocumentSize);
  90. }
  91. [Test]
  92. public void MaxMessageSize_should_return_isMasterResult_MaxMessageSize()
  93. {
  94. var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
  95. subject.MaxMessageSize.Should().Be(__isMasterResult.MaxMessageSize);
  96. }
  97. [Test]
  98. public void ServerVersion_should_return_buildInfoResult_ServerVersion()
  99. {
  100. var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
  101. subject.ServerVersion.Should().Be(__buildInfoResult.ServerVersion);
  102. }
  103. [Test]
  104. public void WithConnectionId_should_return_new_instance_even_when_only_the_serverValue_differs()
  105. {
  106. var clusterId = new ClusterId();
  107. var serverId = new ServerId(clusterId, new DnsEndPoint("localhost", 1));
  108. var connectionId1 = new ConnectionId(serverId, 1);
  109. var connectionId2 = new ConnectionId(serverId, 1).WithServerValue(2);
  110. var isMasterResult = new IsMasterResult(new BsonDocument());
  111. var buildInfoResult = new BuildInfoResult(new BsonDocument("version", "2.6.0"));
  112. var subject = new ConnectionDescription(connectionId1, isMasterResult, buildInfoResult);
  113. var result = subject.WithConnectionId(connectionId2);
  114. result.Should().NotBeSameAs(subject);
  115. result.ConnectionId.Should().BeSameAs(connectionId2);
  116. }
  117. }
  118. }