public void AddMultipleForeignIds()
        {
            var collection = new EntityNavigationCollection <ObjectIdIdModel>("Id");

            collection.AddForeignIds(new object[] { ObjectId.GenerateNewId(), ObjectId.GenerateNewId() });
            Assert.AreEqual(2, collection.UnloadedCount);
        }
        public void AddForeignIdWithWrongType()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty();
            var collection      = new EntityNavigationCollection <StringIdModel>(foreignProperty);

            collection.AddForeignId(ObjectId.GenerateNewId());
        }
        public void AddForeignIdWithNull()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var collection      = new EntityNavigationCollection <StringIdModel>(foreignProperty);

            collection.AddForeignId(null);
        }
        public void AddForeignIdWithRightType()
        {
            var collection = new EntityNavigationCollection <StringIdModel>("Id");

            collection.AddForeignId("12345678");
            Assert.AreEqual(1, collection.UnloadedCount);
        }
        public void AddMultipleForeignIds()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var collection      = new EntityNavigationCollection <ObjectIdIdModel>(foreignProperty);

            collection.AddForeignIds(new object[] { ObjectId.GenerateNewId(), ObjectId.GenerateNewId() });
            Assert.AreEqual(2, collection.UnloadedCount);
        }
        public void AddForeignIdWithRightType()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty();
            var collection      = new EntityNavigationCollection <StringIdModel>(foreignProperty);

            collection.AddForeignId("12345678");
            Assert.AreEqual(1, collection.UnloadedCount);
        }
Esempio n. 7
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)));
        }
Esempio n. 8
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)));
        }
Esempio n. 9
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)));
        }
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var type = context.Reader.GetCurrentBsonType();

            if (type == BsonType.Array)
            {
                var collection = new EntityNavigationCollection <TEntity>(ForeignProperty);
                context.Reader.ReadStartArray();

                while (context.Reader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (context.Reader.CurrentBsonType == BsonType.ObjectId)
                    {
                        var entityId = context.Reader.ReadObjectId();
                        collection.AddForeignId(entityId);
                    }
                    else if (context.Reader.CurrentBsonType == BsonType.String)
                    {
                        var entityId = context.Reader.ReadString();
                        collection.AddForeignId(entityId);
                    }
                    else
                    {
                        context.Reader.SkipValue();
                    }
                }

                context.Reader.ReadEndArray();
                return(collection);
            }
            else if (type == BsonType.Null)
            {
                context.Reader.ReadNull();
                return(new EntityNavigationCollection <TEntity>(ForeignProperty));
            }
            else
            {
                throw new NotSupportedException($"Unable to deserialize {type} into an ICollection<{typeof(TEntity).Name}>");
            }
        }
        public void AddForeignIdWithWrongType()
        {
            var collection = new EntityNavigationCollection <StringIdModel>("Id");

            collection.AddForeignId(ObjectId.GenerateNewId());
        }
        public void AddForeignIdWithNull()
        {
            var collection = new EntityNavigationCollection <StringIdModel>("Id");

            collection.AddForeignId(null);
        }