PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/AsyncCursorHelperTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 260 lines | 181 code | 65 blank | 14 comment | 0 complexity | 6e0772818de39d63c81d146634d0efb9 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2016-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.Collections.Generic;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using FluentAssertions;
  20. using Moq;
  21. using Xunit;
  22. namespace MongoDB.Driver.Tests
  23. {
  24. public class AsyncCursorHelperTests
  25. {
  26. [Fact]
  27. public void AnyAsync_should_return_true_when_a_result_exists()
  28. {
  29. var task = SetupResultInFirstBatch();
  30. var result = AsyncCursorHelper.AnyAsync(task, CancellationToken.None).Result;
  31. result.Should().Be(true);
  32. }
  33. [Fact]
  34. public void AnyAsync_should_return_false_when_no_result_exists()
  35. {
  36. var task = SetupNoResultIn1Batch();
  37. var result = AsyncCursorHelper.AnyAsync(task, CancellationToken.None).Result;
  38. result.Should().Be(false);
  39. }
  40. [Fact]
  41. public void AnyAsync_should_return_true_when_result_exists_but_is_in_the_second_batch()
  42. {
  43. var task = SetupResultInSecondBatch();
  44. var result = AsyncCursorHelper.AnyAsync(task, CancellationToken.None).Result;
  45. result.Should().Be(true);
  46. }
  47. [Fact]
  48. public void AnyAsync_should_return_false_when_no_result_exists_delayed_to_the_second_batch()
  49. {
  50. var task = SetupNoResultInTwoBatches();
  51. var result = AsyncCursorHelper.AnyAsync(task, CancellationToken.None).Result;
  52. result.Should().Be(false);
  53. }
  54. [Fact]
  55. public void FirstAsync_should_return_first_result_when_one_exists()
  56. {
  57. var task = SetupResultInFirstBatch();
  58. var result = AsyncCursorHelper.FirstAsync(task, CancellationToken.None).Result;
  59. result.Should().Be(1);
  60. }
  61. [Fact]
  62. public void FirstAsync_should_throw_an_exception_when_no_results_exist()
  63. {
  64. var task = SetupNoResultIn1Batch();
  65. Action act = () => AsyncCursorHelper.FirstAsync(task, CancellationToken.None).GetAwaiter().GetResult();
  66. act.ShouldThrow<Exception>();
  67. }
  68. [Fact]
  69. public void FirstAsync_should_return_first_result_when_one_exists_but_is_in_the_second_batch()
  70. {
  71. var task = SetupResultInSecondBatch();
  72. var result = AsyncCursorHelper.FirstAsync(task, CancellationToken.None).Result;
  73. result.Should().Be(1);
  74. }
  75. [Fact]
  76. public void FirstAsync_should_throw_an_exception_when_no_result_exists_delayed_to_the_second_batch()
  77. {
  78. var task = SetupNoResultInTwoBatches();
  79. Action act = () => AsyncCursorHelper.FirstAsync(task, CancellationToken.None).GetAwaiter().GetResult();
  80. act.ShouldThrow<Exception>();
  81. }
  82. [Fact]
  83. public void FirstOrDefaultAsync_should_return_first_result_when_one_exists()
  84. {
  85. var task = SetupResultInFirstBatch();
  86. var result = AsyncCursorHelper.FirstOrDefaultAsync(task, CancellationToken.None).Result;
  87. result.Should().Be(1);
  88. }
  89. [Fact]
  90. public void FirstOrDefaultAsync_should_return_default_when_no_results_exist()
  91. {
  92. var task = SetupNoResultIn1Batch();
  93. var result = AsyncCursorHelper.FirstOrDefaultAsync(task, CancellationToken.None).Result;
  94. result.Should().Be(0);
  95. }
  96. [Fact]
  97. public void FirstOrDefaultAsync_should_return_first_result_when_one_exists_but_is_in_the_second_batch()
  98. {
  99. var task = SetupResultInSecondBatch();
  100. var result = AsyncCursorHelper.FirstOrDefaultAsync(task, CancellationToken.None).Result;
  101. result.Should().Be(1);
  102. }
  103. [Fact]
  104. public void FirstOrDefaultAsync_should_return_default_value_when_no_result_exists_delayed_to_the_second_batch()
  105. {
  106. var task = SetupNoResultInTwoBatches();
  107. var result = AsyncCursorHelper.FirstOrDefaultAsync(task, CancellationToken.None).Result;
  108. result.Should().Be(0);
  109. }
  110. [Fact]
  111. public void SingleAsync_should_return_first_result_when_one_exists()
  112. {
  113. var task = SetupResultInFirstBatch();
  114. var result = AsyncCursorHelper.SingleAsync(task, CancellationToken.None).Result;
  115. result.Should().Be(1);
  116. }
  117. [Fact]
  118. public void SingleAsync_should_throw_an_exception_when_no_results_exist()
  119. {
  120. var task = SetupNoResultIn1Batch();
  121. Action act = () => AsyncCursorHelper.SingleAsync(task, CancellationToken.None).GetAwaiter().GetResult();
  122. act.ShouldThrow<Exception>();
  123. }
  124. [Fact]
  125. public void SingleAsync_should_return_first_result_when_one_exists_but_is_in_the_second_batch()
  126. {
  127. var task = SetupResultInSecondBatch();
  128. var result = AsyncCursorHelper.SingleAsync(task, CancellationToken.None).Result;
  129. result.Should().Be(1);
  130. }
  131. [Fact]
  132. public void SingleAsync_should_throw_an_exception_when_no_result_exists_delayed_to_the_second_batch()
  133. {
  134. var task = SetupNoResultInTwoBatches();
  135. Action act = () => AsyncCursorHelper.SingleAsync(task, CancellationToken.None).GetAwaiter().GetResult();
  136. act.ShouldThrow<Exception>();
  137. }
  138. [Fact]
  139. public void SingleOrDefaultAsync_should_return_first_result_when_one_exists()
  140. {
  141. var task = SetupResultInFirstBatch();
  142. var result = AsyncCursorHelper.SingleOrDefaultAsync(task, CancellationToken.None).Result;
  143. result.Should().Be(1);
  144. }
  145. [Fact]
  146. public void SingleOrDefaultAsync_should_return_default_when_no_results_exist()
  147. {
  148. var task = SetupNoResultIn1Batch();
  149. var result = AsyncCursorHelper.SingleOrDefaultAsync(task, CancellationToken.None).Result;
  150. result.Should().Be(0);
  151. }
  152. [Fact]
  153. public void SingleOrDefaultAsync_should_return_first_result_when_one_exists_but_is_in_the_second_batch()
  154. {
  155. var task = SetupResultInSecondBatch();
  156. var result = AsyncCursorHelper.SingleOrDefaultAsync(task, CancellationToken.None).Result;
  157. result.Should().Be(1);
  158. }
  159. [Fact]
  160. public void SingleOrDefaultAsync_should_return_default_value_when_no_result_exists_delayed_to_the_second_batch()
  161. {
  162. var task = SetupNoResultInTwoBatches();
  163. var result = AsyncCursorHelper.SingleOrDefaultAsync(task, CancellationToken.None).Result;
  164. result.Should().Be(0);
  165. }
  166. private Task<IAsyncCursor<int>> SetupResultInFirstBatch()
  167. {
  168. var mockCursor = new Mock<IAsyncCursor<int>>();
  169. mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(true)).Returns(Task.FromResult(false));
  170. mockCursor.SetupSequence(c => c.Current).Returns(new List<int> { 1 }).Returns((List<int>)null);
  171. return Task.FromResult(mockCursor.Object);
  172. }
  173. private Task<IAsyncCursor<int>> SetupResultInSecondBatch()
  174. {
  175. var mockCursor = new Mock<IAsyncCursor<int>>();
  176. mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(true)).Returns(Task.FromResult(true)).Returns(Task.FromResult(false));
  177. mockCursor.SetupSequence(c => c.Current).Returns(new List<int>()).Returns(new List<int> { 1 }).Returns((List<int>)null);
  178. return Task.FromResult(mockCursor.Object);
  179. }
  180. private Task<IAsyncCursor<int>> SetupNoResultIn1Batch()
  181. {
  182. var mockCursor = new Mock<IAsyncCursor<int>>();
  183. mockCursor.Setup(c => c.MoveNextAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(false));
  184. mockCursor.SetupGet(c => c.Current).Returns((IEnumerable<int>)null);
  185. return Task.FromResult(mockCursor.Object);
  186. }
  187. private Task<IAsyncCursor<int>> SetupNoResultInTwoBatches()
  188. {
  189. var mockCursor = new Mock<IAsyncCursor<int>>();
  190. mockCursor.SetupSequence(c => c.MoveNextAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(true)).Returns(Task.FromResult(false));
  191. mockCursor.SetupSequence(c => c.Current).Returns(new List<int>()).Returns((List<int>)null);
  192. return Task.FromResult(mockCursor.Object);
  193. }
  194. }
  195. }