-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathCanMapExpressionWithListConstants.cs
More file actions
189 lines (170 loc) · 7.01 KB
/
CanMapExpressionWithListConstants.cs
File metadata and controls
189 lines (170 loc) · 7.01 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapExpressionWithListConstants
{
[Fact]
public void Map_expression_with_constant_array()
{
//Arrange
var config = ConfigurationHelper.GetMapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
cfg.CreateMap<Entity, EntityModel>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<EntityModel> source1 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value3 }
};
List<EntityModel> source2 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value1 }
};
Entity[] entities = new Entity[] { new Entity { SimpleEnum = SimpleEnum.Value1 }, new Entity { SimpleEnum = SimpleEnum.Value2 } };
Expression<Func<Entity, bool>> filter = e => entities.Any(en => e.SimpleEnum == en.SimpleEnum);
//act
Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter);
//assert
Assert.False(source1.AsQueryable().Any(mappedFilter));
Assert.True(source2.AsQueryable().Any(mappedFilter));
}
[Fact]
public void Map_expression_with_constant_list_using_generic_list_dot_contains()
{
//Arrange
var config = ConfigurationHelper.GetMapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<EntityModel> source1 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value3 }
};
List<EntityModel> source2 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value1 }
};
List<SimpleEnum> enums = new() { SimpleEnum.Value1, SimpleEnum.Value2 };
Expression<Func<Entity, bool>> filter = e => enums.Contains(e.SimpleEnum);
//act
Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter);
//assert
Assert.False(source1.AsQueryable().Any(mappedFilter));
Assert.True(source2.AsQueryable().Any(mappedFilter));
}
[Fact]
public void Map_expression_with_constant_list_using_generic_enumerable_dot_contains()
{
//Arrange
var config = ConfigurationHelper.GetMapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<EntityModel> source1 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value3 }
};
List<EntityModel> source2 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value1 }
};
List<SimpleEnum> enums = new() { SimpleEnum.Value1, SimpleEnum.Value2 };
Expression<Func<Entity, bool>> filter = e => Enumerable.Contains(enums, e.SimpleEnum);
//act
Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter);
//assert
Assert.False(source1.AsQueryable().Any(mappedFilter));
Assert.True(source2.AsQueryable().Any(mappedFilter));
}
[Fact]
public void Map_expression_with_constant_dictionary()
{
//Arrange
var config = ConfigurationHelper.GetMapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<EntityModel> source1 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value3 }
};
List<EntityModel> source2 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value1 }
};
Dictionary<string, SimpleEnum> enumDictionary = new() { ["A"] = SimpleEnum.Value1, ["B"] = SimpleEnum.Value2 };
Expression<Func<Entity, bool>> filter = e => enumDictionary.Any(i => i.Value == e.SimpleEnum);
//act
Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter);
//assert
Assert.False(source1.AsQueryable().Any(mappedFilter));
Assert.True(source2.AsQueryable().Any(mappedFilter));
}
[Fact]
public void Map_expression_with_constant_dictionary_mapping_both_Key_and_value()
{
//Arrange
var config = ConfigurationHelper.GetMapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
cfg.CreateMap<Entity, EntityModel>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<EntityModel> source1 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value3 }
};
List<EntityModel> source2 = new() {
new EntityModel { SimpleEnum = SimpleEnumModel.Value1 }
};
Dictionary<SimpleEnum, Entity> enumDictionary = new() { [SimpleEnum.Value1] = new Entity { SimpleEnum = SimpleEnum.Value1 }, [SimpleEnum.Value2] = new Entity { SimpleEnum = SimpleEnum.Value2 } };
Expression<Func<Entity, bool>> filter = e => enumDictionary.Any(i => i.Key == e.SimpleEnum && i.Value.SimpleEnum == e.SimpleEnum);
//act
Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter);
//assert
Assert.False(source1.AsQueryable().Any(mappedFilter));
Assert.True(source2.AsQueryable().Any(mappedFilter));
}
public enum SimpleEnum
{
Value1,
Value2,
Value3
}
public record Entity
{
public int Id { get; set; }
public SimpleEnum SimpleEnum { get; set; }
}
public enum SimpleEnumModel
{
Value1,
Value2,
Value3
}
public record EntityModel
{
public int Id { get; set; }
public SimpleEnumModel SimpleEnum { get; set; }
}
}
}