using System.Xml; using Xunit; namespace VCommon.VAutoMapper.Tests { public class IgnoreMappingTest { [AutoMapFrom(typeof(Entity))] [AutoMapTo(typeof(Entity))] public class Dto { public int Id { get; set; } [AutoMapperIgnore] public string BothIgnoreValue { get; set; } [AutoMapperIgnore(AutoMapperIgnoreDirection.From)] public string ToDtoIgnoreValue { get; set; } [AutoMapperIgnore(AutoMapperIgnoreDirection.To)] public string ToEntityIgnoreValue { get; set; } } public class Entity { public int Id { get; set; } public string BothIgnoreValue { get; set; } public string ToDtoIgnoreValue { get; set; } public string ToEntityIgnoreValue { get; set; } } [Fact] public void DtoToEntityTest() { var dto = new Dto { Id = 114, BothIgnoreValue = "ShouldIgnoreBoth", ToDtoIgnoreValue = "ShouldIgnoreToDto", ToEntityIgnoreValue = "ShouldIgnoreToEntity", }; var entity = dto.MapTo(); Assert.Equal(dto.Id, entity.Id); Assert.Equal(dto.ToDtoIgnoreValue, entity.ToDtoIgnoreValue); Assert.NotEqual(dto.BothIgnoreValue, entity.BothIgnoreValue); Assert.NotEqual(dto.ToEntityIgnoreValue, entity.ToEntityIgnoreValue); } [Fact] public void EntityToDtoTest() { var entity = new Entity { Id = 514, BothIgnoreValue = "ShouldIgnoreBoth", ToDtoIgnoreValue = "ShouldIgnoreToDto", ToEntityIgnoreValue = "ShouldIgnoreToEntity", }; var dto = entity.MapTo(); Assert.Equal(entity.Id, dto.Id); Assert.Equal(entity.ToEntityIgnoreValue, dto.ToEntityIgnoreValue); Assert.NotEqual(entity.BothIgnoreValue, dto.BothIgnoreValue); Assert.NotEqual(entity.ToDtoIgnoreValue, dto.ToDtoIgnoreValue); } [Fact] public void EntityUpdateDtoTest() { var dto = new Dto { Id = 114, BothIgnoreValue = "ShouldIgnoreBoth", ToDtoIgnoreValue = "ShouldIgnoreToDto", ToEntityIgnoreValue = "ShouldIgnoreToEntity", }; var entity = new Entity { Id = 514, BothIgnoreValue = "BOTH", ToDtoIgnoreValue = "ETD", ToEntityIgnoreValue = "DTE", }; var r = dto.MapTo(entity); Assert.Equal(dto.Id, entity.Id); Assert.Equal(dto.ToDtoIgnoreValue, entity.ToDtoIgnoreValue); Assert.NotEqual(dto.ToEntityIgnoreValue, entity.ToEntityIgnoreValue); Assert.NotEqual(dto.BothIgnoreValue, entity.BothIgnoreValue); } } }