-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
754 additions
and
694 deletions.
There are no files selected for viewing
101 changes: 54 additions & 47 deletions
101
tests/PersistedCache.Tests/Common/BaseDatabaseFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,76 @@ | ||
using Dapper; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Dapper; | ||
using DotNet.Testcontainers.Containers; | ||
using PersistedCache.PostgreSql; | ||
using PersistedCache.Sql; | ||
using Xunit; | ||
|
||
namespace PersistedCache.Tests.Common; | ||
|
||
public abstract class BaseDatabaseFixture<TDriver> : IAsyncLifetime where TDriver : ISqlCacheDriver | ||
namespace PersistedCache.Tests.Common | ||
{ | ||
public IPersistedCache PersistedCache { get; private set; } = null!; | ||
public abstract class BaseDatabaseFixture<TDriver> : IAsyncLifetime where TDriver : ISqlCacheDriver | ||
{ | ||
public IPersistedCache PersistedCache { get; private set; } | ||
|
||
protected DockerContainer Container = null!; | ||
protected DockerContainer Container; | ||
|
||
protected abstract char LeftEscapeCharacter { get; } | ||
protected abstract char RightEscapeCharacter { get; } | ||
protected abstract char LeftEscapeCharacter { get; } | ||
protected abstract char RightEscapeCharacter { get; } | ||
|
||
private ISqlCacheDriver _driver = null!; | ||
private ISqlCacheDriver _driver; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await Container.StartAsync(); | ||
public async Task InitializeAsync() | ||
{ | ||
await Container.StartAsync(); | ||
|
||
var options = GetOptions((Container as IDatabaseContainer)!.GetConnectionString()); | ||
var driver = (TDriver)Activator.CreateInstance(typeof(TDriver), options)!; | ||
var options = GetOptions((Container as IDatabaseContainer).GetConnectionString()); | ||
var driver = (TDriver)Activator.CreateInstance(typeof(TDriver), options); | ||
|
||
SetupStorage(driver); | ||
SetupStorage(driver); | ||
|
||
_driver = driver; | ||
PersistedCache = new SqlPersistedCache(driver, options); | ||
} | ||
_driver = driver; | ||
PersistedCache = new SqlPersistedCache(driver, options); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await Container.DisposeAsync(); | ||
} | ||
|
||
public IEnumerable<dynamic> ExecuteSql(string sql) | ||
{ | ||
sql = sql.Replace("<|", $"{LeftEscapeCharacter}") | ||
.Replace("|>", $"{RightEscapeCharacter}"); | ||
using var connection = _driver.CreateConnection(); | ||
var result = connection.Query(sql); | ||
return result; | ||
} | ||
public async Task DisposeAsync() | ||
{ | ||
await Container.DisposeAsync(); | ||
} | ||
|
||
private static void SetupStorage(ISqlCacheDriver driver) | ||
{ | ||
var connectionFactory = new SqlConnectionFactory(driver); | ||
|
||
connectionFactory.RunInTransaction((connection, transaction) => | ||
public IEnumerable<dynamic> ExecuteSql(string sql) | ||
{ | ||
connection.Execute(driver.SetupStorageScript, transaction: transaction); | ||
}); | ||
} | ||
sql = sql.Replace("<|", $"{LeftEscapeCharacter}") | ||
.Replace("|>", $"{RightEscapeCharacter}"); | ||
using (var connection = _driver.CreateConnection()) | ||
{ | ||
var result = connection.Query(sql); | ||
return result; | ||
} | ||
} | ||
|
||
private static ISqlPersistedCacheOptions GetOptions(string connectionString) | ||
{ | ||
ISqlPersistedCacheOptions options = new SqlPersistedCacheOptions(connectionString); | ||
if (typeof(TDriver) == typeof(PostgreSqlCacheDriver)) | ||
private static void SetupStorage(ISqlCacheDriver driver) | ||
{ | ||
options = new PostgreSqlPersistedCacheOptions(connectionString); | ||
var connectionFactory = new SqlConnectionFactory(driver); | ||
|
||
connectionFactory.RunInTransaction((connection, transaction) => | ||
{ | ||
connection.Execute(driver.SetupStorageScript, transaction: transaction); | ||
}); | ||
} | ||
|
||
private static ISqlPersistedCacheOptions GetOptions(string connectionString) | ||
{ | ||
ISqlPersistedCacheOptions options = new SqlPersistedCacheOptions(connectionString); | ||
if (typeof(TDriver) == typeof(PostgreSqlCacheDriver)) | ||
{ | ||
options = new PostgreSqlPersistedCacheOptions(connectionString); | ||
} | ||
|
||
options.TableName = TestConstants.TableName; | ||
options.CreateTableIfNotExists = false; | ||
options.TableName = TestConstants.TableName; | ||
options.CreateTableIfNotExists = false; | ||
|
||
return options; | ||
return options; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
namespace PersistedCache.Tests.Common; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
public class BaseTest : IAsyncLifetime | ||
namespace PersistedCache.Tests.Common | ||
{ | ||
private readonly IPersistedCache _cache; | ||
|
||
public BaseTest(IPersistedCache cache) | ||
public class BaseTest : IAsyncLifetime | ||
{ | ||
_cache = cache; | ||
} | ||
private readonly IPersistedCache _cache; | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
public BaseTest(IPersistedCache cache) | ||
{ | ||
_cache = cache; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _cache.FlushAsync(); | ||
public Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _cache.FlushAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
namespace PersistedCache.Tests.Common; | ||
|
||
public static class TestConstants | ||
namespace PersistedCache.Tests.Common | ||
{ | ||
public const string TableName = "persisted_cache"; | ||
public static class TestConstants | ||
{ | ||
public const string TableName = "persisted_cache"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
using PersistedCache.MySql; | ||
using PersistedCache.Tests.Common; | ||
using Testcontainers.MySql; | ||
using Xunit; | ||
|
||
|
||
namespace PersistedCache.Tests.Fixtures; | ||
|
||
[CollectionDefinition(nameof(MySqlFixture))] | ||
public class MySqlFixture : BaseDatabaseFixture<MySqlCacheDriver>, ICollectionFixture<MySqlFixture> | ||
namespace PersistedCache.Tests.Fixtures | ||
{ | ||
public MySqlFixture() | ||
[CollectionDefinition(nameof(MySqlFixture))] | ||
public class MySqlFixture : BaseDatabaseFixture<MySqlCacheDriver>, ICollectionFixture<MySqlFixture> | ||
{ | ||
Container = new MySqlBuilder() | ||
.WithDatabase("PersistedCache") | ||
.WithUsername("root") | ||
.WithPassword("root") | ||
.Build(); | ||
} | ||
public MySqlFixture() | ||
{ | ||
Container = new MySqlBuilder() | ||
.WithDatabase("PersistedCache") | ||
.WithUsername("root") | ||
.WithPassword("root") | ||
.Build(); | ||
} | ||
|
||
protected override char LeftEscapeCharacter => '`'; | ||
protected override char RightEscapeCharacter => '`'; | ||
protected override char LeftEscapeCharacter => '`'; | ||
protected override char RightEscapeCharacter => '`'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
using PersistedCache.PostgreSql; | ||
using PersistedCache.Tests.Common; | ||
using Testcontainers.PostgreSql; | ||
using Xunit; | ||
|
||
namespace PersistedCache.Tests.Fixtures; | ||
|
||
[CollectionDefinition(nameof(PostgreSqlFixture))] | ||
public class PostgreSqlFixture : BaseDatabaseFixture<PostgreSqlCacheDriver>, ICollectionFixture<PostgreSqlFixture> | ||
namespace PersistedCache.Tests.Fixtures | ||
{ | ||
public PostgreSqlFixture() | ||
[CollectionDefinition(nameof(PostgreSqlFixture))] | ||
public class PostgreSqlFixture : BaseDatabaseFixture<PostgreSqlCacheDriver>, ICollectionFixture<PostgreSqlFixture> | ||
{ | ||
Container = new PostgreSqlBuilder() | ||
.WithDatabase("PersistedCache") | ||
.WithUsername("postgres") | ||
.WithPassword("postgres") | ||
.Build(); | ||
} | ||
public PostgreSqlFixture() | ||
{ | ||
Container = new PostgreSqlBuilder() | ||
.WithDatabase("PersistedCache") | ||
.WithUsername("postgres") | ||
.WithPassword("postgres") | ||
.Build(); | ||
} | ||
|
||
protected override char LeftEscapeCharacter => '"'; | ||
protected override char RightEscapeCharacter => '"'; | ||
protected override char LeftEscapeCharacter => '"'; | ||
protected override char RightEscapeCharacter => '"'; | ||
} | ||
} |
Oops, something went wrong.