Skip to content

Commit 2ed9251

Browse files
author
Jeff Treuting
committed
Convention test tweaks and addition
1 parent 3f7c21b commit 2ed9251

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

SharpRepository.Tests.Integration/Spikes/ConventionSpikes.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ namespace SharpRepository.Tests.Integration.Spikes
99
[TestFixture]
1010
public class ConventionSpikes
1111
{
12+
[Test]
13+
public void Changed_Default_Suffix_To_Key_Should_Work()
14+
{
15+
var origSuffix = DefaultRepositoryConventions.PrimaryKeySuffix;
16+
17+
DefaultRepositoryConventions.PrimaryKeySuffix = "Key";
18+
var repository = new InMemoryRepository<ConventionTestItem1>();
19+
20+
var item = new ConventionTestItem1() { Name = "Test1" };
21+
repository.Add(item);
22+
23+
// The PK should have been found and updated so it's not zero anymore
24+
item.ConventionTestItem1Key.ShouldNotEqual(0);
25+
26+
// reset convention to the default orig for the rest of the tests
27+
DefaultRepositoryConventions.PrimaryKeySuffix = origSuffix;
28+
}
29+
1230
[Test]
1331
public void Changed_Convention_To_Key_Should_Work()
1432
{
@@ -17,10 +35,11 @@ public void Changed_Convention_To_Key_Should_Work()
1735
DefaultRepositoryConventions.GetPrimaryKeyName = type => type.Name + "Key";
1836
var repository = new InMemoryRepository<ConventionTestItem1>();
1937

20-
var testItem1 = new ConventionTestItem1() { Name = "Test1" };
21-
repository.Add(testItem1);
38+
var item = new ConventionTestItem1() { Name = "Test1" };
39+
repository.Add(item);
2240

23-
testItem1.ConventionTestItem1Key.ShouldNotEqual(0);
41+
// The PK should have been found and updated so it's not zero anymore
42+
item.ConventionTestItem1Key.ShouldNotEqual(0);
2443

2544
// reset convention to the default orig for the rest of the tests
2645
DefaultRepositoryConventions.GetPrimaryKeyName = origConvention;
@@ -32,10 +51,11 @@ public void Changed_Convention_To_Key_Should_Work_Just_Key_Property()
3251
var repository = new InMemoryRepository<ConventionTestItem2>();
3352
repository.Conventions.GetPrimaryKeyName = _ => "Key";
3453

35-
var testItem1 = new ConventionTestItem2() { Name = "Test1" };
36-
repository.Add(testItem1);
54+
var item = new ConventionTestItem2() { Name = "Test1" };
55+
repository.Add(item);
3756

38-
testItem1.Key.ShouldNotEqual(0);
57+
// The PK should have been found and updated so it's not zero anymore
58+
item.Key.ShouldNotEqual(0);
3959
}
4060

4161
[Test]
@@ -44,10 +64,11 @@ public void Changed_Convention_To_SomeRandomPrimaryKeyProperty_Should_Work()
4464
var repository = new InMemoryRepository<ConventionTestItem3>();
4565
repository.Conventions.GetPrimaryKeyName = _ => "SomeRandomPrimaryKeyProperty" ;
4666

47-
var testItem1 = new ConventionTestItem3() { Name = "Test1" };
48-
repository.Add(testItem1);
67+
var item = new ConventionTestItem3() { Name = "Test1" };
68+
repository.Add(item);
4969

50-
testItem1.SomeRandomPrimaryKeyProperty.ShouldNotEqual(0);
70+
// The PK should have been found and updated so it's not zero anymore
71+
item.SomeRandomPrimaryKeyProperty.ShouldNotEqual(0);
5172
}
5273
}
5374
}

SharpRepository.Tests/Conventions/DefaultConventionTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,29 @@ public void RepositoryConventions_Uses_Default_PrimaryKeySuffix()
2727
}
2828

2929
[Test]
30-
public void Default_PrimaryKeyNameChecks()
30+
public void Default_PrimaryKeyName()
3131
{
3232
DefaultRepositoryConventions.GetPrimaryKeyName(typeof (Contact)).ShouldEqual("ContactId");
3333
}
34+
35+
[Test]
36+
public void Change_PrimaryKeyName()
37+
{
38+
var orig = DefaultRepositoryConventions.GetPrimaryKeyName;
39+
40+
DefaultRepositoryConventions.GetPrimaryKeyName = type => "PK_" + type.Name + "_Id";
41+
42+
DefaultRepositoryConventions.GetPrimaryKeyName(typeof(TestConventionObject)).ShouldEqual("PK_TestConventionObject_Id");
43+
44+
DefaultRepositoryConventions.GetPrimaryKeyName = orig;
45+
}
46+
47+
internal class TestConventionObject
48+
{
49+
public int PK_TestConventionObject_Id { get; set; }
50+
public string Name { get; set; }
51+
}
3452
}
53+
54+
3555
}

0 commit comments

Comments
 (0)