|
| 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 | +} |
0 commit comments