PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 248 lines | 183 code | 43 blank | 22 comment | 0 complexity | 19e02b35acd8ff1d95cfbf71536c8d0b 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 System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using MongoDB.Bson;
  21. using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
  22. using FluentAssertions;
  23. using NSubstitute;
  24. using NUnit.Framework;
  25. using MongoDB.Bson.Serialization.Serializers;
  26. using MongoDB.Bson.Serialization;
  27. using System.Reflection;
  28. using System.Threading;
  29. namespace MongoDB.Driver.Core.Operations
  30. {
  31. [TestFixture]
  32. public class MapReduceOutputToCollectionOperationTests : OperationTestBase
  33. {
  34. // fields
  35. private readonly BsonJavaScript _mapFunction = "map";
  36. private CollectionNamespace _outputCollectionNamespace;
  37. private readonly BsonJavaScript _reduceFunction = "reduce";
  38. // setup methods
  39. public override void TestFixtureSetUp()
  40. {
  41. base.TestFixtureSetUp();
  42. _outputCollectionNamespace = new CollectionNamespace(_databaseNamespace, _collectionNamespace + "Output");
  43. }
  44. // test methods
  45. [Test]
  46. public void constructor_should_initialize_instance()
  47. {
  48. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  49. subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
  50. subject.OutputCollectionNamespace.Should().BeSameAs(_outputCollectionNamespace);
  51. subject.MapFunction.Should().BeSameAs(_mapFunction);
  52. subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
  53. subject.Filter.Should().BeNull();
  54. subject.ReduceFunction.Should().BeSameAs(_reduceFunction);
  55. subject.OutputMode.Should().Be(MapReduceOutputMode.Replace);
  56. }
  57. [Test]
  58. public void constructor_should_throw_when_outputCollectionNamespace_is_null()
  59. {
  60. Action action = () => new MapReduceOutputToCollectionOperation(_collectionNamespace, null, _mapFunction, _reduceFunction, _messageEncoderSettings);
  61. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("outputCollectionNamespace");
  62. }
  63. [Test]
  64. public void CreateOutputOptions_should_return_expected_result()
  65. {
  66. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  67. var subjectReflector = new Reflector(subject);
  68. var expectedResult = new BsonDocument
  69. {
  70. { "replace", _outputCollectionNamespace.CollectionName },
  71. { "db", _outputCollectionNamespace.DatabaseNamespace.DatabaseName }
  72. };
  73. var result = subjectReflector.CreateOutputOptions();
  74. result.Should().Be(expectedResult);
  75. }
  76. [Test]
  77. public void CreateOutputOptions_should_return_expected_result_when_ShardedOutput_is_provided()
  78. {
  79. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  80. {
  81. ShardedOutput = true
  82. };
  83. var subjectReflector = new Reflector(subject);
  84. var expectedResult = new BsonDocument
  85. {
  86. { "replace", _outputCollectionNamespace.CollectionName },
  87. { "db", _outputCollectionNamespace.DatabaseNamespace.DatabaseName },
  88. { "sharded", true }
  89. };
  90. var result = subjectReflector.CreateOutputOptions();
  91. result.Should().Be(expectedResult);
  92. }
  93. [Test]
  94. public void CreateOutputOptions_should_return_expected_result_when_NonAtomicOutput_is_provided()
  95. {
  96. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
  97. {
  98. NonAtomicOutput = true
  99. };
  100. var subjectReflector = new Reflector(subject);
  101. var expectedResult = new BsonDocument
  102. {
  103. { "replace", _outputCollectionNamespace.CollectionName },
  104. { "db", _outputCollectionNamespace.DatabaseNamespace.DatabaseName },
  105. { "nonAtomic", true }
  106. };
  107. var result = subjectReflector.CreateOutputOptions();
  108. result.Should().Be(expectedResult);
  109. }
  110. [Test]
  111. [RequiresServer(ClusterTypes = ClusterTypes.StandaloneOrReplicaSet)]
  112. public async Task ExecuteAsync_should_return_expected_result()
  113. {
  114. await EnsureTestDataAsync();
  115. var mapFunction = "function() { emit(this.x, this.v); }";
  116. var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
  117. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, mapFunction, reduceFunction, _messageEncoderSettings);
  118. var expectedDocuments = new BsonDocument[]
  119. {
  120. new BsonDocument { {"_id", 1 }, { "value", 3 } },
  121. new BsonDocument { {"_id", 2 }, { "value", 4 } },
  122. };
  123. var response = await ExecuteOperationAsync(subject);
  124. response["ok"].ToBoolean().Should().BeTrue();
  125. var documents = await ReadAllFromCollectionAsync(_outputCollectionNamespace);
  126. documents.Should().BeEquivalentTo(expectedDocuments);
  127. }
  128. [Test]
  129. public void ExecuteAsync_should_throw_when_binding_is_null()
  130. {
  131. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  132. Action action = () => subject.ExecuteAsync(null, CancellationToken.None);
  133. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
  134. }
  135. [Test]
  136. public void Filter_should_get_and_set_value()
  137. {
  138. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  139. var value = new BsonDocument("_id", 1);
  140. subject.Filter = value;
  141. var result = subject.Filter;
  142. result.Should().Be(value);
  143. }
  144. [Test]
  145. public void NonAtomicOutput_should_get_and_set_value()
  146. {
  147. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  148. var value = true;
  149. subject.NonAtomicOutput = value;
  150. var result = subject.NonAtomicOutput;
  151. result.Should().Be(value);
  152. }
  153. [Test]
  154. public void OutputCollectionNamespace_should_get__value()
  155. {
  156. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  157. var result = subject.OutputCollectionNamespace;
  158. result.Should().BeSameAs(_outputCollectionNamespace);
  159. }
  160. [Test]
  161. public void OutputMode_should_get_and_set_value()
  162. {
  163. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  164. var value = MapReduceOutputMode.Merge;
  165. subject.OutputMode = value;
  166. var result = subject.OutputMode;
  167. result.Should().Be(value);
  168. }
  169. [Test]
  170. public void ShardedOutput_should_get_and_set_value()
  171. {
  172. var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
  173. var value = true;
  174. subject.ShardedOutput = value;
  175. var result = subject.ShardedOutput;
  176. result.Should().Be(value);
  177. }
  178. // helper methods
  179. private async Task EnsureTestDataAsync()
  180. {
  181. await DropCollectionAsync();
  182. await InsertAsync(
  183. new BsonDocument { { "_id", 1 }, { "x", 1 }, { "v", 1 } },
  184. new BsonDocument { { "_id", 2 }, { "x", 1 }, { "v", 2 } },
  185. new BsonDocument { { "_id", 3 }, { "x", 2 }, { "v", 4 } });
  186. }
  187. // nested types
  188. private class Reflector
  189. {
  190. // fields
  191. private readonly MapReduceOutputToCollectionOperation _instance;
  192. // constructor
  193. public Reflector(MapReduceOutputToCollectionOperation instance)
  194. {
  195. _instance = instance;
  196. }
  197. // methods
  198. public BsonDocument CreateOutputOptions()
  199. {
  200. var method = typeof(MapReduceOutputToCollectionOperation).GetMethod("CreateOutputOptions", BindingFlags.NonPublic | BindingFlags.Instance);
  201. return (BsonDocument)method.Invoke(_instance, new object[0]);
  202. }
  203. }
  204. }
  205. }