Last active
August 29, 2015 14:19
-
-
Save kmoormann/b3949d006f4083ab6ee4 to your computer and use it in GitHub Desktop.
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
using System.Collections.Generic; | |
using System.Linq; | |
using AutoMapper; | |
using AutoMapper.QueryableExtensions; | |
using FluentAssertions; | |
using NUnit.Framework; | |
namespace Automapper.PolymorphicList.Tests | |
{ | |
[TestFixture] | |
class AutomapperQueryableExtensionPolymorphism | |
{ | |
//taking the class structure from: https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance | |
public class Order { } | |
public class OnlineOrder : Order | |
{ | |
public string Referrer { get; set; } | |
} | |
public class MailOrder : Order { } | |
//Dtos | |
public class OrderDto | |
{ | |
public string Referrer { get; set; } | |
} | |
[Test(Description = "Does the same mapping behavior exist for a polymorphic list when doing the project querable extension as when doing the static mapper map method()")] | |
public void IsSameBehaviorForQueryableExtensionAndStaticMap() | |
{ | |
Mapper.Reset(); | |
//Mappings | |
Mapper.CreateMap<Order, OrderDto>() | |
.Include<OnlineOrder, OrderDto>() | |
.Include<MailOrder, OrderDto>() | |
.ForMember(o => o.Referrer, m => m.Ignore()); | |
Mapper.CreateMap<OnlineOrder, OrderDto>(); | |
Mapper.CreateMap<MailOrder, OrderDto>(); | |
//build lists | |
var onlineOrders = new List<OnlineOrder>() { new OnlineOrder() { Referrer = "one" }, new OnlineOrder() { Referrer = "two" } }; | |
var mailOrders = new List<MailOrder>() { new MailOrder() }; | |
//single typed list mapping | |
var mappedOnlineOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(onlineOrders); | |
var projectedOnlineOrderDtos = onlineOrders.AsQueryable().Project().To<OrderDto>(); | |
//using FluentAssertions for collection assertions | |
projectedOnlineOrderDtos.ShouldBeEquivalentTo(mappedOnlineOrderDtos, "automapper can handle singly typed lists"); | |
//other single typed list mapping | |
var mappedMailOrderDtos = Mapper.Map<IEnumerable<OrderDto>>(mailOrders); | |
var projectedMailOrderDtos = mailOrders.AsQueryable().Project().To<OrderDto>(); | |
projectedMailOrderDtos.ShouldBeEquivalentTo(mappedMailOrderDtos, "automapper can handle singly typed lists"); | |
//build a polymorphic list | |
var orders = new List<Order>(); | |
orders.AddRange(onlineOrders); | |
orders.AddRange(mailOrders); | |
// Perform Mapping and Projection | |
var mappedPolymorhpicOrders = Mapper.Map<IEnumerable<OrderDto>>(orders); | |
var projectedPolymorphicOrders = orders.AsQueryable().Project().To<OrderDto>(); | |
projectedPolymorphicOrders.ShouldBeEquivalentTo(mappedPolymorhpicOrders, "automapper can handle polymorphic typed lists?"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment