示例#1
0
        public void CreateWrappedQuery_should_create_the_correct_query_when_connected_to_a_shard_router(long maxTimeTicks, int expectedMaxTimeMS)
        {
            var subject = new FindOpcodeOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Comment    = "funny",
                Filter     = BsonDocument.Parse("{x: 1}"),
                MaxTime    = TimeSpan.FromTicks(maxTimeTicks),
                Modifiers  = BsonDocument.Parse("{$comment: \"notfunny\", $snapshot: true}"),
                Projection = BsonDocument.Parse("{y: 1}"),
                Sort       = BsonDocument.Parse("{a: 1}")
            };

            var expectedResult = new BsonDocument
            {
                { "$query", BsonDocument.Parse("{x: 1}") },
                { "$readPreference", BsonDocument.Parse("{mode: \"secondary\"}") },
                { "$orderby", BsonDocument.Parse("{a: 1}") },
                { "$comment", "funny" },
                { "$maxTimeMS", expectedMaxTimeMS },
                { "$snapshot", true }
            };

            var result = subject.CreateWrappedQuery(ServerType.ShardRouter, ReadPreference.Secondary, out var secondaryOk);

            result.Should().Be(expectedResult);
            result["$maxTimeMS"].BsonType.Should().Be(BsonType.Int32);
            secondaryOk.Should().BeTrue();
        }
        public void CreateWrappedQuery_should_create_the_correct_query_when_not_connected_to_a_shard_router()
        {
            var subject = new FindOpcodeOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Comment    = "funny",
                Filter     = BsonDocument.Parse("{x: 1}"),
                MaxTime    = TimeSpan.FromSeconds(20),
                Modifiers  = BsonDocument.Parse("{$comment: \"notfunny\", $snapshot: true}"),
                Projection = BsonDocument.Parse("{y: 1}"),
                Sort       = BsonDocument.Parse("{a: 1}")
            };

            var expectedResult = new BsonDocument
            {
                { "$query", BsonDocument.Parse("{x: 1}") },
                { "$orderby", BsonDocument.Parse("{a: 1}") },
                { "$comment", "funny" },
                { "$maxTimeMS", 20000 },
                { "$snapshot", true }
            };


            var result = subject.CreateWrappedQuery(ServerType.ReplicaSetArbiter, ReadPreference.Secondary);

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