Introduced the `TryResolveProfileIdAsync` method in the `RecApplicationTestBase` class to resolve a usable profile ID for tests. The method prioritizes a configured `FakeProfileId` and falls back to querying the database for the first available profile. Added necessary namespaces (`System.Linq`, `System.Threading.Tasks`, and `MediatR`) to support LINQ, async operations, and the `ISender` interface. Implemented dependency injection for querying profiles and added error handling to ensure robustness.
104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using ReC.Application;
|
|
using ReC.Application.Common.Options;
|
|
using ReC.Application.Profile.Queries;
|
|
using ReC.Infrastructure;
|
|
|
|
namespace ReC.Tests.Application;
|
|
|
|
public abstract class RecApplicationTestBase : IDisposable
|
|
{
|
|
protected RecApplicationTestBase()
|
|
{
|
|
Configuration = BuildConfiguration();
|
|
ServiceProvider = BuildServiceProvider(Configuration);
|
|
}
|
|
|
|
protected IConfiguration Configuration { get; }
|
|
|
|
protected IServiceProvider ServiceProvider { get; }
|
|
|
|
/// <summary>
|
|
/// Resolves a usable profile id for tests that require an existing profile in the database.
|
|
/// Prefers the configured <c>FakeProfileId</c> value; otherwise asks the server for the first
|
|
/// available profile via the standard read query. Returns <c>null</c> when no profile is
|
|
/// configured and none can be discovered.
|
|
/// </summary>
|
|
protected async Task<long?> TryResolveProfileIdAsync()
|
|
{
|
|
var configured = Configuration.GetValue<long?>("FakeProfileId");
|
|
if (configured is > 0)
|
|
return configured;
|
|
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateScope();
|
|
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
|
|
var profiles = await sender.Send(new ReadProfileViewQuery { IncludeActions = false });
|
|
return profiles?.FirstOrDefault()?.Id;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static IConfiguration BuildConfiguration()
|
|
{
|
|
var appSettingsPath = LocateApiAppSettings();
|
|
|
|
return new ConfigurationBuilder()
|
|
.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: false)
|
|
.Build();
|
|
}
|
|
|
|
private static IServiceProvider BuildServiceProvider(IConfiguration configuration)
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddSingleton(configuration);
|
|
|
|
services.AddRecServices(options =>
|
|
{
|
|
options.LuckyPennySoftwareLicenseKey = configuration["LuckyPennySoftwareLicenseKey"];
|
|
options.ConfigureRecActions(configuration.GetSection("RecAction"));
|
|
options.ConfigureSqlException(configuration.GetSection("SqlException"));
|
|
});
|
|
|
|
services.AddRecInfrastructure(opt =>
|
|
{
|
|
opt.ConfigureDbContext((_, builder) => builder.UseSqlServer(configuration.GetConnectionString("Default")));
|
|
});
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
private static string LocateApiAppSettings()
|
|
{
|
|
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (current is not null)
|
|
{
|
|
var candidate = Path.Combine(current.FullName, "src", "ReC.API", "appsettings.json");
|
|
if (File.Exists(candidate))
|
|
return candidate;
|
|
|
|
current = current.Parent;
|
|
}
|
|
|
|
throw new FileNotFoundException("Could not locate src/ReC.API/appsettings.json from test base directory.");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (ServiceProvider is IDisposable disposable)
|
|
disposable.Dispose();
|
|
}
|
|
}
|