persistence/DD.Persistence.Repository.Test/RepositoryTestFixture.cs

33 lines
1.0 KiB
C#
Raw Normal View History


using DD.Persistence.Database;
using DD.Persistence.Database.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Testcontainers.PostgreSql;
using Xunit;
namespace DD.Persistence.Repository.Test;
public class RepositoryTestFixture : IAsyncLifetime
{
public readonly PostgreSqlContainer dbContainer = new PostgreSqlBuilder().Build();
public PersistencePostgresContext GetDbContext() => new(new DbContextOptionsBuilder<PersistencePostgresContext>()
.UseNpgsql(dbContainer.GetConnectionString()).Options);
public IMemoryCache GetMemoryCache() => new MemoryCache(new MemoryCacheOptions());
public virtual async Task InitializeAsync()
{
await dbContainer.StartAsync();
var forumDbContext = new PersistencePostgresContext(new DbContextOptionsBuilder<PersistencePostgresContext>()
.UseNpgsql(dbContainer.GetConnectionString()).Options);
await forumDbContext.Database.MigrateAsync();
}
public async Task DisposeAsync() => await dbContainer.DisposeAsync();
}