Skip to content

Commit

Permalink
Merge branch 'feat/postgres'
Browse files Browse the repository at this point in the history
  • Loading branch information
mnbuhl committed Aug 15, 2024
2 parents bfa5743 + c05e154 commit 21d65d0
Show file tree
Hide file tree
Showing 16 changed files with 754 additions and 694 deletions.
101 changes: 54 additions & 47 deletions tests/PersistedCache.Tests/Common/BaseDatabaseFixture.cs
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;
}
}
}
32 changes: 18 additions & 14 deletions tests/PersistedCache.Tests/Common/BaseTest.cs
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();
}
}
}
9 changes: 5 additions & 4 deletions tests/PersistedCache.Tests/Common/TestConstants.cs
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";
}
}
28 changes: 15 additions & 13 deletions tests/PersistedCache.Tests/Fixtures/MySqlFixture.cs
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 => '`';
}
}
28 changes: 15 additions & 13 deletions tests/PersistedCache.Tests/Fixtures/PostgreSqlFixture.cs
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 => '"';
}
}
Loading

0 comments on commit 21d65d0

Please sign in to comment.