public void CreateCommand_should_return_expected_result_when_WriteConcern_is_set(
            [Values(null, 1, 2)]
            int?w)
        {
            var writeConcern = w.HasValue ? new WriteConcern(w.Value) : null;

#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                WriteConcern = writeConcern
            };
            var connectionDescription = OperationTestHelper.CreateConnectionDescription();
            var session = OperationTestHelper.CreateSession();

            var result = subject.CreateCommand(session, connectionDescription);

            var expectedResult = new BsonDocument
            {
                { "mapReduce", _collectionNamespace.CollectionName },
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", new BsonDocument {
                      { "replace", _outputCollectionNamespace.CollectionName }, { "db", _databaseNamespace.DatabaseName }
                  } },
                { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null }
            };
            result.Should().Be(expectedResult);
        }
Example #2
0
        public async Task ExecuteAsync_should_return_expected_result()
        {
            await EnsureTestDataAsync();

            var mapFunction       = "function() { emit(this.x, this.v); }";
            var reduceFunction    = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject           = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, mapFunction, reduceFunction, _messageEncoderSettings);
            var expectedDocuments = new BsonDocument[]
            {
                new BsonDocument {
                    { "_id", 1 }, { "value", 3 }
                },
                new BsonDocument {
                    { "_id", 2 }, { "value", 4 }
                },
            };

            var response = await ExecuteOperationAsync(subject);

            response["ok"].ToBoolean().Should().BeTrue();

            var documents = await ReadAllFromCollectionAsync(_outputCollectionNamespace);

            documents.Should().BeEquivalentTo(expectedDocuments);
        }
Example #3
0
        public void CreateCommand_should_return_expected_result_when_BypassDocumentValidation_is_set(
            [Values(null, false, true)]
            bool?bypassDocumentValidation,
            [Values(false, true)]
            bool useServerVersionSupportingBypassDocumentValidation)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
            {
                BypassDocumentValidation = bypassDocumentValidation
            };
            var subjectReflector = new Reflector(subject);
            var serverVersion    = Feature.BypassDocumentValidation.SupportedOrNotSupportedVersion(useServerVersionSupportingBypassDocumentValidation);

            var connectionDescription = OperationTestHelper.CreateConnectionDescription(serverVersion);
            var session = OperationTestHelper.CreateSession();

            var result = subjectReflector.CreateCommand(connectionDescription, session);

            var expectedResult = new BsonDocument
            {
                { "mapreduce", _collectionNamespace.CollectionName },
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", new BsonDocument {
                      { "replace", _outputCollectionNamespace.CollectionName }, { "db", _databaseNamespace.DatabaseName }
                  } },
                { "bypassDocumentValidation", () => bypassDocumentValidation.Value, bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) }
            };

            result.Should().Be(expectedResult);
        }
        public void constructor_should_initialize_instance()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
#pragma warning restore CS0618 // Type or member is obsolete

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.OutputCollectionNamespace.Should().BeSameAs(_outputCollectionNamespace);
            subject.MapFunction.Should().BeSameAs(_mapFunction);
            subject.ReduceFunction.Should().BeSameAs(_reduceFunction);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.BypassDocumentValidation.Should().NotHaveValue();
            subject.Collation.Should().BeNull();
            subject.Filter.Should().BeNull();
            subject.FinalizeFunction.Should().BeNull();
#pragma warning disable 618
            subject.JavaScriptMode.Should().NotHaveValue();
#pragma warning restore 618
            subject.Limit.Should().NotHaveValue();
            subject.MaxTime.Should().NotHaveValue();
#pragma warning disable 618
            subject.NonAtomicOutput.Should().NotHaveValue();
            subject.OutputMode.Should().Be(MapReduceOutputMode.Replace);
#pragma warning restore 618
            subject.Scope.Should().BeNull();
            subject.Sort.Should().BeNull();
            subject.Verbose.Should().NotHaveValue();
        }
        public void Execute_should_return_expected_results_when_Limit_is_set(
            [Values(1, 2)]
            long limit,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                Limit = limit
            };

            ExecuteOperation(subject, async);

            var expectedResults = new[]
            {
                new BsonDocument {
                    { "_id", 1 }, { "value", limit == 1 ? 1 : 3 }
                }
            };
            ReadAllFromCollection(_outputCollectionNamespace).Should().Equal(expectedResults);
        }
Example #6
0
        public void CreateCommand_should_return_expected_result_when_WriteConcern_is_set(
            [Values(null, 1, 2)]
            int?w,
            [Values(false, true)]
            bool isWriteConcernSupported)
        {
            var writeConcern = w.HasValue ? new WriteConcern(w.Value) : null;
            var subject      = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
            {
                WriteConcern = writeConcern
            };
            var subjectReflector = new Reflector(subject);
            var serverVersion    = Feature.CommandsThatWriteAcceptWriteConcern.SupportedOrNotSupportedVersion(isWriteConcernSupported);

            var connectionDescription = OperationTestHelper.CreateConnectionDescription(serverVersion);
            var session = OperationTestHelper.CreateSession();

            var result = subjectReflector.CreateCommand(connectionDescription, session);

            var expectedResult = new BsonDocument
            {
                { "mapreduce", _collectionNamespace.CollectionName },
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", new BsonDocument {
                      { "replace", _outputCollectionNamespace.CollectionName }, { "db", _databaseNamespace.DatabaseName }
                  } },
                { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null && isWriteConcernSupported }
            };

            result.Should().Be(expectedResult);
        }
Example #7
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            EnsureTestData();

            var mapFunction    = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject        = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, mapFunction, reduceFunction, _messageEncoderSettings)
            {
                BypassDocumentValidation = true
            };
            var expectedDocuments = new BsonDocument[]
            {
                new BsonDocument {
                    { "_id", 1 }, { "value", 3 }
                },
                new BsonDocument {
                    { "_id", 2 }, { "value", 4 }
                },
            };

            var response = ExecuteOperation(subject, async);

            response["ok"].ToBoolean().Should().BeTrue();

            var documents = ReadAllFromCollection(_outputCollectionNamespace, async);

            documents.Should().BeEquivalentTo <BsonDocument>(expectedDocuments);
        }
        public void CreateCommand_should_return_expected_result_when_BypassDocumentValidation_is_set(
            [Values(null, false, true)]
            bool?bypassDocumentValidation)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                BypassDocumentValidation = bypassDocumentValidation
            };
            var connectionDescription = OperationTestHelper.CreateConnectionDescription();
            var session = OperationTestHelper.CreateSession();

            var result = subject.CreateCommand(session, connectionDescription);

            var expectedResult = new BsonDocument
            {
                { "mapReduce", _collectionNamespace.CollectionName },
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", new BsonDocument {
                      { "replace", _outputCollectionNamespace.CollectionName }, { "db", _databaseNamespace.DatabaseName }
                  } },
                { "bypassDocumentValidation", () => bypassDocumentValidation.Value, bypassDocumentValidation.HasValue }
            };
            result.Should().Be(expectedResult);
        }
Example #9
0
        public void Execute_should_return_expected_results_when_Sort_is_set(
            [Values(1, -1)]
            int direction,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();
            var sort    = new BsonDocument("_id", direction);
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
            {
                Limit = 2,
                Sort  = sort
            };

            ExecuteOperation(subject, async);

            BsonDocument[] expectedResults;
            if (direction == 1)
            {
                expectedResults = new[]
                {
                    BsonDocument.Parse("{ _id : 1, value : 3 }")
                };
            }
            else
            {
                expectedResults = new[]
                {
                    BsonDocument.Parse("{ _id : 1, value : 2 }"),
                    BsonDocument.Parse("{ _id : 2, value : 4 }")
                };
            }
            ReadAllFromCollection(_outputCollectionNamespace).Should().Equal(expectedResults);
        }
Example #10
0
        public void ExecuteAsync_should_throw_when_binding_is_null()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            Action action = () => subject.ExecuteAsync(null, CancellationToken.None);

            action.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("binding");
        }
Example #11
0
        public void OutputCollectionNamespace_should_get__value()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            var result = subject.OutputCollectionNamespace;

            result.Should().BeSameAs(_outputCollectionNamespace);
        }
Example #12
0
        public void Execute_should_send_session_id_when_supported(
            [Values(false, true)] bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            VerifySessionIdWasSentWhenSupported(subject, "mapreduce", async);
        }
Example #13
0
        public void BypassDocumentValidation_should_get_and_set_value()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = true;

            subject.BypassDocumentValidation = value;
            var result = subject.BypassDocumentValidation;

            result.Should().Be(value);
        }
Example #14
0
        public void OutputMode_should_get_and_set_value()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = MapReduceOutputMode.Merge;

            subject.OutputMode = value;
            var result = subject.OutputMode;

            result.Should().Be(value);
        }
Example #15
0
        public void NonAtomicOutput_should_get_and_set_value()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = true;

            subject.NonAtomicOutput = value;
            var result = subject.NonAtomicOutput;

            result.Should().Be(value);
        }
Example #16
0
        public void Filter_should_get_and_set_value()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = new BsonDocument("_id", 1);

            subject.Filter = value;
            var result = subject.Filter;

            result.Should().Be(value);
        }
Example #17
0
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            Action action = () => ExecuteOperation(subject, null, async);

            action.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("binding");
        }
Example #18
0
        public void BypassDocumentValidation_get_and_set_should_work(
            [Values(null, false, true)]
            bool?value)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            subject.BypassDocumentValidation = value;
            var result = subject.BypassDocumentValidation;

            result.Should().Be(value);
        }
Example #19
0
        public void OutputCollectionNamespace_get_and_set_should_work(
            [Values("a", "b")]
            string collectionName)
        {
            var outputCollectionNamespace = new CollectionNamespace(_databaseNamespace, collectionName);
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            var result = subject.OutputCollectionNamespace;

            result.Should().BeSameAs(outputCollectionNamespace);
        }
Example #20
0
        public void Filter_should_get_and_set_value(
            [Values(null, "{ x : 1 }", "{ x : 2 }")]
            string valueString)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = valueString == null ? null : BsonDocument.Parse(valueString);

            subject.Filter = value;
            var result = subject.Filter;

            result.Should().Be(value);
        }
Example #21
0
        public void constructor_should_initialize_instance()
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.OutputCollectionNamespace.Should().BeSameAs(_outputCollectionNamespace);
            subject.MapFunction.Should().BeSameAs(_mapFunction);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
            subject.Filter.Should().BeNull();
            subject.ReduceFunction.Should().BeSameAs(_reduceFunction);
            subject.OutputMode.Should().Be(MapReduceOutputMode.Replace);
        }
Example #22
0
        public void WriteConcern_get_and_set_should_work(
            [Values(null, 1, 2)]
            int?w)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
            var value   = w.HasValue ? new WriteConcern(w.Value) : null;

            subject.WriteConcern = value;
            var result = subject.WriteConcern;

            result.Should().BeSameAs(value);
        }
        public void ShardedOutput_get_and_set_should_work(
            [Values(null, false, true)]
            bool?value)
        {
#pragma warning disable 618
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            subject.ShardedOutput = value;
            var result = subject.ShardedOutput;
#pragma warning restore 618

            result.Should().Be(value);
        }
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
#pragma warning restore CS0618 // Type or member is obsolete

            var exception = Record.Exception(() => ExecuteOperation(subject, null, async));

            var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject;
            argumentNullException.ParamName.Should().Be("binding");
        }
        public void BypassDocumentValidation_get_and_set_should_work(
            [Values(null, false, true)]
            bool?value)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
#pragma warning restore CS0618 // Type or member is obsolete

            subject.BypassDocumentValidation = value;
            var result = subject.BypassDocumentValidation;

            result.Should().Be(value);
        }
        public void OutputCollectionNamespace_get_and_set_should_work(
            [Values("a", "b")]
            string collectionName)
        {
            var outputCollectionNamespace = new CollectionNamespace(_databaseNamespace, collectionName);

#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
#pragma warning restore CS0618 // Type or member is obsolete

            var result = subject.OutputCollectionNamespace;

            result.Should().BeSameAs(outputCollectionNamespace);
        }
        public void OutputMode_get_and_set_should_work(
#pragma warning disable CS0618 // Type or member is obsolete
            [Values(MapReduceOutputMode.Merge, MapReduceOutputMode.Reduce)]
            MapReduceOutputMode value)
        {
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            subject.OutputMode = value;
            var result = subject.OutputMode;

#pragma warning restore CS0618 // Type or member is obsolete

            result.Should().Be(value);
        }
Example #28
0
        public void Execute_should_throw_when_a_write_concern_error_occurs(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.CommandsThatWriteAcceptWriteConcern).ClusterType(ClusterType.ReplicaSet);
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
            {
                WriteConcern = new WriteConcern(9)
            };

            var exception = Record.Exception(() => ExecuteOperation(subject, async));

            exception.Should().BeOfType <MongoWriteConcernException>();
        }
Example #29
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);

            ExecuteOperation(subject, async);

            ReadAllFromCollection(_outputCollectionNamespace).Should().Equal(
                BsonDocument.Parse("{ _id : 1, value : 3 }"),
                BsonDocument.Parse("{ _id : 2, value : 4 }"));
        }
        public void WriteConcern_get_and_set_should_work(
            [Values(null, 1, 2)]
            int?w)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
#pragma warning restore CS0618 // Type or member is obsolete
            var value = w.HasValue ? new WriteConcern(w.Value) : null;

            subject.WriteConcern = value;
            var result = subject.WriteConcern;

            result.Should().BeSameAs(value);
        }