PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 297 lines | 242 code | 39 blank | 16 comment | 12 complexity | 54486a70eed952db5bea96a2499946a2 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-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.Linq;
  18. using FluentAssertions;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.Serialization.Serializers;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using MongoDB.Driver.Core.Bindings;
  23. using MongoDB.Driver.Core.Operations;
  24. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
  25. using Moq;
  26. using Xunit;
  27. namespace MongoDB.Driver
  28. {
  29. public class IAsyncCursorExtensionsTests
  30. {
  31. // public methods
  32. [Theory]
  33. [ParameterAttributeData]
  34. public void Any_should_return_expected_result(
  35. [Values(0, 1, 2)] int count,
  36. [Values(false, true)] bool async)
  37. {
  38. var cursor = CreateCursor(count);
  39. var expectedResult = count > 0;
  40. bool result;
  41. if (async)
  42. {
  43. result = cursor.AnyAsync().GetAwaiter().GetResult();
  44. }
  45. else
  46. {
  47. result = cursor.Any();
  48. }
  49. result.Should().Be(expectedResult);
  50. }
  51. [Theory]
  52. [ParameterAttributeData]
  53. public void First_should_return_expected_result(
  54. [Values(1, 2)] int count,
  55. [Values(false, true)] bool async)
  56. {
  57. var cursor = CreateCursor(count);
  58. var expectedResult = new BsonDocument("_id", 0);
  59. BsonDocument result;
  60. if (async)
  61. {
  62. result = cursor.FirstAsync().GetAwaiter().GetResult();
  63. }
  64. else
  65. {
  66. result = cursor.First();
  67. }
  68. result.Should().Be(expectedResult);
  69. }
  70. [Theory]
  71. [ParameterAttributeData]
  72. public void First_should_throw_when_cursor_has_no_documents(
  73. [Values(false, true)] bool async)
  74. {
  75. var cursor = CreateCursor(0);
  76. Action action;
  77. if (async)
  78. {
  79. action = () => cursor.FirstAsync().GetAwaiter().GetResult();
  80. }
  81. else
  82. {
  83. action = () => cursor.First();
  84. }
  85. action.ShouldThrow<InvalidOperationException>();
  86. }
  87. [Theory]
  88. [ParameterAttributeData]
  89. public void FirstOrDefault_should_return_expected_result(
  90. [Values(0, 1, 2)] int count,
  91. [Values(false, true)] bool async)
  92. {
  93. var cursor = CreateCursor(count);
  94. var expectedResult = count == 0 ? null : new BsonDocument("_id", 0);
  95. BsonDocument result;
  96. if (async)
  97. {
  98. result = cursor.FirstOrDefaultAsync().GetAwaiter().GetResult();
  99. }
  100. else
  101. {
  102. result = cursor.FirstOrDefault();
  103. }
  104. result.Should().Be(expectedResult);
  105. }
  106. [Theory]
  107. [ParameterAttributeData]
  108. public void Single_should_return_expected_result(
  109. [Values(false, true)] bool async)
  110. {
  111. var cursor = CreateCursor(1);
  112. var expectedResult = new BsonDocument("_id", 0);
  113. BsonDocument result;
  114. if (async)
  115. {
  116. result = cursor.SingleAsync().GetAwaiter().GetResult();
  117. }
  118. else
  119. {
  120. result = cursor.Single();
  121. }
  122. result.Should().Be(expectedResult);
  123. }
  124. [Theory]
  125. [ParameterAttributeData]
  126. public void Single_should_throw_when_cursor_has_wrong_number_of_documents(
  127. [Values(0, 2)] int count,
  128. [Values(false, true)] bool async)
  129. {
  130. var cursor = CreateCursor(count);
  131. Action action;
  132. if (async)
  133. {
  134. action = () => cursor.SingleAsync().GetAwaiter().GetResult();
  135. }
  136. else
  137. {
  138. action = () => cursor.Single();
  139. }
  140. action.ShouldThrow<InvalidOperationException>();
  141. }
  142. [Theory]
  143. [ParameterAttributeData]
  144. public void SingleOrDefault_should_return_expected_result(
  145. [Values(0, 1)] int count,
  146. [Values(false, true)] bool async)
  147. {
  148. var cursor = CreateCursor(count);
  149. var expectedResult = count == 0 ? null : new BsonDocument("_id", 0);
  150. BsonDocument result;
  151. if (async)
  152. {
  153. result = cursor.SingleOrDefaultAsync().GetAwaiter().GetResult();
  154. }
  155. else
  156. {
  157. result = cursor.SingleOrDefault();
  158. }
  159. result.Should().Be(expectedResult);
  160. }
  161. [Theory]
  162. [ParameterAttributeData]
  163. public void SingleOrDefault_should_throw_when_cursor_has_wrong_number_of_documents(
  164. [Values(false, true)] bool async)
  165. {
  166. var cursor = CreateCursor(2);
  167. Action action;
  168. if (async)
  169. {
  170. action = () => cursor.SingleOrDefaultAsync().GetAwaiter().GetResult();
  171. }
  172. else
  173. {
  174. action = () => cursor.SingleOrDefault();
  175. }
  176. action.ShouldThrow<InvalidOperationException>();
  177. }
  178. [Fact]
  179. public void ToEnumerable_result_should_only_be_enumerable_one_time()
  180. {
  181. var cursor = CreateCursor(2);
  182. var enumerable = cursor.ToEnumerable();
  183. enumerable.GetEnumerator();
  184. Action action = () => enumerable.GetEnumerator();
  185. action.ShouldThrow<InvalidOperationException>();
  186. }
  187. [Fact]
  188. public void ToEnumerable_should_return_expected_result()
  189. {
  190. var cursor = CreateCursor(2);
  191. var expectedDocuments = new[]
  192. {
  193. new BsonDocument("_id", 0),
  194. new BsonDocument("_id", 1)
  195. };
  196. var result = cursor.ToEnumerable();
  197. result.ToList().Should().Equal(expectedDocuments);
  198. }
  199. [Theory]
  200. [ParameterAttributeData]
  201. public void ToList_should_only_be_callable_one_time(
  202. [Values(false, true)] bool async)
  203. {
  204. var cursor = CreateCursor(2);
  205. cursor.ToList();
  206. Action action;
  207. if (async)
  208. {
  209. action = () => cursor.ToListAsync().GetAwaiter().GetResult();
  210. }
  211. else
  212. {
  213. action = () => cursor.ToList();
  214. }
  215. action.ShouldThrow<InvalidOperationException>();
  216. }
  217. [Theory]
  218. [ParameterAttributeData]
  219. public void ToList_should_return_expected_result(
  220. [Values(false, true)] bool async)
  221. {
  222. var cursor = CreateCursor(2);
  223. var expectedResult = new[]
  224. {
  225. new BsonDocument("_id", 0),
  226. new BsonDocument("_id", 1)
  227. };
  228. List<BsonDocument> result;
  229. if (async)
  230. {
  231. result = cursor.ToListAsync().GetAwaiter().GetResult();
  232. }
  233. else
  234. {
  235. result = cursor.ToList();
  236. }
  237. result.Should().Equal(expectedResult);
  238. }
  239. // private methods
  240. private IAsyncCursor<BsonDocument> CreateCursor(int count)
  241. {
  242. var firstBatch = Enumerable.Range(0, count)
  243. .Select(i => new BsonDocument("_id", i))
  244. .ToArray();
  245. return new AsyncCursor<BsonDocument>(
  246. channelSource: new Mock<IChannelSource>().Object,
  247. collectionNamespace: new CollectionNamespace("foo", "bar"),
  248. query: new BsonDocument(),
  249. firstBatch: firstBatch,
  250. cursorId: 0,
  251. batchSize: null,
  252. limit: null,
  253. serializer: BsonDocumentSerializer.Instance,
  254. messageEncoderSettings: new MessageEncoderSettings(),
  255. maxTime: null);
  256. }
  257. }
  258. }