2024-11-20 16:39:52 +05:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2024-11-14 15:17:43 +05:00
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-11-25 10:09:38 +05:00
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2024-11-21 11:41:53 +05:00
|
|
|
using Persistence.API;
|
2024-11-22 17:52:15 +05:00
|
|
|
using Persistence.Database;
|
2024-11-25 10:09:38 +05:00
|
|
|
using Persistence.Client;
|
2024-11-14 15:17:43 +05:00
|
|
|
using Persistence.Database.Model;
|
2024-11-18 14:22:09 +05:00
|
|
|
using Persistence.Database.Postgres;
|
2024-11-21 11:41:53 +05:00
|
|
|
using RestSharp;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
|
|
namespace Persistence.IntegrationTests;
|
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
|
|
|
{
|
2024-11-25 10:09:38 +05:00
|
|
|
private string connectionString = string.Empty;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
2024-11-25 10:09:38 +05:00
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
{
|
|
|
|
builder.ConfigureAppConfiguration((hostingContext, config) =>
|
|
|
|
{
|
|
|
|
config.AddJsonFile("appsettings.Tests.json");
|
2024-11-21 11:41:53 +05:00
|
|
|
|
2024-11-25 10:09:38 +05:00
|
|
|
var dbConnection = config.Build().GetSection("DbConnection").Get<DbConnection>()!;
|
|
|
|
connectionString = dbConnection.GetConnectionString();
|
|
|
|
});
|
2024-11-21 11:41:53 +05:00
|
|
|
|
2024-11-25 10:09:38 +05:00
|
|
|
builder.ConfigureServices(services =>
|
2024-11-14 15:17:43 +05:00
|
|
|
{
|
|
|
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<PersistenceDbContext>));
|
|
|
|
if (descriptor != null)
|
|
|
|
services.Remove(descriptor);
|
2024-11-25 10:09:38 +05:00
|
|
|
services.AddDbContext<PersistenceDbContext>(options =>
|
2024-11-14 15:17:43 +05:00
|
|
|
options.UseNpgsql(connectionString));
|
|
|
|
|
2024-11-21 14:50:36 +05:00
|
|
|
services.RemoveAll<IHttpClientFactory>();
|
|
|
|
services.AddSingleton<IHttpClientFactory>(provider =>
|
|
|
|
{
|
|
|
|
return new TestHttpClientFactory(this);
|
|
|
|
});
|
|
|
|
|
2024-11-25 10:09:38 +05:00
|
|
|
services.AddSingleton<PersistenceClientFactory>();
|
|
|
|
|
2024-11-21 14:50:36 +05:00
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
var scopedServices = scope.ServiceProvider;
|
|
|
|
|
2024-11-21 14:50:36 +05:00
|
|
|
var dbContext = scopedServices.GetRequiredService<PersistenceDbContext>();
|
2024-11-18 14:22:09 +05:00
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
2024-11-14 15:17:43 +05:00
|
|
|
dbContext.SaveChanges();
|
2024-11-25 10:09:38 +05:00
|
|
|
});
|
2024-11-14 15:17:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
var dbContext = new PersistenceDbContext(
|
|
|
|
new DbContextOptionsBuilder<PersistenceDbContext>()
|
|
|
|
.UseNpgsql(connectionString)
|
|
|
|
.Options);
|
|
|
|
|
|
|
|
await dbContext.Database.EnsureDeletedAsync();
|
|
|
|
}
|
|
|
|
}
|