public void CreateCommand_should_create_the_correct_command()
        {
            var filter = new BsonDocument("x", 1);
            var hint = "funny";
            var limit = 10;
            var skip = 30;
            var maxTime = TimeSpan.FromSeconds(20);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter = filter,
                Hint = hint,
                Limit = limit,
                MaxTime = maxTime,
                Skip = skip
            };
            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "query", filter },
                { "limit", limit },
                { "skip", skip },
                { "hint", hint },
                { "maxTimeMS", maxTime.TotalMilliseconds }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result.Should().Be(5);
        }
        public void Filter_get_and_set_should_work(
            [Values(null, "{ x : 1 }")]
            string valueString)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            var value = valueString == null ? null : BsonDocument.Parse(valueString);

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

            result.Should().BeSameAs(value);
        }
        public async Task ExecuteAsync_should_return_expected_result()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            long result;
            using (var binding = CoreTestConfiguration.GetReadBinding())
            {
                result = await subject.ExecuteAsync(binding, CancellationToken.None);
            }

            result.Should().Be(5);
        }
        public void Collation_get_and_set_should_work(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            var value = locale == null ? null : new Collation(locale);

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

            result.Should().BeSameAs(value);
        }
        public void CreateCommand_should_create_the_correct_command()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter = new BsonDocument("x", 1),
                Hint = "funny",
                Limit = 10,
                MaxTime = TimeSpan.FromSeconds(20),
                Skip = 30
            };

            var cmd = subject.CreateCommand();

            cmd.Should().Be("{ count: \"bar\", query: {x: 1}, limit: 10, skip: 30, hint: \"funny\", maxTimeMS: 20000 }");
        }
        public void constructor_should_initialize_instance()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.Collation.Should().BeNull();
            subject.Filter.Should().BeNull();
            subject.Hint.Should().BeNull();
            subject.Limit.Should().NotHaveValue();
            subject.MaxTime.Should().NotHaveValue();
            subject.ReadConcern.IsServerDefault.Should().BeTrue();
            subject.Skip.Should().NotHaveValue();
        }
示例#8
0
        public Task <long> CountAsync(object filter, CountOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(filter, "filter");

            options = options ?? new CountOptions();

            var operation = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter                           = ConvertFilterToBsonDocument(filter),
                Hint                             = options.Hint is string?BsonValue.Create((string)options.Hint) : ConvertToBsonDocument(options.Hint),
                                         Limit   = options.Limit,
                                         MaxTime = options.MaxTime,
                                         Skip    = options.Skip
            };

            return(ExecuteReadOperation(operation, cancellationToken));
        }
        public override Task <long> CountAsync(FilterDefinition <TDocument> filter, CountOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(filter, "filter");

            options = options ?? new CountOptions();

            var operation = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter  = filter.Render(_documentSerializer, _settings.SerializerRegistry),
                Hint    = options.Hint,
                Limit   = options.Limit,
                MaxTime = options.MaxTime,
                Skip    = options.Skip
            };

            return(ExecuteReadOperation(operation, cancellationToken));
        }
示例#10
0
        public Task <long> CountAsync(CountModel model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

            var operation = new CountOperation(
                _collectionNamespace,
                GetMessageEncoderSettings())
            {
                Filter                           = ConvertToBsonDocument(model.Filter),
                Hint                             = model.Hint is string?BsonValue.Create((string)model.Hint) : ConvertToBsonDocument(model.Hint),
                                         Limit   = model.Limit,
                                         MaxTime = model.MaxTime,
                                         Skip    = model.Skip
            };

            return(ExecuteReadOperation(operation, timeout, cancellationToken));
        }
        public void CreateCommand_should_create_the_correct_command(
            [Values("3.0.0", "3.2.0")] string serverVersion,
            [Values(null, ReadConcernLevel.Local, ReadConcernLevel.Majority)] ReadConcernLevel? readConcernLevel)
        {
            var semanticServerVersion = SemanticVersion.Parse(serverVersion);
            var filter = new BsonDocument("x", 1);
            var hint = "funny";
            var limit = 10;
            var skip = 30;
            var maxTime = TimeSpan.FromSeconds(20);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter = filter,
                Hint = hint,
                Limit = limit,
                MaxTime = maxTime,
                ReadConcern = new ReadConcern(readConcernLevel),
                Skip = skip
            };
            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "query", filter },
                { "limit", limit },
                { "skip", skip },
                { "hint", hint },
                { "maxTimeMS", maxTime.TotalMilliseconds }
            };

            if (!subject.ReadConcern.IsServerDefault)
            {
                expectedResult["readConcern"] = subject.ReadConcern.ToBsonDocument();
            }

            if (!subject.ReadConcern.IsSupported(semanticServerVersion))
            {
                Action act = () => subject.CreateCommand(semanticServerVersion);
                act.ShouldThrow<MongoClientException>();
            }
            else
            {
                var result = subject.CreateCommand(semanticServerVersion);
                result.Should().Be(expectedResult);
            }
        }
        protected override void Given()
        {
            Insert(new[] {
                new BsonDocument("x", 1),
                new BsonDocument("x", 2),
                new BsonDocument("x", 3),
                new BsonDocument("x", 4),
                new BsonDocument("x", 5),
                new BsonDocument("x", 6),
            });

            _subject = new CountOperation(CollectionNamespace, MessageEncoderSettings)
            {
                Criteria = BsonDocument.Parse("{ x : { $gt : 2 } }"),
                Limit = 2,
                Skip = 1
            };
        }
示例#13
0
        protected override void Given()
        {
            Insert(new[] {
                new BsonDocument("x", 1),
                new BsonDocument("x", 2),
                new BsonDocument("x", 3),
                new BsonDocument("x", 4),
                new BsonDocument("x", 5),
                new BsonDocument("x", 6),
            });

            _subject = new CountOperation(CollectionNamespace, MessageEncoderSettings)
            {
                Filter = BsonDocument.Parse("{ x : { $gt : 2 } }"),
                Limit  = 2,
                Skip   = 1
            };
        }
示例#14
0
        protected VisitResult VisitCount(CountOperation operation)
        {
            var source = Visit(operation.Source);

            if (source.ResultSchema.Kind != SchemaNodeKind.Collection)
            {
                throw new InvalidOperationException("Can only use call operations on collections");
            }
            var arguments = new[] { source.Expression };

            return(new VisitResult
            {
                ResultSchema = new SchemaNodeInteger(null, 0),
                Expression = Expression.Call(
                    null,
                    typeof(Queryable).GetMethod("Count", arguments.Select(expr => expr.Type).ToArray()),
                    arguments),
            });
        }
        public async Task ExecuteAsync_should_return_expected_result_when_filter_is_provided()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            subject.Filter = BsonDocument.Parse("{ _id : { $gt : 1 } }");

            long result;
            using (var binding = CoreTestConfiguration.GetReadBinding())
            {
                result = await subject.ExecuteAsync(binding, CancellationToken.None);
            }

            result.Should().Be(4);
        }
        public void CreateCommand_should_return_expected_result_when_Limit_is_set(
            [Values(null, 1L, 2L)]
            long? limit)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Limit = limit
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "limit", () => limit.Value, limit != null }
            };
            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_Hint_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Hint = BsonDocument.Parse("{ _id : 1 }")
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be(2);
        }
        public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(
            [Values(null, 1L, 2L)]
            long? milliseconds)
        {
            var maxTime = milliseconds == null ? (TimeSpan?)null : TimeSpan.FromMilliseconds(milliseconds.Value);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "maxTimeMS", () => maxTime.Value.TotalMilliseconds, maxTime != null }
            };
            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_throw_when_Collation_is_set_but_not_supported()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Collation = new Collation("en_US")
            };

            var exception = Record.Exception(() => subject.CreateCommand(Feature.Collation.LastNotSupportedVersion));

            exception.Should().BeOfType<NotSupportedException>();
        }
        public void Execute_should_return_expected_result_when_filter_is_provided(
            [Values(false, true)]
            bool async)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            subject.Filter = BsonDocument.Parse("{ _id : { $gt : 1 } }");

            var result = ExecuteOperation(subject, async);

            result.Should().Be(4);
        }
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);

            var result = ExecuteOperation(subject, async);

            result.Should().Be(2);
        }
示例#22
0
 public static int Count(Type t, String condition)
 {
     return(CountOperation.Count(t, condition));
 }
        public void Execute_should_return_expected_result_when_Skip_is_set(
            [Values(null, 1L, 2L)]
            long? skip,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Skip = skip
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be(2 - (skip ?? 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.Collation);
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Collation = new Collation("en_US", caseLevel: caseSensitive, strength: CollationStrength.Primary),
                Filter = BsonDocument.Parse("{ x : \"x\" }")
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be(caseSensitive ? 1 : 2);
        }
示例#25
0
 private void WriteCountOperation(BinaryWriter writer, CountOperation operation)
 {
     writer.Write(operation.Count);
 }
        public void Execute_should_return_expected_result_when_ReadConcern_is_set(
            [Values(null, ReadConcernLevel.Local)]
            ReadConcernLevel? level,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.ReadConcern);
            EnsureTestData();
            var readConcern = level == null ? ReadConcern.Default : new ReadConcern(level.Value);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be(2);
        }
        public void Execute_should_return_expected_result_when_MaxTime_is_set(
            [Values(null, 1000L)]
            long? milliseconds,
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                MaxTime = milliseconds == null ? (TimeSpan?)null : TimeSpan.FromMilliseconds(milliseconds.Value)
            };

            // TODO: use failpoints to force a timeout?
            var result = ExecuteOperation(subject, async);

            result.Should().Be(2);
        }
        public void CreateCommand_should_return_expected_result_when_Hint_is_set(
            [Values(null, "{ hint : \"x_1\" }", "{ hint : { x : 1 } }")]
            string hintString)
        {
            var hint = hintString == null ? null : BsonDocument.Parse(hintString)["hint"];
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Hint = hint
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "hint", hint, hint != null }
            };
            result.Should().Be(expectedResult);
        }
        public void Hint_get_and_set_should_work(
            [Values(null, "{ hint : \"x_1\" }", "{ hint : { x : 1 } }")]
            string valueString)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            var value = valueString == null ? null : BsonDocument.Parse(valueString)["hint"];

            subject.Hint = value;
            var result = subject.Hint;

            result.Should().BeSameAs(value);
        }
        public void CreateCommand_should_return_expected_result_when_Filter_is_set(
            [Values(null, "{ x : 1 }", "{ x : 2 }")]
            string filterString)
        {
            var filter = filterString == null ? null : BsonDocument.Parse(filterString);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Filter = filter
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "query", filter, filter != null }
            };
            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_Skip_is_set(
            [Values(null, 1L, 2L)]
            long? skip)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Skip = skip
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "skip", () => skip.Value, skip != null }
            };
            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_limit_is_provided(
            [Values(false, true)]
            bool async)
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings);
            subject.Limit = 3;

            var result = ExecuteOperation(subject, async);

            result.Should().Be(3);
        }
        public void CreateCommand_should_throw_when_ReadConcern_is_set_but_not_supported()
        {
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                ReadConcern = new ReadConcern(ReadConcernLevel.Linearizable)
            };

            var exception = Record.Exception(() => subject.CreateCommand(Feature.ReadConcern.LastNotSupportedVersion));

            exception.Should().BeOfType<MongoClientException>();
        }
        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 CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Collation = collation
            };

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

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "collation", () => collation.ToBsonDocument(), collation != null }
            };
            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_ReadConcern_is_set(
            [Values(null, ReadConcernLevel.Linearizable, ReadConcernLevel.Local)]
            ReadConcernLevel? level)
        {
            var readConcern = level == null ? ReadConcern.Default : new ReadConcern(level);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

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

            var expectedResult = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "readConcern", () => readConcern.ToBsonDocument(), !readConcern.IsServerDefault }
            };
            result.Should().Be(expectedResult);
        }
        public void Execute_should_throw_when_ReadConcern_is_set_but_not_supported(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().DoesNotSupport(Feature.ReadConcern);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                ReadConcern = new ReadConcern(ReadConcernLevel.Local)
            };

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

            exception.Should().BeOfType<MongoClientException>();
        }
        public void Execute_should_throw_when_Collation_is_set_but_not_supported(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().DoesNotSupport(Feature.Collation);
            var subject = new CountOperation(_collectionNamespace, _messageEncoderSettings)
            {
                Collation = new Collation("en_US")
            };

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

            exception.Should().BeOfType<NotSupportedException>();
        }