Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for collection aggregate mapping issue #171

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading