IgnoreMappingTest.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Xml;
  2. using Xunit;
  3. namespace VCommon.VAutoMapper.Tests
  4. {
  5. public class IgnoreMappingTest
  6. {
  7. [AutoMapFrom(typeof(Entity))]
  8. [AutoMapTo(typeof(Entity))]
  9. public class Dto
  10. {
  11. public int Id { get; set; }
  12. [AutoMapperIgnore]
  13. public string BothIgnoreValue { get; set; }
  14. [AutoMapperIgnore(AutoMapperIgnoreDirection.From)]
  15. public string ToDtoIgnoreValue { get; set; }
  16. [AutoMapperIgnore(AutoMapperIgnoreDirection.To)]
  17. public string ToEntityIgnoreValue { get; set; }
  18. }
  19. public class Entity
  20. {
  21. public int Id { get; set; }
  22. public string BothIgnoreValue { get; set; }
  23. public string ToDtoIgnoreValue { get; set; }
  24. public string ToEntityIgnoreValue { get; set; }
  25. }
  26. [Fact]
  27. public void DtoToEntityTest()
  28. {
  29. var dto = new Dto
  30. {
  31. Id = 114,
  32. BothIgnoreValue = "ShouldIgnoreBoth",
  33. ToDtoIgnoreValue = "ShouldIgnoreToDto",
  34. ToEntityIgnoreValue = "ShouldIgnoreToEntity",
  35. };
  36. var entity = dto.MapTo<Entity>();
  37. Assert.Equal(dto.Id, entity.Id);
  38. Assert.Equal(dto.ToDtoIgnoreValue, entity.ToDtoIgnoreValue);
  39. Assert.NotEqual(dto.BothIgnoreValue, entity.BothIgnoreValue);
  40. Assert.NotEqual(dto.ToEntityIgnoreValue, entity.ToEntityIgnoreValue);
  41. }
  42. [Fact]
  43. public void EntityToDtoTest()
  44. {
  45. var entity = new Entity
  46. {
  47. Id = 514,
  48. BothIgnoreValue = "ShouldIgnoreBoth",
  49. ToDtoIgnoreValue = "ShouldIgnoreToDto",
  50. ToEntityIgnoreValue = "ShouldIgnoreToEntity",
  51. };
  52. var dto = entity.MapTo<Dto>();
  53. Assert.Equal(entity.Id, dto.Id);
  54. Assert.Equal(entity.ToEntityIgnoreValue, dto.ToEntityIgnoreValue);
  55. Assert.NotEqual(entity.BothIgnoreValue, dto.BothIgnoreValue);
  56. Assert.NotEqual(entity.ToDtoIgnoreValue, dto.ToDtoIgnoreValue);
  57. }
  58. [Fact]
  59. public void EntityUpdateDtoTest()
  60. {
  61. var dto = new Dto
  62. {
  63. Id = 114,
  64. BothIgnoreValue = "ShouldIgnoreBoth",
  65. ToDtoIgnoreValue = "ShouldIgnoreToDto",
  66. ToEntityIgnoreValue = "ShouldIgnoreToEntity",
  67. };
  68. var entity = new Entity
  69. {
  70. Id = 514,
  71. BothIgnoreValue = "BOTH",
  72. ToDtoIgnoreValue = "ETD",
  73. ToEntityIgnoreValue = "DTE",
  74. };
  75. var r = dto.MapTo(entity);
  76. Assert.Equal(dto.Id, entity.Id);
  77. Assert.Equal(dto.ToDtoIgnoreValue, entity.ToDtoIgnoreValue);
  78. Assert.NotEqual(dto.ToEntityIgnoreValue, entity.ToEntityIgnoreValue);
  79. Assert.NotEqual(dto.BothIgnoreValue, entity.BothIgnoreValue);
  80. }
  81. }
  82. }