public async Task AggregateAsync_should_execute_the_AggregateOperation_when_out_is_not_specified()
        {
            var pipeline = new object[] { BsonDocument.Parse("{$match: {x: 2}}") };
            var model    = new AggregateModel <BsonDocument>(pipeline)
            {
                AllowDiskUse = true,
                BatchSize    = 10,
                MaxTime      = TimeSpan.FromSeconds(3),
                UseCursor    = false
            };

            var fakeCursor = NSubstitute.Substitute.For <IAsyncCursor <BsonDocument> >();

            _operationExecutor.EnqueueResult(fakeCursor);

            var result = await _subject.AggregateAsync(model, Timeout.InfiniteTimeSpan, CancellationToken.None);

            // we haven't executed the operation yet.
            _operationExecutor.QueuedCallCount.Should().Be(0);

            // this causes execution of the operation
            await result.GetAsyncEnumerator().MoveNextAsync();

            var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >();

            call.Operation.Should().BeOfType <AggregateOperation <BsonDocument> >();
            var operation = (AggregateOperation <BsonDocument>)call.Operation;

            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.AllowDiskUse.Should().Be(model.AllowDiskUse);
            operation.BatchSize.Should().Be(model.BatchSize);
            operation.MaxTime.Should().Be(model.MaxTime);
            operation.UseCursor.Should().Be(model.UseCursor);

            operation.Pipeline.Should().ContainInOrder(pipeline);
        }
        public async Task Aggregate_should_execute_the_AggregateOperation_when_out_is_not_specified()
        {
            var pipeline = new object[] { BsonDocument.Parse("{$match: {x: 2}}") };

            var fluent = _subject.Aggregate(new AggregateOptions
            {
                AllowDiskUse = true,
                BatchSize    = 10,
                MaxTime      = TimeSpan.FromSeconds(3),
                UseCursor    = false
            })
                         .Match("{x: 2}");

            var options = fluent.Options;

            var fakeCursor = NSubstitute.Substitute.For <IAsyncCursor <BsonDocument> >();

            _operationExecutor.EnqueueResult(fakeCursor);

            await fluent.ToCursorAsync(CancellationToken.None);

            var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >();

            call.Operation.Should().BeOfType <AggregateOperation <BsonDocument> >();
            var operation = (AggregateOperation <BsonDocument>)call.Operation;

            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.AllowDiskUse.Should().Be(options.AllowDiskUse);
            operation.BatchSize.Should().Be(options.BatchSize);
            operation.MaxTime.Should().Be(options.MaxTime);
            operation.UseCursor.Should().Be(options.UseCursor);

            operation.Pipeline.Should().ContainInOrder(pipeline);
        }
Esempio n. 3
0
        public async Task Aggregate_should_execute_the_AggregateOperation_when_out_is_not_specified()
        {
            var pipeline = new object[]
            {
                BsonDocument.Parse("{ $match: { x: 2 } }"),
                BsonDocument.Parse("{ $project : { Age : \"$age\", Name : { $concat : [\"$firstName\", \" \", \"$lastName\"] }, _id : 0 } }"),
                BsonDocument.Parse("{ $group : { _id : \"$Age\", Name : { \"$first\" : \"$Name\" } } }"),
                BsonDocument.Parse("{ $project : { _id: 1 } }")
            };

            var fluent = _subject.Aggregate(new AggregateOptions
            {
                AllowDiskUse = true,
                BatchSize    = 10,
                MaxTime      = TimeSpan.FromSeconds(3),
                UseCursor    = false
            })
                         .Match("{x: 2}")
                         .Project(x => new { Age = x["age"], Name = (string)x["firstName"] + " " + (string)x["lastName"] })
                         .Group(x => x.Age, g => new { _id = g.Key, Name = g.First().Name })
                         .Project("{ _id: 1 }");

            var options = fluent.Options;

            var fakeCursor = NSubstitute.Substitute.For <IAsyncCursor <BsonDocument> >();

            _operationExecutor.EnqueueResult(fakeCursor);

            await fluent.ToCursorAsync(CancellationToken.None);

            var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >();

            call.Operation.Should().BeOfType <AggregateOperation <BsonDocument> >();
            var operation = (AggregateOperation <BsonDocument>)call.Operation;

            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.AllowDiskUse.Should().Be(options.AllowDiskUse);
            operation.BatchSize.Should().Be(options.BatchSize);
            operation.MaxTime.Should().Be(options.MaxTime);
            operation.UseCursor.Should().Be(options.UseCursor);

            operation.Pipeline.Should().Equal(pipeline);
        }