示例#1
0
        public void Execute_should_return_expected_result_when_MaxTime_is_set(
            [Values(null, 1000)]
            int?milliseconds,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Aggregate, Feature.MaxTime);
            EnsureTestData();
            var maxTime = milliseconds == null ? (TimeSpan?)null : TimeSpan.FromMilliseconds(milliseconds.Value);
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().NotBeNull();
            result.Should().HaveCount(1);
        }
        public void Execute_should_return_expected_result_when_ReadConcern_is_set(
            [Values(null, ReadConcernLevel.Local)]
            ReadConcernLevel?level,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var readConcern = new ReadConcern(level);
            var subject     = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().NotBeNull();
            result.Should().HaveCount(1);
        }
示例#3
0
        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.Aggregate, Feature.Collation);
            EnsureTestData();
            var collation = new Collation("en_US", caseLevel: caseSensitive, strength: CollationStrength.Primary);
            var subject   = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                Collation = collation
            };

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().NotBeNull();
            result.Should().HaveCount(caseSensitive ? 1 : 2);
        }
        public void Constructor_with_database_should_create_a_valid_instance()
        {
            var subject = new AggregateOperation <BsonDocument>(_databaseNamespace, __pipeline, __resultSerializer, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeNull();
            subject.DatabaseNamespace.Should().BeSameAs(_databaseNamespace);
            subject.Pipeline.Should().Equal(__pipeline);
            subject.ResultSerializer.Should().BeSameAs(__resultSerializer);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.AllowDiskUse.Should().NotHaveValue();
            subject.BatchSize.Should().NotHaveValue();
            subject.Collation.Should().BeNull();
            subject.MaxAwaitTime.Should().NotHaveValue();
            subject.MaxTime.Should().NotHaveValue();
            subject.ReadConcern.IsServerDefault.Should().BeTrue();
#pragma warning disable 618
            subject.UseCursor.Should().NotHaveValue();
#pragma warning restore 618
            subject.RetryRequested.Should().BeFalse();
        }
        public void Execute_should_return_expected_result_when_BatchSize_is_set(
            [Values(null, 1, 10)]
            int?batchSize,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Aggregate);
            EnsureTestData();
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                BatchSize = batchSize
            };

            using (var cursor = ExecuteOperation(subject, async))
            {
                var result = ReadCursorToEnd(cursor, async);

                result.Should().NotBeNull();
                result.Should().HaveCount(1);
            }
        }
示例#6
0
        public void CreateCommand_should_return_the_expected_result_when_AllowDiskUse_is_set(
            [Values(null, false, true)]
            bool?allowDiskUse)
        {
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                AllowDiskUse = allowDiskUse
            };

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "allowDiskUse", () => allowDiskUse.Value, allowDiskUse != null },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_the_expected_result(
            [Values(false, true)]
            bool useServerVersionSupportingAggregateCursorResult)
        {
            var subject       = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings);
            var serverVersion = Feature.AggregateCursorResult.SupportedOrNotSupportedVersion(useServerVersionSupportingAggregateCursorResult);

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

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "cursor", () => new BsonDocument(), Feature.AggregateCursorResult.IsSupported(serverVersion) }
            };

            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_UseCursor_is_set(
            [Values(null, false, true)]
            bool?useCursor,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
#pragma warning disable 618
                UseCursor = useCursor
#pragma warning restore 618
            };

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().NotBeNull();
            result.Should().HaveCount(1);
        }
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings);

            Exception exception;

            if (async)
            {
                exception = Record.Exception(() => subject.ExecuteAsync(null, CancellationToken.None).GetAwaiter().GetResult());
            }
            else
            {
                exception = Record.Exception(() => 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_Let_is_set_with_match_expression(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.AggregateOptionsLet);
            EnsureTestData();
            var pipeline = new[] { BsonDocument.Parse("{ $match : { $expr : { $eq : [ '$x', '$$y'] } } }") };
            var subject  = new AggregateOperation <BsonDocument>(_collectionNamespace, pipeline, __resultSerializer, _messageEncoderSettings)
            {
                Let = new BsonDocument("y", "x")
            };

            var cursor = ExecuteOperation(subject, async);
            var result = ReadCursorToEnd(cursor, async);

            result.Should().BeEquivalentTo(new[]
            {
                new BsonDocument {
                    { "_id", 1 }, { "x", "x" }
                }
            });
        }
示例#11
0
        public void CreateCommand_should_return_the_expected_result_when_ReadConcern_is_set(
            [Values(null, ReadConcernLevel.Linearizable)]
            ReadConcernLevel?level)
        {
            var readConcern = new ReadConcern(level);
            var subject     = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "readConcern", () => readConcern.ToBsonDocument(), level != null },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
        }
示例#12
0
        public void CreateCommand_should_return_the_expected_result_when_MaxTime_is_set(
            [Values(null, 1)]
            int?milliSeconds)
        {
            var maxTime = milliSeconds == null ? (TimeSpan?)null : TimeSpan.FromMilliseconds(milliSeconds.Value);
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "maxTimeMS", () => maxTime.Value.TotalMilliseconds, maxTime != null },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
        }
示例#13
0
        public void CreateCommand_should_return_the_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 AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                Collation = collation
            };

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "collation", () => new BsonDocument("locale", locale), collation != null },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_MaxAwaitTime_is_set(
            [Values(null, 1000)]
            int?milliseconds,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.ChangeStreamStage);
            EnsureTestData();
            var maxAwaitTime = milliseconds == null ? (TimeSpan?)null : TimeSpan.FromMilliseconds(milliseconds.Value);
            var subject      = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                MaxAwaitTime = maxAwaitTime
            };

            var cursor = ExecuteOperation(subject, async);

            cursor.Should().BeOfType <AsyncCursor <BsonDocument> >();
            var cursorMaxTimeInfo = typeof(AsyncCursor <BsonDocument>).GetField("_maxTime", BindingFlags.NonPublic | BindingFlags.Instance);
            var cursorMaxTime     = (TimeSpan?)cursorMaxTimeInfo.GetValue(cursor);

            cursorMaxTime.Should().Be(maxAwaitTime);
        }
        public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(long maxTimeTicks, int expectedMaxTimeMS)
        {
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                MaxTime = TimeSpan.FromTicks(maxTimeTicks)
            };
            var connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.AggregateCursorResult.FirstSupportedVersion);
            var session = OperationTestHelper.CreateSession();

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "maxTimeMS", expectedMaxTimeMS },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
            result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
        }
        public void CreateCommand_should_create_the_correct_command(
            [Values("2.4.0", "2.6.0", "2.8.0")] string serverVersion,
            [Values(null, false, true)] bool?allowDiskUse,
            [Values(null, 10, 20)] int?batchSize,
            [Values(null, 2000)] int?maxTime,
            [Values(null, false, true)] bool?useCursor)
        {
            var semanticServerVersion = SemanticVersion.Parse(serverVersion);
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, Enumerable.Empty <BsonDocument>(), BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                AllowDiskUse = allowDiskUse,
                BatchSize    = batchSize,
                MaxTime      = maxTime.HasValue ? TimeSpan.FromMilliseconds(maxTime.Value) : (TimeSpan?)null,
                UseCursor    = useCursor
            };

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(subject.Pipeline) },
                { "allowDiskUse", () => allowDiskUse.Value, allowDiskUse.HasValue },
                { "maxTimeMS", () => maxTime.Value, maxTime.HasValue }
            };

            var defaultCursorValue = semanticServerVersion >= new SemanticVersion(2, 6, 0);

            if (useCursor.GetValueOrDefault(defaultCursorValue))
            {
                expectedResult["cursor"] = new BsonDocument
                {
                    { "batchSize", () => batchSize.Value, batchSize.HasValue }
                };
            }

            var result = subject.CreateCommand(semanticServerVersion);

            result.Should().Be(expectedResult);
        }
示例#17
0
        public void CreateCommand_should_return_the_expected_result_when_UseCursor_is_set(
            [Values(null, false, true)]
            bool?useCursor,
            [Values(false, true)]
            bool useServerVersionSupportingAggregateCursorResult)
        {
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                UseCursor = useCursor
            };
            var serverVersion = Feature.AggregateCursorResult.SupportedOrNotSupportedVersion(useServerVersionSupportingAggregateCursorResult);

            var result = subject.CreateCommand(serverVersion);

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "cursor", () => new BsonDocument(), useCursor.GetValueOrDefault(true) && Feature.AggregateCursorResult.IsSupported(serverVersion) }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_Comment_is_set(
            [Values(null, "test")]
            string comment)
        {
            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                Comment = comment,
            };

            var connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.AggregateComment.FirstSupportedVersion);
            var session = OperationTestHelper.CreateSession();

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

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(__pipeline) },
                { "comment", () => comment, comment != null },
                { "cursor", new BsonDocument() }
            };

            result.Should().Be(expectedResult);
        }
        // private methods
        private AggregateOperation <RawBsonDocument> CreateAggregateOperation()
        {
            var changeStreamStage = CreateChangeStreamStage();
            var combinedPipeline  = CreateCombinedPipeline(changeStreamStage);

            AggregateOperation <RawBsonDocument> operation;

            if (_collectionNamespace != null)
            {
                operation = new AggregateOperation <RawBsonDocument>(_collectionNamespace, combinedPipeline, RawBsonDocumentSerializer.Instance, _messageEncoderSettings);
            }
            else
            {
                var databaseNamespace = _databaseNamespace ?? DatabaseNamespace.Admin;
                operation = new AggregateOperation <RawBsonDocument>(databaseNamespace, combinedPipeline, RawBsonDocumentSerializer.Instance, _messageEncoderSettings);
            }

            operation.BatchSize    = _batchSize;
            operation.Collation    = _collation;
            operation.MaxAwaitTime = _maxAwaitTime;
            operation.ReadConcern  = _readConcern;

            return(operation);
        }
        public void Execute_should_return_expected_result_when_Comment_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();

            var subject = new AggregateOperation <BsonDocument>(_collectionNamespace, __pipeline, __resultSerializer, _messageEncoderSettings)
            {
                Comment = "test"
            };

            using (var profile = Profile(_collectionNamespace.DatabaseNamespace))
            {
                var cursor = ExecuteOperation(subject, async);
                var result = ReadCursorToEnd(cursor, async);

                result.Should().NotBeNull();

                var profileEntries = profile.Find(new BsonDocument("command.aggregate", new BsonDocument("$exists", true)));
                profileEntries.Should().HaveCount(1);
                profileEntries[0]["command"]["comment"].AsString.Should().Be(subject.Comment);
            }
        }