67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using JSMR.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Testcontainers.MariaDb;
|
|
|
|
namespace JSMR.Tests.Fixtures;
|
|
|
|
public class MariaDbFixture : IAsyncLifetime
|
|
{
|
|
const int MajorVersion = 10;
|
|
const int MinorVersion = 11;
|
|
const int Build = 6;
|
|
|
|
public MariaDbContainer? MariaDbContainer { get; private set; }
|
|
|
|
public string ConnectionString { get; private set; } = default!;
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
MariaDbContainer = new MariaDbBuilder()
|
|
.WithImage($"mariadb:{MajorVersion}.{MinorVersion}.{Build}")
|
|
.Build();
|
|
|
|
await MariaDbContainer.StartAsync();
|
|
|
|
ConnectionString = MariaDbContainer.GetConnectionString();
|
|
|
|
await using AppDbContext context = CreateDbContext();
|
|
await context.Database.EnsureCreatedAsync();
|
|
//await context.Database.MigrateAsync(); // Testing
|
|
await OnInitializedAsync(context);
|
|
}
|
|
|
|
protected virtual Task OnInitializedAsync(AppDbContext context)
|
|
{
|
|
return Task.FromResult(Task.CompletedTask);
|
|
}
|
|
|
|
public async Task DisposeAsync()
|
|
{
|
|
if (MariaDbContainer is not null)
|
|
{
|
|
await MariaDbContainer.StopAsync();
|
|
await MariaDbContainer.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
public AppDbContext CreateDbContext()
|
|
{
|
|
MySqlServerVersion serverVersion = new(new Version(MajorVersion, MinorVersion, Build));
|
|
|
|
DbContextOptions<AppDbContext> options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseMySql(ConnectionString, serverVersion,
|
|
o => o.EnableRetryOnFailure())
|
|
.EnableSensitiveDataLogging()
|
|
.Options;
|
|
|
|
return new AppDbContext(options);
|
|
}
|
|
|
|
public async Task ResetAsync()
|
|
{
|
|
await using AppDbContext context = CreateDbContext();
|
|
|
|
await context.Database.EnsureDeletedAsync();
|
|
await context.Database.EnsureCreatedAsync();
|
|
}
|
|
} |