Skip to content

Commit

Permalink
(GH-613) Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johelvisguzman committed Oct 18, 2021
1 parent 2fa7ede commit f6a1af7
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 28 deletions.
1 change: 0 additions & 1 deletion test/DotNetToolkit.Repository.Integration.Test/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</system.data>
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" />
<add name="AzureStorageTableConnection" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;" />
</connectionStrings>
<repository>
<defaultContextFactory type="DotNetToolkit.Repository.InMemory.Internal.InMemoryRepositoryContextFactory, DotNetToolkit.Repository.InMemory" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,98 @@
namespace DotNetToolkit.Repository.Integration.Test.Fixtures
{
using Couchbase;
using Couchbase.Configuration.Client;
#if NETFULL
using Enyim.Caching;
using Enyim.Caching.Configuration;
#endif
using StackExchange.Redis;
using System;
using System.Collections.Generic;

public class RepositoryCachingTestsFixture : IDisposable
{
public RepositoryCachingTestsFixture()
{
// ClearCouchbaseDatabase();
#if NETFULL
Running.CachingServerManager.StartMemcached();
ClearMemcachedDatabase();
#endif
Running.CachingServerManager.StartRedis();
ClearRedisDatabase();
}

public void Dispose()
{
// ClearCouchbaseDatabase();
#if NETFULL
ClearMemcachedDatabase();
Running.CachingServerManager.StopMemcached();
#endif
ClearRedisDatabase();
Running.CachingServerManager.StopRedis();
}

#if NETFULL
private void ClearMemcachedDatabase()
{
var config = new MemcachedClientConfiguration();

config.AddServer("127.0.0.1", 11211);

using (var client = new MemcachedClient(config))
{
client.FlushAll();
}
}
#endif

private void ClearRedisDatabase()
{
var options = new ConfigurationOptions()
{
AllowAdmin = true,
DefaultDatabase = 0,
EndPoints =
{
{ "localhost" }
}
};

using (var connection = ConnectionMultiplexer.Connect(options))
{
var server = connection.GetServer(connection.GetEndPoints()[0]);
server.FlushAllDatabases();
}
}

private void ClearCouchbaseDatabase()
{
const string username = "default";
const string bucketName = "default";
const string password = "password";

var config = new ClientConfiguration
{
Servers = new List<Uri> { new Uri("http://localhost:8091") },
BucketConfigs = new Dictionary<string, BucketConfiguration>
{
{ bucketName, new BucketConfiguration
{
Username = username,
Password = password,
BucketName = bucketName
}
}
}
};

using (var cluster = new Cluster(config))
using (var bucket = cluster.OpenBucket())
{
var result = bucket.CreateManager(username, password).Flush();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public RepositoryCachingTests(ITestOutputHelper testOutputHelper) : base(testOut
public void CacheEnabled()
{
var options = GetRepositoryOptionsBuilder(ContextProviderType.InMemory)
.UseCachingProvider(new InMemoryCacheProvider())
.UseInMemoryCache()
.Options;

var repo = new Repository<Customer>(options);
Expand Down Expand Up @@ -57,7 +57,7 @@ public void CacheDisabled()
public void ClearCache()
{
var options = GetRepositoryOptionsBuilder(ContextProviderType.InMemory)
.UseCachingProvider(new InMemoryCacheProvider())
.UseInMemoryCache()
.Options;

var customerRepo = new Repository<Customer>(options);
Expand Down
39 changes: 20 additions & 19 deletions test/DotNetToolkit.Repository.Integration.Test/Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,41 +208,42 @@ private static void ApplyCachingProvider(CachingProviderType cachingProvider, Re
{
case CachingProviderType.MicrosoftInMemory:
{
builder.UseCachingProvider(new InMemoryCacheProvider());
builder.UseInMemoryCache();

break;
}
case CachingProviderType.Redis:
{
var provider = new RedisCacheProvider(allowAdmin: true, defaultDatabase: 0, expiry: null);

provider.Cache.Server.FlushAllDatabases();

builder.UseCachingProvider(provider);
builder.UseRedis(options =>
{
options
.WithAllowAdmin()
.WithDefaultDatabase(0);
});

break;
}
#if NETFULL
case CachingProviderType.Memcached:
{
var provider = new MemcachedCacheProvider("127.0.0.1", 11211);

provider.Cache.Client.FlushAll();

builder.UseCachingProvider(provider);
builder.UseMemcached(options =>
{
options.WithEndPoint("127.0.0.1", 11211);
});

break;
}
#endif
case CachingProviderType.Couchbase:
{
var provider = new CouchbaseCacheProvider("http://localhost:8091", "default", "password");

using (var bucket = provider.Cache.Cluster.OpenBucket())
builder.UseCouchbase(options =>
{
bucket.CreateManager("default", "password").Flush();
}

builder.UseCachingProvider(provider);
options
.WithEndPoint("http://localhost:8091")
.WithBucketName("default")
.WithUsername("default")
.WithPassword("password");
});

break;
}
Expand All @@ -258,7 +259,7 @@ protected static ContextProviderType[] InMemoryContextProviders()
=> new[]
{
ContextProviderType.InMemory,
ContextProviderType.EntityFrameworkCore,
ContextProviderType.EntityFrameworkCore,
};

protected static ContextProviderType[] SqlServerContextProviders()
Expand Down
2 changes: 1 addition & 1 deletion test/DotNetToolkit.Repository.Test/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<param name="minLogLevel" value="Debug" />
</loggingProvider>
<cachingProvider type="DotNetToolkit.Repository.Test.Data.TestCacheProvider, DotNetToolkit.Repository.Test">
<expiry value="00:00:30" />
<param name="expiry" value="00:00:30" />
</cachingProvider>
<interceptors>
<interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">
Expand Down
19 changes: 16 additions & 3 deletions test/DotNetToolkit.Repository.Test/Data/TestCacheProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@

public class TestCacheProvider : ICacheProvider
{
public ICacheKeyTransformer KeyTransformer { get; set; }
public int Increment(string key, int defaultValue, int incrementValue)
{
return 0;
}

public TimeSpan? Expiry { get; set; }
public void Remove(string key)
{
}

public ICache Cache { get; set; }
public void Set<T>(string key, T value, TimeSpan? expiry = null, Action<string> cacheRemovedCallback = null)
{
}

public bool TryGetValue<T>(string key, out T value)
{
value = default(T);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ private static void TestConfiguration(RepositoryOptionsBuilder optionsBuilder)

// caching provider
Assert.NotNull(optionsBuilder.Options.CachingProvider);
Assert.Equal(optionsBuilder.Options.CachingProvider.Expiry, TimeSpan.FromSeconds(30));

// interceptor
Assert.Single(optionsBuilder.Options.Interceptors);
Expand Down
2 changes: 1 addition & 1 deletion test/DotNetToolkit.Repository.Test/repository.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<param name="minLogLevel" value="Debug" />
</loggingProvider>
<cachingProvider type="DotNetToolkit.Repository.Test.Data.TestCacheProvider, DotNetToolkit.Repository.Test">
<expiry value="00:00:30" />
<param name="expiry" value="00:00:30" />
</cachingProvider>
<interceptors>
<interceptor type="DotNetToolkit.Repository.Test.Data.TestRepositoryInterceptor, DotNetToolkit.Repository.Test">
Expand Down

0 comments on commit f6a1af7

Please sign in to comment.