public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(
            [Values(null, 1, 2)]
            int?seconds)
        {
            var maxTime = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "key", _key },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial }
                  } },
                { "maxTimeMS", () => seconds.Value * 1000, seconds.HasValue }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_Collation_is_set(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var collation = locale == null ? null : new Collation(locale);
            var subject   = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                Collation = collation
            };

            var result = subject.CreateCommand(Feature.Collation.FirstSupportedVersion);

            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "key", _key },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial },
                      { "collation", () => collation.ToBsonDocument(), collation != null }
                  } }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_finalizeFunction_is_set(
            [Values(false, true)]
            bool isFinalizeFunctionNull)
        {
            var finalizeFunction = isFinalizeFunctionNull ? null : _finalizeFunction;
            var subject          = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                FinalizeFunction = finalizeFunction
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "key", _key },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial },
                      { "finalize", finalizeFunction, finalizeFunction != null }
                  } }
            };

            result.Should().Be(expectedResult);
        }
Пример #4
0
        public void CreateCommand_should_return_expected_result_when_maxTime_was_provided()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var messageEncoderSettings = new MessageEncoderSettings();
            var subject                = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, null, messageEncoderSettings);

            subject.MaxTime = TimeSpan.FromSeconds(1);
            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", collectionNamespace.CollectionName },
                      { "key", key },
                      { "$reduce", reduceFunction },
                      { "initial", initial }
                  } },
                { "maxTimeMS", 1000 }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
        public void Execute_should_send_session_id_when_supported(
            [Values(false, true)] bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings);

            VerifySessionIdWasSentWhenSupported(subject, "group", async);
        }
Пример #6
0
 // constructors
 public Builder(GroupOperation other)
 {
     _collectionName   = other.CollectionName;
     _databaseName     = other.DatabaseName;
     _finalizeFunction = other.FinalizeFunction;
     _initial          = other.Initial;
     _key            = other.Key;
     _keyFunction    = other.KeyFunction;
     _query          = other.Query;
     _reduceFunction = other.ReduceFunction;
 }
        public void MaxTime_get_and_set_should_work(
            [Values(-10000, 0, 1, 10000, 99999)] long maxTimeTicks)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = TimeSpan.FromTicks(maxTimeTicks);

            subject.MaxTime = value;
            var result = subject.MaxTime;

            result.Should().Be(value);
        }
        public void MaxTime_set_should_throw_when_value_is_invalid(
            [Values(-10001, -9999, -1)] long maxTimeTicks)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = TimeSpan.FromTicks(maxTimeTicks);

            var exception = Record.Exception(() => subject.MaxTime = value);

            var e = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject;

            e.ParamName.Should().Be("value");
        }
        public void FinalizeFunction_get_and_set_should_work(
            [Values(null, "x", "y")]
            string code)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = code == null ? null : new BsonJavaScript(code);

            subject.FinalizeFunction = value;
            var result = subject.FinalizeFunction;

            result.Should().BeSameAs(value);
        }
        public void Collation_get_and_set_should_work(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = locale == null ? null : new Collation(locale);

            subject.Collation = value;
            var result = subject.Collation;

            result.Should().BeSameAs(value);
        }
Пример #11
0
        public void MaxTime_get_and_set_should_work(
            [Values(null, 1, 2)]
            int?seconds)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;

            subject.MaxTime = value;
            var result = subject.MaxTime;

            result.Should().Be(value);
        }
        public void ResultSerializer_get_and_set_should_work(
            [Values(false, true)]
            bool isNull)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);
            var value   = isNull ? null : new BsonDocumentSerializer();

            subject.ResultSerializer = value;
            var result = subject.ResultSerializer;

            result.Should().Be(value);
        }
Пример #13
0
        public void MaxTime_should_work()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var criteria               = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();
            var subject                = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, criteria, messageEncoderSettings);
            var maxTime                = TimeSpan.FromSeconds(1);

            subject.MaxTime = maxTime;

            subject.MaxTime.Should().Be(maxTime);
        }
Пример #14
0
        public void FinalizeFunction_should_work()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var criteria               = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();
            var subject                = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, criteria, messageEncoderSettings);
            var finalizeFunction       = new BsonJavaScript("finalizeFunction");

            subject.FinalizeFunction = finalizeFunction;

            subject.FinalizeFunction.Should().Be(finalizeFunction);
        }
        public void Execute_should_throw_when_maxTime_is_exceeded(
            [Values(false, true)] bool async)
        {
            RequireServer.Check().Supports(Feature.FailPoints).ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings);

            subject.MaxTime = TimeSpan.FromSeconds(9001);

            using (var failPoint = FailPoint.ConfigureAlwaysOn(CoreTestConfiguration.Cluster, _session, FailPointName.MaxTimeAlwaysTimeout))
            {
                var exception = Record.Exception(() => ExecuteOperation(subject, failPoint.Binding, async));

                exception.Should().BeOfType <MongoExecutionTimeoutException>();
            }
        }
        public void Execute_should_return_expected_result_when_keyFunction_is_used(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _keyFunction, _initial, _reduceFunction, null, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result.Should().Equal(
                BsonDocument.Parse("{ x : 1, count : 2 }"),
                BsonDocument.Parse("{ x : 2, count : 1 }"),
                BsonDocument.Parse("{ x : 3, count : 3 }"));
        }
        public void Execute_should_throw_when_Collation_is_set_but_not_supported(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().DoesNotSupport(Feature.Collation);
            EnsureTestData();
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                Collation = new Collation("en_US")
            };

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

            exception.Should().BeOfType <NotSupportedException>();
        }
Пример #18
0
        public void ResultSerializer_should_work()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var criteria               = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();
            var subject                = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, criteria, messageEncoderSettings);
            var resultSerializer       = Substitute.For <IBsonSerializer <BsonDocument> >();

            subject.ResultSerializer = resultSerializer;

            subject.ResultSerializer.Should().BeSameAs(resultSerializer);
        }
        public void Execute_should_return_expected_result_when_ResultSerializer_is_used(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var resultSerializer = new ElementDeserializer <int>("x", new Int32Serializer());
            var subject          = new GroupOperation <int>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                ResultSerializer = resultSerializer
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Equal(1, 2, 3);
        }
        public void constructor_with_keyFunction_should_initialize_subject()
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _keyFunction, _initial, _reduceFunction, _filter, _messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(_collectionNamespace);
            subject.KeyFunction.Should().Be(_keyFunction);
            subject.Initial.Should().Be(_initial);
            subject.ReduceFunction.Should().Be(_reduceFunction);
            subject.Filter.Should().Be(_filter);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.Collation.Should().BeNull();
            subject.FinalizeFunction.Should().BeNull();
            subject.Key.Should().BeNull();
            subject.MaxTime.Should().Be(default(TimeSpan?));
            subject.ResultSerializer.Should().BeNull();
        }
        public void Execute_should_return_expected_result_when_FinalizeFunction_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.GroupCommand);
            EnsureTestData();
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                FinalizeFunction = _finalizeFunction
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Equal(
                BsonDocument.Parse("{ x : 1, count : -2 }"),
                BsonDocument.Parse("{ x : 2, count : -1 }"),
                BsonDocument.Parse("{ x : 3, count : -3 }"));
        }
Пример #22
0
        public void constructor_with_key_should_initialize_subject()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var criteria               = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            var subject = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, criteria, messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(collectionNamespace);
            subject.Criteria.Should().Be(criteria);
            subject.FinalizeFunction.Should().BeNull();
            subject.Initial.Should().Be(initial);
            subject.Key.Should().Be(key);
            subject.KeyFunction.Should().BeNull();
            subject.MaxTime.Should().Be(default(TimeSpan?));
            Assert.That(subject.MessageEncoderSettings, Is.EqualTo(messageEncoderSettings));
            subject.ReduceFunction.Should().Be(reduceFunction);
            subject.ResultSerializer.Should().BeNull();
        }
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, _filter, _messageEncoderSettings);

            var exception = Record.Exception(() =>
            {
                if (async)
                {
                    subject.ExecuteAsync(null, CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    subject.Execute(null, CancellationToken.None);
                }
            });

            var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject;

            argumentNullException.ParamName.Should().Be("binding");
        }
        public void Execute_should_return_expected_result_when_MaxTime_is_used(
            [Values(null, 1000)]
            int?seconds,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var maxTime = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            // TODO: force a timeout on the server? for now we're just smoke testing
            var result = ExecuteOperation(subject, async);

            result.Should().Equal(
                BsonDocument.Parse("{ x : 1, count : 2 }"),
                BsonDocument.Parse("{ x : 2, count : 1 }"),
                BsonDocument.Parse("{ x : 3, count : 3 }"));
        }
        public void CreateCommand_should_return_expected_result_when_keyFunction_is_used(
            [Values(false, true)]
            bool isFilterNull)
        {
            var filter  = isFilterNull ? _filter : null;
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _keyFunction, _initial, _reduceFunction, filter, _messageEncoderSettings);

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "$keyf", _keyFunction },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial },
                      { "cond", filter, filter != null }
                  } }
            };

            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_Collation_is_set(
            [Values(false, true)]
            bool caseSensitive,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Collation);
            EnsureTestData();
            var collation = new Collation("en_US", caseLevel: caseSensitive, strength: CollationStrength.Primary);
            var filter    = BsonDocument.Parse("{ y : 'a' }");
            var subject   = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, filter, _messageEncoderSettings)
            {
                Collation = collation
            };

            var result = ExecuteOperation(subject, async);

            BsonDocument[] expectedResult;
            if (caseSensitive)
            {
                expectedResult = new[]
                {
                    BsonDocument.Parse("{ x : 1, count : 2 }"),
                    BsonDocument.Parse("{ x : 3, count : 2 }")
                };
            }
            else
            {
                expectedResult = new[]
                {
                    BsonDocument.Parse("{ x : 1, count : 2 }"),
                    BsonDocument.Parse("{ x : 2, count : 1 }"),
                    BsonDocument.Parse("{ x : 3, count : 3 }")
                };
            }
            result.Should().Equal(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(long maxTimeTicks, int expectedMaxTimeMS)
        {
            var subject = new GroupOperation <BsonDocument>(_collectionNamespace, _key, _initial, _reduceFunction, null, _messageEncoderSettings)
            {
                MaxTime = TimeSpan.FromTicks(maxTimeTicks)
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "key", _key },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial }
                  } },
                { "maxTimeMS", expectedMaxTimeMS }
            };

            result.Should().Be(expectedResult);
            result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
        }