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;
|
|
|
|
using Persistence.Database.Model;
|
2024-11-18 14:22:09 +05:00
|
|
|
using Persistence.Database.Postgres;
|
2024-11-14 15:17:43 +05:00
|
|
|
using Persistence.API;
|
|
|
|
using Refit;
|
|
|
|
using System.Text.Json;
|
2024-11-18 11:32:57 +05:00
|
|
|
using Persistence.Database.Postgres;
|
2024-11-20 16:08:16 +05:00
|
|
|
using System.Net.Http.Headers;
|
2024-11-21 14:50:36 +05:00
|
|
|
using Persistence.Client;
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2024-11-14 15:17:43 +05:00
|
|
|
|
|
|
|
namespace Persistence.IntegrationTests;
|
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
|
|
|
{
|
|
|
|
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
//Converters = { new ValidationResultConverter() }
|
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
|
|
|
|
|
|
|
private readonly string connectionString;
|
|
|
|
|
|
|
|
public WebAppFactoryFixture()
|
|
|
|
{
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
.AddJsonFile("appsettings.Tests.json")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
|
|
|
|
connectionString = dbConnection.GetConnectionString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 11:32:57 +05:00
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
2024-11-14 15:17:43 +05:00
|
|
|
//dbContext.Deposits.AddRange(Data.Defaults.Deposits);
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|