Beispiel #1
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ChangeStreamDocument{TDocument}" /> class.
 /// </summary>
 /// <param name="resumeToken">The resume token.</param>
 /// <param name="operationType">Type of the operation.</param>
 /// <param name="collectionNamespace">Namespace of the collection.</param>
 /// <param name="documentKey">The document key.</param>
 /// <param name="updateDescription">The update description.</param>
 /// <param name="fullDocument">The full document.</param>
 public ChangeStreamDocument(
     BsonDocument resumeToken,
     ChangeStreamOperationType operationType,
     CollectionNamespace collectionNamespace,
     BsonDocument documentKey,
     ChangeStreamUpdateDescription updateDescription,
     TDocument fullDocument)
 {
     _resumeToken         = Ensure.IsNotNull(resumeToken, nameof(resumeToken));
     _operationType       = operationType;
     _collectionNamespace = collectionNamespace; // can be null when operationType is Invalidate
     _documentKey         = documentKey;         // can be null
     _updateDescription   = updateDescription;   // can be null
     _fullDocument        = fullDocument;        // can be null
 }
        public void Serialize_should_have_expected_result_when_value_is_null()
        {
            var subject = CreateSubject();
            ChangeStreamUpdateDescription value = null;

            string result;

            using (var textWriter = new StringWriter())
                using (var writer = new JsonWriter(textWriter))
                {
                    var context = BsonSerializationContext.CreateRoot(writer);
                    subject.Serialize(context, value);
                    result = textWriter.ToString();
                }

            result.Should().Be("null");
        }
Beispiel #3
0
        public void UpdateDescription_should_return_expected_result(string updatedFieldsJson, string[] removedFields)
        {
            var updatedFields             = BsonDocument.Parse(updatedFieldsJson);
            var updateDescriptionDocument = new BsonDocument {
                { "updatedFields", updatedFields }, { "removedFields", new BsonArray(removedFields) }
            };
            var backingDocument = new BsonDocument {
                { "other", 1 }, { "updateDescription", updateDescriptionDocument }
            };
            var subject = CreateSubject(backingDocument: backingDocument);

            var result = subject.UpdateDescription;

            var expectedResult = new ChangeStreamUpdateDescription(updatedFields, removedFields);

            result.Should().Be(expectedResult);
        }
        public void Serialize_should_have_expected_result()
        {
            var subject = CreateSubject();
            var value   = new ChangeStreamUpdateDescription(
                new BsonDocument("f", 1),
                new [] { "f" });
            var expectedResult = "{ \"updatedFields\" : { \"f\" : 1 }, \"removedFields\" : [\"f\"] }";

            string result;

            using (var textWriter = new StringWriter())
                using (var writer = new JsonWriter(textWriter))
                {
                    var context = BsonSerializationContext.CreateRoot(writer);
                    subject.Serialize(context, value);
                    result = textWriter.ToString();
                }

            result.Should().Be(expectedResult);
        }
        public void Serialize_should_have_expected_result()
        {
            var subject = CreateSubject();
            var value   = new ChangeStreamUpdateDescription(
                new BsonDocument("f", 1),
                new[] { "f" },
                BsonArray.Create(new[] { BsonDocument.Parse("{ field : 'arrayForSubdiff', newSize : 2 }") }));
            var expectedResult = "{ \"updatedFields\" : { \"f\" : 1 }, \"removedFields\" : [\"f\"], \"truncatedArrays\" : [{ \"field\" : \"arrayForSubdiff\", \"newSize\" : 2 }] }";

            string result;

            using (var textWriter = new StringWriter())
                using (var writer = new JsonWriter(textWriter))
                {
                    var context = BsonSerializationContext.CreateRoot(writer);
                    subject.Serialize(context, value);
                    result = textWriter.ToString();
                }

            result.Should().Be(expectedResult);
        }
Beispiel #6
0
        public void UpdateDescription_should_return_expected_result(string updatedFieldsJson, string[] removedFields, string truncatedArrays)
        {
            var updatedFields            = BsonDocument.Parse(updatedFieldsJson);
            var truncatedArraysBsonArray = truncatedArrays != null?BsonSerializer.Deserialize <BsonArray>(truncatedArrays) : null;

            var updateDescriptionDocument = new BsonDocument
            {
                { "updatedFields", updatedFields },
                { "removedFields", new BsonArray(removedFields) },
                { "truncatedArrays", truncatedArraysBsonArray, truncatedArraysBsonArray != null }
            };
            var backingDocument = new BsonDocument {
                { "other", 1 }, { "updateDescription", updateDescriptionDocument }
            };
            var subject = CreateSubject(backingDocument: backingDocument);

            var result = subject.UpdateDescription;

            var expectedResult = new ChangeStreamUpdateDescription(updatedFields, removedFields, truncatedArraysBsonArray);

            result.Should().Be(expectedResult);
        }
Beispiel #7
0
        // public methods
        /// <inheritdoc />
        protected override ChangeStreamDocument <TDocument> DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var reader = context.Reader;

            CollectionNamespace           collectionNamespace = null;
            BsonDocument                  documentKey         = null;
            TDocument                     fullDocument        = default(TDocument);
            BsonDocument                  resumeToken         = null;
            ChangeStreamOperationType?    operationType       = null;
            ChangeStreamUpdateDescription updateDescription   = null;

            reader.ReadStartDocument();
            while (reader.ReadBsonType() != 0)
            {
                var fieldName = reader.ReadName();
                switch (fieldName)
                {
                case "_id":
                    resumeToken = BsonDocumentSerializer.Instance.Deserialize(context);
                    break;

                case "ns":
                    collectionNamespace = DeserializeCollectionNamespace(reader);
                    break;

                case "documentKey":
                    documentKey = BsonDocumentSerializer.Instance.Deserialize(context);
                    break;

                case "fullDocument":
                    if (reader.CurrentBsonType == BsonType.Null)
                    {
                        reader.ReadNull();
                        fullDocument = default(TDocument);
                    }
                    else
                    {
                        fullDocument = _documentSerializer.Deserialize(context);
                    }
                    break;

                case "operationType":
                    operationType = __operationTypeSerializer.Deserialize(context);
                    break;

                case "updateDescription":
                    updateDescription = __updateDescriptionSerializer.Deserialize(context);
                    break;

                default:
                    throw new FormatException($"Invalid field name: \"{fieldName}\".");
                }
            }
            reader.ReadEndDocument();

            return(new ChangeStreamDocument <TDocument>(
                       resumeToken,
                       operationType.Value,
                       collectionNamespace,
                       documentKey,
                       updateDescription,
                       fullDocument));
        }