Skip to content

Commit e4ef272

Browse files
author
Jeff Treuting
committed
RepositoryPrimaryKeyAttribute bug fix
Fixes #54 It wasn't properly finding the primary key when the attribute was used. Added some test coverage for the GetPrimaryKeyPropertyInfo() method and fixed the logic to use x.PropertyType instead of x.GetType()
1 parent 93a3afd commit e4ef272

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ protected PropertyInfo GetPrimaryKeyPropertyInfo()
397397
var type = typeof(T);
398398
var keyType = typeof(TKey);
399399

400-
return type.GetProperties().FirstOrDefault(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>() && x.GetType() == keyType)
400+
return type.GetProperties().FirstOrDefault(x => x.HasAttribute<RepositoryPrimaryKeyAttribute>() && x.PropertyType == keyType)
401401
?? GetPropertyCaseInsensitive(type, "Id", keyType)
402402
?? GetPropertyCaseInsensitive(type, _typeName + "Id", keyType);
403403
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System.Reflection;
2+
using NUnit.Framework;
3+
using SharpRepository.Repository;
4+
using Should;
5+
6+
namespace SharpRepository.Tests.PrimaryKey
7+
{
8+
[TestFixture]
9+
public class RepositoryPrimaryKeyAttributeTests : TestBase
10+
{
11+
[Test]
12+
public void No_Primary_Key_Should_Return_Null()
13+
{
14+
var repos = new TestRepository<NoPrimaryKeyObject, int>();
15+
repos.TestGetPrimaryKeyPropertyInfo().ShouldBeNull();
16+
}
17+
18+
[Test]
19+
public void Id_Primary_Key_Should_Return_Id_Property()
20+
{
21+
var repos = new TestRepository<IdPrimaryKeyObject, int>();
22+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
23+
24+
propInfo.PropertyType.ShouldEqual(typeof(int));
25+
propInfo.Name.ShouldEqual("Id");
26+
}
27+
28+
[Test]
29+
public void Id_Primary_Key_With_Wrong_Type_Should_Return_Null()
30+
{
31+
var repos = new TestRepository<IdPrimaryKeyObject, string>();
32+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
33+
34+
propInfo.ShouldBeNull();
35+
}
36+
37+
[Test]
38+
public void ClassId_Primary_Key_Should_Return_Id_Property()
39+
{
40+
var repos = new TestRepository<ClassNameIdPrimaryKeyObject, int>();
41+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
42+
43+
propInfo.PropertyType.ShouldEqual(typeof(int));
44+
propInfo.Name.ShouldEqual("ClassNameIdPrimaryKeyObjectId");
45+
}
46+
47+
[Test]
48+
public void ClassId_Primary_Key_With_Wrong_Type_Should_Return_Null()
49+
{
50+
var repos = new TestRepository<ClassNameIdPrimaryKeyObject, string>();
51+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
52+
53+
propInfo.ShouldBeNull();
54+
}
55+
56+
[Test]
57+
public void Attribute_Primary_Key_Should_Return_Id_Property()
58+
{
59+
var repos = new TestRepository<UseAttributePrimaryKeyObject, int>();
60+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
61+
62+
propInfo.PropertyType.ShouldEqual(typeof(int));
63+
propInfo.Name.ShouldEqual("SomeRandomName");
64+
}
65+
66+
[Test]
67+
public void Attribute_Primary_Key_With_Wrong_Type_Should_Return_Null()
68+
{
69+
var repos = new TestRepository<UseAttributePrimaryKeyObject, string>();
70+
var propInfo = repos.TestGetPrimaryKeyPropertyInfo();
71+
72+
propInfo.ShouldBeNull();
73+
}
74+
}
75+
76+
internal class NoPrimaryKeyObject
77+
{
78+
public NoPrimaryKeyObject()
79+
{
80+
81+
}
82+
public string Value { get; set; }
83+
}
84+
85+
internal class IdPrimaryKeyObject
86+
{
87+
public IdPrimaryKeyObject()
88+
{
89+
90+
}
91+
public int Id { get; set; }
92+
public string Value { get; set; }
93+
}
94+
95+
internal class ClassNameIdPrimaryKeyObject
96+
{
97+
public ClassNameIdPrimaryKeyObject()
98+
{
99+
100+
}
101+
public int ClassNameIdPrimaryKeyObjectId { get; set; }
102+
public string Value { get; set; }
103+
}
104+
105+
internal class UseAttributePrimaryKeyObject
106+
{
107+
public UseAttributePrimaryKeyObject()
108+
{
109+
110+
}
111+
[RepositoryPrimaryKey]
112+
public int SomeRandomName { get; set; }
113+
public string Value { get; set; }
114+
}
115+
116+
internal class TestRepository<T, TKey> : InMemoryRepository.InMemoryRepository<T, TKey> where T : class, new()
117+
{
118+
public PropertyInfo TestGetPrimaryKeyPropertyInfo()
119+
{
120+
return GetPrimaryKeyPropertyInfo();
121+
}
122+
}
123+
}

SharpRepository.Tests/SharpRepository.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Configuration\ConfigurationTests.cs" />
8989
<Compile Include="Enumerable\ForEachTests.cs" />
9090
<Compile Include="FetchStrategies\FetchStrategyTests.cs" />
91+
<Compile Include="PrimaryKey\RepositoryPrimaryKeyAttributeTests.cs" />
9192
<Compile Include="QueryOptions\PagingOptionsTests.cs" />
9293
<Compile Include="QueryOptions\SortingOptionsTests.cs" />
9394
<Compile Include="ReflectionExtensions.cs" />
@@ -99,6 +100,7 @@
99100
<Compile Include="TestBase.cs" />
100101
<Compile Include="Properties\AssemblyInfo.cs" />
101102
<Compile Include="TestObjects\Contact.cs" />
103+
<Compile Include="TestObjects\ContactType.cs" />
102104
<Compile Include="TestObjects\EmailAddress.cs" />
103105
<Compile Include="TestObjects\PhoneNumber.cs" />
104106
<Compile Include="TestObjects\TestObjectEntities.cs" />

0 commit comments

Comments
 (0)