DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/IntegrationTests/TestWebApplicationFactory.cs

46 lines
1.2 KiB
C#
Raw Normal View History

using System.Linq;
using System.Threading.Tasks;
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;
namespace AsbCloudWebApi.Tests.IntegrationTests;
public class TestWebApplicationFactory : WebApplicationFactory<Startup>,
IAsyncLifetime
{
private readonly PostgreSqlContainer _dbContainer = new PostgreSqlBuilder()
.WithImage("timescale/timescaledb:latest-pg15")
.WithDatabase("AsbCloud")
.WithUsername("postgres")
.WithPassword("postgres")
.Build();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>));
if (descriptor is not null)
services.Remove(descriptor);
services.AddDbContext<AsbCloudDbContext>(options =>
options.UseNpgsql(_dbContainer.GetConnectionString()));
});
}
public Task InitializeAsync()
{
return _dbContainer.StartAsync();
}
public new Task DisposeAsync()
{
return _dbContainer.StopAsync();
}
}