using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using DD.Persistence.API; using DD.Persistence.Client; using DD.Persistence.Database.Model; using DD.Persistence.Database.Postgres; using RestSharp; using DD.Persistence.App; using DD.Persistence.Client.Helpers; using DD.Persistence.Factories; using System.Net; namespace DD.Persistence.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory { private string connectionString = string.Empty; protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.Tests.json"); var dbConnection = config.Build().GetSection("DbConnection").Get()!; connectionString = dbConnection.GetConnectionString(); //connectionString = "Host=postgres;Port=5442;Username=postgres;Password=postgres;Database=persistence"; }); builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); if (descriptor != null) services.Remove(descriptor); services.AddDbContext(options => options.UseNpgsql(connectionString)); services.AddLogging(builder => builder.AddConsole()); services.RemoveAll(); services.AddSingleton((provider) => { return new TestHttpClientFactory(this, provider.GetRequiredService()); }); services.AddHttpClient(); services.AddPersistenceClients(); var serviceProvider = services.BuildServiceProvider(); using var scope = serviceProvider.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureCreatedAndMigrated(); dbContext.SaveChanges(); }); } public override async ValueTask DisposeAsync() { var dbContext = new PersistencePostgresContext( new DbContextOptionsBuilder() .UseNpgsql(connectionString) .Options); await dbContext.Database.EnsureDeletedAsync(); GC.SuppressFinalize(this); } }