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

/src/MongoDB.Driver.Core.Tests/Core/WireProtocol/Messages/ReplyMessageTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 126 lines | 101 code | 11 blank | 14 comment | 0 complexity | bc19eb0db6dd7f4cb167555766cfdd2c 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.Collections.Generic;
  17. using FluentAssertions;
  18. using MongoDB.Bson;
  19. using MongoDB.Bson.Serialization;
  20. using MongoDB.Bson.Serialization.Serializers;
  21. using MongoDB.Driver.Core.WireProtocol.Messages;
  22. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
  23. using NSubstitute;
  24. using NUnit.Framework;
  25. namespace MongoDB.Driver.Core.WireProtocol.Messages
  26. {
  27. [TestFixture]
  28. public class ReplyMessageTests
  29. {
  30. private readonly long _cursorId = 2;
  31. private readonly List<BsonDocument> _documents = new List<BsonDocument> { new BsonDocument("x", 1) };
  32. private readonly int _numberReturned = 1; // should match _documents.Count
  33. private readonly BsonDocument _queryFailureDocument = new BsonDocument("y", 2);
  34. private readonly int _requestId = 3;
  35. private readonly int _responseTo = 4;
  36. private readonly IBsonSerializer<BsonDocument> _serializer = BsonDocumentSerializer.Instance;
  37. private readonly int _startingFrom = 5;
  38. [Test]
  39. public void Constructor_should_initialize_instance()
  40. {
  41. var subject = new ReplyMessage<BsonDocument>(true, _cursorId, false, _documents, _numberReturned, false, null, _requestId, _responseTo, _serializer, _startingFrom);
  42. subject.AwaitCapable.Should().BeTrue();
  43. subject.CursorId.Should().Be(_cursorId);
  44. subject.CursorNotFound.Should().BeFalse();
  45. subject.Documents.Should().BeSameAs(_documents);
  46. subject.NumberReturned.Should().Be(_numberReturned);
  47. subject.QueryFailure.Should().BeFalse();
  48. subject.QueryFailureDocument.Should().BeNull();
  49. subject.RequestId.Should().Be(_requestId);
  50. subject.ResponseTo.Should().Be(_responseTo);
  51. subject.Serializer.Should().BeSameAs(_serializer);
  52. subject.StartingFrom.Should().Be(_startingFrom);
  53. }
  54. [Test]
  55. public void Constructor_with_cursor_not_found_should_initialize_instance()
  56. {
  57. var subject = new ReplyMessage<BsonDocument>(true, _cursorId, true, null, 0, false, null, _requestId, _responseTo, _serializer, 0);
  58. subject.AwaitCapable.Should().BeTrue();
  59. subject.CursorId.Should().Be(_cursorId);
  60. subject.CursorNotFound.Should().BeTrue();
  61. subject.Documents.Should().BeNull();
  62. subject.NumberReturned.Should().Be(0);
  63. subject.QueryFailure.Should().BeFalse();
  64. subject.QueryFailureDocument.Should().BeNull();
  65. subject.RequestId.Should().Be(_requestId);
  66. subject.ResponseTo.Should().Be(_responseTo);
  67. subject.Serializer.Should().BeSameAs(_serializer);
  68. subject.StartingFrom.Should().Be(0);
  69. }
  70. [Test]
  71. public void Constructor_with_both_documents_nor_queryFailureDocument_should_throw()
  72. {
  73. Action action = () => new ReplyMessage<BsonDocument>(true, _cursorId, false, _documents, _numberReturned, false, _queryFailureDocument, _requestId, _responseTo, null, _startingFrom);
  74. action.ShouldThrow<ArgumentException>();
  75. }
  76. [Test]
  77. public void Constructor_with_neither_documents_nor_queryFailureDocument_should_throw()
  78. {
  79. Action action = () => new ReplyMessage<BsonDocument>(true, _cursorId, false, null, _numberReturned, false, null, _requestId, _responseTo, null, _startingFrom);
  80. action.ShouldThrow<ArgumentException>();
  81. }
  82. [Test]
  83. public void Constructor_with_null_serializer_should_throw()
  84. {
  85. Action action = () => new ReplyMessage<BsonDocument>(true, _cursorId, false, _documents, _numberReturned, false, null, _requestId, _responseTo, null, _startingFrom);
  86. action.ShouldThrow<ArgumentNullException>();
  87. }
  88. [Test]
  89. public void Constructor_with_queryFailure_should_initialize_instance()
  90. {
  91. var subject = new ReplyMessage<BsonDocument>(true, _cursorId, false, null, 0, true, _queryFailureDocument, _requestId, _responseTo, _serializer, 0);
  92. subject.AwaitCapable.Should().BeTrue();
  93. subject.CursorId.Should().Be(_cursorId);
  94. subject.CursorNotFound.Should().BeFalse();
  95. subject.Documents.Should().BeNull();
  96. subject.NumberReturned.Should().Be(0);
  97. subject.QueryFailure.Should().BeTrue();
  98. subject.QueryFailureDocument.Should().Be(_queryFailureDocument);
  99. subject.RequestId.Should().Be(_requestId);
  100. subject.ResponseTo.Should().Be(_responseTo);
  101. subject.Serializer.Should().BeSameAs(_serializer);
  102. subject.StartingFrom.Should().Be(0);
  103. }
  104. [Test]
  105. public void GetEncoder_should_return_encoder()
  106. {
  107. var subject = new ReplyMessage<BsonDocument>(true, _cursorId, false, _documents, _numberReturned, false, null, _requestId, _responseTo, _serializer, _startingFrom);
  108. var stubEncoderFactory = Substitute.For<IMessageEncoderFactory>();
  109. var stubEncoder = Substitute.For<IMessageEncoder>();
  110. stubEncoderFactory.GetReplyMessageEncoder(_serializer).Returns(stubEncoder);
  111. var result = subject.GetEncoder(stubEncoderFactory);
  112. result.Should().BeSameAs(stubEncoder);
  113. }
  114. }
  115. }