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-12-09 14:45:35 +05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2024-12-16 15:38:46 +05:00
|
|
|
using DD.Persistence.API;
|
|
|
|
using DD.Persistence.Client;
|
|
|
|
using DD.Persistence.Database.Model;
|
|
|
|
using DD.Persistence.Database.Postgres;
|
2024-11-21 11:41:53 +05:00
|
|
|
using RestSharp;
|
2024-12-16 17:47:12 +05:00
|
|
|
using DD.Persistence.App;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
2024-12-16 15:38:46 +05:00
|
|
|
namespace DD.Persistence.IntegrationTests;
|
2024-12-16 17:47:12 +05:00
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Program>
|
2024-11-14 15:17:43 +05:00
|
|
|
{
|
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)
|
2024-12-09 13:19:55 +05:00
|
|
|
{
|
2024-11-25 10:09:38 +05:00
|
|
|
builder.ConfigureAppConfiguration((hostingContext, config) =>
|
|
|
|
{
|
|
|
|
config.AddJsonFile("appsettings.Tests.json");
|
2024-11-21 11:41:53 +05:00
|
|
|
|
2024-12-09 13:19:55 +05:00
|
|
|
var dbConnection = config.Build().GetSection("DbConnection").Get<DbConnection>()!;
|
2024-12-17 17:53:30 +05:00
|
|
|
//connectionString = dbConnection.GetConnectionString();
|
|
|
|
connectionString = "postgres://postgres:postgres@localhost:5442/persistence";
|
2024-12-09 13:19:55 +05:00
|
|
|
});
|
2024-11-21 11:41:53 +05:00
|
|
|
|
2024-12-09 13:19:55 +05:00
|
|
|
builder.ConfigureServices(services =>
|
2024-11-14 15:17:43 +05:00
|
|
|
{
|
2024-11-26 15:07:18 +05:00
|
|
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<PersistencePostgresContext>));
|
2024-11-14 15:17:43 +05:00
|
|
|
if (descriptor != null)
|
|
|
|
services.Remove(descriptor);
|
2024-11-26 15:07:18 +05:00
|
|
|
|
|
|
|
services.AddDbContext<PersistencePostgresContext>(options =>
|
2024-11-14 15:17:43 +05:00
|
|
|
options.UseNpgsql(connectionString));
|
|
|
|
|
2024-12-09 14:45:35 +05:00
|
|
|
services.AddLogging(builder => builder.AddConsole());
|
|
|
|
|
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-12-09 13:19:55 +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-26 15:07:18 +05:00
|
|
|
var dbContext = scopedServices.GetRequiredService<PersistencePostgresContext>();
|
2024-11-18 14:22:09 +05:00
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
2024-11-14 15:17:43 +05:00
|
|
|
dbContext.SaveChanges();
|
2024-12-09 13:19:55 +05:00
|
|
|
});
|
2024-11-14 15:17:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
{
|
2024-11-26 15:07:18 +05:00
|
|
|
var dbContext = new PersistencePostgresContext(
|
|
|
|
new DbContextOptionsBuilder<PersistencePostgresContext>()
|
2024-11-14 15:17:43 +05:00
|
|
|
.UseNpgsql(connectionString)
|
|
|
|
.Options);
|
|
|
|
|
|
|
|
await dbContext.Database.EnsureDeletedAsync();
|
2024-12-10 10:43:12 +05:00
|
|
|
|
2024-12-12 13:37:07 +05:00
|
|
|
GC.SuppressFinalize(this);
|
2024-11-14 15:17:43 +05:00
|
|
|
}
|
|
|
|
}
|