Skip to content

Commit da6e9fd

Browse files
committed
Converted NUnit Test Base
1 parent 50f2af2 commit da6e9fd

File tree

54 files changed

+3965
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3965
-25
lines changed

SharpRepository.Repository/Queries/CompoundKeyQueryManager.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public CompoundKeyQueryManager(ICompoundKeyCachingStrategy<T> cachingStrategy)
2727

2828
public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResult>> selector, object[] keys)
2929
{
30-
TResult result;
31-
if (_cachingStrategy.TryGetResult(keys, selector, out result))
30+
if (_cachingStrategy.TryGetResult(keys, selector, out TResult result))
3231
{
3332
CacheUsed = true;
3433
return result;
@@ -44,8 +43,7 @@ public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResu
4443

4544
public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> query, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
4645
{
47-
IEnumerable<TResult> result;
48-
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out result))
46+
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out IEnumerable<TResult> result))
4947
{
5048
CacheUsed = true;
5149
return result;
@@ -61,8 +59,7 @@ public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> qu
6159

6260
public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
6361
{
64-
IEnumerable<TResult> result;
65-
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out result))
62+
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out IEnumerable<TResult> result))
6663
{
6764
CacheUsed = true;
6865
return result;
@@ -78,8 +75,7 @@ public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> q
7875

7976
public TResult ExecuteFind<TResult>(Func<TResult> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
8077
{
81-
TResult result;
82-
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out result))
78+
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out TResult result))
8379
{
8480
CacheUsed = true;
8581
return result;
@@ -136,8 +132,7 @@ public CompoundKeyQueryManager(ICompoundKeyCachingStrategy<T, TKey, TKey2> cachi
136132

137133
public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResult>> selector, TKey key, TKey2 key2)
138134
{
139-
TResult result;
140-
if (_cachingStrategy.TryGetResult(key, key2, selector, out result))
135+
if (_cachingStrategy.TryGetResult(key, key2, selector, out TResult result))
141136
{
142137
CacheUsed = true;
143138
return result;
@@ -153,8 +148,7 @@ public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResu
153148

154149
public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> query, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
155150
{
156-
IEnumerable<TResult> result;
157-
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out result))
151+
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out IEnumerable<TResult> result))
158152
{
159153
CacheUsed = true;
160154
return result;
@@ -170,8 +164,7 @@ public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> qu
170164

171165
public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
172166
{
173-
IEnumerable<TResult> result;
174-
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out result))
167+
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out IEnumerable<TResult> result))
175168
{
176169
CacheUsed = true;
177170
return result;
@@ -187,8 +180,7 @@ public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> q
187180

188181
public TResult ExecuteFind<TResult>(Func<TResult> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
189182
{
190-
TResult result;
191-
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out result))
183+
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out TResult result))
192184
{
193185
CacheUsed = true;
194186
return result;
@@ -246,8 +238,7 @@ public CompoundKeyQueryManager(ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3
246238

247239
public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResult>> selector, TKey key, TKey2 key2, TKey3 key3)
248240
{
249-
TResult result;
250-
if (_cachingStrategy.TryGetResult(key, key2, key3, selector, out result))
241+
if (_cachingStrategy.TryGetResult(key, key2, key3, selector, out TResult result))
251242
{
252243
CacheUsed = true;
253244
return result;
@@ -263,8 +254,7 @@ public TResult ExecuteGet<TResult>(Func<TResult> query, Expression<Func<T, TResu
263254

264255
public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> query, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
265256
{
266-
IEnumerable<TResult> result;
267-
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out result))
257+
if (_cachingStrategy.TryGetAllResult(queryOptions, selector, out IEnumerable<TResult> result))
268258
{
269259
CacheUsed = true;
270260
return result;
@@ -280,8 +270,7 @@ public IEnumerable<TResult> ExecuteGetAll<TResult>(Func<IEnumerable<TResult>> qu
280270

281271
public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
282272
{
283-
IEnumerable<TResult> result;
284-
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out result))
273+
if (_cachingStrategy.TryFindAllResult(criteria, queryOptions, selector, out IEnumerable<TResult> result))
285274
{
286275
CacheUsed = true;
287276
return result;
@@ -297,8 +286,7 @@ public IEnumerable<TResult> ExecuteFindAll<TResult>(Func<IEnumerable<TResult>> q
297286

298287
public TResult ExecuteFind<TResult>(Func<TResult> query, ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
299288
{
300-
TResult result;
301-
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out result))
289+
if (_cachingStrategy.TryFindResult(criteria, queryOptions, selector, out TResult result))
302290
{
303291
CacheUsed = true;
304292
return result;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
// using ServiceStack.Common.Extensions;
4+
using SharpRepository.Repository.Aspects;
5+
using SharpRepository.Tests.PrimaryKey;
6+
using SharpRepository.Tests.TestObjects;
7+
8+
namespace SharpRepository.Tests.Aspects
9+
{
10+
[TestFixture]
11+
public class RepositoryActionBaseAttributeTests
12+
{
13+
[Test]
14+
public void Aspect_IsNotCalled_WhenDisabled()
15+
{
16+
//Arrange
17+
var repository = new TestRepository<Product, int>();
18+
var aspect = repository
19+
.GetAspects()
20+
.OfType<AuditAttributeMock>()
21+
.First();
22+
23+
//Act
24+
repository.SuppressAudit();
25+
var product = repository.Get(1);
26+
27+
28+
//Assert
29+
Assert.IsTrue(aspect.OnInitializedCalled);
30+
Assert.IsFalse(aspect.Enabled);
31+
Assert.IsFalse(aspect.OnGetExecutingCalled);
32+
Assert.IsFalse(aspect.OnGetExecutedCalled);
33+
}
34+
35+
[Test]
36+
public void Aspect_IsCalled_WhenReEnabled()
37+
{
38+
//Arrange
39+
var repository = new TestRepository<Product, int>();
40+
var aspect = repository
41+
.GetAspects()
42+
.OfType<AuditAttributeMock>()
43+
.First();
44+
45+
//Act
46+
repository.SuppressAudit();
47+
repository.RestoreAudit();
48+
var product = repository.Get(1);
49+
50+
51+
//Assert
52+
Assert.IsTrue(aspect.OnInitializedCalled);
53+
Assert.IsTrue(aspect.Enabled);
54+
Assert.IsTrue(aspect.OnGetExecutingCalled);
55+
Assert.IsTrue(aspect.OnGetExecutedCalled);
56+
}
57+
58+
[Test]
59+
public void Aspect_WhenMultipleApplied_ExecutedInOrder()
60+
{
61+
//Arrange
62+
var repository = new TestRepository<Product, int>();
63+
var aspects = repository
64+
.GetAspects()
65+
.ToArray();
66+
var audit = (AuditAttributeMock)aspects.First(a => a is AuditAttributeMock);
67+
var specificAudit = (SpecificAuditAttribute)aspects.First(a => a is SpecificAuditAttribute);
68+
69+
//Act
70+
var product = repository.Get(1);
71+
72+
//Assert
73+
Assert.Greater(specificAudit.ExecutedOn, audit.ExecutedOn);
74+
}
75+
76+
}
77+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using NUnit.Framework;
3+
using SharpRepository.Repository;
4+
using SharpRepository.Repository.Transactions;
5+
using SharpRepository.Tests.TestObjects;
6+
using Shouldly;
7+
using System.Linq;
8+
using SharpRepository.InMemoryRepository;
9+
10+
namespace SharpRepository.Tests.Batch
11+
{
12+
[TestFixture]
13+
public class BatchTests : TestBase
14+
{
15+
[Test]
16+
public void Batch_Default_Should_Contain_No_Actions()
17+
{
18+
var repository = new InMemoryRepository<Contact, Int32>();
19+
20+
using (var batch = repository.BeginBatch())
21+
{
22+
batch.BatchActions.Count.ShouldBe(0);
23+
}
24+
}
25+
26+
[Test]
27+
public void Batch_Commit_Should_Reset_Actions()
28+
{
29+
var repository = new InMemoryRepository<Contact, Int32>();
30+
31+
using (var batch = repository.BeginBatch())
32+
{
33+
batch.Add(new Contact());
34+
batch.Commit();
35+
batch.BatchActions.Count.ShouldBe(0);
36+
}
37+
}
38+
39+
[Test]
40+
public void Batch_Add_Should_Queue_Add_Action()
41+
{
42+
var repository = new InMemoryRepository<Contact, Int32>();
43+
44+
using (var batch = repository.BeginBatch())
45+
{
46+
var contact = new Contact();
47+
batch.Add(contact);
48+
batch.BatchActions.FirstOrDefault().Item.ShouldBe(contact);
49+
batch.BatchActions.FirstOrDefault().Action.ShouldBe(BatchAction.Add);
50+
}
51+
}
52+
53+
[Test]
54+
public void Batch_Update_Should_Queue_Update_Action()
55+
{
56+
var repository = new InMemoryRepository<Contact, Int32>();
57+
58+
using (var batch = repository.BeginBatch())
59+
{
60+
var contact = new Contact();
61+
batch.Update(contact);
62+
batch.BatchActions.FirstOrDefault().Item.ShouldBe(contact);
63+
batch.BatchActions.FirstOrDefault().Action.ShouldBe(BatchAction.Update);
64+
}
65+
}
66+
67+
[Test]
68+
public void Batch_Update_Should_Queue_Delete_Action()
69+
{
70+
var repository = new InMemoryRepository<Contact, Int32>();
71+
72+
using (var batch = repository.BeginBatch())
73+
{
74+
var contact = new Contact();
75+
batch.Delete(contact);
76+
batch.BatchActions.FirstOrDefault().Item.ShouldBe(contact);
77+
batch.BatchActions.FirstOrDefault().Action.ShouldBe(BatchAction.Delete);
78+
}
79+
}
80+
81+
[Test]
82+
public void Batch_Rollback_Should_Reset_Actions()
83+
{
84+
var repository = new InMemoryRepository<Contact, Int32>();
85+
86+
using (var batch = repository.BeginBatch())
87+
{
88+
batch.Add(new Contact());
89+
batch.Rollback();
90+
batch.BatchActions.Count.ShouldBe(0);
91+
}
92+
}
93+
}
94+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using NUnit.Framework;
3+
using SharpRepository.InMemoryRepository;
4+
using SharpRepository.Repository.Caching;
5+
using SharpRepository.Tests.TestObjects;
6+
using Shouldly;
7+
8+
namespace SharpRepository.Tests.Caching
9+
{
10+
[TestFixture]
11+
public class ClearCacheTests
12+
{
13+
[Test]
14+
public void ClearAllCache_Throws_Exception_WIthout_OutOfBox()
15+
{
16+
try
17+
{
18+
Cache.ClearAll();
19+
Assert.Fail("No exception was thrown when it should have been");
20+
}
21+
catch (Exception)
22+
{
23+
}
24+
}
25+
26+
[Test]
27+
public void ClearAllCache_Changes_FullCachePrefix_When_Configured()
28+
{
29+
Cache.CachePrefixManager = new SingleServerCachePrefixManager();
30+
31+
var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());
32+
33+
var fullCachePrefix = repos.CachingStrategy.FullCachePrefix;
34+
35+
// shouldn't change if we call it again
36+
repos.CachingStrategy.FullCachePrefix.ShouldBe(fullCachePrefix);
37+
38+
// clear out all cached items across all repositories
39+
Cache.ClearAll();
40+
41+
// this should have changed this time
42+
repos.CachingStrategy.FullCachePrefix.ShouldNotBe(fullCachePrefix);
43+
}
44+
45+
[Test]
46+
public void ClearCache_Changes_FullCachePrefix()
47+
{
48+
var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());
49+
50+
var fullCachePrefix = repos.CachingStrategy.FullCachePrefix;
51+
52+
// shouldn't change if we call it again
53+
repos.CachingStrategy.FullCachePrefix.ShouldBe(fullCachePrefix);
54+
55+
// clear out only the cache for this specific repository
56+
repos.ClearCache();
57+
58+
// this should have changed this time
59+
repos.CachingStrategy.FullCachePrefix.ShouldNotBe(fullCachePrefix);
60+
}
61+
}
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NUnit.Framework;
2+
using SharpRepository.InMemoryRepository;
3+
using SharpRepository.Repository.Caching;
4+
using SharpRepository.Tests.TestObjects;
5+
using Shouldly;
6+
7+
namespace SharpRepository.Tests.Caching
8+
{
9+
[TestFixture]
10+
public class DisabledCacheTests
11+
{
12+
[Test]
13+
public void Using_DisableCaching_Should_Disable_Cache_Inside_Using_Block()
14+
{
15+
var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());
16+
17+
repos.CachingEnabled.ShouldBeTrue();
18+
19+
using (repos.DisableCaching())
20+
{
21+
repos.CachingEnabled.ShouldBeFalse();
22+
}
23+
24+
repos.CachingEnabled.ShouldBeTrue();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)