Skip to content

Commit

Permalink
Merge pull request #108 from bo-stig-christensen/master
Browse files Browse the repository at this point in the history
Conditional property setter - MS Dynamics CRM entity compatibility
  • Loading branch information
anisimovyuriy authored Aug 2, 2016
2 parents 3e59dcf + 1e388d9 commit f484e0e
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 11,026 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: csharp
solution: ExpressMapper.sln
install:
- nuget restore ExpressMapper.sln
- nuget install NUnit.Runners -Version 2.6.4 -OutputDirectory testrunner
- nuget install NUnit.Runners -Version 3.4.1 -OutputDirectory testrunner
script:
- xbuild /p:Configuration=Release ExpressMapper.sln
- mono ./testrunner/NUnit.Runners.2.6.4/tools/nunit-console.exe "./ExpressMapper.Tests NET45/bin/Release/ExpressMapper.Tests.dll"
- mono ./testrunner/NUnit.ConsoleRunner.3.4.1/tools/nunit3-console.exe "./ExpressMapper.Tests NET45/bin/Release/ExpressMapper.Tests.dll"
2 changes: 1 addition & 1 deletion ExpressMapper NET40/MappingServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private static Expression CreateAssignExpression(Expression setMethod, Expressio
right = Expression.Convert(getMethod, setType);
}

return Expression.Assign(left, right);
return Expression.Condition(Expression.NotEqual(left, right), Expression.Assign(left, right), Expression.Default(setType));
}

private static Expression CreateConvertibleAssignExpression(Expression setMethod, Expression getMethod, Type setType, Type getType, Type setNullableType)
Expand Down
37 changes: 28 additions & 9 deletions ExpressMapper.Tests NET40/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Moq;

namespace ExpressMapper.Tests
{
Expand Down Expand Up @@ -56,7 +58,7 @@ public void DefaultPrimitiveTypePropertyToStringTest()
{
Mapper.Register<TestDefaultDecimal, TestDefaultDecimalToStringViewModel>()
.Member(dest => dest.TestString, src => src.TestDecimal);
Mapper.RegisterCustom<decimal, string>(src => src.ToString("#0.00"));
Mapper.RegisterCustom<decimal, string>(src => src.ToString("#0.00", CultureInfo.InvariantCulture));
Mapper.Compile();
var test = new TestDefaultDecimal() { TestDecimal = default(decimal) };
test.TestDecimal = default(decimal);
Expand Down Expand Up @@ -413,14 +415,13 @@ public void BeforeMap()
}

[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "BeforeMap already registered for ExpressMapper.Tests.Model.Models.Size")]
public void BeforeMapDuplicateTest()
{
Mapper.Register<Size, SizeViewModel>()
var exception = Assert.Throws<InvalidOperationException>(() => Mapper.Register<Size, SizeViewModel>()
.Before((src, dest) => dest.Name = src.Name)
.Before((src, dest) => dest.Name = src.Name)
.Ignore(dest => dest.Name);
Mapper.Compile();
.Ignore(dest => dest.Name));
Assert.That(exception.Message, Is.EqualTo("BeforeMap already registered for ExpressMapper.Tests.Model.Models.Size"));

var sizeResult = Functional.BeforeMap();
var result = Mapper.Map<Size, SizeViewModel>(sizeResult.Key);
Expand All @@ -439,13 +440,12 @@ public void AfterMap()
}

[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "AfterMap already registered for ExpressMapper.Tests.Model.Models.Size")]
public void AfterMapDuplicateTest()
{
Mapper.Register<Size, SizeViewModel>()
var exception = Assert.Throws<InvalidOperationException>(() => Mapper.Register<Size, SizeViewModel>()
.After((src, dest) => dest.Name = "OVERRIDE BY AFTER MAP")
.After((src, dest) => dest.Name = "Duplicate map");
Mapper.Compile();
.After((src, dest) => dest.Name = "Duplicate map"));
Assert.That(exception.Message, Is.EqualTo("AfterMap already registered for ExpressMapper.Tests.Model.Models.Size"));
var sizeResult = Functional.AfterMap();
var result = Mapper.Map<Size, SizeViewModel>(sizeResult.Key);
Assert.AreEqual(result, sizeResult.Value);
Expand Down Expand Up @@ -1340,5 +1340,24 @@ public void MapExistsTest()
Assert.True(Mapper.MapExists(typeof(Father), typeof(FlattenFatherSonGrandsonDto)));
Assert.False(Mapper.MapExists(typeof(FlattenFatherSonGrandsonDto), typeof(Father)));
}

[Test]
public void DoNotUpdateUnchangedPropertyValuesTest()
{
var srcBrand = new Brand
{
Id = Guid.NewGuid(),
Name = "brand"
};

var existingBrandMock = new Mock<Brand>().SetupAllProperties();
existingBrandMock.Object.Name = "brand";

var destBrand = Mapper.Map(srcBrand, existingBrandMock.Object);
existingBrandMock.VerifySet(x => x.Name = It.IsAny<string>(), Times.Once());

Assert.AreEqual(destBrand.Id, srcBrand.Id);
Assert.AreEqual(destBrand.Name, srcBrand.Name);
}
}
}
15 changes: 10 additions & 5 deletions ExpressMapper.Tests NET40/ExpressMapper.Tests NET40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net40\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
Expand All @@ -62,9 +67,6 @@
<Compile Include="SizeMapper.cs" />
<Compile Include="TestMapper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ExpressMapper NET40\ExpressMapper NET40.csproj">
<Project>{E701191E-BF2E-41B8-A4F0-3D8954E3DD58}</Project>
Expand All @@ -75,6 +77,9 @@
<Name>ExpressMapper.Tests.Model</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion ExpressMapper.Tests NET40/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net40" />
<package id="NUnit" version="3.4.1" targetFramework="net40" />
</packages>
16 changes: 14 additions & 2 deletions ExpressMapper.Tests NET45/ExpressMapper.Tests NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.5.16.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.5.16\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -90,6 +99,9 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 6 additions & 0 deletions ExpressMapper.Tests NET45/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
<package id="Moq" version="4.5.16" targetFramework="net45" />
<package id="NUnit" version="3.4.1" targetFramework="net45" />
</packages>
16 changes: 14 additions & 2 deletions ExpressMapper.Tests NET451/ExpressMapper.Tests NET451.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.5.16.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.5.16\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -87,6 +96,9 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 6 additions & 0 deletions ExpressMapper.Tests NET451/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.3" targetFramework="net451" />
<package id="Moq" version="4.5.16" targetFramework="net451" />
<package id="NUnit" version="3.4.1" targetFramework="net451" />
</packages>
4 changes: 2 additions & 2 deletions ExpressMapper.Tests.Model/Models/Brand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ExpressMapper.Tests.Model.Models
{
public class Brand
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
<HintPath>..\packages\EntityFramework.6.1.3\lib\net40\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.4.1\lib\net40\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion ExpressMapper.Tests.Projections/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net4" />
<package id="NUnit" version="2.6.4" targetFramework="net4" />
<package id="NUnit" version="3.4.1" targetFramework="net40" />
</packages>
Binary file removed packages/NUnit.2.6.4/NUnit.2.6.4.nupkg
Binary file not shown.
Binary file removed packages/NUnit.2.6.4/lib/nunit.framework.dll
Binary file not shown.
Loading

0 comments on commit f484e0e

Please sign in to comment.