43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using Freman.Sample.Web.Data;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Testcontainers.PostgreSql;
|
|
|
|
namespace Freman.Sample.Web.IntegrationTests.TestHost;
|
|
|
|
public sealed class IntegrationTestFixture : WebApplicationFactory<Program>, IAsyncLifetime
|
|
{
|
|
private readonly PostgreSqlContainer _pg = new PostgreSqlBuilder("postgres:16")
|
|
.WithDatabase("testdb")
|
|
.WithUsername("appuser")
|
|
.WithPassword("testpassword")
|
|
.Build();
|
|
|
|
public SampleWebFactory Factory { get; private set; } = null!;
|
|
|
|
public async ValueTask InitializeAsync()
|
|
{
|
|
await _pg.StartAsync();
|
|
|
|
Factory = new SampleWebFactory(_pg.GetConnectionString());
|
|
|
|
// Apply migrations once
|
|
using var scope = Factory.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
await db.Database.MigrateAsync();
|
|
}
|
|
|
|
public new async ValueTask DisposeAsync()
|
|
{
|
|
await Factory.DisposeAsync();
|
|
await _pg.DisposeAsync();
|
|
}
|
|
|
|
public async Task ResetDatabaseAsync()
|
|
{
|
|
using var scope = Factory.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
await db.Database.ExecuteSqlRawAsync(@"TRUNCATE TABLE notes RESTART IDENTITY;");
|
|
}
|
|
} |