public void CreateCommand_should_create_the_correct_command()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Criteria = new BsonDocument("x", 1),
                MaxTime = TimeSpan.FromSeconds(20),
            };

            var cmd = subject.CreateCommand();

            cmd.Should().Be("{ distinct: \"bar\", key: \"a.b\", query: {x: 1}, maxTimeMS: 20000 }");
        }
        public void constructor_should_initialize_instance()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.ValueSerializer.Should().BeSameAs(_valueSerializer);
            subject.FieldName.Should().BeSameAs(_fieldName);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.Collation.Should().BeNull();
            subject.Filter.Should().BeNull();
            subject.MaxTime.Should().NotHaveValue();
            subject.ReadConcern.Should().BeSameAs(ReadConcern.Default);
        }
        protected override void Given()
        {
            Insert(new[] {
                new BsonDocument("x", 1).Add("y", 1),
                new BsonDocument("x", 2).Add("y", 1),
                new BsonDocument("x", 3).Add("y", 2),
                new BsonDocument("x", 4).Add("y", 2),
                new BsonDocument("x", 5).Add("y", 3),
                new BsonDocument("x", 6).Add("y", 3),
            });

            _subject = new DistinctOperation <int>(CollectionNamespace, new Int32Serializer(), "y", MessageEncoderSettings)
            {
                Criteria = BsonDocument.Parse("{ x : { $gt : 2 } }"),
            };
        }
        protected override void Given()
        {
            Insert(new[] {
                new BsonDocument("x", 1).Add("y", 1),
                new BsonDocument("x", 2).Add("y", 1),
                new BsonDocument("x", 3).Add("y", 2),
                new BsonDocument("x", 4).Add("y", 2),
                new BsonDocument("x", 5).Add("y", 3),
                new BsonDocument("x", 6).Add("y", 3),
            });

            _subject = new DistinctOperation<int>(CollectionNamespace, new Int32Serializer(), "y", MessageEncoderSettings)
            {
                Criteria = BsonDocument.Parse("{ x : { $gt : 2 } }"),
            };
        }
Beispiel #5
0
        public Task <IReadOnlyList <TValue> > DistinctAsync <TValue>(DistinctModel <TValue> model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

            var operation = new DistinctOperation <TValue>(
                _collectionNamespace,
                model.ValueSerializer ?? _settings.SerializerRegistry.GetSerializer <TValue>(),
                model.FieldName,
                GetMessageEncoderSettings())
            {
                Filter  = ConvertToBsonDocument(model.Filter),
                MaxTime = model.MaxTime
            };

            return(ExecuteReadOperation(operation, timeout, cancellationToken));
        }
Beispiel #6
0
        public Task <IReadOnlyList <TResult> > DistinctAsync <TResult>(DistinctModel <TResult> model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

            var resultSerializer = model.ResultSerializer ?? _settings.SerializerRegistry.GetSerializer <TResult>();
            var operation        = new DistinctOperation <TResult>(
                _collectionNamespace,
                resultSerializer,
                model.FieldName,
                _messageEncoderSettings)
            {
                Criteria = ConvertToBsonDocument(model.Criteria),
                MaxTime  = model.MaxTime
            };

            return(ExecuteReadOperation(operation, timeout, cancellationToken));
        }
        public void CreateCommand_should_create_the_correct_command()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = new BsonDocument("x", 1),
                MaxTime = TimeSpan.FromSeconds(20),
            };

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", BsonDocument.Parse("{ x: 1 }") },
                { "maxTimeMS", 20000 }
            };
            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
Beispiel #8
0
        public Task <IAsyncCursor <TResult> > DistinctAsync <TResult>(string fieldName, object filter, DistinctOptions <TResult> options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(fieldName, "fieldName");
            Ensure.IsNotNull(filter, "filter");

            options = options ?? new DistinctOptions <TResult>();
            var resultSerializer = ResolveResultSerializer(options.ResultSerializer);

            var operation = new DistinctOperation <TResult>(
                _collectionNamespace,
                resultSerializer,
                fieldName,
                _messageEncoderSettings)
            {
                Filter  = ConvertFilterToBsonDocument(filter),
                MaxTime = options.MaxTime
            };

            return(ExecuteReadOperation(operation, cancellationToken));
        }
        public override Task <IAsyncCursor <TField> > DistinctAsync <TField>(FieldDefinition <TDocument, TField> field, FilterDefinition <TDocument> filter, DistinctOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(field, "field");
            Ensure.IsNotNull(filter, "filter");

            options = options ?? new DistinctOptions();
            var renderedField = field.Render(_documentSerializer, _settings.SerializerRegistry);

            var operation = new DistinctOperation <TField>(
                _collectionNamespace,
                renderedField.FieldSerializer,
                renderedField.FieldName,
                _messageEncoderSettings)
            {
                Filter  = filter.Render(_documentSerializer, _settings.SerializerRegistry),
                MaxTime = options.MaxTime
            };

            return(ExecuteReadOperation(operation, 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 subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = new BsonDocument("x", 1),
                MaxTime = TimeSpan.FromSeconds(20),
                ReadConcern = new ReadConcern(readConcernLevel)
            };

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", BsonDocument.Parse("{ x: 1 }") },
                { "maxTimeMS", 20000 }
            };

            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);
            }
        }
        public void ReadConcern_get_and_set_should_work(
            [Values(null, ReadConcernLevel.Local, ReadConcernLevel.Local)]
            ReadConcernLevel? level)
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);
            var value = level.HasValue ? new ReadConcern(level) : ReadConcern.Default;

            subject.ReadConcern = value;
            var result = subject.ReadConcern;

            result.Should().BeSameAs(value);
        }
        public void MaxTime_get_and_set_should_work(
            [Values(null, 1, 2)]
            int? seconds)
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);
            var value = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;

            subject.MaxTime = value;
            var result = subject.MaxTime;

            result.Should().Be(value);
        }
        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 DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Collation = collation
            };

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

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "collation", () => collation.ToBsonDocument(), collation != null }
            };
            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName }
            };
            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(
            [Values(null, 1, 2)]
            int? milliseconds)
        {
            var maxTime = milliseconds.HasValue ? TimeSpan.FromMilliseconds(milliseconds.Value) : (TimeSpan?)null;
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "maxTimeMS", () => maxTime.Value.TotalMilliseconds, maxTime.HasValue }
            };
            result.Should().Be(expectedResult);
        }
        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 DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = filter
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", filter, filter != 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.HasValue ? new ReadConcern(level.Value) : ReadConcern.Default;
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

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

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "readConcern", () => readConcern.ToBsonDocument(), !readConcern.IsServerDefault }
            };
            result.Should().Be(expectedResult);
        }
        public void Execute_should_return_expected_result_when_Collation_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.Collation);
            EnsureTestData();
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Collation = new Collation("en_US", caseLevel: false, strength: CollationStrength.Primary),
                Filter = BsonDocument.Parse("{ x : \"d\" }")
            };

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

            result.Should().HaveCount(2);
            result.Should().OnlyHaveUniqueItems();
            result.Should().Contain(new[] { 2, 3 });
        }
        public void Execute_should_throw_when_ReadConcern_is_set_and_not_supported(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().DoesNotSupport(Feature.ReadConcern);
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                ReadConcern = new ReadConcern(ReadConcernLevel.Local)
            };

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

            exception.Should().BeOfType<MongoClientException>();
        }
        public void Collation_get_and_set_should_work(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);
            var value = locale == null ? null : new Collation(locale);

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

            result.Should().BeSameAs(value);
        }
        public void CreateCommand_should_throw_when_Collation_is_set_and_not_supported()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Collation = new Collation("en_US")
            };

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

            exception.Should().BeOfType<NotSupportedException>();
        }
        public void Filter_get_and_set_should_work(
            [Values(null, "{ x :  1}", "{ x : 2 }")]
            string filterString)
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings);
            var value = filterString == null ? null : BsonDocument.Parse(filterString);

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

            result.Should().BeSameAs(value);
        }
        public void Execute_should_return_expected_result_when_Filter_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = BsonDocument.Parse("{ _id : { $gt : 2 } }")
            };

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

            result.Should().HaveCount(2);
            result.Should().OnlyHaveUniqueItems();
            result.Should().Contain(new[] { 2, 3 });
        }
        public void Execute_should_return_the_correct_results(
            [Values(false, true)]
            bool async)
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = BsonDocument.Parse("{ _id : { $gt : 2 } }"),
                MaxTime = TimeSpan.FromSeconds(20),
            };

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

            result.Should().HaveCount(2);
            result.Should().OnlyHaveUniqueItems();
            result.Should().Contain(new[] { 2, 3 });
        }
        public async Task ExecuteAsync_should_return_the_correct_results()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                Filter = BsonDocument.Parse("{ _id : { $gt : 2 } }"),
                MaxTime = TimeSpan.FromSeconds(20),
            };

            var cursor = await ExecuteOperationAsync(subject);
            var result = await cursor.ToListAsync();

            result.Should().HaveCount(2);
            result.Should().OnlyHaveUniqueItems();
            result.Should().Contain(new[] { 2, 3 });
        }
        public void Execute_should_return_expected_result_when_ReadConcern_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().Supports(Feature.ReadConcern);
            EnsureTestData();
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                ReadConcern = new ReadConcern(ReadConcernLevel.Local)
            };

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

            result.Should().HaveCount(3);
            result.Should().OnlyHaveUniqueItems();
            result.Should().Contain(new[] { 1, 2, 3 });
        }
        public void CreateCommand_should_throw_when_ReadConcern_is_set_and_not_supported()
        {
            var subject = new DistinctOperation<int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                ReadConcern = new ReadConcern(ReadConcernLevel.Local)
            };

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

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