PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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