-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathMemberMappingsOfLiteralParentTypesMustMatch.cs
More file actions
114 lines (97 loc) · 4.38 KB
/
Copy pathMemberMappingsOfLiteralParentTypesMustMatch.cs
File metadata and controls
114 lines (97 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Linq.Expressions;
using Xunit;
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class MemberMappingsOfLiteralParentTypesMustMatch
{
[Fact]
public void MappingMemberOfNullableParentToMemberOfNonNullableParentWithoutCustomExpressionsThrowsException()
{
//arrange
var mapper = GetMapper();
Expression<Func<ProductModel, bool>> expression = x => x.DateTimeOffset.HasValue && x.DateTimeOffset.Value.Day.ToString() == "2";
//act
var exception = Assert.Throws<InvalidOperationException>(() => mapper.MapExpression<Expression<Func<Product, bool>>>(expression));
//assert
Assert.StartsWith
(
"For members of literal types, use IMappingExpression.ForMember() to make the parent property types an exact match.",
exception.Message
);
}
[Fact]
public void MappingMemberOfNonNullableParentToMemberOfNullableParentWithoutCustomExpressionsThrowsException()
{
//arrange
var mapper = GetMapper();
Expression<Func<ProductModel, bool>> expression = x => x.DateTime.Day.ToString() == "2";
//act
var exception = Assert.Throws<InvalidOperationException>(() => mapper.MapExpression<Expression<Func<Product, bool>>>(expression));
//assert
Assert.StartsWith
(
"For members of literal types, use IMappingExpression.ForMember() to make the parent property types an exact match.",
exception.Message
);
}
[Fact]
public void MappingMemberOfNullableParentToMemberOfNonNullableParentWorksUsingCustomExpressions()
{
//arrange
var mapper = GetMapperWithCustomExpressions();
Expression<Func<ProductModel, bool>> expression = x => x.DateTimeOffset.HasValue && x.DateTimeOffset.Value.Day.ToString() == "2";
//act
var mappedExpression = mapper.MapExpression<Expression<Func<Product, bool>>>(expression);
//assert
Assert.NotNull(mappedExpression);
Func<Product, bool> func = mappedExpression.Compile();
Assert.False(func(new Product { DateTimeOffset = new DateTimeOffset(new DateTime(2000, 3, 3), TimeSpan.Zero) }));
Assert.True(func(new Product { DateTimeOffset = new DateTimeOffset(new DateTime(2000, 2, 2), TimeSpan.Zero) }));
}
[Fact]
public void MappingMemberOfNonNullableParentToMemberOfNullableParentWorksUsingCustomExpressions()
{
//arrange
var mapper = GetMapperWithCustomExpressions();
Expression<Func<ProductModel, bool>> expression = x => x.DateTime.Day.ToString() == "2";
//act
var mappedExpression = mapper.MapExpression<Expression<Func<Product, bool>>>(expression);
//assert
Assert.NotNull(mappedExpression);
Func<Product, bool> func = mappedExpression.Compile();
Assert.False(func(new Product { DateTime = new DateTime(2000, 3, 3) }));
Assert.True(func(new Product { DateTime = new DateTime(2000, 2, 2) }));
}
private static IMapper GetMapper()
{
var config = ConfigurationHelper.GetMapperConfiguration(c =>
{
c.CreateMap<Product, ProductModel>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
private static IMapper GetMapperWithCustomExpressions()
{
var config = ConfigurationHelper.GetMapperConfiguration(c =>
{
c.CreateMap<Product, ProductModel>()
.ForMember(d => d.DateTime, o => o.MapFrom(s => s.DateTime ?? default))
.ForMember(d => d.DateTimeOffset, o => o.MapFrom(s => (DateTimeOffset?)s.DateTimeOffset));
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
class Product
{
public DateTime? DateTime { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
}
class ProductModel
{
public DateTime DateTime { get; set; }
public DateTimeOffset? DateTimeOffset { get; set; }
}
}
}