Beispiel #1
0
        public void CreateCommand_should_return_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 FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Projection = projection
            };

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

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "projection", projection, projection != null }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #2
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 FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Hint = hint
            };

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

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "hint", hint, hint != null }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #3
0
        public void CreateCommand_should_return_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 FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                MaxTime = maxTime
            };

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

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "maxTimeMS", () => seconds.Value * 1000, seconds.HasValue }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #4
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 FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Collation = collation
            };

            var connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.Collation.FirstSupportedVersion);
            var session = OperationTestHelper.CreateSession();

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "collation", () => collation.ToBsonDocument(), collation != null }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #5
0
        public void CreateCommand_should_return_expected_result_when_CursorType_is_Set(
            [Values(CursorType.NonTailable, CursorType.Tailable, CursorType.TailableAwait)]
            CursorType cursorType)
        {
            var subject = new FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                CursorType = cursorType
            };

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

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "tailable", true, cursorType == CursorType.Tailable || cursorType == CursorType.TailableAwait },
                { "awaitData", true, cursorType == CursorType.TailableAwait }
            };

            result.Should().Be(expectedResult);
        }
        public void CreateCommand_should_return_expected_result_when_Limit_is_set(
            [Values(null, -2, -1, 0, 1, 2)]
            int?limit,
            [Values(null, false, true)]
            bool?singleBatch)
        {
            var subject = new FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Limit       = limit,
                SingleBatch = singleBatch
            };

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "limit", () => Math.Abs(limit.Value), limit.HasValue&& limit.Value != 0 },
                { "singleBatch", () => limit < 0 || singleBatch.Value, limit < 0 || singleBatch.HasValue }
            };

            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 FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

            var connectionDescription = OperationTestHelper.CreateConnectionDescription(Feature.ReadConcern.FirstSupportedVersion);
            var session = OperationTestHelper.CreateSession();

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "readConcern", () => readConcern.ToBsonDocument(), readConcern != null && !readConcern.IsServerDefault }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #8
0
        public void CreateCommand_should_return_expected_result_when_AllowPartialResults_is_set(
            [Values(null, false, true)]
            bool?allowPartialResults,
            [Values(ServerType.Standalone, ServerType.ShardRouter)]
            ServerType serverType)
        {
            var subject = new FindCommandOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                AllowPartialResults = allowPartialResults
            };

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

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

            var expectedResult = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "allowPartialResults", () => allowPartialResults.Value, allowPartialResults.HasValue&& serverType == ServerType.ShardRouter }
            };

            result.Should().Be(expectedResult);
        }
Beispiel #9
0
        // private methods
        internal FindCommandOperation <TDocument> CreateFindCommandOperation()
        {
            var comment      = _comment;
            var hint         = _hint;
            var max          = _max;
            var maxScan      = _maxScan;
            var maxTime      = _maxTime;
            var min          = _min;
            var returnKey    = _returnKey;
            var showRecordId = _showRecordId;
            var snapshot     = _snapshot;
            var sort         = _sort;

            if (_modifiers != null)
            {
                foreach (var element in _modifiers)
                {
                    var value = element.Value;
                    switch (element.Name)
                    {
                    case "$comment": comment = _comment ?? value.AsString; break;

                    case "$hint": hint = _hint ?? value; break;

                    case "$max": max = _max ?? value.AsBsonDocument; break;

                    case "$maxScan": maxScan = _maxScan ?? value.ToInt32(); break;

                    case "$maxTimeMS": maxTime = _maxTime ?? TimeSpan.FromMilliseconds(value.ToDouble()); break;

                    case "$min": min = _min ?? value.AsBsonDocument; break;

                    case "$orderby": sort = _sort ?? value.AsBsonDocument; break;

                    case "$returnKey": returnKey = _returnKey ?? value.ToBoolean(); break;

                    case "$showDiskLoc": showRecordId = _showRecordId ?? value.ToBoolean(); break;

                    case "$snapshot": snapshot = _snapshot ?? value.ToBoolean(); break;

                    default: throw new ArgumentException($"Modifier not supported by the Find command: '{element.Name}'.");
                    }
                }
            }

            var operation = new FindCommandOperation <TDocument>(
                _collectionNamespace,
                _resultSerializer,
                _messageEncoderSettings)
            {
                AllowPartialResults = _allowPartialResults,
                BatchSize           = _batchSize,
                Collation           = _collation,
                Comment             = comment,
                CursorType          = _cursorType,
                Filter          = _filter,
                Hint            = hint,
                FirstBatchSize  = _firstBatchSize,
                Limit           = _limit,
                Max             = max,
                MaxAwaitTime    = _maxAwaitTime,
                MaxScan         = maxScan,
                MaxTime         = maxTime,
                Min             = min,
                NoCursorTimeout = _noCursorTimeout,
                OplogReplay     = _oplogReplay,
                Projection      = _projection,
                ReadConcern     = _readConcern,
                ReturnKey       = returnKey,
                ShowRecordId    = showRecordId,
                SingleBatch     = _singleBatch,
                Skip            = _skip,
                Snapshot        = snapshot,
                Sort            = sort
            };

            return(operation);
        }
 // constructors
 public Reflector(FindCommandOperation <BsonDocument> instance)
 {
     _instance = instance;
 }