-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from BlaiseD/master
Fix for Issue 33: Type Binary and Conversion expressions are not being mapped as expected.
- Loading branch information
Showing
5 changed files
with
154 additions
and
4 deletions.
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
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
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
92 changes: 92 additions & 0 deletions
92
...Mapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.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,92 @@ | ||
using Shouldly; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests | ||
{ | ||
public class ExpressionMappingPropertyFromDerviedType : AutoMapperSpecBase | ||
{ | ||
private List<BaseEntity> _source; | ||
private IQueryable<BaseEntity> entityQuery; | ||
|
||
public class BaseDTO | ||
{ | ||
public Guid Id { get; set; } | ||
} | ||
|
||
public class BaseEntity | ||
{ | ||
public Guid Id { get; set; } | ||
} | ||
|
||
public class DTO : BaseDTO | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
} | ||
|
||
public class Entity : BaseEntity | ||
{ | ||
public string Name { get; set; } | ||
} | ||
|
||
protected override MapperConfiguration Configuration | ||
{ | ||
get | ||
{ | ||
var config = new MapperConfiguration(cfg => | ||
{ | ||
cfg.AddExpressionMapping(); | ||
|
||
cfg.CreateMap<BaseEntity, BaseDTO>(); | ||
cfg.CreateMap<BaseDTO, BaseEntity>(); | ||
|
||
cfg.CreateMap<Entity, DTO>() | ||
.ForMember(dest => dest.Description, opts => opts.MapFrom(src => string.Concat(src.Id.ToString(), " - ", src.Name))) | ||
.IncludeBase<BaseEntity, BaseDTO>(); | ||
cfg.CreateMap<DTO, Entity>() | ||
.IncludeBase<BaseDTO, BaseEntity>(); | ||
}); | ||
return config; | ||
} | ||
} | ||
|
||
protected override void Because_of() | ||
{ | ||
//Arrange | ||
_source = new List<BaseEntity> { | ||
new Entity { Id = Guid.NewGuid(), Name = "Sofia" }, | ||
new Entity { Id = Guid.NewGuid(), Name = "Rafael" }, | ||
new BaseEntity { Id = Guid.NewGuid() } | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Should_support_propertypath_expressions_with_properties_from_sub_types_using_explicit_cast() | ||
{ | ||
// Act | ||
Expression<Func<BaseDTO, bool>> dtoQueryExpression = r => (r is DTO ? ((DTO)r).Name : "") == "Sofia"; | ||
Expression<Func<BaseEntity, bool>> entityQueryExpression = Mapper.MapExpression<Expression<Func<BaseEntity, bool>>>(dtoQueryExpression); | ||
entityQuery = _source.AsQueryable().Where(entityQueryExpression); | ||
|
||
// Assert | ||
entityQuery.ToList().Count().ShouldBe(1); | ||
} | ||
|
||
[Fact] | ||
public void Should_support_propertypath_expressions_with_properties_from_sub_types_using_as_keyword() | ||
{ | ||
// Act | ||
Expression<Func<BaseDTO, bool>> dtoQueryExpression = r => (r is DTO ? (r as DTO).Name : "") == "Sofia"; | ||
Expression<Func<BaseEntity, bool>> entityQueryExpression = Mapper.MapExpression<Expression<Func<BaseEntity, bool>>>(dtoQueryExpression); | ||
entityQuery = _source.AsQueryable().Where(entityQueryExpression); | ||
|
||
// Assert | ||
entityQuery.ToList().Count().ShouldBe(1); | ||
} | ||
} | ||
} |
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