示例#1
0
        public void CreateCommand_should_return_the_expected_result_when_Sort_is_set(
            [Values(null, "{ x : 1 }", "{ y : 1 }")]
            string sortString)
        {
            var sort    = sortString == null ? null : BsonDocument.Parse(sortString);
            var subject = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Sort = sort
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "sort", sort, sort != null }
            };

            result.Should().Be(expectedResult);
        }
示例#2
0
        public void CreateCommand_should_return_the_expected_result_when_MaxTime_is_set(
            [Values(null, 1, 2)]
            int?seconds)
        {
            var maxTime = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;
            var subject = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "maxTimeMS", () => seconds.Value * 1000, seconds.HasValue }
            };

            result.Should().Be(expectedResult);
        }
示例#3
0
        public void CreateCommand_should_return_the_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 FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Collation = collation
            };

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "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 FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                MaxTime = TimeSpan.FromTicks(maxTimeTicks)
            };
            var session = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription();

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "maxTimeMS", expectedMaxTimeMS }
            };

            result.Should().Be(expectedResult);
            result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
        }
        public void CreateCommand_should_return_the_expected_result_when_WriteConcern_is_set(
            [Values(null, 1, 2)]
            int?w)
        {
            var writeConcern = w.HasValue ? new WriteConcern(w.Value) : null;
            var subject      = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                WriteConcern = writeConcern
            };

            var result = subject.CreateCommand(Feature.FindAndModifyWriteConcern.FirstSupportedVersion, null);

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null }
            };

            result.Should().Be(expectedResult);
        }
示例#6
0
        public void Execute_should_throw_when_there_is_a_write_concern_error(
            [Values(false, true)] bool async)
        {
            var subject = new FindOneAndDeleteOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{ x : 1 }"),
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                WriteConcern = new WriteConcern(9)
            };

            Action action = () => ExecuteOperation(subject, async);

            var exception     = action.ShouldThrow <MongoWriteConcernException>().Which;
            var commandResult = exception.Result;
            var result        = commandResult["value"].AsBsonDocument;

            result.Should().Be("{_id: 10, x: 1}");
            var serverList = ReadAllFromCollection(async);

            serverList.Should().BeEmpty();
        }
        public void CreateCommand_should_return_expected_result_when_Hint_is_set(
            [Values(null, "_id_")] string hintString)
        {
            var hint    = (BsonValue)hintString;
            var subject = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Hint = hint
            };
            var session = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription(serverVersion: Feature.HintForFindAndModifyFeature.FirstSupportedVersion);

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "hint", () => hint, hint != null }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_the_expected_result_when_Projection_is_set(
            [Values(null, "{ x : 1 }", "{ y : 1 }")]
            string projectionString)
        {
            var projection = projectionString == null ? null : BsonDocument.Parse(projectionString);
            var subject    = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Projection = projection
            };
            var session = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription();

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "fields", projection, projection != null }
            };

            result.Should().Be(expectedResult);
        }
示例#9
0
        public void Execute_with_hint_should_throw_when_hint_is_not_supported(
            [Values(false, true)] bool async)
        {
            var serverVersion = CoreTestConfiguration.ServerVersion;
            var subject       = new FindOneAndDeleteOperation <BsonDocument>(_collectionNamespace, _filter, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Hint = new BsonDocument("_id", 1)
            };

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

            if (Feature.HintForFindAndModifyFeature.IsSupported(serverVersion))
            {
                exception.Should().BeNull();
            }
            else if (Feature.HintForFindAndModifyFeature.DriverMustThrowIfNotSupported(serverVersion))
            {
                exception.Should().BeOfType <NotSupportedException>();
            }
            else
            {
                exception.Should().BeOfType <MongoCommandException>();
            }
        }