PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/Operations/MapReduceOperationBaseTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 433 lines | 339 code | 77 blank | 17 comment | 0 complexity | 523a64742ca90860dc342a46db8b275e 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 FluentAssertions;
  17. using MongoDB.Bson;
  18. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
  19. using NUnit.Framework;
  20. namespace MongoDB.Driver.Core.Operations
  21. {
  22. [TestFixture]
  23. public class MapReduceOperationBaseTests
  24. {
  25. // fields
  26. private readonly CollectionNamespace _collectionNamespace = new CollectionNamespace(new DatabaseNamespace("databaseName"), "collectionName");
  27. private readonly BsonJavaScript _mapFunction = "map";
  28. private readonly MessageEncoderSettings _messageEncoderSettings = new MessageEncoderSettings();
  29. private readonly BsonJavaScript _reduceFunction = "reduce";
  30. // test methods
  31. [Test]
  32. public void CollectionNamespace_should_get_value()
  33. {
  34. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  35. var result = subject.CollectionNamespace;
  36. result.Should().BeSameAs(_collectionNamespace);
  37. }
  38. [Test]
  39. public void constructor_should_initialize_instance()
  40. {
  41. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  42. subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
  43. subject.FinalizeFunction.Should().BeNull();
  44. subject.JavaScriptMode.Should().NotHaveValue();
  45. subject.Limit.Should().NotHaveValue();
  46. subject.MapFunction.Should().BeSameAs(_mapFunction);
  47. subject.MaxTime.Should().NotHaveValue();
  48. subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
  49. subject.Filter.Should().BeNull();
  50. subject.ReduceFunction.Should().BeSameAs(_reduceFunction);
  51. subject.Scope.Should().BeNull();
  52. subject.Sort.Should().BeNull();
  53. subject.Verbose.Should().NotHaveValue();
  54. }
  55. [Test]
  56. public void constructor_should_throw_when_collectionNamespace_is_null()
  57. {
  58. Action action = () => new FakeMapReduceOperation(null, _mapFunction, _reduceFunction, _messageEncoderSettings);
  59. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("collectionNamespace");
  60. }
  61. [Test]
  62. public void constructor_should_throw_when_mapFunction_is_null()
  63. {
  64. Action action = () => new FakeMapReduceOperation(_collectionNamespace, null, _reduceFunction, _messageEncoderSettings);
  65. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("mapFunction");
  66. }
  67. [Test]
  68. public void constructor_should_throw_when_messageEncoderSettings_is_null()
  69. {
  70. Action action = () => new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, null);
  71. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("messageEncoderSettings");
  72. }
  73. [Test]
  74. public void constructor_should_throw_when_reduceFunction_is_null()
  75. {
  76. Action action = () => new FakeMapReduceOperation(_collectionNamespace, _mapFunction, null, _messageEncoderSettings);
  77. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("reduceFunction");
  78. }
  79. [Test]
  80. public void CreateCommand_should_return_the_expected_result()
  81. {
  82. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  83. var expectedResult = new BsonDocument
  84. {
  85. { "mapreduce", "collectionName" },
  86. { "map", _mapFunction },
  87. { "reduce", _reduceFunction },
  88. { "out", new BsonDocument("fake", 1) }
  89. };
  90. var result = subject.CreateCommand();
  91. result.Should().Be(expectedResult);
  92. }
  93. [Test]
  94. public void CreateCommand_should_return_the_expected_result_when_Filter_is_provided()
  95. {
  96. var filter = new BsonDocument("filter", 1);
  97. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  98. {
  99. Filter = filter
  100. };
  101. var expectedResult = new BsonDocument
  102. {
  103. { "mapreduce", "collectionName" },
  104. { "map", _mapFunction },
  105. { "reduce", _reduceFunction },
  106. { "out", new BsonDocument("fake", 1) },
  107. { "query", filter }
  108. };
  109. var result = subject.CreateCommand();
  110. result.Should().Be(expectedResult);
  111. }
  112. [Test]
  113. public void CreateCommand_should_return_the_expected_result_when_FinalizeFunction_is_provided()
  114. {
  115. var finalizeFunction = new BsonJavaScript("finalize");
  116. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  117. {
  118. FinalizeFunction = finalizeFunction
  119. };
  120. var expectedResult = new BsonDocument
  121. {
  122. { "mapreduce", "collectionName" },
  123. { "map", _mapFunction },
  124. { "reduce", _reduceFunction },
  125. { "out", new BsonDocument("fake", 1) },
  126. { "finalize", finalizeFunction }
  127. };
  128. var result = subject.CreateCommand();
  129. result.Should().Be(expectedResult);
  130. }
  131. [Test]
  132. public void CreateCommand_should_return_the_expected_result_when_JavaScriptMode_is_provided()
  133. {
  134. var javaScriptMode = true;
  135. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  136. {
  137. JavaScriptMode = javaScriptMode
  138. };
  139. var expectedResult = new BsonDocument
  140. {
  141. { "mapreduce", "collectionName" },
  142. { "map", _mapFunction },
  143. { "reduce", _reduceFunction },
  144. { "out", new BsonDocument("fake", 1) },
  145. { "jsMode", javaScriptMode }
  146. };
  147. var result = subject.CreateCommand();
  148. result.Should().Be(expectedResult);
  149. }
  150. [Test]
  151. public void CreateCommand_should_return_the_expected_result_when_Limit_is_provided()
  152. {
  153. var limit = 1L;
  154. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  155. {
  156. Limit = limit
  157. };
  158. var expectedResult = new BsonDocument
  159. {
  160. { "mapreduce", "collectionName" },
  161. { "map", _mapFunction },
  162. { "reduce", _reduceFunction },
  163. { "out", new BsonDocument("fake", 1) },
  164. { "limit", limit }
  165. };
  166. var result = subject.CreateCommand();
  167. result.Should().Be(expectedResult);
  168. }
  169. [Test]
  170. public void CreateCommand_should_return_the_expected_result_when_MaxTime_is_provided()
  171. {
  172. var maxTime = TimeSpan.FromSeconds(1.5);
  173. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  174. {
  175. MaxTime = maxTime
  176. };
  177. var expectedResult = new BsonDocument
  178. {
  179. { "mapreduce", "collectionName" },
  180. { "map", _mapFunction },
  181. { "reduce", _reduceFunction },
  182. { "out", new BsonDocument("fake", 1) },
  183. { "maxTimeMS", 1500.0 }
  184. };
  185. var result = subject.CreateCommand();
  186. result.Should().Be(expectedResult);
  187. }
  188. [Test]
  189. public void CreateCommand_should_return_the_expected_result_when_Scope_is_provided()
  190. {
  191. var scope = new BsonDocument("scope", 1);
  192. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  193. {
  194. Scope = scope
  195. };
  196. var expectedResult = new BsonDocument
  197. {
  198. { "mapreduce", "collectionName" },
  199. { "map", _mapFunction },
  200. { "reduce", _reduceFunction },
  201. { "out", new BsonDocument("fake", 1) },
  202. { "scope", scope }
  203. };
  204. var result = subject.CreateCommand();
  205. result.Should().Be(expectedResult);
  206. }
  207. [Test]
  208. public void CreateCommand_should_return_the_expected_result_when_Sort_is_provided()
  209. {
  210. var sort = new BsonDocument("sort", 1);
  211. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  212. {
  213. Sort = sort
  214. };
  215. var expectedResult = new BsonDocument
  216. {
  217. { "mapreduce", "collectionName" },
  218. { "map", _mapFunction },
  219. { "reduce", _reduceFunction },
  220. { "out", new BsonDocument("fake", 1) },
  221. { "sort", sort }
  222. };
  223. var result = subject.CreateCommand();
  224. result.Should().Be(expectedResult);
  225. }
  226. [Test]
  227. public void CreateCommand_should_return_the_expected_result_when_Verbose_is_provided()
  228. {
  229. var verbose = true;
  230. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  231. {
  232. Verbose = verbose
  233. };
  234. var expectedResult = new BsonDocument
  235. {
  236. { "mapreduce", "collectionName" },
  237. { "map", _mapFunction },
  238. { "reduce", _reduceFunction },
  239. { "out", new BsonDocument("fake", 1) },
  240. { "verbose", verbose }
  241. };
  242. var result = subject.CreateCommand();
  243. result.Should().Be(expectedResult);
  244. }
  245. [Test]
  246. public void Filter_should_get_and_set_value()
  247. {
  248. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  249. var value = new BsonDocument("filter", 1);
  250. subject.Filter = value;
  251. var result = subject.Filter;
  252. result.Should().BeSameAs(value);
  253. }
  254. [Test]
  255. public void FinalizeFunction_should_get_and_set_value()
  256. {
  257. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  258. var value = new BsonJavaScript("finalize");
  259. subject.FinalizeFunction = value;
  260. var result = subject.FinalizeFunction;
  261. result.Should().BeSameAs(value);
  262. }
  263. [Test]
  264. public void JavaScriptMode_should_get_and_set_value()
  265. {
  266. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  267. var value = true;
  268. subject.JavaScriptMode = value;
  269. var result = subject.JavaScriptMode;
  270. result.Should().Be(value);
  271. }
  272. [Test]
  273. public void Limit_should_get_and_set_value()
  274. {
  275. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  276. var value = 1L;
  277. subject.Limit = value;
  278. var result = subject.Limit;
  279. result.Should().Be(value);
  280. }
  281. [Test]
  282. public void MapFunction_should_get_value()
  283. {
  284. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  285. var result = subject.MapFunction;
  286. result.Should().BeSameAs(_mapFunction);
  287. }
  288. [Test]
  289. public void MaxTime_should_get_and_set_value()
  290. {
  291. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  292. var value = TimeSpan.FromSeconds(1.5);
  293. subject.MaxTime = value;
  294. var result = subject.MaxTime;
  295. result.Should().Be(value);
  296. }
  297. [Test]
  298. public void MessageEncoderSettings_should_get_value()
  299. {
  300. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  301. var result = subject.MessageEncoderSettings;
  302. result.Should().BeSameAs(_messageEncoderSettings);
  303. }
  304. [Test]
  305. public void ReduceFunction_should_get_value()
  306. {
  307. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  308. var result = subject.ReduceFunction;
  309. result.Should().BeSameAs(_reduceFunction);
  310. }
  311. [Test]
  312. public void Scope_should_get_and_set_value()
  313. {
  314. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  315. var value = new BsonDocument("scope", 1);
  316. subject.Scope = value;
  317. var result = subject.Scope;
  318. result.Should().Be(value);
  319. }
  320. [Test]
  321. public void Sort_should_get_and_set_value()
  322. {
  323. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  324. var value = new BsonDocument("sort", 1);
  325. subject.Sort = value;
  326. var result = subject.Sort;
  327. result.Should().Be(value);
  328. }
  329. [Test]
  330. public void Verbose_should_get_and_set_value()
  331. {
  332. var subject = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  333. var value = true;
  334. subject.Verbose = value;
  335. var result = subject.Verbose;
  336. result.Should().Be(value);
  337. }
  338. // nested types
  339. private class FakeMapReduceOperation : MapReduceOperationBase
  340. {
  341. public FakeMapReduceOperation(
  342. CollectionNamespace collectionNamespace,
  343. BsonJavaScript mapFunction,
  344. BsonJavaScript reduceFunction,
  345. MessageEncoderSettings messageEncoderSettings
  346. )
  347. : base(collectionNamespace, mapFunction, reduceFunction, messageEncoderSettings)
  348. {
  349. }
  350. protected override BsonDocument CreateOutputOptions()
  351. {
  352. return new BsonDocument("fake", 1);
  353. }
  354. }
  355. }
  356. }