コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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 }");
        }
コード例 #7
0
        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));
        }
コード例 #9
0
        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));
        }
コード例 #11
0
        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);
            }
        }
コード例 #12
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)
            {
                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),
            });
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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>();
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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
ファイル: ObjectDB.cs プロジェクト: xia7410/cnblogsbywojilu
 public static int Count(Type t, String condition)
 {
     return(CountOperation.Count(t, condition));
 }
コード例 #23
0
        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));
        }
コード例 #24
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);
 }
コード例 #26
0
        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);
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        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);
        }
コード例 #31
0
        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);
        }
コード例 #32
0
        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);
        }
コード例 #33
0
        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>();
        }
コード例 #34
0
        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);
        }
コード例 #35
0
        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);
        }
コード例 #36
0
        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>();
        }
コード例 #37
0
        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>();
        }