Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions AgileMapper.UnitTests.Orms.EfCore2/WhenMappingOverEnumerables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ await writeContext.Persons.AddRangeAsync(

var localPersons = readContext.Persons.Local;

localPersons.Count.ShouldBe(4);
localPersons.First().Name.ShouldBe("One");
localPersons.Second().Name.ShouldBe("Two");
localPersons.Third().Name.ShouldBe("Three");
localPersons.Fourth().Name.ShouldBe("Four");
var orderedPersons = localPersons.OrderBy(p => p.PersonId).ToArray();

orderedPersons.Length.ShouldBe(4);
orderedPersons.First().Name.ShouldBe("One");
orderedPersons.Second().Name.ShouldBe("Two");
orderedPersons.Third().Name.ShouldBe("Three");
orderedPersons.Fourth().Name.ShouldBe("Four");

mapper.WhenMapping.InstancesOf<Person>().IdentifyUsing(p => p.Name);

Expand All @@ -55,24 +57,26 @@ await writeContext.Persons.AddRangeAsync(

await readContext.SaveChangesAsync();

localPersons.Count.ShouldBe(4);
localPersons.First().Name.ShouldBe("One");
localPersons.First().Address.ShouldBeNull();
orderedPersons = localPersons.OrderBy(p => p.PersonId).ToArray();

orderedPersons.Length.ShouldBe(4);
orderedPersons.First().Name.ShouldBe("One");
orderedPersons.First().Address.ShouldBeNull();

localPersons.Second().Name.ShouldBe("Two");
localPersons.Second().Address.ShouldNotBeNull();
localPersons.Second().Address.Line1.ShouldBe("Two Line 1");
localPersons.Second().Address.Line2.ShouldBeNull();
orderedPersons.Second().Name.ShouldBe("Two");
orderedPersons.Second().Address.ShouldNotBeNull();
orderedPersons.Second().Address.Line1.ShouldBe("Two Line 1");
orderedPersons.Second().Address.Line2.ShouldBeNull();

localPersons.Third().Name.ShouldBe("Three");
localPersons.Third().Address.ShouldNotBeNull();
localPersons.Third().Address.Line1.ShouldBe("Three Line 1");
localPersons.Third().Address.Line2.ShouldBe("Three Line 2");
orderedPersons.Third().Name.ShouldBe("Three");
orderedPersons.Third().Address.ShouldNotBeNull();
orderedPersons.Third().Address.Line1.ShouldBe("Three Line 1");
orderedPersons.Third().Address.Line2.ShouldBe("Three Line 2");

localPersons.Fourth().Name.ShouldBe("Five");
localPersons.Fourth().Address.ShouldNotBeNull();
localPersons.Fourth().Address.Line1.ShouldBe("Five Line 1");
localPersons.Fourth().Address.Line2.ShouldBeNull();
orderedPersons.Fourth().Name.ShouldBe("Five");
orderedPersons.Fourth().Address.ShouldNotBeNull();
orderedPersons.Fourth().Address.Line1.ShouldBe("Five Line 1");
orderedPersons.Fourth().Address.Line2.ShouldBeNull();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AgileMapper.Configuration;
using Common;
using TestClasses;
#if !NET35
Expand Down Expand Up @@ -81,5 +82,141 @@ public void ShouldExtendConfiguredIdentifiersInline()
mapper.InlineContexts().ShouldHaveSingleItem();
}
}

[Fact]
public void ShouldUseACompositeIdentifierInline()
{
using (var mapper = Mapper.CreateNew())
{
var source = new[]
{
new WeddingDto
{
BrideName = "Nat",
GroomName = "Andy",
BrideAddressLine1 = "Nat + Andy's House",
GroomAddressLine1 = "Nat + Andy's House"
},
new WeddingDto
{
BrideName = "Timea",
GroomName = "David",
BrideAddressLine1 = "Timea + David's House",
GroomAddressLine1 = "Timea + David's House"
}
};

var target = new List<WeddingDto>
{
new WeddingDto
{
BrideName = "Nat",
GroomName = "Andy"
},
new WeddingDto
{
BrideName = "Kate",
GroomName = "Steve"
}
};

mapper.Map(source).OnTo(target, cfg => cfg
.WhenMapping
.InstancesOf<WeddingDto>()
.IdentifyUsing(a => a.BrideName, a => a.GroomName));

target.Count.ShouldBe(3);

target.First().BrideName.ShouldBe("Nat");
target.First().GroomName.ShouldBe("Andy");
target.First().BrideAddressLine1.ShouldBe("Nat + Andy's House");
target.First().GroomAddressLine1.ShouldBe("Nat + Andy's House");

target.Second().BrideName.ShouldBe("Kate");
target.Second().GroomName.ShouldBe("Steve");
target.Second().BrideAddressLine1.ShouldBeNull();
target.Second().GroomAddressLine1.ShouldBeNull();

target.Third().BrideName.ShouldBe("Timea");
target.Third().GroomName.ShouldBe("David");
target.Third().BrideAddressLine1.ShouldBe("Timea + David's House");
target.Third().GroomAddressLine1.ShouldBe("Timea + David's House");
}
}

[Fact]
public void ShouldUseConfiguredIdentifierInCompositeIdentifierInline()
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.InstancesOf<PublicTwoFields<int, int>>()
.IdentifyUsing(ptf => ptf.Value1);

var source = new[]
{
new PublicTwoFields<object, PublicTwoFields<int, int>>
{
Value1 = 111,
Value2 = new PublicTwoFields<int, int> { Value1 = 222, Value2 = 333 }
},
new PublicTwoFields<object, PublicTwoFields<int, int>>
{
Value1 = null,
Value2 = null
},
};

var target = new List<PublicTwoFields<object, PublicTwoFields<int, int>>>
{
new PublicTwoFields<object, PublicTwoFields<int, int>>
{
Value1 = 111,
Value2 = new PublicTwoFields<int, int> { Value1 = 222 }
}
};

var itemOne = target.First();

mapper.Map(source).OnTo(target, cfg => cfg
.WhenMapping
.InstancesOf<PublicTwoFields<object, PublicTwoFields<int, int>>>()
.IdentifyUsing(ptf => ptf.Value1, ptf => ptf.Value2));

target.Count.ShouldBe(2);

target.First().ShouldBeSameAs(itemOne);
target.First().Value1.ShouldBe(111);
target.First().Value2.ShouldNotBeNull();
target.First().Value2.Value1.ShouldBe(222);
target.First().Value2.Value2.ShouldBe(333);

target.Second().Value1.ShouldBeNull();
}
}

[Fact]
public void ShouldErrorIfUnidentifiableComplexTypeIdentifierSuppliedInline()
{
var idsEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
var source = new[]
{
new PublicTwoFields<int, PublicField<int>>()
};

mapper.Map(source).Over(new List<PublicTwoFields<int, PublicField<int>>>(1), cfg => cfg
.WhenMapping
.InstancesOf<PublicTwoFields<int, PublicField<int>>>()
.IdentifyUsing(ptf => ptf.Value1, ptf => ptf.Value2));
}
});

idsEx.Message.ShouldContain("Unable to determine identifier");
idsEx.Message.ShouldContain("ptf.Value2 of Type 'PublicField<int>'");
idsEx.InnerException.ShouldBeOfType<ArgumentException>();
}
}
}
122 changes: 106 additions & 16 deletions AgileMapper.UnitTests/Configuration/WhenConfiguringTypeIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,129 @@ public void ShouldUseAConfiguredIdentifierExpression()
}

[Fact]
public void ShouldErrorIfRedundantIdentifierSpecified()
public void ShouldUseACompositeIdentifierWithAnEntityKey()
{
using (var mapper = Mapper.CreateNew())
{
var idEx = Should.Throw<MappingConfigurationException>(() =>
mapper.WhenMapping
.InstancesOf<Person>()
.IdentifyUsing(p => p.Id));
mapper.WhenMapping
.InstancesOf<PublicTwoFields<int, Product>>()
.IdentifyUsing(ptf => ptf.Value1, ptf => ptf.Value2);

var source = new[]
{
new PublicTwoFields<int, Product>
{
Value1 = 123,
Value2 = new Product { ProductId = "321", Price = 99.99 }
},
new PublicTwoFields<int, Product>
{
Value1 = 456,
Value2 = new Product { ProductId = "654", Price = 11.99 }
}
};

var target = new List<PublicTwoFields<int, Product>>
{
new PublicTwoFields<int, Product>
{
Value1 = 123,
Value2 = new Product { ProductId = "333", Price = 10.99 }
},
new PublicTwoFields<int, Product>
{
Value1 = 456,
Value2 = new Product { ProductId = "654", Price = 10.99 }
}
};

var itemOne = target.First();
var itemTwo = target.Second();

mapper.Map(source).Over(target);

target.Count.ShouldBe(2);

idEx.Message.ShouldContain("Id is automatically used as the identifier");
idEx.Message.ShouldContain("does not need to be configured");
target.First().ShouldBeSameAs(itemTwo);
target.First().Value1.ShouldBe(456);
target.First().Value2.ShouldNotBeNull();
target.First().Value2.ProductId.ShouldBe("654");
target.First().Value2.Price.ShouldBe(11.99);

target.Second().ShouldNotBeSameAs(itemOne);
target.Second().Value1.ShouldBe(123);
target.Second().Value2.ShouldNotBeNull();
target.Second().Value2.ProductId.ShouldBe("321");
target.Second().Value2.Price.ShouldBe(99.99);
}
}

[Fact]
public void ShouldErrorIfRedundantCustomNamingIdentifierSpecified()
public void ShouldErrorIfNoCompositeIdentifiersSupplied()
{
using (var mapper = Mapper.CreateNew())
var idsEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping.InstancesOf<Person>().IdentifyUsing();
}
});

idsEx.Message.ShouldContain("composite identifier values must be specified");
idsEx.InnerException.ShouldBeOfType<ArgumentException>();
}

[Fact]
public void ShouldErrorIfNullCompositeIdentifierSupplied()
{
var idsEx = Should.Throw<MappingConfigurationException>(() =>
{
mapper.WhenMapping.UseNamePattern("^_(.+)_$");
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping.InstancesOf<Person>().IdentifyUsing(p => p.Name, null);
}
});

idsEx.Message.ShouldContain("composite identifier values must be non-null");
idsEx.InnerException.ShouldBeOfType<ArgumentNullException>();
}

[Fact]
public void ShouldErrorIfRedundantIdentifierSupplied()
{
var idEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping.InstancesOf<Person>().IdentifyUsing(p => p.Id);
}
});

idEx.Message.ShouldContain("Id is automatically used as the identifier");
idEx.Message.ShouldContain("does not need to be configured");
}

[Fact]
public void ShouldErrorIfRedundantCustomNamingIdentifierSupplied()
{
var idEx = Should.Throw<MappingConfigurationException>(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping.UseNamePattern("^_(.+)_$");

var idEx = Should.Throw<MappingConfigurationException>(() =>
mapper.WhenMapping
.InstancesOf(new { _Id_ = default(int) })
.IdentifyUsing(d => d._Id_));
.IdentifyUsing(d => d._Id_);
}
});

idEx.Message.ShouldContain("_Id_ is automatically used as the identifier");
idEx.Message.ShouldContain("does not need to be configured");
}
idEx.Message.ShouldContain("_Id_ is automatically used as the identifier");
idEx.Message.ShouldContain("does not need to be configured");
}

[Fact]
public void ShouldErrorIfMultipleIdentifiersSpecifiedForSameType()
public void ShouldErrorIfMultipleIdentifiersSuppliedForSameType()
{
Should.Throw<MappingConfigurationException>(() =>
{
Expand Down
Loading