public void CreateCommand_should_return_the_expected_result_when_using_causal_consistency(
            [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 connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.ReadConcern.FirstSupportedVersion, supportsSessions: true);
            var session = OperationTestHelper.CreateSession(true, new BsonTimestamp(100));

            var result = subject.CreateCommand(connectionDescription, session);

            var expectedReadConcernDocument = readConcern.ToBsonDocument();

            expectedReadConcernDocument["afterClusterTime"] = new BsonTimestamp(100);

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "readConcern", expectedReadConcernDocument }
            };

            result.Should().Be(expectedResult);
        }
        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>();
        }
        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>();
        }
Beispiel #4
0
        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 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_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 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);
        }
        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 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_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_when_MaxTime_is_set(long maxTimeTicks, int expectedMaxTimeMS)
        {
            var subject = new DistinctOperation <int>(_collectionNamespace, _valueSerializer, _fieldName, _messageEncoderSettings)
            {
                MaxTime = TimeSpan.FromTicks(maxTimeTicks)
            };

            var connectionDescription = OperationTestHelper.CreateConnectionDescription();
            var session = OperationTestHelper.CreateSession();

            var result = subject.CreateCommand(connectionDescription, session);

            var expectedResult = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "maxTimeMS", expectedMaxTimeMS }
            };

            result.Should().Be(expectedResult);
            result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
        }