Пример #1
0
            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var deleteRequest = (DeleteRequest)request;

                bsonWriter.PushMaxDocumentSize(MaxDocumentSize);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("q");
                BsonSerializer.Serialize(bsonWriter, deleteRequest.Query ?? new QueryDocument());
                bsonWriter.WriteInt32("limit", deleteRequest.Limit);
                bsonWriter.WriteEndDocument();
                bsonWriter.PopMaxDocumentSize();
            }
        public void BsonWriter_should_fail_on_invalid_child_elements_names(
            [Values(1, 2)]
            int levelsCount,
            [Values(true, false)]
            bool addValidatorOnRoot)
        {
            var levelNames      = Enumerable.Range(0, levelsCount).Select(i => $"level{i}").ToArray();
            var validatorsMocks = CreatePrefixValidatorsNestedMock(levelNames);

            using (var stream = new MemoryStream())
                using (var writer = new BsonBinaryWriter(stream))
                {
                    if (addValidatorOnRoot)
                    {
                        writer.PushElementNameValidator(validatorsMocks[0].Object);
                    }

                    writer.WriteStartDocument();

                    if (!addValidatorOnRoot)
                    {
                        writer.PushElementNameValidator(validatorsMocks[0].Object);
                    }

                    foreach (var levelName in levelNames)
                    {
                        Record.Exception(() => writer.WriteInt32($"wrongname", 1)).Should().BeOfType <BsonSerializationException>();

                        writer.WriteInt32($"{levelName}_int", 1);
                        writer.WriteStartDocument($"{levelName}_nested");
                    }

                    for (int i = 0; i < levelsCount; i++)
                    {
                        writer.WriteEndDocument(); // $"{levelName}_nested"
                    }

                    writer.WriteEndDocument();
                }
        }
Пример #3
0
        /// <summary>
        /// 写bson
        /// </summary>
        /// <param name="outputFileName"></param>
        public static void WriteBson(string outputFileName)
        {
            //如果存在,则覆盖(注意原来的不截断)
            using (FileStream stream = File.OpenWrite(outputFileName))
            {
                using (BsonBinaryWriter writer = new BsonBinaryWriter(stream))
                {
                    writer.WriteStartDocument();
                    writer.WriteName("a");
                    writer.WriteInt32(1);

                    writer.WriteEndDocument();
                }
            }
        }
        public void BsonBinaryWriter_should_support_writing_multiple_documents(
            [Range(0, 3)]
            int numberOfDocuments)
        {
            var document       = new BsonDocument("x", 1);
            var bson           = document.ToBson();
            var expectedResult = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty <byte>(), (a, b) => a.Concat(b)).ToArray();

            using (var stream = new MemoryStream())
                using (var binaryWriter = new BsonBinaryWriter(stream))
                {
                    for (var n = 0; n < numberOfDocuments; n++)
                    {
                        binaryWriter.WriteStartDocument();
                        binaryWriter.WriteName("x");
                        binaryWriter.WriteInt32(1);
                        binaryWriter.WriteEndDocument();
                    }

                    var result = stream.ToArray();
                    result.Should().Equal(expectedResult);
                }
        }
        public void BsonWriter_should_validate_child_elements_names(
            [Values(1, 2, 3)]
            int levelsCount,
            [Values(true, false)]
            bool addValidatorOnRoot)
        {
            var levelNames      = Enumerable.Range(0, levelsCount).Select(i => $"level{i}").ToArray();
            var validatorsMocks = CreatePrefixValidatorsNestedMock(levelNames);

            using (var stream = new MemoryStream())
                using (var writer = new BsonBinaryWriter(stream))
                {
                    if (addValidatorOnRoot)
                    {
                        writer.PushElementNameValidator(validatorsMocks[0].Object);
                    }

                    writer.WriteStartDocument();

                    if (!addValidatorOnRoot)
                    {
                        writer.PushElementNameValidator(validatorsMocks[0].Object);
                    }

                    foreach (var levelName in levelNames)
                    {
                        writer.WriteInt32($"{levelName}_int", 1);

                        writer.WriteStartDocument($"{levelName}_nested_empty");
                        writer.WriteEndDocument();

                        writer.WriteStartDocument($"{levelName}_nested");
                    }

                    // Add additional level
                    writer.WriteStartDocument($"{levelsCount}_nested");
                    writer.WriteEndDocument(); // $"{levelsCount}_nested"

                    for (int i = 0; i < levelsCount; i++)
                    {
                        writer.WriteEndDocument(); // $"{levelName}_nested"
                    }

                    // Pop validator
                    if (!addValidatorOnRoot)
                    {
                        writer.PopElementNameValidator();
                        writer.WriteInt32("nonvalidatedname", 1);
                    }
                    writer.WriteEndDocument();
                }

            for (int i = 0; i < levelsCount; i++)
            {
                var levelName     = levelNames[i];
                var validatorMock = validatorsMocks[i];

                var children  = new[] { $"{levelName}_nested_empty", $"{levelName}_nested" };
                var fieldsAll = children.Concat(new[] { $"{levelName}_int" }).ToArray();

                foreach (var field in fieldsAll)
                {
                    validatorMock.Verify(v => v.IsValidElementName(field), Times.Once);
                }

                foreach (var child in children)
                {
                    validatorMock.Verify(v => v.GetValidatorForChildContent(child), Times.Once);
                }

                validatorMock.Verify(v => v.IsValidElementName(It.IsNotIn(fieldsAll)), Times.Never);
                validatorMock.Verify(v => v.GetValidatorForChildContent(It.IsNotIn(children)), Times.Never);
            }
        }