public void replace_owned_entity_when_key_not_matching() { // GIVEN models SourceDTO source = new SourceDTO { Id = 1, Name = "dto", Owned = new OwnedDTO { Id = 2, Name = "owned_dto" } }; TargetEntity target = new TargetEntity { Id = 1, Name = "entity", ExtraProperty = "extra_prop_not_mapped", Owned = new OwnedEntity { Id = 3, Name = "owned_entity" } }; int targetCheck = target.GetHashCode(); int ownedCheck = target.Owned.GetHashCode(); MapperContext context = new MapperContext(); // WHEN mapped TargetEntity mapped = mapper.Map(source, target, context); // THEN Assert.Equal(targetCheck, mapped.GetHashCode()); // root is merged Assert.Equal(1, mapped.Id); Assert.Equal("dto", mapped.Name); Assert.Equal("extra_prop_not_mapped", mapped.ExtraProperty); // extra property is not overwritten Assert.NotNull(mapped.Owned); Assert.NotEqual(ownedCheck, target.Owned.GetHashCode()); // owned is replaced Assert.Equal(2, mapped.Owned.Id); Assert.Equal("owned_dto", mapped.Owned.Name); Assert.True(context.TryGetEntry <OwnedEntity>(new EntityKey <int>(2), out MapperContextEntry createdEntry)); Assert.Equal(MapperActionType.Create, createdEntry.ActionType); Assert.True(context.TryGetEntry <OwnedEntity>(new EntityKey <int>(3), out MapperContextEntry deletedEntry)); Assert.Equal(MapperActionType.Delete, deletedEntry.ActionType); }
public void create_owned_entity_when_target_is_null() { // GIVEN models SourceDTO source = new SourceDTO { Id = 1, Name = "dto", Owned = new OwnedDTO { Id = 2, Name = "owned_dto" } }; TargetEntity target = new TargetEntity { Id = 1, Name = "entity", ExtraProperty = "extra_prop_not_mapped", Owned = null }; MapperContext context = new MapperContext(); int targetCheck = target.GetHashCode(); // WHEN mapped TargetEntity mapped = mapper.Map(source, target, context); // THEN Assert.Equal(targetCheck, mapped.GetHashCode()); // root is merged Assert.Equal(1, mapped.Id); Assert.Equal("dto", mapped.Name); Assert.Equal("extra_prop_not_mapped", mapped.ExtraProperty); Assert.NotNull(mapped.Owned); Assert.Equal(2, mapped.Owned.Id); Assert.Equal("owned_dto", mapped.Owned.Name); Assert.True(context.TryGetEntry <OwnedEntity>(new EntityKey <int>(2), out MapperContextEntry entry)); Assert.Equal(MapperActionType.Create, entry.ActionType); }