Add CacheUsed flag to Repository that pulls from QueryManager · SharpRepository/SharpRepository@d456d99 · GitHub
Skip to content

Commit d456d99

Browse files
author
Jeff Treuting
committed
Add CacheUsed flag to Repository that pulls from QueryManager
Added tests for it in a QueryManagerTests file Also cleaned up the other caching tests to remove unused stuff
1 parent e058231 commit d456d99

6 files changed

Lines changed: 150 additions & 7 deletions

File tree

SharpRepository.Repository/Queries/QueryManager.cs

Lines changed: 11 additions & 0 deletions

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ protected string TypeName
2727
get { return _typeName; }
2828
}
2929

30+
public bool CacheUsed
31+
{
32+
get { return _queryManager.CacheUsed; }
33+
}
34+
3035
public IBatch<T> BeginBatch()
3136
{
3237
// Return the privately scoped batch via the publicly available interface.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.Caching;
3+
using NUnit.Framework;
4+
using SharpRepository.Repository.Caching;
5+
using SharpRepository.Repository.Queries;
6+
using SharpRepository.Repository.Specifications;
7+
using SharpRepository.Tests.TestObjects;
8+
using Should;
9+
10+
namespace SharpRepository.Tests.Caching
11+
{
12+
[TestFixture]
13+
public class QueryManagerTests : TestBase
14+
{
15+
protected QueryManager<Contact, int> QueryManager;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
// need to clear out the InMemory cache before each test is run so that each is independent and won't effect the next one
21+
var cache = MemoryCache.Default;
22+
foreach (var item in cache)
23+
{
24+
cache.Remove(item.Key);
25+
}
26+
27+
QueryManager = new QueryManager<Contact, int>(new StandardCachingStrategy<Contact, int>()
28+
{
29+
CachePrefix =
30+
"#RepoStandardCache"
31+
});
32+
}
33+
34+
[TearDown]
35+
public void Teardown()
36+
{
37+
//Repository = null;
38+
}
39+
40+
[Test]
41+
public void ExecuteGet_Should_Not_Use_Cache()
42+
{
43+
QueryManager.ExecuteGet(FakeGet, 1);
44+
QueryManager.CacheUsed.ShouldBeFalse();
45+
}
46+
47+
[Test]
48+
public void ExecuteGet_Should_Use_Cache_After_First_Call()
49+
{
50+
// first time no cache yet
51+
QueryManager.ExecuteGet(FakeGet, 1);
52+
QueryManager.CacheUsed.ShouldBeFalse();
53+
54+
// second time the cache has been populated from the last call
55+
QueryManager.ExecuteGet(FakeGet, 1);
56+
QueryManager.CacheUsed.ShouldBeTrue();
57+
}
58+
59+
[Test]
60+
public void ExecuteGetAll_Should_Not_Use_Cache()
61+
{
62+
QueryManager.ExecuteGetAll(FakeGetAll, null);
63+
QueryManager.CacheUsed.ShouldBeFalse();
64+
}
65+
66+
[Test]
67+
public void ExecuteGetAll_Should_Use_Cache_After_First_Call()
68+
{
69+
// first time should not find anything
70+
QueryManager.ExecuteGetAll(FakeGetAll, null);
71+
QueryManager.CacheUsed.ShouldBeFalse();
72+
73+
// second time it should be from cache
74+
QueryManager.ExecuteGetAll(FakeGetAll, null);
75+
QueryManager.CacheUsed.ShouldBeTrue();
76+
}
77+
78+
[Test]
79+
public void ExecuteFindAll_Should_Not_Use_Cache()
80+
{
81+
QueryManager.ExecuteFindAll(FakeGetAll, new Specification<Contact>(c => c.ContactId < 10), null);
82+
QueryManager.CacheUsed.ShouldBeFalse();
83+
}
84+
85+
[Test]
86+
public void ExecuteFindAll_Should_Use_Cache_After_First_Call()
87+
{
88+
// first time should not find anything
89+
QueryManager.ExecuteFindAll(FakeGetAll, new Specification<Contact>(c => c.ContactId < 10), null);
90+
QueryManager.CacheUsed.ShouldBeFalse();
91+
92+
// second time it should be from cache
93+
QueryManager.ExecuteFindAll(FakeGetAll, new Specification<Contact>(c => c.ContactId < 10), null);
94+
QueryManager.CacheUsed.ShouldBeTrue();
95+
}
96+
97+
[Test]
98+
public void ExecuteFind_Should_Not_Use_Cache()
99+
{
100+
QueryManager.ExecuteFind(FakeGet, new Specification<Contact>(c => c.ContactId < 10), null);
101+
QueryManager.CacheUsed.ShouldBeFalse();
102+
}
103+
104+
[Test]
105+
public void ExecuteFind_Should_Use_Cache_After_First_Call()
106+
{
107+
// first time should not find anything
108+
QueryManager.ExecuteFind(FakeGet, new Specification<Contact>(c => c.ContactId < 10), null);
109+
QueryManager.CacheUsed.ShouldBeFalse();
110+
111+
// second time it should be from cache
112+
QueryManager.ExecuteFind(FakeGet, new Specification<Contact>(c => c.ContactId < 10), null);
113+
QueryManager.CacheUsed.ShouldBeTrue();
114+
}
115+
116+
117+
#region fake calls
118+
119+
public Contact FakeGet()
120+
{
121+
return new Contact();
122+
}
123+
124+
public IEnumerable<Contact> FakeGetAll()
125+
{
126+
return new List<Contact>();
127+
}
128+
129+
#endregion
130+
}
131+
}

SharpRepository.Tests/Caching/StandardCachingStrategyTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Linq;
33
using System.Runtime.Caching;
44
using NUnit.Framework;
5-
using SharpRepository.Repository;
65
using SharpRepository.Repository.Caching;
76
using SharpRepository.Repository.Queries;
87
using SharpRepository.Repository.Specifications;
@@ -14,7 +13,6 @@ namespace SharpRepository.Tests.Caching
1413
[TestFixture]
1514
public class StandardCachingStrategyTests : TestBase
1615
{
17-
protected IRepository<Contact, int> Repository;
1816
protected ICachingStrategy<Contact, int> CachingStrategy;
1917

2018
[SetUp]
@@ -33,7 +31,7 @@ public void Setup()
3331
[TearDown]
3432
public void Teardown()
3533
{
36-
//Repository = null;
34+
3735
}
3836

3937
[Test]

SharpRepository.Tests/Caching/StandardCachingWithPartitionStrategyTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using System.Runtime.Caching;
54
using NUnit.Framework;
6-
using SharpRepository.Repository;
75
using SharpRepository.Repository.Caching;
86
using SharpRepository.Repository.Queries;
97
using SharpRepository.Repository.Specifications;
@@ -15,7 +13,6 @@ namespace SharpRepository.Tests.Caching
1513
[TestFixture]
1614
public class StandardCachingWithPartitionStrategyTests : TestBase
1715
{
18-
protected IRepository<Contact, int> Repository;
1916
protected ICachingStrategy<Contact, int> CachingStrategy;
2017

2118
[SetUp]

SharpRepository.Tests/SharpRepository.Tests.csproj

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)