PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Misc/BatchableSourceTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 414 lines | 338 code | 61 blank | 15 comment | 3 complexity | 8dc3657fecfffa939ad25387835c8406 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-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.TestHelpers.XunitExtensions;
  20. using Xunit;
  21. namespace MongoDB.Driver.Core.Misc
  22. {
  23. public class BatchableSourceTests
  24. {
  25. [Theory]
  26. [ParameterAttributeData]
  27. public void constructor_with_list_should_initialize_instance(
  28. [Values(0, 1, 2, 3)] int length)
  29. {
  30. var list = new List<int>();
  31. for (var i = 0; i < length; i++)
  32. {
  33. list.Add(i);
  34. }
  35. var result = new BatchableSource<int>(list);
  36. result.CanBeSplit.Should().BeFalse();
  37. result.Count.Should().Be(length);
  38. result.Items.Should().Equal(list);
  39. result.Offset.Should().Be(0);
  40. result.ProcessedCount.Should().Be(0);
  41. }
  42. [Fact]
  43. public void constructor_with_list_should_throw_when_list_is_null()
  44. {
  45. var exception = Record.Exception(() => new BatchableSource<int>((IReadOnlyList<int>)null));
  46. var e = exception.Should().BeOfType<ArgumentNullException>().Subject;
  47. e.ParamName.Should().Be("items");
  48. }
  49. [Theory]
  50. [ParameterAttributeData]
  51. public void constructor_with_list_and_canBeSplit_should_initialize_instance(
  52. [Values(0, 1, 2, 3)] int length,
  53. [Values(false, true)] bool canBeSplit)
  54. {
  55. var list = new List<int>();
  56. for (var i = 0; i < length; i++)
  57. {
  58. list.Add(i);
  59. }
  60. var result = new BatchableSource<int>(list, canBeSplit);
  61. result.CanBeSplit.Should().Be(canBeSplit);
  62. result.Count.Should().Be(length);
  63. result.Items.Should().Equal(list);
  64. result.Offset.Should().Be(0);
  65. result.ProcessedCount.Should().Be(0);
  66. }
  67. [Fact]
  68. public void constructor_with_list_and_canBeSplit_should_throw_when_list_is_null()
  69. {
  70. var exception = Record.Exception(() => new BatchableSource<int>((IReadOnlyList<int>)null, true));
  71. var e = exception.Should().BeOfType<ArgumentNullException>().Subject;
  72. e.ParamName.Should().Be("items");
  73. }
  74. [Theory]
  75. [ParameterAttributeData]
  76. public void constructor_with_list_offset_count_and_canBeSplit_should_initialize_instance(
  77. [Values(3, 4)] int length,
  78. [Values(0, 1)] int offset,
  79. [Values(1, 2)] int count,
  80. [Values(false, true)] bool canBeSplit)
  81. {
  82. var list = new List<int>();
  83. for (var i = 0; i < length; i++)
  84. {
  85. list.Add(i);
  86. }
  87. var result = new BatchableSource<int>(list, offset, count, canBeSplit);
  88. result.CanBeSplit.Should().Be(canBeSplit);
  89. result.Count.Should().Be(count);
  90. result.Items.Should().Equal(list);
  91. result.Offset.Should().Be(offset);
  92. result.ProcessedCount.Should().Be(0);
  93. }
  94. [Fact]
  95. public void constructor_with_list_offset_count_and_canBeSplit_should_throw_when_list_is_null()
  96. {
  97. var exception = Record.Exception(() => new BatchableSource<int>((IReadOnlyList<int>)null, 0, 0, true));
  98. var e = exception.Should().BeOfType<ArgumentNullException>().Subject;
  99. e.ParamName.Should().Be("items");
  100. }
  101. [Theory]
  102. [InlineData(0, -1)]
  103. [InlineData(0, 1)]
  104. [InlineData(1, -1)]
  105. [InlineData(1, 2)]
  106. [InlineData(2, -1)]
  107. [InlineData(2, 3)]
  108. public void constructor_with_list_offset_count_and_canBeSplit_should_throw_when_offset_is_invalid(int length, int offset)
  109. {
  110. var list = Enumerable.Range(0, length).ToList();
  111. var exception = Record.Exception(() => new BatchableSource<int>(list, offset, 0, true));
  112. var e = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject;
  113. e.ParamName.Should().Be("offset");
  114. }
  115. [Theory]
  116. [InlineData(0, 0, -1)]
  117. [InlineData(0, 0, 1)]
  118. [InlineData(1, 0, -1)]
  119. [InlineData(1, 0, 2)]
  120. [InlineData(1, 1, 1)]
  121. [InlineData(2, 0, -1)]
  122. [InlineData(2, 0, 3)]
  123. [InlineData(2, 1, -1)]
  124. [InlineData(2, 1, 2)]
  125. [InlineData(2, 2, -1)]
  126. [InlineData(2, 2, 1)]
  127. public void constructor_with_list_offset_count_and_canBeSplit_should_throw_when_count_is_invalid(int length, int offset, int count)
  128. {
  129. var list = Enumerable.Range(0, length).ToList();
  130. var exception = Record.Exception(() => new BatchableSource<int>(list, offset, count, true));
  131. var e = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject;
  132. e.ParamName.Should().Be("count");
  133. }
  134. [Theory]
  135. [InlineData(0, 0, true)]
  136. [InlineData(1, 0, false)]
  137. [InlineData(1, 1, true)]
  138. [InlineData(2, 0, false)]
  139. [InlineData(2, 1, false)]
  140. [InlineData(2, 2, true)]
  141. [InlineData(3, 0, false)]
  142. [InlineData(3, 1, false)]
  143. [InlineData(3, 2, false)]
  144. [InlineData(3, 3, true)]
  145. public void AllItemsWereProcessed_should_return_expected_result(int length, int processedCount, bool expectedResult)
  146. {
  147. var subject = CreateSubject(length: length);
  148. subject.SetProcessedCount(processedCount);
  149. var result = subject.AllItemsWereProcessed;
  150. result.Should().Be(expectedResult);
  151. }
  152. [Theory]
  153. [ParameterAttributeData]
  154. public void CanBeSplit_should_return_expected_result(
  155. [Values(false, true)] bool value)
  156. {
  157. var subject = CreateSubject(canBeSplit: value);
  158. var result = subject.CanBeSplit;
  159. result.Should().Be(value);
  160. }
  161. [Theory]
  162. [ParameterAttributeData]
  163. public void Count_should_return_expected_result(
  164. [Values(0, 1, 2, 3)] int value)
  165. {
  166. var subject = CreateSubject(count: value);
  167. var result = subject.Count;
  168. result.Should().Be(value);
  169. }
  170. [Theory]
  171. [ParameterAttributeData]
  172. public void Items_should_return_expected_result(
  173. [Values(0, 1, 2, 3)] int length)
  174. {
  175. var items = Enumerable.Range(0, length).ToList();
  176. var subject = new BatchableSource<int>(items);
  177. var result = subject.Items;
  178. result.Should().BeSameAs(items);
  179. }
  180. [Theory]
  181. [ParameterAttributeData]
  182. public void Offset_should_return_expected_result(
  183. [Values(0, 1, 2, 3)] int value)
  184. {
  185. var subject = CreateSubject(length: 4, offset: value, count: 1);
  186. var result = subject.Offset;
  187. result.Should().Be(value);
  188. }
  189. [Theory]
  190. [ParameterAttributeData]
  191. public void ProcessedCount_should_return_expected_result(
  192. [Values(0, 1, 2, 3)] int value)
  193. {
  194. var subject = CreateSubject();
  195. subject.SetProcessedCount(value);
  196. var result = subject.ProcessedCount;
  197. result.Should().Be(value);
  198. }
  199. [Theory]
  200. [InlineData(0, 0, 0, 0, 0, 0)]
  201. [InlineData(1, 0, 0, 0, 0, 0)]
  202. [InlineData(1, 0, 1, 0, 0, 1)]
  203. [InlineData(1, 0, 1, 1, 1, 0)]
  204. [InlineData(1, 1, 0, 0, 1, 0)]
  205. [InlineData(2, 0, 0, 0, 0, 0)]
  206. [InlineData(2, 0, 1, 0, 0, 1)]
  207. [InlineData(2, 0, 1, 1, 1, 0)]
  208. [InlineData(2, 0, 2, 0, 0, 2)]
  209. [InlineData(2, 0, 2, 1, 1, 1)]
  210. [InlineData(2, 0, 2, 2, 2, 0)]
  211. [InlineData(2, 1, 0, 0, 1, 0)]
  212. [InlineData(2, 1, 1, 0, 1, 1)]
  213. [InlineData(2, 1, 1, 1, 2, 0)]
  214. [InlineData(2, 2, 0, 0, 2, 0)]
  215. public void AdvancePastProcessedItems_should_have_expected_result(int length, int offset, int count, int processedCount, int expectedOffset, int expectedCount)
  216. {
  217. var subject = CreateSubject(length: length, offset: offset, count: count, canBeSplit: true);
  218. subject.SetProcessedCount(processedCount);
  219. subject.AdvancePastProcessedItems();
  220. subject.Offset.Should().Be(expectedOffset);
  221. subject.Count.Should().Be(expectedCount);
  222. subject.ProcessedCount.Should().Be(0);
  223. }
  224. [Theory]
  225. [InlineData(0, 0, 0, new int[] { })]
  226. [InlineData(1, 0, 0, new int[] { })]
  227. [InlineData(1, 0, 1, new int[] { 0 })]
  228. [InlineData(1, 1, 0, new int[] { })]
  229. [InlineData(2, 0, 0, new int[] { })]
  230. [InlineData(2, 0, 1, new int[] { 0 })]
  231. [InlineData(2, 0, 2, new int[] { 0, 1 })]
  232. [InlineData(2, 1, 0, new int[] { })]
  233. [InlineData(2, 1, 1, new int[] { 1 })]
  234. [InlineData(2, 2, 0, new int[] { })]
  235. public void GetBatchItems_should_return_expected_result(int length, int offset, int count, int[] expectedResult)
  236. {
  237. var subject = CreateSubject(length: length, offset: offset, count: count);
  238. var result = subject.GetBatchItems();
  239. result.Should().Equal(expectedResult);
  240. }
  241. [Theory]
  242. [InlineData(0, 0, 0, 0, new int[] { })]
  243. [InlineData(1, 0, 0, 0, new int[] { })]
  244. [InlineData(1, 0, 1, 0, new int[] { })]
  245. [InlineData(1, 0, 1, 1, new int[] { 0 })]
  246. [InlineData(1, 1, 0, 0, new int[] { })]
  247. [InlineData(2, 0, 0, 0, new int[] { })]
  248. [InlineData(2, 0, 1, 0, new int[] { })]
  249. [InlineData(2, 0, 1, 1, new int[] { 0 })]
  250. [InlineData(2, 0, 2, 0, new int[] { })]
  251. [InlineData(2, 0, 2, 1, new int[] { 0 })]
  252. [InlineData(2, 0, 2, 2, new int[] { 0, 1 })]
  253. [InlineData(2, 1, 0, 0, new int[] { })]
  254. [InlineData(2, 1, 1, 0, new int[] { })]
  255. [InlineData(2, 1, 1, 1, new int[] { 1 })]
  256. [InlineData(2, 2, 0, 0, new int[] { })]
  257. public void GetProcessedItems_should_return_expected_result(int length, int offset, int count, int processedCount, int[] expectedResult)
  258. {
  259. var subject = CreateSubject(length: length, offset: offset, count: count);
  260. subject.SetProcessedCount(processedCount);
  261. var result = subject.GetProcessedItems();
  262. result.Should().Equal(expectedResult);
  263. }
  264. [Theory]
  265. [InlineData(0, 0, 0, 0, new int[] { })]
  266. [InlineData(1, 0, 0, 0, new int[] { })]
  267. [InlineData(1, 0, 1, 0, new int[] { 0 })]
  268. [InlineData(1, 0, 1, 1, new int[] { })]
  269. [InlineData(1, 1, 0, 0, new int[] { })]
  270. [InlineData(2, 0, 0, 0, new int[] { })]
  271. [InlineData(2, 0, 1, 0, new int[] { 0 })]
  272. [InlineData(2, 0, 1, 1, new int[] { })]
  273. [InlineData(2, 0, 2, 0, new int[] { 0, 1 })]
  274. [InlineData(2, 0, 2, 1, new int[] { 1 })]
  275. [InlineData(2, 0, 2, 2, new int[] { })]
  276. [InlineData(2, 1, 0, 0, new int[] { })]
  277. [InlineData(2, 1, 1, 0, new int[] { 1 })]
  278. [InlineData(2, 1, 1, 1, new int[] { })]
  279. [InlineData(2, 2, 0, 0, new int[] { })]
  280. public void GetUnprocessedItems_should_return_expected_result(int length, int offset, int count, int processedCount, int[] expectedResult)
  281. {
  282. var subject = CreateSubject(length: length, offset: offset, count: count);
  283. subject.SetProcessedCount(processedCount);
  284. var result = subject.GetUnprocessedItems();
  285. result.Should().Equal(expectedResult);
  286. }
  287. [Theory]
  288. [InlineData(0, 0, 0, 0)]
  289. [InlineData(1, 0, 0, 0)]
  290. [InlineData(1, 0, 1, 0)]
  291. [InlineData(1, 0, 1, 1)]
  292. [InlineData(1, 1, 0, 0)]
  293. [InlineData(2, 0, 0, 0)]
  294. [InlineData(2, 1, 0, 0)]
  295. [InlineData(2, 1, 1, 0)]
  296. [InlineData(2, 1, 1, 1)]
  297. [InlineData(2, 2, 0, 0)]
  298. public void SetProcessedCount_should_have_expected_result(int length, int offset, int count, int value)
  299. {
  300. var subject = CreateSubject(length: length, offset: offset, count: count, canBeSplit: true);
  301. subject.SetProcessedCount(value);
  302. subject.ProcessedCount.Should().Be(value);
  303. }
  304. [Theory]
  305. [InlineData(2, 0)]
  306. [InlineData(2, 1)]
  307. [InlineData(3, 0)]
  308. [InlineData(3, 1)]
  309. [InlineData(3, 2)]
  310. [InlineData(4, 0)]
  311. [InlineData(4, 1)]
  312. [InlineData(4, 2)]
  313. [InlineData(4, 3)]
  314. public void SetProcessedCount_should_throw_when_batch_cannot_be_split(int length, int value)
  315. {
  316. var subject = CreateSubject(length: length, canBeSplit: false);
  317. var exception = Record.Exception(() => subject.SetProcessedCount(value));
  318. exception.Should().BeOfType<InvalidOperationException>();
  319. }
  320. [Theory]
  321. [InlineData(0, 0, -1)]
  322. [InlineData(0, 0, 1)]
  323. [InlineData(1, 0, -1)]
  324. [InlineData(1, 0, 1)]
  325. [InlineData(1, 1, -1)]
  326. [InlineData(1, 1, 2)]
  327. [InlineData(2, 0, -1)]
  328. [InlineData(2, 0, 1)]
  329. [InlineData(2, 1, -1)]
  330. [InlineData(2, 1, 2)]
  331. [InlineData(2, 2, -1)]
  332. [InlineData(2, 2, 3)]
  333. public void SetProcessedCount_should_throw_when_value_is_invalid(int length, int count, int value)
  334. {
  335. var subject = CreateSubject(length: length, count: count);
  336. var exception = Record.Exception(() => subject.SetProcessedCount(value));
  337. var e = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject;
  338. e.ParamName.Should().Be("value");
  339. }
  340. // private methods
  341. private BatchableSource<int> CreateSubject(
  342. int? length = null,
  343. int? offset = null,
  344. int? count = null,
  345. bool canBeSplit = true)
  346. {
  347. var list = Enumerable.Range(0, length ?? 3).ToList();
  348. offset = offset ?? 0;
  349. count = count ?? list.Count;
  350. return new BatchableSource<int>(list, offset.Value, count.Value, canBeSplit);
  351. }
  352. }
  353. }