Skip to content

Commit

Permalink
Added support for child class patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
omaxel committed Sep 1, 2018
1 parent fd6184a commit 3cf1d60
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
3.0.1 - Sep 01, 2018
* Added support for child class patch.

3.0.0 - July 18, 2018
* Break change: All entities types which is used in Delta<T> or DeltaCollection<T> must be declared in DeltaConfig.Init().
* Break change: excluded property and 'ignore null value' option must be declared in DeltaConfig.Init().
Expand Down
45 changes: 45 additions & 0 deletions src/SimplePatch.Tests/ClassInheritTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SimplePatch.Tests
{
[TestClass, TestCategory(TestCategories.ClassInheritance)]
public class ClassInheritTests : TestBase
{
[ClassInitialize]
public static void ClassInit(TestContext context)
{
DeltaConfig.Init(cfg => cfg.AddEntity<PersonExtended>());
}

[ClassCleanup]
public static void ClassCleanup()
{
DeltaConfig.Clean();
}

private class PersonExtended : Person
{
public string Address { get; set; }
}

[TestMethod]
public void TestMethod()
{
const string name = "John";
const string surname = "Doe";
const string address = "Person address";

var johnExtended = new PersonExtended();

var delta = new Delta<PersonExtended>();
delta.Add(x => x.Name, name);
delta.Add(x => x.Surname, surname);
delta.Add(x => x.Address, address);
delta.Patch(johnExtended);

Assert.AreEqual(name, johnExtended.Name);
Assert.AreEqual(surname, johnExtended.Surname);
Assert.AreEqual(address, johnExtended.Address);
}
}
}
6 changes: 3 additions & 3 deletions src/SimplePatch.Tests/ConfigurationTests/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void IgnoreLetterCase()
.AddEntity<Person>();
});

GetDelta("AgE", 23).Patch(John);
CreateDelta<Person>("AgE", 23).Patch(John);
Assert.AreEqual(23, John.Age);
}

Expand Down Expand Up @@ -66,11 +66,11 @@ then the assigned value will be the length of the string + 0.5*/
});

// First mapping function will be executed here, Age type is int
GetDelta(x => x.Age, "abc").Patch(John);
CreateDelta<Person, int>(x => x.Age, "abc").Patch(John);
Assert.AreEqual("abc".Length, John.Age);

// Second mapping function will be executed here, Height type is double
GetDelta(x => x.Height, "abcdef").Patch(John);
CreateDelta<Person, double>(x => x.Height, "abcdef").Patch(John);
Assert.AreEqual("abcdef".Length + 0.5, John.Height);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/SimplePatch.Tests/ConfigurationTests/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Exclude()

var initialAge = John.Age;

GetDelta(x => x.Age, 23).Patch(John);
CreateDelta<Person, int>(x => x.Age, 23).Patch(John);

Assert.AreEqual(initialAge, John.Age);
}
Expand All @@ -44,7 +44,7 @@ public void IgnoreNullValue()

var initialName = John.Name;

GetDelta<string, string>(x => x.Name, null).Patch(John);
CreateDelta<Person, string>(x => x.Name, null).Patch(John);

Assert.AreEqual(initialName, John.Name);
}
Expand Down Expand Up @@ -108,19 +108,19 @@ public void MappingFunction()
});

// Global mapping function executed here
GetDelta(x => x.Surname, "Rossi").Patch(John);
CreateDelta<Person, string>(x => x.Surname, "Rossi").Patch(John);
Assert.AreEqual("issoR", John.Surname);

// First property mapping function executed here
GetDelta(x => x.Name, "Mario").Patch(John);
CreateDelta<Person, string>(x => x.Name, "Mario").Patch(John);
Assert.AreEqual("Mario", John.Name);

// Second property mapping function executed here
GetDelta(x => x.Name, 15).Patch(John);
CreateDelta<Person, string>(x => x.Name, 15).Patch(John);
Assert.AreEqual("number:15", John.Name);

// Third property mapping function executed here
GetDelta(x => x.Name, John.BirthDate).Patch(John);
CreateDelta<Person, string>(x => x.Name, John.BirthDate).Patch(John);
Assert.AreEqual("datetime:1990-02-01T20:15:10", John.Name);
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/SimplePatch.Tests/DeltaUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ namespace SimplePatch.Tests
{
internal class DeltaUtils
{
internal static Delta<Person> GetDelta<TProp, T>(Expression<Func<Person, TProp>> property, T propValue)
internal static Delta<TEntity> CreateDelta<TEntity, TProp>(Expression<Func<TEntity, TProp>> property, object propValue)
where TEntity : class, new()
{
var delta = new Delta<Person>();
var delta = new Delta<TEntity>();
delta.Add(property, propValue);
return delta;
}

internal static Delta<Person> GetDelta<T>(string propertyName, T propValue)
internal static Delta<TEntity> CreateDelta<TEntity>(string property, object propValue)
where TEntity : class, new()
{
var delta = new Delta<Person>();
delta.Add(propertyName, propValue);
var delta = new Delta<TEntity>();
delta.Add(property, propValue);
return delta;
}

}
}
18 changes: 9 additions & 9 deletions src/SimplePatch.Tests/PropertiesTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ public static void ClassCleanup()
[TestMethod]
public void IntProp()
{
GetDelta(x => x.Age, 23).Patch(John);
CreateDelta<Person, int>(x => x.Age, 23).Patch(John);
Assert.AreEqual(23, John.Age);
}

[TestMethod]
public void StringProp()
{
GetDelta(x => x.Name, "John Marco").Patch(John);
CreateDelta<Person, string>(x => x.Name, "John Marco").Patch(John);
Assert.AreEqual("John Marco", John.Name);
}

[TestMethod]
public void DoubleProp()
{
GetDelta(x => x.Height, 1.65).Patch(John);
CreateDelta<Person, double>(x => x.Height, 1.65).Patch(John);
Assert.AreEqual(1.65, John.Height);
}

[TestMethod]
public void DateTimeProp()
{
var date = DateTime.UtcNow;
GetDelta(x => x.BirthDate, date).Patch(John);
CreateDelta<Person, DateTime>(x => x.BirthDate, date).Patch(John);
Assert.AreEqual(date, John.BirthDate);
}

[TestMethod]
public void GuidProp()
{
var guid = Guid.NewGuid();
GetDelta(x => x.Guid, guid).Patch(John);
CreateDelta<Person, Guid>(x => x.Guid, guid).Patch(John);
Assert.AreEqual(guid, John.Guid);
}

Expand All @@ -63,30 +63,30 @@ public void GuidProp()
[TestMethod]
public void IntPropFromString()
{
GetDelta(x => x.Age, "28").Patch(John);
CreateDelta<Person, int>(x => x.Age, "28").Patch(John);
Assert.AreEqual(28, John.Age);
}

[TestMethod]
public void DoublePropFromString()
{
GetDelta(x => x.Height, "28,5").Patch(John);
CreateDelta<Person, double>(x => x.Height, "28,5").Patch(John);
Assert.AreEqual(28.5, John.Height);
}

[TestMethod]
public void DateTimePropFromString()
{
var date = DateTime.UtcNow;
GetDelta(x => x.BirthDate, date.ToString("s")).Patch(John);
CreateDelta<Person, DateTime>(x => x.BirthDate, date.ToString("s")).Patch(John);
Assert.AreEqual(date.ToString("s"), John.BirthDate.ToString("s"));
}

[TestMethod]
public void GuidPropFromString()
{
var guid = Guid.NewGuid();
GetDelta(x => x.Guid, guid.ToString()).Patch(John);
CreateDelta<Person, Guid>(x => x.Guid, guid.ToString()).Patch(John);
Assert.AreEqual(guid, John.Guid);
}

Expand Down
7 changes: 4 additions & 3 deletions src/SimplePatch.Tests/SimplePatch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ClassInheritTests.cs" />
<Compile Include="ConfigurationTests\Properties.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="ConfigurationTests\Globals.cs" />
Expand All @@ -57,15 +58,15 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestCategories.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SimplePatch\SimplePatch.csproj">
<Project>{71d49858-8706-4e19-ab53-022320b263cd}</Project>
<Name>SimplePatch</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
1 change: 1 addition & 0 deletions src/SimplePatch.Tests/TestCategories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ internal class TestCategories
{
public const string PropertiesTypes = "Properties Types";
public const string Configuration = "Configuration";
public const string ClassInheritance = "Class Inheritance";
}
}
2 changes: 1 addition & 1 deletion src/SimplePatch/Helpers/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class TypeHelper
/// <returns>List of properties belonging to the specified identity as <see cref="IEnumerable{DeltaInfo}"/></returns>
internal static IEnumerable<DeltaPropInfo> GetEntityProperties<TEntity>()
{
return typeof(TEntity).GetTypeInfo().DeclaredProperties.Where(x => x.GetMethod.IsPublic && x.SetMethod.IsPublic && x.CanRead && x.CanWrite).Select(x => new DeltaPropInfo(x));
return typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.GetMethod != null && x.GetMethod.IsPublic && x.SetMethod != null && x.SetMethod.IsPublic && x.CanRead && x.CanWrite).Select(x => new DeltaPropInfo(x));
}


Expand Down
26 changes: 16 additions & 10 deletions src/SimplePatch/SimplePatch.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>
<Version>3.0.0</Version>
<TargetFrameworks>net461;netstandard1.4</TargetFrameworks>
<Version>3.0.1</Version>
<Authors>Omar Muscatello</Authors>
<Company>Omar Muscatello</Company>
<Description>A simple library for partial entity changes in ASP.NET and ASP.NET Core.</Description>
<Copyright>Copyright (c) Omar Muscatello 2018</Copyright>
<PackageLicenseUrl>https://github.com/OmarMuscatello/SimplePatch/blob/master/LICENSE</PackageLicenseUrl>
<PackageReleaseNotes>* Break change: All entities types which is used in Delta&lt;T&gt; or DeltaCollection&lt;T&gt; must be declared in DeltaConfig.Init().
* Break change: excluded property and 'ignore null value' option must be declared in DeltaConfig.Init().
* Added global and properties mapping functions.
* Added support to add property using expressions in Delta&lt;T&gt;.Add method.
* Added unit tests.</PackageReleaseNotes>
<PackageReleaseNotes>* Added support for child class patch.</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/OmarMuscatello/SimplePatch</PackageProjectUrl>
<RepositoryUrl>https://github.com/OmarMuscatello/SimplePatch</RepositoryUrl>
<PackageIconUrl>http://raw.github.com/OmarMuscatello/SimplePatch/master/simplepatch-icon.png</PackageIconUrl>
<RepositoryType></RepositoryType>
<PackageTags>patch http-patch partial-entity-changes web-api asp-net-web-api entity-framework entity-framework-core asp-net asp-net-core</PackageTags>
<ApplicationIcon></ApplicationIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<AssemblyVersion>3.0.1.0</AssemblyVersion>
<FileVersion>3.0.1.0</FileVersion>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.4'">
<PackageReference Include="System.Reflection.TypeExtensions">
<Version>4.5.1</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
</ItemGroup>

</Project>

0 comments on commit 3cf1d60

Please sign in to comment.