MapperTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Masuit.Tools.Mapping;
  2. using Masuit.Tools.Mapping.Core;
  3. using Masuit.Tools.Mapping.Exceptions;
  4. using Masuit.Tools.UnitTest.Mapping.ClassTests;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. namespace Masuit.Tools.UnitTest.Mapping
  7. {
  8. [TestClass]
  9. public class MapperTests
  10. {
  11. [ClassInitialize]
  12. public static void Init(TestContext context)
  13. {
  14. Clean();
  15. ExpressionMapper.CreateMap<ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
  16. }
  17. [ClassCleanup]
  18. public static void Clean()
  19. {
  20. ExpressionMapper.Reset();
  21. }
  22. [TestMethod, TestCategory("CreateMap")]
  23. public void Mapper_CreateMap_NotExist_ContainerCount1()
  24. {
  25. Clean();
  26. ExpressionMapper.CreateMap<ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
  27. Assert.AreEqual(MapperConfigurationCollectionContainer.Instance.Count, 1);
  28. }
  29. [TestMethod, TestCategory("CreateMap")]
  30. public void Mapper_CreateMap_Already_Exist_ContainerCount1()
  31. {
  32. ExpressionMapper.CreateMap<ClassSource, ClassDest>();
  33. Assert.IsTrue(MapperConfigurationCollectionContainer.Instance.Exists(m => m.SourceType == typeof(ClassSource) && m.TargetType == typeof(ClassDest)));
  34. }
  35. [TestMethod, TestCategory("CreateMap")]
  36. public void Mapper_CreateMap_With_Name()
  37. {
  38. ExpressionMapper.CreateMap<ClassSource, ClassDest>("test");
  39. Assert.IsTrue(MapperConfigurationCollectionContainer.Instance.Exists(m => m.Name == "test"));
  40. }
  41. [TestMethod, TestCategory("Map")]
  42. public void Map_ReturnDestinationObject_Success()
  43. {
  44. ExpressionMapper.Initialize();
  45. ClassSource expected = new ClassSource()
  46. {
  47. PropInt1 = 1,
  48. PropSourceInt1 = 1,
  49. PropString1 = "test"
  50. };
  51. var actual = expected.Map<ClassSource, ClassDest>();
  52. Assert.AreEqual(actual.PropInt1, expected.PropInt1);
  53. Assert.AreEqual(actual.PropString2, expected.PropString1);
  54. Assert.AreEqual(actual.PropInt2, 0);
  55. }
  56. [TestMethod, TestCategory("GetQueryExpression")]
  57. public void GetQueryExpression_ReturnExpression()
  58. {
  59. Clean();
  60. ExpressionMapper.CreateMap<ClassSource, ClassDest>().ForMember(s => s.PropString1, d => d.PropString2);
  61. ExpressionMapper.Initialize();
  62. var actual = ExpressionMapper.GetQueryExpression<ClassSource, ClassDest>();
  63. Assert.IsNotNull(actual);
  64. }
  65. [TestMethod, TestCategory("GetQuery")]
  66. public void GetQuery_ReturnFunc()
  67. {
  68. Init(null);
  69. ExpressionMapper.Initialize();
  70. var actual = ExpressionMapper.GetQuery<ClassSource, ClassDest>();
  71. Assert.IsNotNull(actual);
  72. }
  73. [TestMethod, TestCategory("Exception"), ExpectedException(typeof(NoFoundMapperException))]
  74. public void Map_NoFoundMapperException_Exception()
  75. {
  76. new ClassSource().Map<ClassSource, ClassDest2>();
  77. }
  78. [TestMethod, TestCategory("Exception"), ExpectedException(typeof(NoActionAfterMappingException))]
  79. public void Map_NoActionException_Exception()
  80. {
  81. ExpressionMapper.GetMapper<ClassSource, ClassDest>().AfterMap(null);
  82. ExpressionMapper.Initialize();
  83. new ClassSource().Map<ClassSource, ClassDest>();
  84. Clean();
  85. }
  86. [TestMethod]
  87. public void GetPropertiesNotMapped_ReturnProperties_Success()
  88. {
  89. ExpressionMapper.Initialize();
  90. var actual = ExpressionMapper.GetPropertiesNotMapped<ClassSource, ClassDest>();
  91. Assert.IsTrue(actual.SourceProperties.Count > 0);
  92. Assert.IsTrue(actual.TargetProperties.Count > 0);
  93. }
  94. }
  95. }