-
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.
Add test for collection aggregate mapping issue
- Loading branch information
Michael Croes
committed
Aug 8, 2023
1 parent
fd47b84
commit bed7f89
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/MappingCollectionAggregateTests.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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using AutoMapper.Execution; | ||
using AutoMapper.Internal; | ||
using Xunit; | ||
|
||
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests; | ||
|
||
public class MappingCollectionAggregateTests : AutoMapperSpecBase | ||
{ | ||
private IQueryable<Source> _sources; | ||
|
||
public class Source | ||
{ | ||
public ICollection<Item> Items { get; set; } = new List<Item> { new Item { Timestamp = DateTime.Now } }; | ||
} | ||
|
||
public class Item | ||
{ | ||
public DateTime Timestamp { get; set; } | ||
} | ||
|
||
public class Dest | ||
{ | ||
public DateTime? ItemsTimestampMax { get; set; } | ||
} | ||
|
||
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => | ||
{ | ||
cfg.CreateMap<Source, Dest>() | ||
.ForMember(dst => dst.ItemsTimestampMax, opt => opt.MapFrom(src => src.Items.Max(x => x.Timestamp))); | ||
cfg.Internal().ForAllPropertyMaps( | ||
p => p.SourceType == typeof(DateTimeOffset) && p.DestinationType == typeof(DateTimeOffset?) && | ||
p.CustomMapExpression != null, (pMap, _) => | ||
{ | ||
var resolver = new ExpressionResolver(Expression.Lambda( | ||
Expression.Convert(pMap.CustomMapExpression.Body, typeof(DateTimeOffset?)), | ||
pMap.CustomMapExpression.Parameters)); | ||
pMap.SetResolver(resolver); | ||
}); | ||
}); | ||
|
||
protected override void Because_of() | ||
{ | ||
_sources = new[] { new Source() }.AsQueryable(); | ||
} | ||
|
||
[Fact] | ||
public void Maps_Date_Filter() | ||
{ | ||
_sources.UseAsDataSource(Configuration).For<Dest>() | ||
.Where(d => d.ItemsTimestampMax > DateTime.Today).ToList(); | ||
} | ||
|
||
[Fact] | ||
public void Maps_Date_Value_Filter() | ||
{ | ||
_sources.UseAsDataSource(Configuration).For<Dest>() | ||
.Where(d => d.ItemsTimestampMax.Value.Date == DateTime.Today).ToList(); | ||
} | ||
} |