public void AddForeignIdWithRightType() { var collection = new EntityNavigationCollection <StringIdModel>("Id"); collection.AddForeignId("12345678"); Assert.AreEqual(1, 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 foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty(); var collection = new EntityNavigationCollection <StringIdModel>(foreignProperty); collection.AddForeignId("12345678"); Assert.AreEqual(1, collection.UnloadedCount); }
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 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))); }
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))); }
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); }