Add integration and unit tests for Notes API with PostgreSQL-based test hosting configuration
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
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;");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user