From eee8708909c056431955ede2a789ba55100a170b Mon Sep 17 00:00:00 2001 From: Haritha Rathnayake Date: Sun, 8 Oct 2023 19:03:42 +0530 Subject: [PATCH] Implemented testing for TypeAdapter.ValidateAndAdapt --- .../WhenRequiresPropsValidation.cs | 48 +++++++++++++++++ ...equiresPropsValidationWithAdapterConfig.cs | 53 +++++++++++++++++++ src/Mapster/TypeAdapter.cs | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/Mapster.Tests/WhenRequiresPropsValidation.cs create mode 100644 src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs diff --git a/src/Mapster.Tests/WhenRequiresPropsValidation.cs b/src/Mapster.Tests/WhenRequiresPropsValidation.cs new file mode 100644 index 00000000..0652df6d --- /dev/null +++ b/src/Mapster.Tests/WhenRequiresPropsValidation.cs @@ -0,0 +1,48 @@ +using System; +using Mapster.Tests.Classes; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace Mapster.Tests +{ + [TestClass] + public class WhenRequiresPropsValidation + { + [TestInitialize] + public void Setup() + { + TypeAdapterConfig.GlobalSettings.Clear(); + } + + [TestCleanup] + public void TestCleanup() + { + TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.Exact); + } + + [TestMethod] + public void DestinationProps_Exist_In_Source() + { + var product = new Product {Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User {Name = "UserA"}}; + + var dto = product.ValidateAndAdapt(); + + dto.ShouldNotBeNull(); + dto.Id.ShouldBe(product.Id); + } + + [TestMethod] + public void DestinationProps_Not_Exist_In_Source() + { + var product = new Product {Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User {Name = "UserA"}}; + + ProductDTO productDtoRef; + var notExistingPropName = nameof(productDtoRef.CreatedUserName); + + var ex = Should.Throw(() => product.ValidateAndAdapt()); + + ex.Message.ShouldContain(notExistingPropName); + ex.Message.ShouldContain(nameof(Product)); + } + } +} diff --git a/src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs b/src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs new file mode 100644 index 00000000..eb845416 --- /dev/null +++ b/src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs @@ -0,0 +1,53 @@ +using System; +using Mapster.Tests.Classes; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace Mapster.Tests +{ + [TestClass] + public class WhenRequiresPropsValidationWithAdapterConfig + { + [TestInitialize] + public void Setup() + { + TypeAdapterConfig.GlobalSettings.Clear(); + } + + [TestCleanup] + public void TestCleanup() + { + TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.Exact); + } + + [TestMethod] + public void DestinationProps_Not_Exist_In_Source_But_Configured() + { + var product = new Product {Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User {Name = "UserA"}}; + + var adapterSettings = TypeAdapterConfig.NewConfig() + .Map(dest => dest.CreatedUserName, src => $"{src.CreatedUser.Name} {src.CreatedUser.Surname}"); + + var dto = product.ValidateAndAdapt(adapterSettings.Config); + + dto.ShouldNotBeNull(); + dto.CreatedUserName.ShouldBe($"{product.CreatedUser.Name} {product.CreatedUser.Surname}"); + } + + [TestMethod] + public void DestinationProps_Not_Exist_In_Source_And_MisConfigured() + { + var product = new Product {Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User {Name = "UserA"}}; + + var adapterSettings = TypeAdapterConfig.NewConfig(); + + ProductDTO productDtoRef; + var notExistingPropName = nameof(productDtoRef.CreatedUserName); + + var ex = Should.Throw(() => product.ValidateAndAdapt(adapterSettings.Config)); + + ex.Message.ShouldContain(notExistingPropName); + ex.Message.ShouldContain(nameof(Product)); + } + } +} diff --git a/src/Mapster/TypeAdapter.cs b/src/Mapster/TypeAdapter.cs index ad2d0c1b..9758fd31 100644 --- a/src/Mapster/TypeAdapter.cs +++ b/src/Mapster/TypeAdapter.cs @@ -198,7 +198,7 @@ public static TDestination ValidateAndAdapt(this TSource } /// - /// Validate properties and Adapt the source object to the destination type. + /// Validate properties with configuration and Adapt the source object to the destination type. /// /// Source type. /// Destination type.