Skip to content

Commit

Permalink
Implemented testing for TypeAdapter.ValidateAndAdapt
Browse files Browse the repository at this point in the history
  • Loading branch information
haritha99ch committed Oct 8, 2023
1 parent 7445200 commit eee8708
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/Mapster.Tests/WhenRequiresPropsValidation.cs
Original file line number Diff line number Diff line change
@@ -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<Product, ProductNestedDTO>();

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<Exception>(() => product.ValidateAndAdapt<Product, ProductDTO>());

ex.Message.ShouldContain(notExistingPropName);
ex.Message.ShouldContain(nameof(Product));
}
}
}
53 changes: 53 additions & 0 deletions src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs
Original file line number Diff line number Diff line change
@@ -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<Product, ProductDTO>.NewConfig()
.Map(dest => dest.CreatedUserName, src => $"{src.CreatedUser.Name} {src.CreatedUser.Surname}");

var dto = product.ValidateAndAdapt<Product, ProductDTO>(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<Product, ProductDTO>.NewConfig();

ProductDTO productDtoRef;
var notExistingPropName = nameof(productDtoRef.CreatedUserName);

var ex = Should.Throw<Exception>(() => product.ValidateAndAdapt<Product, ProductDTO>(adapterSettings.Config));

ex.Message.ShouldContain(notExistingPropName);
ex.Message.ShouldContain(nameof(Product));
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster/TypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static TDestination ValidateAndAdapt<TSource, TDestination>(this TSource
}

/// <summary>
/// Validate properties and Adapt the source object to the destination type.
/// Validate properties with configuration and Adapt the source object to the destination type.
/// </summary>
/// <typeparam name="TSource">Source type.</typeparam>
/// <typeparam name="TDestination">Destination type.</typeparam>
Expand Down

0 comments on commit eee8708

Please sign in to comment.