59 lines
No EOL
1.6 KiB
C#
59 lines
No EOL
1.6 KiB
C#
using DotNet.Testcontainers.Containers;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using Npgsql;
|
|
using Testcontainers.PostgreSql;
|
|
|
|
namespace IdentityShroud.Core.Tests.Fixtures;
|
|
|
|
public class DbFixture : IAsyncLifetime
|
|
{
|
|
private readonly PostgreSqlContainer _postgresqlServer;
|
|
|
|
public Db CreateDbContext(string dbName = "testdb")
|
|
{
|
|
var db = new Db(Options.Create<DbConfiguration>(new()
|
|
{
|
|
ConnectionString = _postgresqlServer.GetConnectionString(),
|
|
LogSensitiveData = false,
|
|
}), new NullLoggerFactory());
|
|
return db;
|
|
}
|
|
|
|
public DbFixture()
|
|
{
|
|
_postgresqlServer = new PostgreSqlBuilder("postgres:18.1")
|
|
.WithName("is-dbfixture-" + Guid.NewGuid().ToString("D"))
|
|
.Build();
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await _postgresqlServer.StopAsync();
|
|
}
|
|
|
|
public async ValueTask InitializeAsync()
|
|
{
|
|
await _postgresqlServer.StartAsync();
|
|
}
|
|
|
|
public NpgsqlConnection GetConnection(string dbname)
|
|
{
|
|
string connString = _postgresqlServer.GetConnectionString()
|
|
+ $";Database={dbname}";
|
|
var connection = new NpgsqlConnection(connString);
|
|
connection.Open();
|
|
return connection;
|
|
}
|
|
}
|
|
|
|
/*
|
|
|
|
[CollectionDefinition("PostgresqlFixtureCollection", DisableParallelization = false)]
|
|
public class PostgresqlFactoryCollection : ICollectionFixture<PostgresqlFixture>
|
|
{
|
|
// This class has no code, and is never created. Its purpose is simply
|
|
// to be the place to apply [CollectionDefinition] and all the
|
|
// ICollectionFixture<> interfaces.
|
|
}
|
|
*/ |