Skip to content

Commit 3208582

Browse files
(GH-613) Update unit tests
1 parent a97baec commit 3208582

File tree

10 files changed

+202
-40
lines changed

10 files changed

+202
-40
lines changed

test/DotNetToolkit.Repository.Integration.Test/App.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
</system.data>
2828
<connectionStrings>
2929
<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" />
30-
<add name="AzureStorageTableConnection" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;" />
3130
</connectionStrings>
3231
<repository>
3332
<defaultContextFactory type="DotNetToolkit.Repository.InMemory.Internal.InMemoryRepositoryContextFactory, DotNetToolkit.Repository.InMemory" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace DotNetToolkit.Repository.Integration.Test.Helpers
2+
{
3+
using Couchbase;
4+
using Couchbase.Configuration.Client;
5+
using System;
6+
using System.Collections.Generic;
7+
8+
public static class CouchbaseHelper
9+
{
10+
public static void ClearDatabase(string host, string username, string password, string bucketName)
11+
{
12+
var config = new ClientConfiguration
13+
{
14+
Servers = new List<Uri> { new Uri(host) },
15+
BucketConfigs = new Dictionary<string, BucketConfiguration>
16+
{
17+
{ bucketName, new BucketConfiguration
18+
{
19+
Username = username,
20+
Password = password,
21+
BucketName = bucketName
22+
}
23+
}
24+
}
25+
};
26+
27+
using (var cluster = new Cluster(config))
28+
using (var bucket = cluster.OpenBucket())
29+
{
30+
var result = bucket.CreateManager(username, password).Flush();
31+
}
32+
}
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if NETFULL
2+
namespace DotNetToolkit.Repository.Integration.Test.Helpers
3+
{
4+
using Enyim.Caching;
5+
using Enyim.Caching.Configuration;
6+
7+
public static class MemcachedHelper
8+
{
9+
public static void ClearDatabase(string host, int port)
10+
{
11+
var config = new MemcachedClientConfiguration();
12+
13+
config.AddServer(host, port);
14+
15+
using (var client = new MemcachedClient(config))
16+
{
17+
client.FlushAll();
18+
}
19+
}
20+
}
21+
}
22+
#endif
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace DotNetToolkit.Repository.Integration.Test.Helpers
2+
{
3+
using StackExchange.Redis;
4+
5+
public static class RedisHelper
6+
{
7+
public static void ClearDatabase(string host, int defaultDatabase)
8+
{
9+
var options = new ConfigurationOptions()
10+
{
11+
AllowAdmin = true,
12+
DefaultDatabase = defaultDatabase,
13+
EndPoints =
14+
{
15+
{ host }
16+
}
17+
};
18+
19+
using (var connection = ConnectionMultiplexer.Connect(options))
20+
{
21+
var server = connection.GetServer(connection.GetEndPoints()[0]);
22+
server.FlushAllDatabases();
23+
}
24+
}
25+
}
26+
}

test/DotNetToolkit.Repository.Integration.Test/Tests/CachingProvider/RepositoryCachingTests.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Caching.InMemory;
44
using Configuration.Caching.Internal;
55
using Data;
6+
using DotNetToolkit.Repository.Integration.Test.Helpers;
67
using Fixtures;
78
using Query;
89
using System.Threading.Tasks;
@@ -22,11 +23,21 @@ public RepositoryCachingTests(ITestOutputHelper testOutputHelper) : base(testOut
2223
#endif
2324
}
2425

26+
protected override void BeforeTest(CachingProviderType cachingProvider)
27+
{
28+
ClearCacheProvider(cachingProvider);
29+
}
30+
31+
protected override void AfterTest(CachingProviderType cachingProvider)
32+
{
33+
ClearCacheProvider(cachingProvider);
34+
}
35+
2536
[Fact]
2637
public void CacheEnabled()
2738
{
2839
var options = GetRepositoryOptionsBuilder(ContextProviderType.InMemory)
29-
.UseCachingProvider(new InMemoryCacheProvider())
40+
.UseInMemoryCache()
3041
.Options;
3142

3243
var repo = new Repository<Customer>(options);
@@ -57,7 +68,7 @@ public void CacheDisabled()
5768
public void ClearCache()
5869
{
5970
var options = GetRepositoryOptionsBuilder(ContextProviderType.InMemory)
60-
.UseCachingProvider(new InMemoryCacheProvider())
71+
.UseInMemoryCache()
6172
.Options;
6273

6374
var customerRepo = new Repository<Customer>(options);
@@ -1068,5 +1079,24 @@ private static async Task TestGroupByWithOptionsAsync(IRepositoryFactory repoFac
10681079

10691080
Assert.False(repo.CacheUsed);
10701081
}
1082+
1083+
private static void ClearCacheProvider(CachingProviderType cachingProvider)
1084+
{
1085+
#if NETFULL
1086+
if (cachingProvider == CachingProviderType.Memcached)
1087+
{
1088+
MemcachedHelper.ClearDatabase("127.0.0.1", 11211);
1089+
}
1090+
#endif
1091+
if (cachingProvider == CachingProviderType.Redis)
1092+
{
1093+
RedisHelper.ClearDatabase("localhost", 0);
1094+
}
1095+
1096+
if (cachingProvider == CachingProviderType.Couchbase)
1097+
{
1098+
CouchbaseHelper.ClearDatabase("http://localhost:8091", "default", "password", "default");
1099+
}
1100+
}
10711101
}
10721102
}

test/DotNetToolkit.Repository.Integration.Test/Tests/TestBase.cs

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ namespace DotNetToolkit.Repository.Integration.Test
2828

2929
public abstract class TestBase
3030
{
31+
protected ILoggerProvider TestXUnitLoggerProvider { get; }
32+
3133
protected TestBase(ITestOutputHelper testOutputHelper)
3234
{
3335
TestXUnitLoggerProvider = new TestXUnitLoggerProvider(testOutputHelper);
3436
}
3537

36-
protected ILoggerProvider TestXUnitLoggerProvider { get; }
38+
protected virtual void BeforeTest(CachingProviderType cachingProvider) { }
39+
protected virtual void AfterTest(CachingProviderType cachingProvider) { }
40+
protected virtual void BeforeTest(ContextProviderType contextProvider) { }
41+
protected virtual void AfterTest(ContextProviderType contextProvider) { }
3742

3843
protected void ForRepositoryFactoryWithAllCachingProviders(ContextProviderType contextProvider, Action<IRepositoryFactory, CachingProviderType> action)
3944
=> CachingProviders()
@@ -42,9 +47,13 @@ protected void ForRepositoryFactoryWithAllCachingProviders(ContextProviderType c
4247
{
4348
var builder = GetRepositoryOptionsBuilder(contextProvider);
4449

50+
BeforeTest(cachingProvider);
51+
4552
ApplyCachingProvider(cachingProvider, builder);
4653

4754
action(new RepositoryFactory(builder.Options), cachingProvider);
55+
56+
AfterTest(cachingProvider);
4857
});
4958

5059
protected void ForRepositoryFactoryWithAllCachingProviders(ContextProviderType contextProvider, Action<IRepositoryFactory> action)
@@ -57,11 +66,13 @@ protected void ForRepositoryFactoryWithAllCachingProvidersAsync(ContextProviderT
5766
{
5867
var builder = GetRepositoryOptionsBuilder(contextProvider);
5968

69+
BeforeTest(cachingProvider);
70+
6071
ApplyCachingProvider(cachingProvider, builder);
6172

62-
await HandleExceptionAsync(() => action(
63-
new RepositoryFactory(builder.Options),
64-
cachingProvider));
73+
await HandleExceptionAsync(() => action(new RepositoryFactory(builder.Options), cachingProvider));
74+
75+
AfterTest(cachingProvider);
6576
});
6677

6778
protected void ForRepositoryFactoryWithAllCachingProvidersAsync(ContextProviderType contextProvider, Func<IRepositoryFactory, Task> action)
@@ -75,7 +86,11 @@ protected void ForAllRepositoryFactories(Action<IRepositoryFactory, ContextProvi
7586
if (exclude != null && exclude.Contains(x))
7687
return;
7788

89+
BeforeTest(x);
90+
7891
action(new RepositoryFactory(BuildOptions(x)), x);
92+
93+
AfterTest(x);
7994
});
8095

8196
protected void ForAllRepositoryFactories(Action<IRepositoryFactory> action, params ContextProviderType[] exclude)
@@ -89,8 +104,11 @@ protected void ForAllRepositoryFactoriesAsync(Func<IRepositoryFactory, ContextPr
89104
if (exclude != null && exclude.Contains(x))
90105
return;
91106

92-
await HandleExceptionAsync(() => action(
93-
new RepositoryFactory(BuildOptions(x)), x));
107+
BeforeTest(x);
108+
109+
await HandleExceptionAsync(() => action(new RepositoryFactory(BuildOptions(x)), x));
110+
111+
AfterTest(x);
94112
});
95113

96114
protected void ForAllRepositoryFactoriesAsync(Func<IRepositoryFactory, Task> action, params ContextProviderType[] exclude)
@@ -105,7 +123,11 @@ protected void ForAllServiceFactories(Action<IServiceFactory, ContextProviderTyp
105123
if (exclude != null && exclude.Contains(x))
106124
return;
107125

126+
BeforeTest(x);
127+
108128
action(new ServiceFactory(new UnitOfWorkFactory(BuildOptions(x))), x);
129+
130+
AfterTest(x);
109131
});
110132

111133
protected void ForAllServiceFactories(Action<IServiceFactory> action, params ContextProviderType[] exclude)
@@ -120,10 +142,11 @@ protected void ForAllServiceFactoriesAsync(Func<IServiceFactory, ContextProvider
120142
if (exclude != null && exclude.Contains(x))
121143
return;
122144

123-
await HandleExceptionAsync(() => action(
124-
new ServiceFactory(
125-
new UnitOfWorkFactory(
126-
BuildOptions(x))), x));
145+
BeforeTest(x);
146+
147+
await HandleExceptionAsync(() => action(new ServiceFactory(new UnitOfWorkFactory(BuildOptions(x))), x));
148+
149+
AfterTest(x);
127150
});
128151

129152
protected void ForAllServiceFactoriesAsync(Func<IServiceFactory, Task> action, params ContextProviderType[] exclude)
@@ -133,7 +156,14 @@ protected void ForAllUnitOfWorkFactories(Action<IUnitOfWorkFactory, ContextProvi
133156
=> ContextProviders()
134157
.Where(SupportsTransactions)
135158
.ToList()
136-
.ForEach(x => action(new UnitOfWorkFactory(BuildOptions(x)), x));
159+
.ForEach(x =>
160+
{
161+
BeforeTest(x);
162+
163+
action(new UnitOfWorkFactory(BuildOptions(x)), x);
164+
165+
AfterTest(x);
166+
});
137167

138168
protected void ForAllUnitOfWorkFactories(Action<IUnitOfWorkFactory> action)
139169
=> ForAllUnitOfWorkFactories((factory, type) => action(factory));
@@ -208,41 +238,42 @@ private static void ApplyCachingProvider(CachingProviderType cachingProvider, Re
208238
{
209239
case CachingProviderType.MicrosoftInMemory:
210240
{
211-
builder.UseCachingProvider(new InMemoryCacheProvider());
241+
builder.UseInMemoryCache();
242+
212243
break;
213244
}
214245
case CachingProviderType.Redis:
215246
{
216-
var provider = new RedisCacheProvider(allowAdmin: true, defaultDatabase: 0, expiry: null);
217-
218-
provider.Cache.Server.FlushAllDatabases();
219-
220-
builder.UseCachingProvider(provider);
247+
builder.UseRedis(options =>
248+
{
249+
options
250+
.WithEndPoint("localhost")
251+
.WithDefaultDatabase(0);
252+
});
221253

222254
break;
223255
}
224256
#if NETFULL
225257
case CachingProviderType.Memcached:
226258
{
227-
var provider = new MemcachedCacheProvider("127.0.0.1", 11211);
228-
229-
provider.Cache.Client.FlushAll();
230-
231-
builder.UseCachingProvider(provider);
259+
builder.UseMemcached(options =>
260+
{
261+
options.WithEndPoint("127.0.0.1", 11211);
262+
});
232263

233264
break;
234265
}
235266
#endif
236267
case CachingProviderType.Couchbase:
237268
{
238-
var provider = new CouchbaseCacheProvider("http://localhost:8091", "default", "password");
239-
240-
using (var bucket = provider.Cache.Cluster.OpenBucket())
269+
builder.UseCouchbase(options =>
241270
{
242-
bucket.CreateManager("default", "password").Flush();
243-
}
244-
245-
builder.UseCachingProvider(provider);
271+
options
272+
.WithEndPoint("http://localhost:8091")
273+
.WithBucketName("default")
274+
.WithUsername("default")
275+
.WithPassword("password");
276+
});
246277

247278
break;
248279
}
@@ -258,7 +289,7 @@ protected static ContextProviderType[] InMemoryContextProviders()
258289
=> new[]
259290
{
260291
ContextProviderType.InMemory,
261-
ContextProviderType.EntityFrameworkCore,
292+
ContextProviderType.EntityFrameworkCore,
262293
};
263294

264295
protected static ContextProviderType[] SqlServerContextProviders()
@@ -293,7 +324,7 @@ private static CachingProviderType[] CachingProviders()
293324
//TODO: Cannot test when Appveyor is running tests.
294325
//I am not able to find the pre-built binaries so that I can manually run
295326
//the server (similar to redis and memcached). I am going to comment out testing couchbase for now
296-
//CachingProviderType.Couchbase,
327+
// CachingProviderType.Couchbase,
297328
};
298329

299330
private static ContextProviderType[] ContextProviders()

test/DotNetToolkit.Repository.Test/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<param name="minLogLevel" value="Debug" />
1414
</loggingProvider>
1515
<cachingProvider type="DotNetToolkit.Repository.Test.Data.TestCacheProvider, DotNetToolkit.Repository.Test">
16-
<expiry value="00:00:30" />
16+
<param name="expiry" value="00:00:30" />
1717
</cachingProvider>
1818
<interceptors>
1919
<interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">

0 commit comments

Comments
 (0)