public void CreateCommand_should_create_the_correct_command(
            [Values(false, true)] bool isUpsert,
            [Values(null, 10)] int?maxTimeMS,
            [Values(null, "{a: 1}")] string projection,
            [Values(ReturnDocument.Before, ReturnDocument.After)] ReturnDocument returnDocument,
            [Values(null, "{b: 1}")] string sort)
        {
            var projectionDoc = projection == null ? (BsonDocument)null : BsonDocument.Parse(projection);
            var sortDoc       = sort == null ? (BsonDocument)null : BsonDocument.Parse(sort);
            var subject       = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                IsUpsert       = isUpsert,
                MaxTime        = maxTimeMS.HasValue ? TimeSpan.FromMilliseconds(maxTimeMS.Value) : (TimeSpan?)null,
                Projection     = projectionDoc,
                ReturnDocument = returnDocument,
                Sort           = sortDoc
            };

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "sort", sortDoc, sortDoc != null },
                { "update", _replacement, _replacement != null },
                { "new", returnDocument == ReturnDocument.After },
                { "fields", projectionDoc, projectionDoc != null },
                { "upsert", isUpsert },
                { "maxTimeMS", () => maxTimeMS.Value, maxTimeMS.HasValue }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
Пример #2
0
        public void CreateCommand_should_return_expected_result_when_WriteConcern_is_set(
            [Values(null, 1, 2)]
            int?w,
            [Values(false, true)]
            bool useServerVersionSupportingFindAndModifyWriteConcern)
        {
            var writeConcern = w.HasValue ? new WriteConcern(w.Value) : null;
            var subject      = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                WriteConcern = writeConcern
            };
            var serverVersion         = Feature.FindAndModifyWriteConcern.SupportedOrNotSupportedVersion(useServerVersionSupportingFindAndModifyWriteConcern);
            var session               = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription(serverVersion: serverVersion);

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) }
            };

            result.Should().Be(expectedResult);
        }
Пример #3
0
        public void Execute_with_hint_should_throw_when_hint_is_not_supported(
            [Values(0, 1)] int w,
            [Values(false, true)] bool async)
        {
            var writeConcern  = new WriteConcern(w);
            var serverVersion = CoreTestConfiguration.ServerVersion;
            var subject       = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, _findAndModifyValueDeserializer, _messageEncoderSettings)
            {
                Hint         = new BsonDocument("_id", 1),
                WriteConcern = writeConcern
            };

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

            if (!writeConcern.IsAcknowledged)
            {
                exception.Should().BeOfType <NotSupportedException>();
            }
            else if (Feature.HintForFindAndModifyFeature.DriverMustThrowIfNotSupported(serverVersion))
            {
                exception.Should().BeOfType <NotSupportedException>();
            }
            else if (Feature.HintForFindAndModifyFeature.IsSupported(serverVersion))
            {
                exception.Should().BeNull();
            }
            else
            {
                exception.Should().BeOfType <MongoCommandException>();
            }
        }
        public void Execute_should_throw_when_there_is_a_write_concern_error(
            [Values(false, true)]
            bool async)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(
                _collectionNamespace,
                _filter,
                _replacement,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                ReturnDocument           = ReturnDocument.Before,
                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[0].Should().Be("{_id: 10, a: 2}");
        }
        public void Execute_against_a_non_existing_document_returning_the_replacement_with_upsert(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            DropCollection();
            var subject = new FindOneAndReplaceOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{_id: 10}"),
                _replacement,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                IsUpsert       = true,
                ReturnDocument = ReturnDocument.After
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be("{_id: 10, a: 2}");

            var serverList = ReadAllFromCollection(async);

            serverList[0].Should().Be("{_id: 10, a: 2}");
        }
        public void Execute_against_a_non_existing_document_returning_the_original(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var subject = new FindOneAndReplaceOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{alkjasdf: 10}"),
                _replacement,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                BypassDocumentValidation = true,
                ReturnDocument           = ReturnDocument.Before
            };

            var result = ExecuteOperation(subject, async);

            result.Should().BeNull();

            var serverList = ReadAllFromCollection(async);

            serverList[0].Should().Be("{_id: 10, x: 1}");
        }
        public void CreateCommand_should_return_expected_result(
            [Values(null, "{ name : 'name' }")] string let,
            [Values(null, 100L)] long?transactionNumber)
        {
            var letDocument = let != null?BsonDocument.Parse(let) : null;

            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Let = letDocument
            };
            var session = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription();

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "txnNumber", () => transactionNumber, transactionNumber != null },
                { "let", letDocument, letDocument != null }
            };

            result.Should().Be(expectedResult);
        }
Пример #8
0
        public void CreateCommand_should_return_expected_result_when_BypassDocumentValidation_is_set(
            [Values(null, false, true)]
            bool?value,
            [Values(false, true)]
            bool useServerVersionSupportingBypassDocumentValidation)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                BypassDocumentValidation = value
            };
            var serverVersion         = Feature.BypassDocumentValidation.SupportedOrNotSupportedVersion(useServerVersionSupportingBypassDocumentValidation);
            var session               = OperationTestHelper.CreateSession();
            var connectionDescription = OperationTestHelper.CreateConnectionDescription(serverVersion: serverVersion);

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

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "bypassDocumentValidation", () => value.Value, value.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) }
            };

            result.Should().Be(expectedResult);
        }
        public void Constructor_should_initialize_object()
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(_collectionNamespace);
            subject.Filter.Should().Be(_filter);
            subject.Replacement.Should().Be(_replacement);
            subject.ResultSerializer.Should().NotBeNull();
            subject.MessageEncoderSettings.Should().BeEquivalentTo(_messageEncoderSettings);
        }
        public void Let_get_and_set_should_work(
            [Values(null, "{ name : 'name' }")] string let)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = let != null?BsonDocument.Parse(let) : null;

            subject.Let = value;
            var result = subject.Let;

            result.Should().Be(value);
        }
Пример #11
0
        public void IsUpsert_get_and_set_should_work(
            [Values(false, true)]
            bool value)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.IsUpsert = true;
            var result = subject.IsUpsert;

            result.Should().Be(true);
        }
Пример #12
0
        public void ReturnDocument_get_and_set_should_work(
            [Values(ReturnDocument.After, ReturnDocument.Before)]
            ReturnDocument value)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.ReturnDocument = value;
            var result = subject.ReturnDocument;

            result.Should().Be(value);
        }
Пример #13
0
        public void MaxTime_get_and_set_should_work(
            [Values(-10000, 0, 1, 10000, 99999)] long maxTimeTicks)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = TimeSpan.FromTicks(maxTimeTicks);

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

            result.Should().Be(value);
        }
Пример #14
0
        public void CreateCommand_should_throw_when_Collation_is_set_but_not_supported()
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Collation = new Collation("en_US")
            };

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

            exception.Should().BeOfType <NotSupportedException>();
        }
Пример #15
0
        public void Hint_get_and_set_should_work(
            [Values(null, "_id_")] string hintString)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = (BsonValue)hintString;

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

            result.Should().Be(value);
        }
Пример #16
0
        public void BypassDocumentValidation_get_and_set_should_work(
            [Values(null, false, true)]
            bool?value)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.BypassDocumentValidation = value;
            var result = subject.BypassDocumentValidation;

            result.Should().Be(value);
        }
Пример #17
0
        public void Sort_get_and_set_should_work(
            [Values(null, "{ x : 1 }", "{ y : 2 }")]
            string valueString)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = valueString == null ? null : BsonDocument.Parse(valueString);

            subject.Sort = value;
            var result = subject.Sort;

            result.Should().Be(value);
        }
Пример #18
0
        public void MaxTime_set_should_throw_when_value_is_invalid(
            [Values(-10001, -9999, -1)] long maxTimeTicks)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = TimeSpan.FromTicks(maxTimeTicks);

            var exception = Record.Exception(() => subject.MaxTime = value);

            var e = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject;

            e.ParamName.Should().Be("value");
        }
Пример #19
0
        public void Collation_get_and_set_should_work(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = locale == null ? null : new Collation(locale);

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

            result.Should().BeSameAs(value);
        }
Пример #20
0
        public void WriteConcern_get_and_set_should_work(
            [Values(null, 1, 2)]
            int?w)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = w.HasValue ? new WriteConcern(w.Value) : null;

            subject.WriteConcern = value;
            var result = subject.WriteConcern;

            result.Should().BeSameAs(value);
        }
Пример #21
0
        public void MaxTime_get_and_set_should_work(
            [Values(null, 1, 2)]
            int?seconds)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value   = seconds.HasValue ? TimeSpan.FromSeconds(seconds.Value) : (TimeSpan?)null;

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

            result.Should().Be(value);
        }
Пример #22
0
        public void CreateCommand_should_return_expected_result()
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement }
            };

            result.Should().Be(expectedResult);
        }
Пример #23
0
        public void Execute_should_throw_when_maxTime_is_exceeded(
            [Values(false, true)] bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            var filter  = BsonDocument.Parse("{ x : 1 }");
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, _findAndModifyValueDeserializer, _messageEncoderSettings);

            subject.MaxTime = TimeSpan.FromSeconds(9001);

            using (var failPoint = FailPoint.ConfigureAlwaysOn(_cluster, _session, FailPointName.MaxTimeAlwaysTimeout))
            {
                var exception = Record.Exception(() => ExecuteOperation(subject, failPoint.Binding, async));

                exception.Should().BeOfType <MongoExecutionTimeoutException>();
            }
        }
        public void CreateCommand_should_create_the_correct_command(
            [Values(null, false, true)] bool?bypassDocumentValidation,
            [Values(false, true)] bool isUpsert,
            [Values(null, 10)] int?maxTimeMS,
            [Values(null, "{a: 1}")] string projection,
            [Values(ReturnDocument.Before, ReturnDocument.After)] ReturnDocument returnDocument,
            [Values(null, "{b: 1}")] string sort,
            [Values(null, "{ w : 2 }")] string writeConcernString,
            [Values("3.0.0", "3.1.1")] string serverVersionString)
        {
            var projectionDoc = projection == null ? (BsonDocument)null : BsonDocument.Parse(projection);
            var sortDoc       = sort == null ? (BsonDocument)null : BsonDocument.Parse(sort);
            var writeConcern  = writeConcernString == null ? null : WriteConcern.FromBsonDocument(BsonDocument.Parse(writeConcernString));
            var serverVersion = SemanticVersion.Parse(serverVersionString);
            var subject       = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                BypassDocumentValidation = bypassDocumentValidation,
                IsUpsert       = isUpsert,
                MaxTime        = maxTimeMS.HasValue ? TimeSpan.FromMilliseconds(maxTimeMS.Value) : (TimeSpan?)null,
                Projection     = projectionDoc,
                ReturnDocument = returnDocument,
                Sort           = sortDoc,
                WriteConcern   = writeConcern
            };

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "sort", sortDoc, sortDoc != null },
                { "update", _replacement, _replacement != null },
                { "new", returnDocument == ReturnDocument.After },
                { "fields", projectionDoc, projectionDoc != null },
                { "upsert", isUpsert },
                { "maxTimeMS", () => maxTimeMS.Value, maxTimeMS.HasValue },
                { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null && SupportedFeatures.IsFindAndModifyWriteConcernSupported(serverVersion) },
                { "bypassDocumentValidation", () => bypassDocumentValidation.Value, bypassDocumentValidation.HasValue&& SupportedFeatures.IsBypassDocumentValidationSupported(serverVersion) }
            };

            var result = subject.CreateCommand(serverVersion);

            result.Should().Be(expectedResult);
        }
Пример #25
0
        public void Constructor_should_initialize_object()
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.Filter.Should().BeSameAs(_filter);
            subject.Replacement.Should().BeSameAs(_replacement);
            subject.ResultSerializer.Should().BeSameAs(BsonDocumentSerializer.Instance);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);

            subject.BypassDocumentValidation.Should().NotHaveValue();
            subject.Collation.Should().BeNull();
            subject.IsUpsert.Should().BeFalse();
            subject.MaxTime.Should().NotHaveValue();
            subject.Projection.Should().BeNull();
            subject.ReturnDocument.Should().Be(ReturnDocument.Before);
            subject.Sort.Should().BeNull();
            subject.WriteConcern.Should().BeNull();
        }
Пример #26
0
        public void Execute_against_an_existing_document_returning_the_replacement(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, _findAndModifyValueDeserializer, _messageEncoderSettings)
            {
                ReturnDocument = ReturnDocument.After
            };

            var result = ExecuteOperation(subject, async);

            result.Should().Be("{ _id : 10, a : 1 }");
            ReadAllFromCollection().Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id : 10, a : 1 }"),
                BsonDocument.Parse("{ _id : 11, x : 2, y : 'A' }")
                );
        }
        public async Task ExecuteAsync_against_a_non_existing_document_returning_the_replacement()
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{alkjasdf: 10}"),
                _replacement,
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                ReturnDocument = ReturnDocument.After
            };

            var result = await ExecuteOperationAsync(subject);

            result.Should().BeNull();

            var serverList = await ReadAllFromCollectionAsync();

            serverList[0].Should().Be("{_id: 10, x: 1}");
        }
Пример #28
0
        public void CreateCommand_should_return_expected_result_when_MaxTime_is_set(long maxTimeTicks, int expectedMaxTimeMS)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                MaxTime = TimeSpan.FromTicks(maxTimeTicks)
            };

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

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

            result.Should().Be(expectedResult);
            result["maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
        }
Пример #29
0
        public void CreateCommand_should_return_expected_result_when_ReturnDocument_is_set(
            [Values(ReturnDocument.After, ReturnDocument.Before)]
            ReturnDocument value)
        {
            var subject = new FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                ReturnDocument = value
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "findAndModify", _collectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "new", () => true, value == ReturnDocument.After }
            };

            result.Should().Be(expectedResult);
        }
Пример #30
0
        public void CreateCommand_should_return_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 FindOneAndReplaceOperation <BsonDocument>(_collectionNamespace, _filter, _replacement, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Sort = sort
            };

            var result = subject.CreateCommand(null);

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

            result.Should().Be(expectedResult);
        }