persistence/Persistence.IntegrationTests/WebAppFactoryFixture.cs

69 lines
2.4 KiB
C#
Raw Permalink Normal View History

2024-11-20 16:39:52 +05:00
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 Persistence.API;
2024-11-22 17:52:15 +05:00
using Persistence.Database;
using Persistence.Client;
using Persistence.Database.Model;
using Persistence.Database.Postgres;
using RestSharp;
namespace Persistence.IntegrationTests;
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
{
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<DbConnection>()!;
connectionString = dbConnection.GetConnectionString();
});
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<PersistenceDbContext>));
if (descriptor != null)
services.Remove(descriptor);
services.AddDbContext<PersistenceDbContext>(options =>
options.UseNpgsql(connectionString));
2024-11-21 14:50:36 +05:00
services.RemoveAll<IHttpClientFactory>();
services.AddSingleton<IHttpClientFactory>(provider =>
{
return new TestHttpClientFactory(this);
});
services.AddSingleton<PersistenceClientFactory>();
2024-11-21 14:50:36 +05:00
var serviceProvider = services.BuildServiceProvider();
2024-11-22 17:52:15 +05:00
services.AddScoped<IPersistenceDbContext>(provider => provider.GetRequiredService<PersistenceDbContext>());
var serviceProvider = services.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var scopedServices = scope.ServiceProvider;
2024-11-21 14:50:36 +05:00
var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>();
dbContext.Database.EnsureCreatedAndMigrated();
dbContext.SaveChanges();
});
}
public override async ValueTask DisposeAsync()
{
var dbContext = new PersistenceDbContext(
new DbContextOptionsBuilder<PersistenceDbContext>()
.UseNpgsql(connectionString)
.Options);
await dbContext.Database.EnsureDeletedAsync();
}
}