Skip to content

Commit

Permalink
Add test for collection aggregate mapping issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Croes committed Aug 8, 2023
1 parent fd47b84 commit bed7f89
Showing 1 changed file with 65 additions and 0 deletions.
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();
}
}

0 comments on commit bed7f89

Please sign in to comment.