|
| 1 | +namespace AgileObjects.AgileMapper.UnitTests.Configuration |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Linq; |
| 6 | + using AgileMapper.Configuration; |
| 7 | + using Shouldly; |
| 8 | + using TestClasses; |
| 9 | + using Xunit; |
| 10 | + |
| 11 | + public class WhenMappingToNull |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void ShouldApplyAUserConfiguredCondition() |
| 15 | + { |
| 16 | + using (var mapper = Mapper.CreateNew()) |
| 17 | + { |
| 18 | + mapper.WhenMapping |
| 19 | + .To<Address>() |
| 20 | + .If((o, a) => string.IsNullOrWhiteSpace(a.Line1)) |
| 21 | + .MapToNull(); |
| 22 | + |
| 23 | + var source = new CustomerViewModel { Name = "Bob" }; |
| 24 | + var result = mapper.Map(source).ToANew<Customer>(); |
| 25 | + |
| 26 | + result.Address.ShouldBeNull(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void ShouldRestrictAConfiguredMapToNullConditionBySourceType() |
| 32 | + { |
| 33 | + using (var mapper = Mapper.CreateNew()) |
| 34 | + { |
| 35 | + mapper.WhenMapping |
| 36 | + .From<CustomerViewModel>() |
| 37 | + .To<Address>() |
| 38 | + .If((o, a) => a.Line1 == "Null!") |
| 39 | + .MapToNull(); |
| 40 | + |
| 41 | + var nonMatchingSource = new Customer |
| 42 | + { |
| 43 | + Name = "Jen", |
| 44 | + Address = new Address { Line1 = "Null!" } |
| 45 | + }; |
| 46 | + var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew<Customer>(); |
| 47 | + |
| 48 | + nonMatchingResult.Address.ShouldNotBeNull(); |
| 49 | + nonMatchingResult.Address.Line1.ShouldBe("Null!"); |
| 50 | + |
| 51 | + var matchingSource = new CustomerViewModel |
| 52 | + { |
| 53 | + Name = "Frank", |
| 54 | + AddressLine1 = "Null!" |
| 55 | + }; |
| 56 | + var matchingResult = mapper.Map(matchingSource).ToANew<Customer>(); |
| 57 | + |
| 58 | + matchingResult.Address.ShouldBeNull(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void ShouldOverwriteAPropertyToNull() |
| 64 | + { |
| 65 | + using (var mapper = Mapper.CreateNew()) |
| 66 | + { |
| 67 | + mapper.WhenMapping |
| 68 | + .Over<Address>() |
| 69 | + .If(ctx => ctx.Target.Line1 == null) |
| 70 | + .MapToNull(); |
| 71 | + |
| 72 | + var nonMatchingSource = new CustomerViewModel |
| 73 | + { |
| 74 | + Id = Guid.NewGuid(), |
| 75 | + AddressLine1 = "Places!" |
| 76 | + }; |
| 77 | + var target = new Customer { Address = new Address { Line1 = "Home!" } }; |
| 78 | + |
| 79 | + mapper.Map(nonMatchingSource).Over(target); |
| 80 | + |
| 81 | + target.Address.ShouldNotBeNull(); |
| 82 | + target.Address.Line1.ShouldBe("Places!"); |
| 83 | + |
| 84 | + var matchingSource = new CustomerViewModel { Id = Guid.NewGuid() }; |
| 85 | + |
| 86 | + mapper.Map(matchingSource).Over(target); |
| 87 | + |
| 88 | + target.Address.ShouldBeNull(); |
| 89 | + |
| 90 | + var nullAddressTarget = new Customer(); |
| 91 | + |
| 92 | + mapper.Map(matchingSource).Over(nullAddressTarget); |
| 93 | + |
| 94 | + target.Address.ShouldBeNull(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void ShouldApplyConfiguredConditionsToDerivedTypes() |
| 100 | + { |
| 101 | + using (var mapper = Mapper.CreateNew()) |
| 102 | + { |
| 103 | + mapper.WhenMapping |
| 104 | + .To<Product>() |
| 105 | + .If((o, p) => p.Price.Equals(0)) |
| 106 | + .MapToNull(); |
| 107 | + |
| 108 | + mapper.WhenMapping |
| 109 | + .To<MegaProduct>() |
| 110 | + .If((o, p) => p.HowMega == 0) |
| 111 | + .MapToNull(); |
| 112 | + |
| 113 | + var nonMatchingProductSource = new { Price = 123 }; |
| 114 | + var nonMatchingProductResult = mapper.Map(nonMatchingProductSource).ToANew<Product>(); |
| 115 | + |
| 116 | + nonMatchingProductResult.Price.ShouldBe(123); |
| 117 | + |
| 118 | + var matchingProductSource = new { Price = 0 }; |
| 119 | + var matchingProductResult = mapper.Map(matchingProductSource).ToANew<Product>(); |
| 120 | + |
| 121 | + matchingProductResult.ShouldBeNull(); |
| 122 | + |
| 123 | + var nonMatchingMegaProductSource = new { HowMega = 0.99 }; |
| 124 | + var nonMatchingMegaProductResult = mapper.Map(nonMatchingMegaProductSource).ToANew<MegaProduct>(); |
| 125 | + |
| 126 | + nonMatchingMegaProductResult.HowMega.ShouldBe(0.99); |
| 127 | + |
| 128 | + var matchingMegaProductSource = new { HowMega = 0.00 }; |
| 129 | + var matchingMegaProductResult = mapper.Map(matchingMegaProductSource).ToANew<MegaProduct>(); |
| 130 | + |
| 131 | + matchingMegaProductResult.ShouldBeNull(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void ShouldNotMapCollectionElementsToNull() |
| 137 | + { |
| 138 | + using (var mapper = Mapper.CreateNew()) |
| 139 | + { |
| 140 | + mapper.WhenMapping |
| 141 | + .To<Address>() |
| 142 | + .If((o, a) => a.Line2 == "Delete me") |
| 143 | + .MapToNull(); |
| 144 | + |
| 145 | + var source = new[] |
| 146 | + { |
| 147 | + new Address { Line1 = "Delete me" }, |
| 148 | + new Address { Line2 = "Delete me" } |
| 149 | + }; |
| 150 | + var result = mapper.Map(source).ToANew<ICollection<Address>>(); |
| 151 | + |
| 152 | + result.First().ShouldNotBeNull(); |
| 153 | + result.First().Line1.ShouldBe("Delete me"); |
| 154 | + |
| 155 | + result.Second().ShouldNotBeNull(); |
| 156 | + result.Second().Line2.ShouldBe("Delete me"); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public void ShouldMapCollectionElementNestedPropertiesToNull() |
| 162 | + { |
| 163 | + using (var mapper = Mapper.CreateNew()) |
| 164 | + { |
| 165 | + mapper.WhenMapping |
| 166 | + .To<Address>() |
| 167 | + .If((o, a) => a.Line1 == null) |
| 168 | + .MapToNull(); |
| 169 | + |
| 170 | + var source = new[] |
| 171 | + { |
| 172 | + new CustomerViewModel { AddressLine1 = null }, |
| 173 | + new CustomerViewModel { AddressLine1 = "Hello!" } |
| 174 | + }; |
| 175 | + var result = mapper.Map(source).ToANew<IEnumerable<MysteryCustomer>>(); |
| 176 | + |
| 177 | + result.First().Address.ShouldBeNull(); |
| 178 | + result.Second().Address.ShouldNotBeNull(); |
| 179 | + result.Second().Address.Line1.ShouldBe("Hello!"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void ShouldErrorIfConditionsAreConfiguredForTheSameType() |
| 185 | + { |
| 186 | + var configEx = Should.Throw<MappingConfigurationException>(() => |
| 187 | + { |
| 188 | + using (var mapper = Mapper.CreateNew()) |
| 189 | + { |
| 190 | + mapper.WhenMapping |
| 191 | + .To<Address>() |
| 192 | + .If((o, a) => a.Line1 == null) |
| 193 | + .MapToNull(); |
| 194 | + |
| 195 | + mapper.WhenMapping |
| 196 | + .To<Address>() |
| 197 | + .If((o, a) => a.Line1 == string.Empty) |
| 198 | + .MapToNull(); |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + configEx.Message.ShouldContain("already has"); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments