|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 8 | +{ |
| 9 | + public class CanMapExpressionWithListConstants |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void Map_expression_with_constant_array() |
| 13 | + { |
| 14 | + //Arrange |
| 15 | + var config = new MapperConfiguration |
| 16 | + ( |
| 17 | + cfg => |
| 18 | + { |
| 19 | + cfg.CreateMap<EntityModel, Entity>(); |
| 20 | + cfg.CreateMap<Entity, EntityModel>(); |
| 21 | + } |
| 22 | + ); |
| 23 | + config.AssertConfigurationIsValid(); |
| 24 | + var mapper = config.CreateMapper(); |
| 25 | + List<EntityModel> source1 = new() { |
| 26 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value3 } |
| 27 | + }; |
| 28 | + List<EntityModel> source2 = new() { |
| 29 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value1 } |
| 30 | + }; |
| 31 | + Entity[] entities = new Entity[] { new Entity { SimpleEnum = SimpleEnum.Value1 }, new Entity { SimpleEnum = SimpleEnum.Value2 } }; |
| 32 | + Expression<Func<Entity, bool>> filter = e => entities.Any(en => e.SimpleEnum == en.SimpleEnum); |
| 33 | + |
| 34 | + //act |
| 35 | + Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter); |
| 36 | + |
| 37 | + //assert |
| 38 | + Assert.False(source1.AsQueryable().Any(mappedFilter)); |
| 39 | + Assert.True(source2.AsQueryable().Any(mappedFilter)); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Map_expression_with_constant_list_using_generic_list_dot_contains() |
| 44 | + { |
| 45 | + //Arrange |
| 46 | + var config = new MapperConfiguration |
| 47 | + ( |
| 48 | + cfg => |
| 49 | + { |
| 50 | + cfg.CreateMap<EntityModel, Entity>(); |
| 51 | + cfg.CreateMap<SimpleEnum, SimpleEnumModel>(); |
| 52 | + } |
| 53 | + ); |
| 54 | + config.AssertConfigurationIsValid(); |
| 55 | + var mapper = config.CreateMapper(); |
| 56 | + List<EntityModel> source1 = new() { |
| 57 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value3 } |
| 58 | + }; |
| 59 | + List<EntityModel> source2 = new() { |
| 60 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value1 } |
| 61 | + }; |
| 62 | + List<SimpleEnum> enums = new() { SimpleEnum.Value1, SimpleEnum.Value2 }; |
| 63 | + Expression<Func<Entity, bool>> filter = e => enums.Contains(e.SimpleEnum); |
| 64 | + |
| 65 | + //act |
| 66 | + Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter); |
| 67 | + |
| 68 | + //assert |
| 69 | + Assert.False(source1.AsQueryable().Any(mappedFilter)); |
| 70 | + Assert.True(source2.AsQueryable().Any(mappedFilter)); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void Map_expression_with_constant_list_using_generic_enumerable_dot_contains() |
| 75 | + { |
| 76 | + //Arrange |
| 77 | + var config = new MapperConfiguration |
| 78 | + ( |
| 79 | + cfg => |
| 80 | + { |
| 81 | + cfg.CreateMap<EntityModel, Entity>(); |
| 82 | + cfg.CreateMap<SimpleEnum, SimpleEnumModel>(); |
| 83 | + } |
| 84 | + ); |
| 85 | + config.AssertConfigurationIsValid(); |
| 86 | + var mapper = config.CreateMapper(); |
| 87 | + List<EntityModel> source1 = new() { |
| 88 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value3 } |
| 89 | + }; |
| 90 | + List<EntityModel> source2 = new() { |
| 91 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value1 } |
| 92 | + }; |
| 93 | + List<SimpleEnum> enums = new() { SimpleEnum.Value1, SimpleEnum.Value2 }; |
| 94 | + Expression<Func<Entity, bool>> filter = e => Enumerable.Contains(enums, e.SimpleEnum); |
| 95 | + |
| 96 | + //act |
| 97 | + Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter); |
| 98 | + |
| 99 | + //assert |
| 100 | + Assert.False(source1.AsQueryable().Any(mappedFilter)); |
| 101 | + Assert.True(source2.AsQueryable().Any(mappedFilter)); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void Map_expression_with_constant_dictionary() |
| 106 | + { |
| 107 | + //Arrange |
| 108 | + var config = new MapperConfiguration |
| 109 | + ( |
| 110 | + cfg => |
| 111 | + { |
| 112 | + cfg.CreateMap<EntityModel, Entity>(); |
| 113 | + cfg.CreateMap<SimpleEnum, SimpleEnumModel>(); |
| 114 | + } |
| 115 | + ); |
| 116 | + config.AssertConfigurationIsValid(); |
| 117 | + var mapper = config.CreateMapper(); |
| 118 | + List<EntityModel> source1 = new() { |
| 119 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value3 } |
| 120 | + }; |
| 121 | + List<EntityModel> source2 = new() { |
| 122 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value1 } |
| 123 | + }; |
| 124 | + Dictionary<string, SimpleEnum> enumDictionary = new() { ["A"] = SimpleEnum.Value1, ["B"] = SimpleEnum.Value2 }; |
| 125 | + Expression<Func<Entity, bool>> filter = e => enumDictionary.Any(i => i.Value == e.SimpleEnum); |
| 126 | + |
| 127 | + //act |
| 128 | + Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter); |
| 129 | + |
| 130 | + //assert |
| 131 | + Assert.False(source1.AsQueryable().Any(mappedFilter)); |
| 132 | + Assert.True(source2.AsQueryable().Any(mappedFilter)); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void Map_expression_with_constant_dictionary_mapping_both_Key_and_value() |
| 137 | + { |
| 138 | + //Arrange |
| 139 | + var config = new MapperConfiguration |
| 140 | + ( |
| 141 | + cfg => |
| 142 | + { |
| 143 | + cfg.CreateMap<EntityModel, Entity>(); |
| 144 | + cfg.CreateMap<Entity, EntityModel>(); |
| 145 | + cfg.CreateMap<SimpleEnum, SimpleEnumModel>(); |
| 146 | + } |
| 147 | + ); |
| 148 | + config.AssertConfigurationIsValid(); |
| 149 | + var mapper = config.CreateMapper(); |
| 150 | + List<EntityModel> source1 = new() { |
| 151 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value3 } |
| 152 | + }; |
| 153 | + List<EntityModel> source2 = new() { |
| 154 | + new EntityModel { SimpleEnum = SimpleEnumModel.Value1 } |
| 155 | + }; |
| 156 | + Dictionary<SimpleEnum, Entity> enumDictionary = new() { [SimpleEnum.Value1] = new Entity { SimpleEnum = SimpleEnum.Value1 }, [SimpleEnum.Value2] = new Entity { SimpleEnum = SimpleEnum.Value2 } }; |
| 157 | + Expression<Func<Entity, bool>> filter = e => enumDictionary.Any(i => i.Key == e.SimpleEnum && i.Value.SimpleEnum == e.SimpleEnum); |
| 158 | + |
| 159 | + //act |
| 160 | + Expression<Func<EntityModel, bool>> mappedFilter = mapper.MapExpression<Expression<Func<EntityModel, bool>>>(filter); |
| 161 | + |
| 162 | + //assert |
| 163 | + Assert.False(source1.AsQueryable().Any(mappedFilter)); |
| 164 | + Assert.True(source2.AsQueryable().Any(mappedFilter)); |
| 165 | + } |
| 166 | + |
| 167 | + public enum SimpleEnum |
| 168 | + { |
| 169 | + Value1, |
| 170 | + Value2, |
| 171 | + Value3 |
| 172 | + } |
| 173 | + |
| 174 | + public record Entity |
| 175 | + { |
| 176 | + public int Id { get; init; } |
| 177 | + public SimpleEnum SimpleEnum { get; init; } |
| 178 | + } |
| 179 | + |
| 180 | + public enum SimpleEnumModel |
| 181 | + { |
| 182 | + Value1, |
| 183 | + Value2, |
| 184 | + Value3 |
| 185 | + } |
| 186 | + |
| 187 | + public record EntityModel |
| 188 | + { |
| 189 | + public int Id { get; init; } |
| 190 | + public SimpleEnumModel SimpleEnum { get; init; } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments