-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented testing for TypeAdapter.ValidateAndAdapt
- Loading branch information
1 parent
7445200
commit eee8708
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
src/Mapster.Tests/WhenRequiresPropsValidationWithAdapterConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters