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);
        }
        public async Task ListDatabasesAsync_should_invoke_the_correct_operation()
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor);
            await client.ListDatabasesAsync();

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

            call.Operation.Should().BeOfType<ListDatabasesOperation>();
        }
        public async Task ListDatabaseNames()
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor);
            var names = await client.GetDatabaseNamesAsync();

            var call = operationExecutor.GetReadCall<IReadOnlyList<string>>();

            call.Operation.Should().BeOfType<ListDatabaseNamesOperation>();
        }
Example #4
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);
        }
        public async Task CountAsync_should_execute_the_CountOperation()
        {
            var model = new CountModel
            {
                Filter  = new BsonDocument("x", 1),
                Hint    = "funny",
                Limit   = 10,
                MaxTime = TimeSpan.FromSeconds(20),
                Skip    = 30
            };
            await _subject.CountAsync(model, Timeout.InfiniteTimeSpan, CancellationToken.None);

            var call = _operationExecutor.GetReadCall <long>();

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

            operation.CollectionNamespace.FullName.Should().Be("foo.bar");
            operation.Filter.Should().Be((BsonDocument)model.Filter);
            operation.Hint.Should().Be((string)model.Hint);
            operation.Limit.Should().Be(model.Limit);
            operation.MaxTime.Should().Be(model.MaxTime);
            operation.Skip.Should().Be(model.Skip);
        }
        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 void ListDatabases_should_invoke_the_correct_operation(
            [Values(false, true)] bool async)
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor, new MongoClientSettings());

            if (async)
            {
                client.ListDatabasesAsync().GetAwaiter().GetResult();
            }
            else
            {
                client.ListDatabases();
            }

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

            call.Operation.Should().BeOfType<ListDatabasesOperation>();
        }