#region ========================================================================= USING ===================================================================================== using Lumina.DataAccess.Core.UoW; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Diagnostics.CodeAnalysis; using System.Linq; #endregion namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup; /// /// Factory for creating a web application for integration tests. /// [ExcludeFromCodeCoverage] public class LuminaApiFactory : WebApplicationFactory, IDisposable { #region ================================================================== FIELD MEMBERS ================================================================================ private readonly SqliteConnection _connection; #endregion #region ====================================================================== CTOR ===================================================================================== /// /// Initializes a new instance of the class. /// public LuminaApiFactory() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); } #endregion #region ===================================================================== METHODS =================================================================================== /// /// Configures the web host for the integration tests. /// /// The web host builder to configure. protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(DbContextOptions)); if (descriptor is not null) services.Remove(descriptor); services.AddDbContext(options => { options.UseSqlite(_connection); }); var servicePreovider = services.BuildServiceProvider(); using (var scope = servicePreovider.CreateScope()) { var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService(); dbContext.Database.EnsureCreated(); } }); } /// /// Disposes the connection to the database. /// public new void Dispose() { _connection.Close(); base.Dispose(); } #endregion }