예제 #1
0
        public void ReserializingStringIdEntityMaintainsStateExceptNulls()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <StringIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <StringIdModel>(foreignProperty)
            {
                new StringIdModel
                {
                    Description = "1"
                },
                new StringIdModel
                {
                    Id          = "5ac383379a5f1303784400f8",
                    Description = "2"
                }
            };
            EntityNavigationCollection <StringIdModel> deserializedCollection = null;

            initialCollection.AddForeignId("5ac383379a5f1303784400f9");

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <StringIdModel>;
            }

            Assert.AreEqual(3, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(deserializedCollection.GetForeignIds().All(id => initialCollection.GetForeignIds().Contains(id)));
        }
예제 #2
0
        public void ReserializingObjectIdIdEntityMaintainsState()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <ObjectIdIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <ObjectIdIdModel>(foreignProperty)
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            initialCollection.AddForeignId(ObjectId.GenerateNewId());

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(2, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.GetForeignIds().All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }
예제 #3
0
        public void SerializeICollectionCompatibleButIsntEntityNavigationCollection()
        {
            var serializer = new EntityNavigationCollectionSerializer <ObjectIdIdModel>("Id");

            var initialCollection = new List <ObjectIdIdModel>
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(1, initialCollection.Count());
            Assert.AreEqual(1, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.Select(e => e.Id).All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }