Adds Presentation Api Layer integration tests

This commit is contained in:
BinaryAlley committed 2024-08-28 21:52:19 +03:00
1 parent a3011297ca
commit 9d840db864
9 files changed
+1083

No files matched your search

@@ -0,0 +1,39 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
/// <summary>
/// Contains extension methods for the <see cref="Faker"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public static class BogusExtensions
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Adds <see cref="DateOnly"/> random data generation support to the <see cref="Faker"/> class.
/// </summary>
/// <param name="faker">The faker instance to extend.</param>
/// <returns>A random<see cref="DateOnly"/> instance.</returns>
public static DateOnly DateOnly(this Faker faker)
{
return System.DateOnly.FromDateTime(faker.Date.Past());
}
/// <summary>
/// Adds <see cref="DateOnly"/> random data generation within a specified interval support to the <see cref="Faker"/> class.
/// </summary>
/// <param name="faker">The faker instance to extend.</param>
/// <param name="start">The start of the interval.</param>
/// <param name="end">The end of the interval.</param>
/// <returns>A random<see cref="DateOnly"/> instance.</returns>
public static DateOnly DateOnlyBetween(this Faker faker, DateOnly start, DateOnly end)
{
var startDateTime = start.ToDateTime(TimeOnly.MinValue);
var endDateTime = end.ToDateTime(TimeOnly.MinValue);
return System.DateOnly.FromDateTime(faker.Date.Between(startDateTime, endDateTime));
}
#endregion
}
@@ -0,0 +1,28 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
/// <summary>
/// Represents a specimen builder for the <see cref="DateOnly"/> type.
/// </summary>
[ExcludeFromCodeCoverage]
public class DateOnlySpecimenBuilder : ISpecimenBuilder
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Adds <see cref="DateOnly"/> support to the AutoFixture library.
/// </summary>
/// <param name="request">The request to process.</param>
/// <param name="context">The context within which the specimen is created.</param>
/// <returns>A specimen based on the request.</returns>
public object Create(object request, ISpecimenContext context)
{
if (request is Type type && type == typeof(DateOnly))
return DateOnly.FromDateTime(DateTime.Now);
return new NoSpecimen();
}
#endregion
}
@@ -0,0 +1,69 @@
#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.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
/// <summary>
/// Factory for creating a web application for integration tests.
/// </summary>
[ExcludeFromCodeCoverage]
public class LuminaApiFactory : WebApplicationFactory<Program>, IDisposable
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly SqliteConnection _connection;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="LuminaApiFactory"/> class.
/// </summary>
public LuminaApiFactory()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Configures the web host for the integration tests.
/// </summary>
/// <param name="builder">The web host builder to configure.</param>
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(DbContextOptions<LuminaDbContext>));
if (descriptor is not null)
services.Remove(descriptor);
services.AddDbContext<LuminaDbContext>(options =>
{
options.UseSqlite(_connection);
});
var servicePreovider = services.BuildServiceProvider();
using (var scope = servicePreovider.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var dbContext = scopedServices.GetRequiredService<LuminaDbContext>();
dbContext.Database.EnsureCreated();
}
});
}
/// <summary>
/// Disposes the connection to the database.
/// </summary>
public new void Dispose()
{
_connection.Close();
base.Dispose();
}
#endregion
}
@@ -0,0 +1,28 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture.Kernel;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
/// <summary>
/// Represents a specimen builder for the <see cref="DateOnly"/> type.
/// </summary>
[ExcludeFromCodeCoverage]
public class NullableDateOnlySpecimenBuilder : ISpecimenBuilder
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Adds <see cref="DateOnly?"/> support to the AutoFixture library.
/// </summary>
/// <param name="request">The request to process.</param>
/// <param name="context">The context within which the specimen is created.</param>
/// <returns>A specimen based on the request.</returns>
public object Create(object request, ISpecimenContext context)
{
if (request is Type type && type == typeof(DateOnly?))
return (DateOnly?)DateOnly.FromDateTime(DateTime.Now);
return new NoSpecimen();
}
#endregion
}